Advertisement
Guest User

Untitled

a guest
Jan 20th, 2011
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.82 KB | None | 0 0
  1. // gcc -lGL -lGLU -lglut -lSDL -lm KickingDragon_VBODemo.c -o kdrg
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_main.h>
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6. #include <GL/glut.h>
  7. #include <GL/glext.h>
  8. #include <stdbool.h>
  9. #include <math.h>
  10. // 3*2*sx*(sy-1)
  11. #define WD 2000
  12. #define HT 2001
  13. // в действительности: WD*(HT-1)*6*sizeof(GLfloat)
  14. // а если HT четно - прибавить одну вершину
  15. #define SZ WD*HT*6*sizeof(GLfloat)
  16.  
  17. int Initialize()
  18. {
  19.     SDL_Init(SDL_INIT_VIDEO);
  20.     SDL_SetVideoMode(640, 480, 32, 2);
  21. }
  22.  
  23. void normalize(GLfloat *x, GLfloat *y, GLfloat *z) {
  24.     GLfloat d = sqrt((*x*(*x)) + (*y*(*y)) + (*z*(*z)));
  25.     *x /= d;
  26.     *y /= d;
  27.     *z /= d;
  28. }
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.     GLfloat angle = 0., ZZ = -2.5;
  33.     Initialize();
  34.    
  35.     glMatrixMode(GL_PROJECTION);
  36.     glLoadIdentity();
  37.    
  38.     gluPerspective(45.f, 640.f / 480.f, .1f, 25.f);
  39.    
  40.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  41.    
  42. /*  GLfloat vertexData[] =  { 0.f, 1.f, 0.f
  43.                             , -1.f, -1.f, 0.f
  44.                             , 1.f, -1.f, 0.f
  45.                             };
  46. */
  47.     int x, y, ym;
  48.     GLfloat w = (GLfloat)WD;
  49.     GLfloat h = (GLfloat)HT;
  50.     GLfloat *vertexData = (GLfloat*)malloc(SZ);
  51. //  GLfloat *colourData = (GLfloat*)malloc(SZ);
  52.     GLfloat *normalData = (GLfloat*)malloc(SZ);
  53.     GLfloat *ptr = vertexData, *ptr2 = normalData;//, *ptr1=colourData;
  54. //  inline void chptr(GLfloat **vptr, GLfloat **cptr, int x, int y){
  55.     inline void chptr(GLfloat **vptr, GLfloat **nptr, int x, int y, GLfloat S){
  56.         GLfloat *ptr = *vptr, *ptr2 = *nptr;//, *ptr1 = *cptr;
  57.         *ptr++ = x/w;
  58.         *ptr++ = y/h;
  59.         *ptr++ = sin(x/200.)*cos((y-1.)/500.)/4.;
  60.         *ptr++ = x/w;
  61.         *ptr++ = (y+1.)/h;
  62.         *ptr++ = sin(x/200.)*cos(y/500.)/4.;
  63.         *vptr = ptr;
  64.         /**ptr2++ = S*0.001;
  65.         *ptr2++ = S*0.001;
  66.         *ptr2++ = S*(cos(x/200.)*cos((y-1.)/500.)/800.-sin(x/200.)*sin((y-1.)/500.)/2000.);
  67.         normalize(ptr2-1, ptr2-2, ptr2-3);
  68.         *ptr2++ = S*0.001;
  69.         *ptr2++ = S*0.001;
  70.         *ptr2++ = S*(cos(x/200.)*cos(y/500.)/800.-sin(x/200.)*sin(y/500.)/2000.);*/
  71.         *ptr2++ = S/w;
  72.         *ptr2++ = S/h;
  73.         *ptr2++ = S*(cos(x/200.)*cos((y-1.)/500.)/800.-sin(x/200.)*sin((y-1.)/500.)/2000.);
  74.         normalize(ptr2-1, ptr2-2, ptr2-3);
  75.         *ptr2++ = S/w;
  76.         *ptr2++ = S/h;
  77.         *ptr2++ = S*(cos(x/200.)*cos(y/500.)/800.-sin(x/200.)*sin(y/500.)/2000.);
  78.         normalize(ptr2-1, ptr2-2, ptr2-3);
  79.         *nptr = ptr2;
  80. /*      *ptr1++ = sin(y/500.)*cos(x);
  81.         *ptr1++ = x/5000.;
  82.         *ptr1++ = y/5000.;
  83.         *ptr1++ = sin((y+1.)/500.)*cos(x);
  84.         *ptr1++ = x/5000.;
  85.         *ptr1++ = (y+1.)/5000.;
  86.         *cptr = ptr1;
  87. */  }
  88.     ym = HT - 1;
  89.     for(y=0; y < ym; y++){
  90.         if(y%2 == 0)
  91.         for(x=0; x<WD; x++){
  92.             //chptr(&ptr, &ptr1, x, y);
  93.             chptr(&ptr, &ptr2, x, y, 1.);
  94.         }
  95.         else
  96.         for(x=WD-1; x>-1; x-=1){
  97.             //chptr(&ptr, &ptr1, x, y);
  98.             chptr(&ptr, &ptr2, x, y, 1.);
  99.         }
  100.     }
  101.     // "пробегаем" обратно, если четное кол-во строк
  102.     if(HT%2==0){
  103.         y--;
  104.         for(x=WD-1; x>-1; x-=1)
  105.             //chptr(&ptr, &ptr1, x, y);
  106.             chptr(&ptr, &ptr2, x, y, 1.);
  107.     }
  108.     printf("sz: %d\n", ptr-vertexData);
  109. /*  GLfloat colourData[] =  { 0.f, 0.f, 1.f
  110.                             , 0.f, 1.f, 1.f
  111.                             , 1.f, 1.f, 1.f
  112.                             };
  113. */
  114.     glEnable(GL_DEPTH_TEST);
  115.     //glEnable(GL_CULL_FACE);
  116.     //glCullFace(GL_BACK);
  117.     GLfloat lAmbient[] = {0.7,0.8,0.9,1.0};
  118.     GLfloat lDiffuse[] = {0.0,1.0,0.0,1.0};
  119.     GLfloat lSpecular[] = {1.0,0.0,0.0,1.0};
  120.     glLightfv(GL_LIGHT0,GL_AMBIENT,lAmbient);
  121.     glLightfv(GL_LIGHT0,GL_DIFFUSE,lDiffuse);
  122.     glLightfv(GL_LIGHT0,GL_SPECULAR,lSpecular);
  123.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
  124.     glEnable(GL_LIGHTING);
  125.     glEnable(GL_LIGHT0);
  126.     GLfloat lPosition[] = {0.0,1.0,0.0,1.0};
  127.     glLightfv(GL_LIGHT0,GL_POSITION,lPosition);
  128.     GLfloat cWhite[] = {1.0,1.0,1.0,1.0};
  129.     GLfloat cGreen[] = {0.0,0.5,0.0,1.0};
  130.     GLfloat mSpecular[] = {0.0,0.5,0.5,1.0};
  131.     GLfloat mShininess[] = {12.0};
  132.     glMaterialfv(GL_FRONT, GL_SPECULAR, mSpecular);
  133.     glMaterialfv(GL_FRONT, GL_SHININESS,mShininess);
  134.     glMaterialfv(GL_FRONT, GL_AMBIENT, cGreen);
  135.     glMaterialfv(GL_FRONT, GL_DIFFUSE, cWhite);
  136.     GLuint vertexBuffer = 0;
  137.     GLuint normalBuffer = 0;
  138.     GLuint colourBuffer = 0;
  139.     // 1.- Generated a buffer name for our vertex buffer,
  140.     glGenBuffers(1, &vertexBuffer);
  141.     // 2.- Set our current 'in-use' buffer to our vertex buffer.
  142.     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  143.     // 3.- Fill out the data buffer using our initialized values.
  144.     glBufferData(GL_ARRAY_BUFFER, SZ, vertexData, GL_STATIC_DRAW);
  145.     glGenBuffers(1, &normalBuffer);
  146.     glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
  147.     glBufferData(GL_ARRAY_BUFFER, SZ, normalData, GL_STATIC_DRAW);
  148.     // Same as steps 1, 2 and 3 above, but using colour data.
  149. /*  glGenBuffers(1, &colourBuffer);
  150.     glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
  151.     glBufferData(GL_ARRAY_BUFFER, SZ, colourData, GL_STATIC_DRAW);
  152. */  // Initialize the ability to use vertex arrays and colour arrays.
  153.     // I should really be making sure I have this capability and use
  154.     // glIsEnabled first.
  155.     glEnableClientState(GL_VERTEX_ARRAY);
  156.     glEnableClientState(GL_NORMAL_ARRAY);
  157. //  glEnableClientState(GL_COLOR_ARRAY);
  158.     free(vertexData);
  159.     free(normalData);
  160. //  free(colourData);
  161.     bool bRunning = true;
  162.     SDL_Event event;
  163.     while (bRunning)
  164.     {
  165.         if(SDL_PollEvent(&event))
  166.         {
  167.             if(event.key.keysym.sym == SDLK_ESCAPE)
  168.             {
  169.                 bRunning = false;
  170.                 continue;
  171.             }
  172.             else if(event.key.keysym.sym == 'q')
  173.                 angle+=10.;
  174.             else if(event.key.keysym.sym == 'w')
  175.                 angle-=10.;
  176.             else if(event.key.keysym.sym == '=')
  177.                 ZZ+=0.1;
  178.             else if(event.key.keysym.sym == '-')
  179.                 ZZ-=0.1;
  180.             else if(event.key.keysym.sym == '0')
  181.                 ZZ+=0.01;
  182.             else if(event.key.keysym.sym == '9')
  183.                 ZZ-=0.01;
  184.         }
  185.         // Clear our depth buffer and color bit
  186.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  187.         // Reset our view
  188.         glMatrixMode(GL_MODELVIEW);
  189.         glLoadIdentity();
  190.        
  191.         glTranslatef(0., 0., ZZ);
  192.         glRotatef(angle,1.,0.,0.);
  193.         glTranslatef(-.5, -.5, 0.);
  194.         // --- VBO's in action.
  195.         // Set the vertexBuffer as the current buffer.
  196.         glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  197.         // Specify that it's data is vertex data.
  198.         glVertexPointer(3, GL_FLOAT, 0, 0);
  199.         glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
  200.         glNormalPointer(GL_FLOAT, 0, 0);
  201.         // Set the colourBuffer as the current buffer.
  202. //      glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
  203.         // Specify that it's data is colour data.
  204. //      glColorPointer (3, GL_FLOAT, 0, 0);
  205.         // Tell OpenGL to draw what it sees. ;)
  206.         glDrawArrays(GL_TRIANGLE_STRIP, 0, SZ);
  207.         //glDrawArrays(GL_TRIANGLES, 0, SZ);
  208.         //glDrawArrays(GL_POINTS, 0, 3*256*256);
  209.         glBegin(GL_LINES); // линия для масштаба
  210.             glVertex3f(0.,0.,0.);
  211.             glVertex3f(1.,1.,0.);
  212.         glEnd();
  213.         // --- End VBO's in action.
  214.         // Swap buffers!
  215.         SDL_GL_SwapBuffers();
  216.     }
  217.     // Disable the client states.
  218.     glDisableClientState( GL_VERTEX_ARRAY );
  219.     glDisableClientState( GL_COLOR_ARRAY );
  220.     //!TODO: Free the buffers created in glBufferDataARB
  221.    
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement