PDA

View Full Version : Issues drawing my first vertex (ogldev/opengl-tutorial)



fuerchter
03-20-2013, 06:56 AM
Hi there,
I started picking up some tutorials recently as I'm going to learn OpenGL at my university next semester.

I am currently trying to go through the ogldev tutorials and opengl-tutorial.org's tutorials, but I am already stuck at a fairly early stage :S

I managed to compile and (mostly...I think) understand the second tutorial on ogldev (can't post the links here apparently because I am a new forum member?!) but I wondered why on opengl-tutorial.org's tutorial 2 (no links, same here >.<) one has to create a vertex array object. So I decided to try and modify the code presented there to match ogldev's version but somehow as soon as I get rid of the vaos, nothing is being drawn at all. Here's an excerpt of the main drawing part that I changed, maybe someone can spot for my why nothing is drawn to the screen?



static const GLfloat g_vertex_buffer_data[] = {
0.0f, 0.0f, 0.0f,
};

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

do{

// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );

// Use our shader
//glUseProgram(programID);

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

// Draw the triangle !
//glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
glDrawArrays(GL_POINTS, 0, 1);

glDisableVertexAttribArray(0);

// Swap buffers
glfwSwapBuffers();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );

Claude
03-20-2013, 09:01 AM
I'm new to this too,but I noticed that you're not using shaders.
I think this tut app open a core profile context which doesn't support a default rendering state.

fuerchter
03-20-2013, 09:39 AM
I think I don't understand 100% what you're saying but maybe you are referencing the following line from the original code?


glewExperimental = true; // Needed for core profile

Even if I remove this line it won't draw anything though unfortunately.

About shaders: I picked the ogldev tutorial because of that because drawing a vertex was already kind of hard for me to comprehend. I think I should be able to draw a vertex without using the shading part from the other tutorial though, right? :S

carsten neumann
03-20-2013, 09:58 AM
He is referring to these lines:



glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL


Requesting a requesting a core profile context means you don't have access to deprecated functionality and must use vertex buffer objects, shaders, etc. to draw. Yes, that may seem to increase the initial hurdle you have to get over to be able to do anything with OpenGL, but it does pay off, because you will get a better understanding of what happens on the graphics hardware and how you can influence it in the various shader stages.

fuerchter
03-20-2013, 10:13 AM
Oh I see ^^

I just changed the opengl profile to compatibility and it displays the vertex (yay!)

So I have to ask though what exactly does it change to switch the opengl profile?


a core profile context doesn't support a default rendering state.

What do you mean by a default rendering state in this case?
Already thanks a ton guys ;)

carsten neumann
03-20-2013, 12:02 PM
Certain functionality is not available in a core profile, among them glBegin()/glEnd(), the matrix stacks, built in vertex attributes, ...
The list is quite long, you can look at the specs in the registry (http://www.opengl.org/registry/), since version 3.2 there is a core and a compatibility profile spec. For more recent versions the core profile spec tries to keep the same chapter numbers as the compatibility spec (some chapters/sections are then simply empty), so it may be easier to see the differences there.

Terminology nitpick: you don't draw a vertex, you draw a primitive for example a GL_POINT, or GL_TRIANGLE. Each of those is specified by a one or more sets of vertex attributes like positions, normals, colors, texture coordinates, etc. (these are processed by a vertex shader), get rasterized producing fragments (which are processed by a fragment shader), and are then written to a framebuffer. Omitting lots of details and optional steps of course, but the tutorials on OpenGL 3+ out there should take you through these.

fuerchter
03-20-2013, 12:17 PM
Thanks once more ;)

So I literally have to use shaders to draw something though?

Or does that just mean, that the code I have here uses deprecated features of opengl? (what did claude mean by the default rendering state?)

Claude
03-20-2013, 04:50 PM
I meant the way rendering was done before shaders.
As you've seen,you still can use deprecated features by using a compatibility context,but
they strongly recommend you don't.
Anyway,I think the 4th tut of the ogldev serie introduces shader.

fuerchter
03-21-2013, 09:42 AM
you can look at the specs in the registry (http://www.opengl.org/registry/), since version 3.2 there is a core and a compatibility profile spec

Do you think it may make sense to read through the specification instead of using a tutorial? (this is a very broad question, I know, it's just that I find it hard to understand most of the tutorials I see on the web. Maybe I'm not ready for OpenGL yet? :S)

//EDIT:
Also something that managed to confuse myself: On the ogldev version I just switched to the core profile via


glutInitContextProfile(GLUT_CORE_PROFILE);

and the program still draws the dot in the middle of my screen? As far as I know the code doesn't use shaders yet, so what am I missing. To top that off the method "glutInitContextProfile" doesn't even seem to exist according to the documentation I am looking at (maybe I have the wrong documentation? This one? (http://www.opengl.org/resources/libraries/glut/spec3/spec3.html)). I just found that method when I searched for the OpenGL profiles in glut on google.

carsten neumann
03-21-2013, 02:28 PM
No, I would not recommend reading the spec instead of a tutorial. It is not written to teach you OpenGL, it is written so that different OpenGL implementations (i.e. graphics drivers) produce (more or less) the same output, given the same input - modulo driver/spec bugs and parts deliberately left implementation defined of course. You are not saying what your problem with tutorials is, but have you tried different ones? Have you tried working carefully through the initial chapters, there will be a loft of stuff to absorb, some may not make sense on the first reading, so before getting stuck on some detail you could read on a little and come back to the problem once you've gained a better overview of things?

Calls to glutInitContextProfile() probably only have an effect if done before glutCreateWindow() or perhaps even glutInit() (sorry, don't know it off-hand) - an OpenGL context can not switch between profiles, you select the profile before you create the context and stick with it until the end your application - or you create a another context (which is an usual/advanced thing).
The original GLUT library is not being developed any more (the one your documentation link points to), there are alternate implementations of the same functionality (freeglut (http://freeglut.sourceforge.net/) is one) and some have implemented new stuff that did not exist in the original GLUT.

All that being said, don't get hung up on the fact that it happens to work (that only means you either don't have a core profile context or your OpenGL implementation does not fully enforce the "restrictions" of the core profile). Either way, in graphic programming the fact that something works on your OS, graphics card and driver combination does not mean it is correct, OpenGL does not (and sometimes can not) diagnose all kinds of misuse.

fuerchter
03-22-2013, 06:49 AM
No, I would not recommend reading the spec instead of a tutorial. It is not written to teach you OpenGL, it is written so that different OpenGL implementations (i.e. graphics drivers) produce (more or less) the same output, given the same input - modulo driver/spec bugs and parts deliberately left implementation defined of course. You are not saying what your problem with tutorials is, but have you tried different ones? Have you tried working carefully through the initial chapters, there will be a loft of stuff to absorb, some may not make sense on the first reading, so before getting stuck on some detail you could read on a little and come back to the problem once you've gained a better overview of things?

My problem with the tutorials up until now was that the basic process of drawing this point e.g. was not that thoroughly explained. I think ogldev's tutorial did the best at explaining it now though (after the fact). Even reading what the reference said about glGenBuffers, glBindBuffer and glBufferData didn't really help me that much. For example on opengl-tutorial.org's tutorial it says:


// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

Keep in mind here, I am not trying to talk bad about the tutorials, I am very grateful, that people are making an effort to explain the api and distribute their knowledge for free, but I just needed to know a little more about those commands. The specification actually cleared some of that stuff up for me in chapter 2.5 now but then again I don't know how necessary it is to comprehend these things yet, since, as a beginner I don't have much of an overview over the api (obviously ^^).

I do feel that you're right anyways about getting stuck on some details as in that I should probably just continue with the tutorials for now.


Calls to glutInitContextProfile() probably only have an effect if done before glutCreateWindow() or perhaps even glutInit() (sorry, don't know it off-hand) - an OpenGL context can not switch between profiles, you select the profile before you create the context and stick with it until the end your application - or you create a another context (which is an usual/advanced thing).
The original GLUT library is not being developed any more (the one your documentation link points to), there are alternate implementations of the same functionality (freeglut (http://freeglut.sourceforge.net/) is one) and some have implemented new stuff that did not exist in the original GLUT.

I did put that method in front of glutInit() just to test this, but it didn't change it. As a matter of fact I don't feel like finding the reason for that though, as I (as you said) feel like I am getting hung up on the little things with that.

I was actually already using freeglut with the ogldev tutorial but somehow didn't find the reference there at first try ^^ Oddly enough glutInitContextProfile() is not mentioned in that reference either though.