PDA

View Full Version : Vertex array object with lines



advorak
07-21-2015, 08:07 AM
I am trying to draw a set of crosshairs. I am following the learnopengl.com example. Do I need to have a shader if I only want to draw lines and not other geometric objects? The example shows a triangle. He instances a shader so it will fill the triangle correctly. Do I need to have a shader if i am only drawing lines? I am drawing other lines without a shader, but they are dynamic and I am using glBegin/glEnd with glVertex3f calls to do it.

Here is my initialization code:


GLuint CH1_VBO[Output_Count], CH1_VAO[Output_Count];
GLfloat CH1_Vertices[] = { -0.235, 0.000, 0.950,
-0.176, 0.000, 0.950,
-0.118, 0.000, 0.950,
-0.059, 0.000, 0.950,
0.059, 0.000, 0.950,
0.118, 0.000, 0.950,
0.176, 0.000, 0.950,
0.235, 0.000, 0.950,
-0.029, 0.412, 0.950,
0.029, 0.412, 0.950,
-0.059, 0.206, 0.950,
0.059, 0.206, 0.950,
-0.059, -0.206, 0.950,
0.059, -0.206, 0.950,
-0.029, -0.412, 0.950,
0.029, -0.412, 0.950 };

glGenVertexArrays( 1, &CH1_VAO[Output] );
glGenBuffers( 1, &CH1_VBO[Output] );

glBindVertexArray( CH1_VAO[Output] );
glBindBuffer( GL_ARRAY_BUFFER, CH1_VBO[Output] );
glBufferData(GL_ARRAY_BUFFER, sizeof(CH1_Vertices), CH1_Vertices, GL_STATIC_DRAW);
glBindVertexArray( 0 );


once inside the main loop for each output display, I call:


glBindVertexArray( CH1_VAO[Output] );
glDrawArrays( GL_LINES, 0, 16 );
glBindVertexArray( 0 );


absolutely nothing happens. I know the output is being rendered, because I have text being rendered to each output channel.

What am I missing?

GClements
07-21-2015, 10:01 AM
Do I need to have a shader if I only want to draw lines and not other geometric objects? The example shows a triangle. He instances a shader so it will fill the triangle correctly. Do I need to have a shader if i am only drawing lines?
Whether or not you need a shader has nothing to do with whether you're drawing lines or triangles.

You need to use a shader if you don't want to use the fixed-function pipeline .If you're using an OpenGL 3+ core profile context, the fixed-function pipeline isn't available so you have to use shaders for any rendering.

In the compatibility profile, you can mix old and new features more or less freely. E.g. you can use VBOs and VAOs to specify vertex attributes for the fixed-function pipeline, or you can use glBegin/glEnd to specify vertex attributes for a shader program.



Here is my initialization code:



glBindVertexArray( CH1_VAO[Output] );
glBindBuffer( GL_ARRAY_BUFFER, CH1_VBO[Output] );
glBufferData(GL_ARRAY_BUFFER, sizeof(CH1_Vertices), CH1_Vertices, GL_STATIC_DRAW);
glBindVertexArray( 0 );


absolutely nothing happens. I know the output is being rendered, because I have text being rendered to each output channel.

What am I missing?
Nowhere in the posted code do you call glVertexAttribPointer() (or glVertexPointer()) to associate the array with a vertex attribute. When you subsequently call glDrawArrays(), the VAO is still in its default state.

advorak
07-21-2015, 10:53 AM
You had me until you started talking about OpenGL 3+ core profile context -vs- compatibility profile. Could you expound on this a little?

I have the fixed-function pipeline available to me as I have drawn other very dynamic lines, but it appears i can not mix the modes as in your "compatibility profile."

I understood you to say I need a shader even if i am only drawing lines. As I need a shader for other objects on the screen, I will implement it now.

Thank you for your help.

GClements
07-21-2015, 12:53 PM
You had me until you started talking about OpenGL 3+ core profile context -vs- compatibility profile. Could you expound on this a little?
OpenGL 3+ has two profiles: core and compatibility. In the core profile, legacy features such as glBegin/glEnd() and the fixed-function pipeline aren't available. The compatibility profile has all of the legacy features as well as the newer ones.

The main reasons for using the core profile are simplicity (you don't need to understand how legacy features interact with newer features, as the legacy features don't exist), and the fact that some platforms (notably Mac) only provide a compatibility profile for older OpenGL versions (the Mac only offers a compatibility profile for OpenGL 2.1, so if you want to use newer features you're limited to the core profile).


I understood you to say I need a shader even if i am only drawing lines.Thank you for your help.
No. If legacy features such as glBegin/glEnd work, then you don't need a shader. However, if you aren't using a shader, you need to use the fixed-function attributes, i.e. glVertexPointer() etc rather than glVertexAttribPointer(). Bindings for fixed-function attributes are stored in VAOs alongside those for user-defined attributes.

In any case, you appear to have missed my main point:


Nowhere in the posted code do you call glVertexAttribPointer() (or glVertexPointer()) to associate the array with a vertex attribute. When you subsequently call glDrawArrays(), the VAO is still in its default state.


That's the fundamental reason why you aren't getting anything; you've created and filled a buffer but haven't told OpenGL to use it to obtain the vertex positions (as well as calling glVertexPointer(), you also need to enable GL_VERTEX_ARRAY).