Reputation: 395
I wrote this to test how to draw with opengl buffer,here is the function to initialize:
#include "GL/glew.h"
#include "GL/glfw.h"
int main ( int argc, char* argv[] )
{
//init glfw
glfwInit();
glfwOpenWindow ( 1024, 768, 8, 8, 8, 8, 24, 0, GLFW_WINDOW );
//init extension
glewInit();
//i
glGenBuffers ( 1, &buffID );
//init test buffer
for ( int i = 0 ; i < 15; i ++ ) {
testBuffer[i][0] += 1 + i * i;
testBuffer[i][1] += 1 + i + i * i;
testBuffer[i][2] = 0;
}
do {
render_loop();
} while ( glfwGetWindowParam ( GLFW_OPENED ) );
glfwTerminate();
}
and this is the render_loop function:
GLuint buffID;
static GLfloat testBuffer[15][3];
void render_loop()
{
glClearColor ( .7, .1, .1, 1.0f );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glViewport ( 0, 0, 1024, 768 );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
glOrtho ( 0, 1024, 768, 0, 0, -1 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
//draw a test point
glPointSize ( 10 );
glBegin ( GL_POINTS );
glColor4f ( 1, 1, 1, 1 );
glVertex3f ( 512, 384, 0 );
glEnd();
glBindBuffer(GL_ARRAY_BUFFER,buffID);
glBufferData(GL_ARRAY_BUFFER,3 * sizeof(GLfloat) * 15, testBuffer,GL_DYNAMIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,testBuffer);
glDrawArrays(GL_POINTS,0,15);
glBindBuffer(GL_ARRAY_BUFFER,0);
glDisableClientState(GL_VERTEX_ARRAY);
glfwSwapBuffers();
}
all 15 vertices are at 0,0 position which is top left corner of the window,I can see white points there,after I remove these 3 lines from loop:
glBindBuffer(GL_ARRAY_BUFFER,buffID);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat) * 15, testBuffer,GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);
everything works as I expected, I have a few questions:
1.what changed the vertex buffer position?
2.by the way how much perfomance gain using these calls?
Upvotes: 0
Views: 324
Reputation: 395
have to call this :
glBindBuffer(GL_ARRAY_BUFFER,0);
after glBufferData
function.
also I found I need to change this:
//added this line before glVertexPointer:
glBindBuffer(GL_ARRAY_BUFFER,buffID);
//glVertexPointer(3,GL_FLOAT,0,testBuffer); change last param to 0:
glVertexPointer(3,GL_FLOAT,0,0);
after added that,everything seems working well.
Upvotes: 1