Jump to content
  • Advertisement
Sign in to follow this  
skauert

OpenGL Problem rendering geometry with shaders

This topic is 2532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Gamedev.

For the past couple of days, I have been studying GLSL and I've been looking to try and implement it myself. However, despite continuous effort, I have been unable to get anything to draw on the screen. At the moment, as far as I can see, I am out of options. For this reason, I decided to ask for help here.

My current implementation isn't very big. It's available here: http://pastebin.com/Wp76fQDP

The shaders I'm using are here: http://pastebin.com/7xpZncuS

A lot of this is very similar to the 5th edition of the OpenGL Superbible, as that is what I've used to learn shader programming.

The shaders seem to compile and link just fine, and there are no OpenGL errors. As far as I can see, I must be doing something very wrong with geometry storing and/or linking them with the shader programs.

Any help is greatly appreciated. :-)

Share this post


Link to post
Share on other sites
Advertisement
I probably could have helped you but because of your use of SDL I am really lost looking at your code. I don’t know the side-effects of those SDL functions etc.

I know this is not what you came here to get, but my advice is to drop SDL and use OpenGL. Most tutorials and most people will become resources to you once you do this.
Wrappers tend to simplify the language a bit, but with the unfortunate side-effect of restricting the number of useful tutorials and the number of useful people at your disposal.


L. Spiro

Share this post


Link to post
Share on other sites

I probably could have helped you but because of your use of SDL I am really lost looking at your code. I don’t know the side-effects of those SDL functions etc.

I know this is not what you came here to get, but my advice is to drop SDL and use OpenGL.


SDL / OpenGL are not mutually exclusive. The SDL code just sets up the OpenGL context and looks fine (although you might want to use the SDL_GL_GetAttribute after context creation to check you got what you want (e.g. the multisample stuff may not actually be working).

I would suggest leaving deleting the shader objects until after the main loop and moving the link status check to before you try getting attribute locations (I'm not sure this technically matters though).


Otherwise, your buffer binding looks a bit off:

You need to call glEnableVertexAttribArray(vloc) and glEnableVertexAttribArray(cloc).

(Admittedly it's been a while since I've done any VBO stuff,) But I don't think you can put both color and vertex into one buffer like that. Try creating one buffer for each one - something more like this (untested):



glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
glBufferData(GL_ARRAY_BUFFER, sizeof(vVerts), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vVerts), vVerts);
glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, 0); // note last argument not used for vbos

glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
glBufferData(GL_ARRAY_BUFFER, sizeof(vColor), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vColor), vColor);
glVertexAttribPointer(cloc, 4, GL_FLOAT, GL_FALSE, 0, 0); // note last argument not used for vbos

// could really do this later in the rendering loop...
glEnableVertexAttribArray(vloc);
glEnableVertexAttribArray(cloc);


Another thing is that you don't appear to be transforming the vertex positions by the modelview matrix in the shader. If smath is calling the glRotate etc functions, you either need to be transforming by gl_ModelViewMatrix in the vertex shader (if the version allows) or getting the matrix from smath and passing it to the shader yourself.

Share this post


Link to post
Share on other sites
Thanks a lot, sprite! What you provided turns out to be exactly what I needed. What's odd is that I tried working with _just_ vertices yesterday and I still couldn't get it to draw. As far as I can recall, my code was very similar to what you provided here. Then again, I was going into my 6th hour of trying to get it to work, so my head might not have been functioning sufficiently. :/

So just to make sure I fully understand this -- to associate data from C/C++ with the shader program, all I need to do is use VBOs to store the data and then use glVertexAttribPointer() and glEnableVertexAttribArray() to associate it with the appropriate attribute ID?

EDIT: Oh, and sorry about the matrix stuff in there. The points were not actually supposed to be transformed in any way. At the moment I was simply trying to get -something- to display on the screen. I just pre-emptively put some transformation there to test out my home-made matrix library. :-)

Share this post


Link to post
Share on other sites
glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, 0); // note last argument not used for vbos

FYI, actually, the last parameter is used. It is the start address relative to 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!