Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // gcc -lGL -lGLU -lglut -lSDL -lm KickingDragon_VBODemo.c -o kdrg
- #include <SDL/SDL.h>
- #include <SDL/SDL_main.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <GL/glext.h>
- #include <stdbool.h>
- #include <math.h>
- // 3*2*sx*(sy-1)
- #define WD 2000
- #define HT 2001
- // в действительности: WD*(HT-1)*6*sizeof(GLfloat)
- // а если HT четно - прибавить одну вершину
- #define SZ WD*HT*6*sizeof(GLfloat)
- int Initialize()
- {
- SDL_Init(SDL_INIT_VIDEO);
- SDL_SetVideoMode(640, 480, 32, 2);
- }
- void normalize(GLfloat *x, GLfloat *y, GLfloat *z) {
- GLfloat d = sqrt((*x*(*x)) + (*y*(*y)) + (*z*(*z)));
- *x /= d;
- *y /= d;
- *z /= d;
- }
- int main(int argc, char **argv)
- {
- GLfloat angle = 0., ZZ = -2.5;
- Initialize();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.f, 640.f / 480.f, .1f, 25.f);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- /* GLfloat vertexData[] = { 0.f, 1.f, 0.f
- , -1.f, -1.f, 0.f
- , 1.f, -1.f, 0.f
- };
- */
- int x, y, ym;
- GLfloat w = (GLfloat)WD;
- GLfloat h = (GLfloat)HT;
- GLfloat *vertexData = (GLfloat*)malloc(SZ);
- // GLfloat *colourData = (GLfloat*)malloc(SZ);
- GLfloat *normalData = (GLfloat*)malloc(SZ);
- GLfloat *ptr = vertexData, *ptr2 = normalData;//, *ptr1=colourData;
- // inline void chptr(GLfloat **vptr, GLfloat **cptr, int x, int y){
- inline void chptr(GLfloat **vptr, GLfloat **nptr, int x, int y, GLfloat S){
- GLfloat *ptr = *vptr, *ptr2 = *nptr;//, *ptr1 = *cptr;
- *ptr++ = x/w;
- *ptr++ = y/h;
- *ptr++ = sin(x/200.)*cos((y-1.)/500.)/4.;
- *ptr++ = x/w;
- *ptr++ = (y+1.)/h;
- *ptr++ = sin(x/200.)*cos(y/500.)/4.;
- *vptr = ptr;
- /**ptr2++ = S*0.001;
- *ptr2++ = S*0.001;
- *ptr2++ = S*(cos(x/200.)*cos((y-1.)/500.)/800.-sin(x/200.)*sin((y-1.)/500.)/2000.);
- normalize(ptr2-1, ptr2-2, ptr2-3);
- *ptr2++ = S*0.001;
- *ptr2++ = S*0.001;
- *ptr2++ = S*(cos(x/200.)*cos(y/500.)/800.-sin(x/200.)*sin(y/500.)/2000.);*/
- *ptr2++ = S/w;
- *ptr2++ = S/h;
- *ptr2++ = S*(cos(x/200.)*cos((y-1.)/500.)/800.-sin(x/200.)*sin((y-1.)/500.)/2000.);
- normalize(ptr2-1, ptr2-2, ptr2-3);
- *ptr2++ = S/w;
- *ptr2++ = S/h;
- *ptr2++ = S*(cos(x/200.)*cos(y/500.)/800.-sin(x/200.)*sin(y/500.)/2000.);
- normalize(ptr2-1, ptr2-2, ptr2-3);
- *nptr = ptr2;
- /* *ptr1++ = sin(y/500.)*cos(x);
- *ptr1++ = x/5000.;
- *ptr1++ = y/5000.;
- *ptr1++ = sin((y+1.)/500.)*cos(x);
- *ptr1++ = x/5000.;
- *ptr1++ = (y+1.)/5000.;
- *cptr = ptr1;
- */ }
- ym = HT - 1;
- for(y=0; y < ym; y++){
- if(y%2 == 0)
- for(x=0; x<WD; x++){
- //chptr(&ptr, &ptr1, x, y);
- chptr(&ptr, &ptr2, x, y, 1.);
- }
- else
- for(x=WD-1; x>-1; x-=1){
- //chptr(&ptr, &ptr1, x, y);
- chptr(&ptr, &ptr2, x, y, 1.);
- }
- }
- // "пробегаем" обратно, если четное кол-во строк
- if(HT%2==0){
- y--;
- for(x=WD-1; x>-1; x-=1)
- //chptr(&ptr, &ptr1, x, y);
- chptr(&ptr, &ptr2, x, y, 1.);
- }
- printf("sz: %d\n", ptr-vertexData);
- /* GLfloat colourData[] = { 0.f, 0.f, 1.f
- , 0.f, 1.f, 1.f
- , 1.f, 1.f, 1.f
- };
- */
- glEnable(GL_DEPTH_TEST);
- //glEnable(GL_CULL_FACE);
- //glCullFace(GL_BACK);
- GLfloat lAmbient[] = {0.7,0.8,0.9,1.0};
- GLfloat lDiffuse[] = {0.0,1.0,0.0,1.0};
- GLfloat lSpecular[] = {1.0,0.0,0.0,1.0};
- glLightfv(GL_LIGHT0,GL_AMBIENT,lAmbient);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,lDiffuse);
- glLightfv(GL_LIGHT0,GL_SPECULAR,lSpecular);
- glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- GLfloat lPosition[] = {0.0,1.0,0.0,1.0};
- glLightfv(GL_LIGHT0,GL_POSITION,lPosition);
- GLfloat cWhite[] = {1.0,1.0,1.0,1.0};
- GLfloat cGreen[] = {0.0,0.5,0.0,1.0};
- GLfloat mSpecular[] = {0.0,0.5,0.5,1.0};
- GLfloat mShininess[] = {12.0};
- glMaterialfv(GL_FRONT, GL_SPECULAR, mSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS,mShininess);
- glMaterialfv(GL_FRONT, GL_AMBIENT, cGreen);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, cWhite);
- GLuint vertexBuffer = 0;
- GLuint normalBuffer = 0;
- GLuint colourBuffer = 0;
- // 1.- Generated a buffer name for our vertex buffer,
- glGenBuffers(1, &vertexBuffer);
- // 2.- Set our current 'in-use' buffer to our vertex buffer.
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- // 3.- Fill out the data buffer using our initialized values.
- glBufferData(GL_ARRAY_BUFFER, SZ, vertexData, GL_STATIC_DRAW);
- glGenBuffers(1, &normalBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
- glBufferData(GL_ARRAY_BUFFER, SZ, normalData, GL_STATIC_DRAW);
- // Same as steps 1, 2 and 3 above, but using colour data.
- /* glGenBuffers(1, &colourBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
- glBufferData(GL_ARRAY_BUFFER, SZ, colourData, GL_STATIC_DRAW);
- */ // Initialize the ability to use vertex arrays and colour arrays.
- // I should really be making sure I have this capability and use
- // glIsEnabled first.
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- // glEnableClientState(GL_COLOR_ARRAY);
- free(vertexData);
- free(normalData);
- // free(colourData);
- bool bRunning = true;
- SDL_Event event;
- while (bRunning)
- {
- if(SDL_PollEvent(&event))
- {
- if(event.key.keysym.sym == SDLK_ESCAPE)
- {
- bRunning = false;
- continue;
- }
- else if(event.key.keysym.sym == 'q')
- angle+=10.;
- else if(event.key.keysym.sym == 'w')
- angle-=10.;
- else if(event.key.keysym.sym == '=')
- ZZ+=0.1;
- else if(event.key.keysym.sym == '-')
- ZZ-=0.1;
- else if(event.key.keysym.sym == '0')
- ZZ+=0.01;
- else if(event.key.keysym.sym == '9')
- ZZ-=0.01;
- }
- // Clear our depth buffer and color bit
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Reset our view
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0., 0., ZZ);
- glRotatef(angle,1.,0.,0.);
- glTranslatef(-.5, -.5, 0.);
- // --- VBO's in action.
- // Set the vertexBuffer as the current buffer.
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- // Specify that it's data is vertex data.
- glVertexPointer(3, GL_FLOAT, 0, 0);
- glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
- glNormalPointer(GL_FLOAT, 0, 0);
- // Set the colourBuffer as the current buffer.
- // glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
- // Specify that it's data is colour data.
- // glColorPointer (3, GL_FLOAT, 0, 0);
- // Tell OpenGL to draw what it sees. ;)
- glDrawArrays(GL_TRIANGLE_STRIP, 0, SZ);
- //glDrawArrays(GL_TRIANGLES, 0, SZ);
- //glDrawArrays(GL_POINTS, 0, 3*256*256);
- glBegin(GL_LINES); // линия для масштаба
- glVertex3f(0.,0.,0.);
- glVertex3f(1.,1.,0.);
- glEnd();
- // --- End VBO's in action.
- // Swap buffers!
- SDL_GL_SwapBuffers();
- }
- // Disable the client states.
- glDisableClientState( GL_VERTEX_ARRAY );
- glDisableClientState( GL_COLOR_ARRAY );
- //!TODO: Free the buffers created in glBufferDataARB
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement