GazzettaForum - Hacking, msn, informatica, iPhone, Mac, Linux, Vista, XP, eMule, XXX

Reply
[C] Camera Viewing
view post Posted on 20/10/2009, 16:55Quote
Avatar


Group: Admin
Posts: 4028
Location: Gongaga


Status: Offline: ultima azione eseguita il 15/12/2009, 17:46


Posto ora un programmino realizzato 2 o 3 mesi fa (può essere la base per chi vuole iniziare a creare un sparatutto in prima persona, ha gli elementi, ma vanno sviluppati) [sviluppato per windows]




Piccolo programmino che permette di utilizzare la telecamera per vedere una scacchiera..Fatto con allegro.h che spesso si usa per i giochi

Questo è commentato

CODICE
#include <stdio.h>
#include <math.h>

#include <allegro.h>


#ifndef M_PI
  #define M_PI   3.14159
#endif


/* display di una scacchiera reticolata 8x8 */
#define GRID_SIZE    8


/* converte i radianti in gradi */
#define DEG(n)    ((n) * 180.0 / M_PI)


/* quanto volte in un secondo i FPS vengono calcolati */
#define FPS_INT 2


/* decommentare per disattivare l'attesa per vsync */
/* #define DISABLE_VSYNC */


/* parametri che controllano lo stato della telecamera */
int viewport_w = 320;
int viewport_h = 240;
int fov = 48;
float aspect = 1.33f;
float xpos = 0;
float ypos = -2;
float zpos = -4;
float heading = 0;
float pitch = 0;
float roll = 0;

int fps;
int framecount;



/* fa il render di una mattonella del reticolato */
void draw_square(BITMAP *bmp, MATRIX_f *camera, int x, int z)
{
  V3D_f _v[4], _vout[8], _vtmp[8];
  V3D_f *v[4], *vout[8], *vtmp[8];
  int flags[4], out[8];
  int c, vc;

  for (c=0; c<4; c++)
     v[c] = &_v[c];

  for (c=0; c<8; c++) {
     vout[c] = &_vout[c];
     vtmp[c] = &_vtmp[c];
  }

  /* setta quattro vertici con la posizione della mattonella */
  v[0]->x = x - GRID_SIZE/2;
  v[0]->y = 0;
  v[0]->z = z - GRID_SIZE/2;

  v[1]->x = x - GRID_SIZE/2 + 1;
  v[1]->y = 0;
  v[1]->z = z - GRID_SIZE/2;

  v[2]->x = x - GRID_SIZE/2 + 1;
  v[2]->y = 0;
  v[2]->z = z - GRID_SIZE/2 + 1;

  v[3]->x = x - GRID_SIZE/2;
  v[3]->y = 0;
  v[3]->z = z - GRID_SIZE/2 + 1;

  /* applica la camera_matrix */
  for (c=0; c<4; c++) {
     apply_matrix_f(camera, v[c]->x, v[c]->y, v[c]->z,
                    &v[c]->x, &v[c]->y, &v[c]->z);

     flags[c] = 0;

     /* setta flags se questo vertice è fuori dal bordo dello schermo */
     if (v[c]->x < -v[c]->z)
        flags[c] |= 1;
     else if (v[c]->x > v[c]->z)
        flags[c] |= 2;

     if (v[c]->y < -v[c]->z)
        flags[c] |= 4;
     else if (v[c]->y > v[c]->z)
        flags[c] |= 8;

     if (v[c]->z < 0.1)
        flags[c] |= 16;
  }

  /* esce se tutti i vertici sono fuori da bordo dello schermo */
  if (flags[0] & flags[1] & flags[2] & flags[3])
     return;

  if (flags[0] | flags[1] | flags[2] | flags[3]) {
     /* spiana se alcuni vertici sono fuori dallo schermo */
     vc = clip3d_f(POLYTYPE_FLAT, 0.1, 0.1, 4, (AL_CONST V3D_f **)v,
                   vout, vtmp, out);

     if (vc <= 0)
        return;
  }
  else {
       
     vout[0] = v[0];
     vout[1] = v[1];
     vout[2] = v[2];
     vout[3] = v[3];

     vc = 4;
  }

  /* visuale */
  for (c=0; c<vc; c++)
     persp_project_f(vout[c]->x, vout[c]->y, vout[c]->z,
                     &vout[c]->x, &vout[c]->y);

  /* setta il colore */
  vout[0]->c = ((x + z) & 1) ? makecol(0, 255, 0) : makecol(255, 255, 0);

  /* render del poligono */
  polygon3d_f(bmp, POLYTYPE_FLAT, NULL, vc, vout);
}



/* scrive tutto */
void render(BITMAP *bmp)
{
  MATRIX_f roller, camera;
  int x, y, w, h;
  float xfront, yfront, zfront;
  float xup, yup, zup;

  /* cancella lo sfondo */
  clear_to_color(bmp, makecol(255, 255, 255));

  /* setta la regione visiva */
  x = (SCREEN_W - viewport_w) / 2;
  y = (SCREEN_H - viewport_h) / 2;
  w = viewport_w;
  h = viewport_h;

  set_projection_viewport(x, y, w, h);
  rect(bmp, x-1, y-1, x+w, y+h, makecol(255, 0, 0));
  set_clip_rect(bmp, x, y, x+w-1, y+h-1);

  /* calcola il vettore davanti */
  xfront = sin(heading) * cos(pitch);
  yfront = sin(pitch);
  zfront = cos(heading) * cos(pitch);

  /* ruota il vettore superiore intorno al vettore davanti dall'angolo spianato */
  get_vector_rotation_matrix_f(&roller, xfront, yfront, zfront,
                               roll*128.0/M_PI);
  apply_matrix_f(&roller, 0, -1, 0, &xup, &yup, &zup);

  /* costruisce la camera_matrix */
  get_camera_matrix_f(&camera,
                      xpos, ypos, zpos,        /* posizione della camera */
                      xfront, yfront, zfront,  /* vettore davanti */
                      xup, yup, zup,           /* vettore superiore */
                      fov,                     /* campo di visualizzazione */
                      aspect);                 /* rapporto dell'aspetto (aspect ratio) */

  /* disegna il reticolato dei quadrati */
  for (x=0; x<GRID_SIZE; x++)
     for (y=0; y<GRID_SIZE; y++)
        draw_square(bmp, &camera, x, y);

  /* mostra i testi */
  set_clip_rect(bmp, 0, 0, bmp->w, bmp->h);
  textprintf_ex(bmp, font, 0,  0, makecol(0, 0, 0), -1,
                "Viewport width: %d (w/W changes)", viewport_w);
  textprintf_ex(bmp, font, 0,  8, makecol(0, 0, 0), -1,
                "Viewport height: %d (h/H changes)", viewport_h);
  textprintf_ex(bmp, font, 0, 16, makecol(0, 0, 0), -1,
                "Field of view: %d (f/F changes)", fov);
  textprintf_ex(bmp, font, 0, 24, makecol(0, 0, 0), -1,
                "Aspect ratio: %.2f (a/A changes)", aspect);
  textprintf_ex(bmp, font, 0, 32, makecol(0, 0, 0), -1,
                "X position: %.2f (x/X changes)", xpos);
  textprintf_ex(bmp, font, 0, 40, makecol(0, 0, 0), -1,
                "Y position: %.2f (y/Y changes)", ypos);
  textprintf_ex(bmp, font, 0, 48, makecol(0, 0, 0), -1,
                "Z position: %.2f (z/Z changes)", zpos);
  textprintf_ex(bmp, font, 0, 56, makecol(0, 0, 0), -1,
                "Heading: %.2f deg (left/right changes)", DEG(heading));
  textprintf_ex(bmp, font, 0, 64, makecol(0, 0, 0), -1,
                "Pitch: %.2f deg (pgup/pgdn changes)", DEG(pitch));
  textprintf_ex(bmp, font, 0, 72, makecol(0, 0, 0), -1,
                "Roll: %.2f deg (r/R changes)", DEG(roll));
  textprintf_ex(bmp, font, 0, 80, makecol(0, 0, 0), -1,
                "Front vector: %.2f, %.2f, %.2f", xfront, yfront, zfront);
  textprintf_ex(bmp, font, 0, 88, makecol(0, 0, 0), -1,
                "Up vector: %.2f, %.2f, %.2f", xup, yup, zup);
  textprintf_ex(bmp, font, 0, 96, makecol(0, 0, 0), -1,
                "Frames per second: %d", fps);
}



/* accordo con l'utente */
void process_input(void)
{
  double frac, iptr;

  poll_keyboard();

  if (key[KEY_W]) {
     if (key_shifts & KB_SHIFT_FLAG) {
        if (viewport_w < SCREEN_W)
           viewport_w += 8;
     }
     else {
        if (viewport_w > 16)
           viewport_w -= 8;
     }
  }

  if (key[KEY_H]) {
     if (key_shifts & KB_SHIFT_FLAG) {
        if (viewport_h < SCREEN_H)
           viewport_h += 8;
     }
     else {
        if (viewport_h > 16)
           viewport_h -= 8;
     }
  }

  if (key[KEY_F]) {
     if (key_shifts & KB_SHIFT_FLAG) {
        if (fov < 96)
           fov++;
     }
     else {
        if (fov > 16)
           fov--;
     }
  }

  if (key[KEY_A]) {
     frac = modf(aspect*10.0, &iptr);

     if (key_shifts & KB_SHIFT_FLAG) {
        if ((frac>0.59) && (frac<0.61))
           aspect += 0.04f;
        else
           aspect += 0.03f;
        if (aspect > 2)
           aspect = 2;
     }
     else {
        if ((frac>0.99) || (frac<0.01))
           aspect -= 0.04f;
        else
           aspect -= 0.03f;
        if (aspect < .1)
           aspect = .1;
     }
  }

  if (key[KEY_X]) {
     if (key_shifts & KB_SHIFT_FLAG)
        xpos += 0.05;
     else
        xpos -= 0.05;
  }

  if (key[KEY_Y]) {
     if (key_shifts & KB_SHIFT_FLAG)
        ypos += 0.05;
     else
        ypos -= 0.05;
  }

  if (key[KEY_Z]) {
     if (key_shifts & KB_SHIFT_FLAG)
        zpos += 0.05;
     else
        zpos -= 0.05;
  }

  if (key[KEY_LEFT])
     heading -= 0.05;

  if (key[KEY_RIGHT])
     heading += 0.05;

  if (key[KEY_PGUP])
     if (pitch > -M_PI/4)
        pitch -= 0.05;

  if (key[KEY_PGDN])
     if (pitch < M_PI/4)
        pitch += 0.05;

  if (key[KEY_R]) {
     if (key_shifts & KB_SHIFT_FLAG) {
        if (roll < M_PI/4)
           roll += 0.05;
     }
     else {
        if (roll > -M_PI/4)
           roll -= 0.05;
     }
  }

  if (key[KEY_UP]) {
     xpos += sin(heading) / 4;
     zpos += cos(heading) / 4;
  }

  if (key[KEY_DOWN]) {
     xpos -= sin(heading) / 4;
     zpos -= cos(heading) / 4;
  }
}



void fps_check(void)
{
  fps = framecount*FPS_INT;
  framecount = 0;
}



int main(void)
{
  BITMAP *buffer;

  if (allegro_init() != 0)
     return 1;
  install_keyboard();
  install_timer();

  if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
     if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
        set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
        allegro_message("Inabilitato al settaggio della grafican%­sn",
                        allegro_error);
        return 1;
     }
  }

  set_palette(desktop_palette);
  buffer = create_bitmap(SCREEN_W, SCREEN_H);
  install_int_ex(fps_check, BPS_TO_TIMER(FPS_INT));

  while (!key[KEY_ESC]) {
     render(buffer);

     #ifndef DISABLE_VSYNC
        vsync();
     #endif
     
     blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);      
     framecount++;

     process_input();
  }

  destroy_bitmap(buffer);
  return 0;
}

END_OF_MAIN()


Che ne dite?

Ecco la dll da copiare in C:\Windows\system32



Edited by .:Zack Fair:. - 22/10/2009, 15:53

SPOILER (clicca per visualizzare)



image
Which Final Fantasy Character Are You?
image

E tu in che personaggio ti identifichi?





image


image
  • Hey, would you say I became a hero?


  • Embrace your dreams. And whatever happens, protect your honor as...Soldier!!


  • Men don't cry for themselves, they cry for their comrades


  • Embrace your dreams... If you want to be a Hero you need to have dreams... And honor


  • Even if the morrow is barren of promesses, nothing shall forestall my return.


  • We're friends, right?


  • If you see Aerith...Say hi for me...


  • That looks so...liberating...Those wings...I want them too...It feels good...


  • - Tutti dicono che l'onore non conta niente e invece conta più della vita. Senza onore nessuno ti rispetta.
     
    P_MSG P_EMAIL Top
    0 replies since 20/10/2009, 16:55
     
    Reply

    load
    Fast reply

     
     
     

    Enable emoticons
    Clickable Smilies
    Show All


    Nickname:      Email:



     

     
     





    Image and video hosting by TinyPic


    Gazzetta Frum

    Gazzetta Frum

    • Skin by:
    .:spyshok:. & .:Zack Fair:.
    • Distribuito da:
    Gazzetta Forum

    Vietato apportare modifiche al css