Jump to content

  • Log In with Google      Sign In   
  • Create Account

render vbo not working


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Homunculi   Members   -  Reputation: 133

Like
0Likes
Like

Posted 20 May 2012 - 11:49 PM

I am new to opengl. I setup a set of classes to manage shaders. I am now trying to debug it and I want to stick to opengl 3.3+. The following is the code I have written to setup a vbo for rendering a triangle.

	Vertex triangle[3];
						
	triangle[0].x = 0.0f; triangle[0].y = 0.0f; triangle[0].z = 0.0f;
	triangle[0].nx = 0.0f; triangle[0].ny = 0.0f; triangle[0].nz = 1.0f;
	triangle[0].s0 = 0.0f; triangle[0].s1 = 0.0f;

	triangle[1].x = 1.0f; triangle[1].y = 0.0f; triangle[1].z = 0.0f;
	triangle[1].nx = 0.0f; triangle[1].ny = 0.0f; triangle[1].nz = 1.0f;
	triangle[1].s0 = 1.0f; triangle[1].s1 = 0.0f;
	
	triangle[2].x = 0.0f; triangle[2].y = 1.0f; triangle[2].z = 0.0f;
	triangle[2].nx = 0.0f; triangle[2].ny = 0.0f; triangle[2].nz = 1.0f;
	triangle[2].s0 = 0.0f; triangle[2].s1 = 1.0f;
	
	glGenBuffers(1,&tri_id);
	glBindBuffer(GL_ARRAY_BUFFER,tri_id);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*3,&triangle[0].x,GL_STATIC_DRAW);
	
	glGenBuffers(1,&ind_id);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ind_id);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*3,indices,GL_STATIC_DRAW);
	
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(char*)(0));
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(char*)(12));
	glEnableVertexAttribArray(2);
	glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,sizeof(Vertex),(char*)(24));


I then call this function in my main loop of the program:

	glClearColor(0.0f,0.0f,0.0f,1.0f);
	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, indices);

The shaders which I am using are just a basic fragment and vertex shader. I have checked and they have been compiled and loaded succesfully.

What happens is the screen blinks. The entire screen... rapidly... when running my program.

Edit:

I just noticed the triangle 0's for all verts thing and changed it... I also noticed the double bind buffer and removed that --_-

Edited by Homunculi, 21 May 2012 - 12:03 AM.


Sponsor:

#2 Vorel512   Members   -  Reputation: 130

Like
-1Likes
Like

Posted 21 May 2012 - 02:31 AM

Hello,

So...

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*3,&triangle[0].x,GL_STATIC_DRAW);
You should pass a pointer to "&triangle" and with sizeof(triangle) now you allocate memory for only the first position. And then try to set opengl where to search for specified data like normals. It would be a great start to read and try out some example."
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
   
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
   
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(24));

sizeof() gives you the size of the variable in bytes, not the amount of data stored in them. In your case all vertexes has 8 elements (3+3+2 = 8) and 3 vertexes (3*8=24).

#3 mhagain   Crossbones+   -  Reputation: 7979

Like
0Likes
Like

Posted 21 May 2012 - 03:51 AM

sizeof (Vertex) should be fine, and is preferable to hard-coding vertex-sizes in your glVertexAttribPointer calls - what happens if you ever need to add a second pair of texcoords to your vertex definition?

For your glBufferData call it's sufficient to use "glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*3,triangle,GL_STATIC_DRAW);" - triangle is an array and this will work. &triangle will - if you're lucky - crash the program.

If you're getting crazy flickering the most likely explanation is that you originally followed an old GLUT tutorial that created a single-buffered context. Changing that to double-buffered and adding the appropriate SwapBuffers call (glutSwapBuffers I believe) should resolve that.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 Homunculi   Members   -  Reputation: 133

Like
0Likes
Like

Posted 21 May 2012 - 04:26 PM

I am not using GLUT I am using SDL to create an opengl context. I have SDL_GL_DOUBLEBUFFER enabled so that isn't the issue. I even put glEnable(GL_DOUBLEBUFFER) as well.

I changed &triangle[0].x to just triangle as suggested.

I am fully aware of what sizeof() does. Anyways. After making the recommended changes it still doesn't work.

#5 Homunculi   Members   -  Reputation: 133

Like
0Likes
Like

Posted 21 May 2012 - 06:12 PM

Ok, well I have been placing glGetError around my code and doing a bit of debugging. I have narrowed it down a bit. The error I am getting is "GL_INVALID_ENUM" and I you were onto something with the double buffer perhaps... the spot where I get the error is on the call to

glEnable(GL_DOUBLEBUFFER);

I commented it out because it should be doublebuffered anyways since on SDL_SetVideoMode() I put the flag SDL_GL_DOUBLEBUFFER

I no longer get a gl error at any point in my code that I can get with glGetError()

But I am still having the same problem with it making my screen blink and not rendering my triangle.

EDIT:

Ok, so I just tested my shaders using fixed pipeline functions (glVertex3f() etc). The shaders do work fine. It just doesn't work when trying to render the VBO way >.<

EDIT2:

Ok, I fixed the issue last night. It was my call to glDrawElements that was incorrect. I was passing in the address of the array that held the indices when I should have really been passing BUFFER_OFFSET(0)

Edited by Homunculi, 22 May 2012 - 12:15 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS