#include <windows.h> #include <GL/glew.h> #include <GL/glut.h> #define BUFFER_OFFSET(offset) ((GLvoid *) NULL + offset) #define XStart -0.8 #define XEnd 0.8 #define YStart -0.8 #define YEnd 0.8 #define NumXPoints 11 #define NumYPoints 11 #define NumPoints (NumXPoints*NumYPoints) #define NumPointsPerStrip (2*NumXPoints) #define NumStrips (NumYPoints-1) #define RestartIndex 0xffff GLfloat color[6][3]={ {1.0,1.0,1.0},{1.0,0.0,0.0},{1.0,1.0,0.0}, {0.0,1.0,0.0},{0.0,1.0,1.0},{0.0,0.0,1.0} }; int colorIndex =0; void setVertex() { int i,j,n; GLfloat * vertices; GLfloat x,y,dx,dy,*tmp; //glBindBuffer(GL_ARRAY_BUFFER,0); vertices = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY); if(vertices==NULL){ exit(EXIT_FAILURE); } else{ dx = (XEnd-XStart)/(NumXPoints-1); dy = (YEnd-YStart)/(NumYPoints-1); tmp = vertices; n=0; for(j=0;j<NumYPoints;++j) { y=YStart + j*dy; for (i=0;i<NumXPoints;i++) { x = XStart+i*dx; *tmp++ = color[(colorIndex+i+j)%6][0]; *tmp++ = color[(colorIndex+i+j)%6][1]; *tmp++ = color[(colorIndex+i+j)%6][2]; *tmp++ = x; *tmp++ = y; *tmp++ = 0.0; } } glUnmapBuffer(GL_ARRAY_BUFFER); //glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0)); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glInterleavedArrays(GL_C3F_V3F, 0, 0); } } void setIndices() { int i,j; GLushort * indices; GLushort *index,bottomRow,topRow; //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); indices = (unsigned short *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,GL_WRITE_ONLY); if(indices==NULL) { exit(EXIT_FAILURE); } else{ index = indices; for(j=0;j<NumStrips;j++){ bottomRow = j*NumYPoints; topRow= bottomRow+NumYPoints; for(i=0;i<NumXPoints;i++){ *index++=topRow+i; *index++=bottomRow+i; } *index++=RestartIndex; } glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); } } void init() { GLuint vbo,ebo; glGenBuffers(1,&vbo); glBindBuffer(GL_ARRAY_BUFFER,vbo); glBufferData(GL_ARRAY_BUFFER,6*NumPoints*sizeof(GLfloat),NULL,GL_STATIC_DRAW); // setVertex(); glGenBuffers(1,&ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER,NumStrips*(NumPointsPerStrip+1)*sizeof(GLushort),NULL,GL_STATIC_DRAW); // setIndices(); glPrimitiveRestartIndex(RestartIndex); glEnable(GL_PRIMITIVE_RESTART); } void display() { setVertex(); setIndices(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(1,1,1); glDrawElements(GL_TRIANGLE_STRIP,NumStrips*(NumPointsPerStrip+1),GL_UNSIGNED_SHORT,0); glutSwapBuffers(); } void mouse(int button,int state,int x,int y) { switch(button){ case GLUT_LEFT_BUTTON: if(state==GLUT_DOWN) { colorIndex++; if(colorIndex==6) colorIndex = 0; glutPostRedisplay(); } break; case 27: { exit(0); break; } default: break; } } int main(int argc, char ** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(250,250); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); glewInit(); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMainLoop(); return 0; }