terça-feira, maio 26, 2009

Programando A GPU

A algum tempo atrás comecei a estuda programação da GPU parei quando descobrir que minha maltida placa OnBoard SIS só suportava OpenGL 1.5 e era processado por Software, por hardware só DirectX que não é o meu caso. mas mesmo assim fiz alguns progressos no estudo
das extensões OpenGL ARB Vertex Program não vou me meter de Roldão e querer dar um tutorial sobre o assunto o que posso fazer é mostrar um exemplo (inútil) e recomendar documentação adequada.

Begin The Code:



   1:  /*

   2:   * main.cpp

   3:   *

   4:   *  Created on: 22/04/2009

   5:   *      Author: newbie

   6:   */

   7:   

   8:   

   9:  #include <GL/gl.h>

  10:  #include <GL/glext.h>

  11:  #include <SDL/SDL.h>

  12:  #include <GL/glu.h>

  13:  #include <math.h>

  14:  #include <string.h>

  15:   

  16:  /**

  17:   * 

  18:   Representa um vetor tridimensional (x,y,z)

  19:   * 

  20:   *

  21:   */

  22:  typedef struct

  23:  {

  24:      float x;

  25:      float y;

  26:      float z;

  27:  }vec3d;

  28:   

  29:  /**

  30:   * angulos da camera xyz

  31:   */

  32:  vec3d angles = {0.0f,0.0f,0.0f};

  33:   

  34:  /**

  35:   * posição da camera

  36:   */

  37:  vec3d position = {0.0f,0.0f,-5.0f};

  38:   

  39:  /**

  40:   * O vertex program e carregado de uma string isso

  41:   * se mostra interessante pois podemos carregar

  42:   * nossas operacoes com vertice dinamicamente.

  43:   * 

  44:   * segue os links que o vao ajudar com o estudo

  45:   * do vertex program

  46:   * 

  47:   * especificacao official (ARB) http://oss.sgi.com/projects/ogl-sample/registry/ARB/vertex_program.txt

  48:   * tuorial GDC 2003 ARB Vertex Program 

  49:   * http://developer.nvidia.com/docs/IO/8230/GDC2003_OGL_ARBVertexProgram.pdf

  50:   */

  51:  const char arbvp[] = "!!ARBvp1.0 "

  52:               "#na linha acima e assinatura do fragment program"

  53:               "PARAM mvp[4] = { state.matrix.mvp }; #mvp[4] = model view projection matrix reand-only"

  54:               "PARAM yellow = {1.0,1.0,0.0,0.0}; #constante read-only"

  55:               "OUTPUT outPosition = result.position; #obvio"

  56:               "MOV result.color, vertex.color; #MOV destino,fonte"

  57:               "#dot product 4 componentes  dst.x,vertex1,vertex2"

  58:               "DP4 outPosition.x, mvp[0], vertex.position; "

  59:               "DP4 outPosition.y, mvp[1], vertex.position;"

  60:               "DP4 outPosition.z, mvp[2], vertex.position;"

  61:               "DP4 outPosition.w, mvp[3], vertex.position;"

  62:               "MOV result.color, yellow; #MOV dst,src"

  63:               "END";

  64:   

  65:  static void quit_tutorial ( int code )

  66:  {   SDL_Quit( );

  67:     //dWorldDestroy(world);

  68:      /* Exit program. */

  69:     exit( code );

  70:  }

  71:   

  72:  static void handle_key_down( SDL_keysym* keysym )

  73:  {

  74:   

  75:     switch( keysym->sym )

  76:       {

  77:   

  78:        case SDLK_ESCAPE:

  79:          quit_tutorial( 0 );

  80:          break;

  81:   

  82:        default:

  83:          break;

  84:       }

  85:   

  86:  }

  87:   

  88:  static void process_events( void )

  89:  {

  90:     SDL_Event event;

  91:   

  92:     while( SDL_PollEvent( &event ) )

  93:       {

  94:   

  95:          switch( event.type )

  96:        {

  97:         case SDL_KEYUP:

  98:   

  99:           handle_key_down( &event.key.keysym );

 100:           if (event.key.keysym.sym == SDLK_LEFT)

 101:               angles.y += cosf(M_PI/4.0f);

 102:   

 103:           break;

 104:   

 105:         case SDL_MOUSEMOTION:

 106:         case SDL_MOUSEBUTTONDOWN:

 107:             //TODO os angulos devem ser convertidos de radianos p/ graus

 108:             if (event.motion.state == 1 && event.motion.xrel > 0 )

 109:             {

 110:                 angles.y += cosf(M_PI/8.0f);

 111:             }

 112:             if (event.motion.state == 1 && event.motion.xrel < 0 )

 113:             {

 114:                 angles.y -= cosf(M_PI/8.0f);

 115:             }

 116:             if (event.motion.state == 1 && event.motion.yrel > 0 )

 117:             {

 118:                 angles.x += cosf(M_PI/8.0f);

 119:             }

 120:             if (event.motion.state == 1 && event.motion.yrel < 0 )

 121:             {

 122:                 angles.x -= cosf(M_PI/8.0f);

 123:             }

 124:             if (event.motion.state == SDL_BUTTON(3) && event.motion.yrel > 0 )

 125:             {

 126:                 position.z -= 3.5f;

 127:             }

 128:             if (event.motion.state == SDL_BUTTON(3) && event.motion.yrel < 0 )

 129:             {

 130:                 position.z += 3.5f;

 131:             }

 132:             break;

 133:   

 134:         case SDL_QUIT:

 135:           quit_tutorial(0);

 136:        }

 137:   

 138:       }

 139:   

 140:  }

 141:   

 142:  static void draw_screen( void )

 143:  {

 144:   

 145:   

 146:     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

 147:   

 148:   

 149:     glMatrixMode(GL_MODELVIEW);

 150:     glLoadIdentity();

 151:     glTranslatef(position.x,position.y,position.z);

 152:     glRotatef(angles.x,1.0f,0.0f,0.0f);

 153:     glRotatef(angles.y,0.f,1.0f,0.0f);

 154:     glRotatef(angles.z,0.f,0.0f,1.0f);

 155:   

 156:     glEnable(GL_VERTEX_PROGRAM_ARB);

 157:     glBegin(GL_TRIANGLES);

 158:     glVertex3f(0.0f,-1.0f,0.0f);

 159:     glVertex3f(1.0f,0.0f,0.0f);

 160:     glVertex3f(-1.0f,0.0f,0.0f);

 161:     glEnd();

 162:     glDisable(GL_VERTEX_PROGRAM_ARB);

 163:   

 164:   

 165:     SDL_GL_SwapBuffers( );

 166:  }

 167:   

 168:  static void setup_opengl( int width, int height )

 169:  {

 170:     float ratio = (float) width / (float) height;

 171:   

 172:     glShadeModel( GL_SMOOTH );

 173:   

 174:     glCullFace( GL_BACK );

 175:     glFrontFace( GL_CCW );

 176:     glEnable( GL_CULL_FACE );

 177:   

 178:     glClearColor( 0.5f, 0.5f, 0.5f, 0.0f );

 179:   

 180:     glViewport( 0, 0, width, height );

 181:   

 182:     glMatrixMode( GL_PROJECTION );

 183:     glLoadIdentity( );

 184:   

 185:     gluPerspective( 60.0, ratio, 1.0, 1024.0 );

 186:     glEnable(GL_LIGHTING);

 187:     glEnable(GL_LIGHT0);

 188:     glEnable(GL_DEPTH_TEST);

 189:   

 190:     GLfloat light_pos[] = {4.0f,10.0f,0.0f,0.0f};

 191:     GLfloat light_Ka[]  = {0.0f,0.0f,3.0f,1.0f};

 192:     GLfloat light_Kd[]  = {1.0f,0.0f,0.0f,1.0f};

 193:     GLfloat light_Ks[]  = {1.0f,1.0f,1.0f,1.0f};

 194:     GLfloat lmKa[] = {0.0f,0.0f,0.0f,0.0f};

 195:     glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);

 196:     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 0);

 197:     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmKa);

 198:     glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

 199:     glLightfv(GL_LIGHT0,GL_AMBIENT,light_Ka);

 200:     glLightfv(GL_LIGHT0,GL_DIFFUSE,light_Kd);

 201:     glLightfv(GL_LIGHT0,GL_SPECULAR,light_Ks);

 202:  }

 203:   

 204:  int main( int argc, char* argv[] )

 205:  {

 206:     const SDL_VideoInfo* info = NULL;

 207:   

 208:     int width = 0;

 209:     int height = 0;

 210:   

 211:     int bpp = 0;

 212:   

 213:     int flags = 0;

 214:   

 215:   

 216:   

 217:     if( SDL_Init( SDL_INIT_VIDEO|SDL_INIT_TIMER ) < 0 )

 218:       {

 219:      fprintf( stderr, "Video initialization failed: %s\n",

 220:           SDL_GetError( ) );

 221:      quit_tutorial( 1 );

 222:       }

 223:   

 224:     info = SDL_GetVideoInfo( );

 225:   

 226:     if( !info )

 227:       {

 228:   

 229:      fprintf( stderr, "Video query failed: %s\n",

 230:           SDL_GetError( ) );

 231:      quit_tutorial( 1 );

 232:       }

 233:   

 234:     width = 400;

 235:     height = 400;

 236:   

 237:     bpp = info->vfmt->BitsPerPixel;

 238:   

 239:     SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );

 240:     SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );

 241:     SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );

 242:     SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );

 243:     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

 244:   

 245:     flags = SDL_OPENGL;

 246:   

 247:     if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 )

 248:       {

 249:   

 250:      fprintf( stderr, "Video mode set failed: %s\n",

 251:           SDL_GetError( ) );

 252:      quit_tutorial( 1 );

 253:       }

 254:   

 255:     GLuint progid;

 256:     glGenProgramsARB(1,&progid);

 257:     glBindProgramARB(GL_VERTEX_PROGRAM_ARB,progid);

 258:     glProgramStringARB(GL_VERTEX_PROGRAM_ARB,GL_PROGRAM_FORMAT_ASCII_ARB,strlen(arbvp),arbvp);

 259:     

 260:     if (GL_INVALID_OPERATION == glGetError())

 261:     {

 262:      printf("erro\n");

 263:      quit_tutorial(0);

 264:     }

 265:     setup_opengl( width, height );

 266:     while( 1 )

 267:       {

 268:      process_events( );

 269:      ;

 270:      draw_screen();

 271:       }

 272:     return 0;

 273:  }






Ta ta é só um quadro Amarelo mais já serve como Getting Started, vou ver c consigo fazer um Phong Light.

Sem comentários: