#include #include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 void init(void) { glClearColor (0,0,0,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable (GL_DITHER); } //color data static GLfloat colbuf[] = { 0.2, 0.4, 0.6, 0.8, 0.4, 0.6, 0.8, 0.2, 0.6, 0.8, 0.2, 0.4, 0.8, 0.2, 0.4, 0.6 }; //vertex data static GLint vertexbuf[] = { 0, 0, 20, 0, 20, 20, 0, 20 }; #define BUF_OFFSET(i) ((char *)NULL+ (i)) static void test(void) { GLuint bufname[1]; GLfloat *readBuf; GLubyte *allbuf, *bufptr, *p; int allsize, elemoffset, offset; GLuint elemArray[] = {0, 1, 2, 3}; offset = 1; glGenBuffers(1, bufname); readBuf = (GLfloat *)malloc (4* sizeof (GLfloat) * WINDSIZEX * WINDSIZEY); //create an allbuf to store all data(vertex, color, element and extra space //to chnage offset values allsize = sizeof(colbuf) + sizeof(vertexbuf) + sizeof(elemArray) + offset; allbuf = malloc( allsize * sizeof(GLubyte) ); glBindBuffer(GL_ARRAY_BUFFER, bufname); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufname); glEnableClientState (GL_COLOR_ARRAY); glEnableClientState (GL_VERTEX_ARRAY); glColorPointer (4, GL_FLOAT, 0, BUF_OFFSET(offset)); glVertexPointer(2, GL_INT, 0, BUF_OFFSET(offset + sizeof(colbuf))); //calculate the offset where indices are stored elemoffset = sizeof(colbuf) + sizeof(vertexbuf) + offset; glClear (GL_COLOR_BUFFER_BIT); bufptr = allbuf; p = memcpy(bufptr + offset, colbuf, sizeof(colbuf)); p = memcpy(p + sizeof(colbuf), vertexbuf, sizeof(vertexbuf)); glBufferData(GL_ARRAY_BUFFER, allsize, bufptr, GL_STREAM_DRAW); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, elemoffset, sizeof(elemArray), elemArray); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, BUF_OFFSET(elemoffset)); glFlush(); glReadPixels(0, 0, WINDSIZEX, WINDSIZEY, GL_RGBA, GL_FLOAT, readBuf); glDeleteBuffers(1, &bufname); glDisableClientState (GL_COLOR_ARRAY); glDisableClientState (GL_VERTEX_ARRAY); free(readBuf); free(allbuf); } void display(void) { glViewport(0, 0, WINDSIZEX, WINDSIZEY); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WINDSIZEX, 0, WINDSIZEY); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); test(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDSIZEX, WINDSIZEY); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }