From somewhere i get this code , First drawing a rectangle and later changing its color(i dont know how its doing)
Can someone please explain it???
#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <string.h>
int nbVertices = 4;
void* colorOffset = (void*) (sizeof( float ) * 12);
float initialVBO[24] = {0,0,0, 0.5,0,0, 0,0.5,0, 0.5,0.5,0, 1,1,1, 1,1,1, 1,1,1, 1,1,1};
GLuint vboID;
void displayFunc(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glColorPointer(3, GL_FLOAT, 0, colorOffset);
glDrawArrays(GL_TRIANGLE_STRIP, 0, nbVertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glFlush();
}
void mouseFunc(int button, int state, int x, int y) {
float color[12] = {1,0,0, 1,0,0, 1,0,0, 1,0,0};
glBindBuffer(GL_ARRAY_BUFFER, vboID);
void* vboAddress = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if(!vboAddress) {
return;
}
memcpy((char*)vboAddress + sizeof( float ) * 12, color, sizeof( float ) * 12);
glUnmapBuffer(GL_ARRAY_BUFFER);
vboAddress = NULL;
glBindBuffer(GL_ARRAY_BUFFER, 0);
displayFunc();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(300, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow("Rectangle fill");
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(initialVBO), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(initialVBO), initialVBO);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glutDisplayFunc(displayFunc);
glutMouseFunc(mouseFunc);
glutMainLoop();
glDeleteBuffers(1, &vboID);
return 0;
}