• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# OpenGL questions

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.

12 replies to this topic

### #1Shawn619  Members

Posted 19 April 2013 - 05:42 PM

I have couple questions that would GREATLY assist me in understanding opengl. Some questions pertain to basic glsl functions, but i think the majority are opengl related, so i posted this in opengl thread instead of graphics.

(i) Each gl_ModelViewMatrix matrix needs to correspond to a gl_PushMatrix, because a gl_PushMatrix "creates" a new modelviewmatrix on the stack, right?(assuming matrixmode=gl_modelview)

If so, does the glsl shader program receive the corresponding vertex's modelviewmatrix when vertices are being passed to the shader?

To Further explain,
If i say

glPushMatrix();

glTranslatef(0,5,0);

glutSolidCube(20);//first cube

glTranslatef(0,5,0);

glutSolidCube(20);//second cube

glPopMatrix();

In the resulting modelviewmatrix from the above code, the components in the matrix would show a +10 x-axis translation. But that translation is only true for the second cube, while the first cube only has a +5 x-axis translation. So, how am i supposed to, for example, transform from object-eye coordinate space in the following code if i recieve a vertex from the first cube, while the corresponding modelviewmatrix has a 10 translation, when i really need the 5 translation:

v = vec3(gl_ModelViewMatrix * gl_Vertex);

(ii) In glsl, are "gl_LightSource[x]", where 'x' is 0 to max_lights, coordinates already in eye view when being passed to glsl?

### #2Brother Bob  Moderators

Posted 20 April 2013 - 03:33 AM

I have couple questions that would GREATLY assist me in understanding opengl. Some questions pertain to basic glsl functions, but i think the majority are opengl related, so i posted this in opengl thread instead of graphics.

(i) Each gl_ModelViewMatrix matrix needs to correspond to a gl_PushMatrix, because a gl_PushMatrix "creates" a new modelviewmatrix on the stack, right?(assuming matrixmode=gl_modelview)

There is one modelview matrix stack, and commands such as glPushMatrix, glTranslate, glRotate all affect the topmost matrix on the modelview matrix stack. Within the shader, gl_ModelViewMatrix reflects the value of the topmost matrix. Same goes for the other matrix stacks such as the projection matrix, the texture matrix and the color matrix.

If so, does the glsl shader program receive the corresponding vertex's modelviewmatrix when vertices are being passed to the shader?

To Further explain,
If i say

glPushMatrix();

glTranslatef(0,5,0);

glutSolidCube(20);//first cube

glTranslatef(0,5,0);

glutSolidCube(20);//second cube

glPopMatrix();

In the resulting modelviewmatrix from the above code, the components in the matrix would show a +10 x-axis translation. But that translation is only true for the second cube, while the first cube only has a +5 x-axis translation. So, how am i supposed to, for example, transform from object-eye coordinate space in the following code if i recieve a vertex from the first cube, while the corresponding modelviewmatrix has a 10 translation, when i really need the 5 translation:

v = vec3(gl_ModelViewMatrix * gl_Vertex);

When you draw the first cube, the matrix on the top of the modelvire matrix stack has the value of containing a translation by 5 units along the Y-axis in addition to what's before the glPushMatrix. When you draw the second cube, the topmost matrix on the modelview matrix stack now has an additional translation of 5 along the Y-axis.

Again, the value of gl_ModelViewMatrix reflects the value of the top of the modelview matrix stack. The matrix thus contains 5 and 10 units of translation when drawing the first and second cube, respectively, because that is what is on top of the matrix stack at the corresponding times.

(ii) In glsl, are "gl_LightSource[x]", where 'x' is 0 to max_lights, coordinates already in eye view when being passed to glsl?

The value of gl_LightSource is the vector you pass to glLight with the parameter GL_POSITION multiplied by the modelview matrix at the time the call was made. Thus:

glLoadIdentity();
glTranslate(...); // matrix A
glLight(GL_POSITION, ...); // position v
// draw 1
glTranslate(...); // matrix B
// draw 2


In draw 1: the value of gl_LightSource is A*v, and the value gl_ModelViewMatrix is A.

In draw 2: the value of gl_LightSource is A*v, and the value gl_ModelViewMatrix is A*B.

### #3Shawn619  Members

Posted 20 April 2013 - 10:19 AM

Thanks for the help, im really learning a lot.

(i) Oh, I believe my concept of the modelviewmatrix was skewed, but I understand now. One last question though, in relation to glsl, when is the shader program called? Is it called at each vertex draw (ie:"glVertex3f(0, 0, 0);") in any glBegin (ie: triangles, quads)? That would make sense, because the top-most modelviewmatrix hasn't been affected by future translations.

### #4Brother Bob  Moderators

Posted 20 April 2013 - 04:14 PM

The vertex shader is called once for each vertex of the primitive you draw. More or leds, once per call to glVertex.

### #5Shawn619  Members

Posted 20 April 2013 - 04:27 PM

When is the fragment shader called, immediately after the vertex shader on each glVertex clal?

### #6tanzanite7  Members

Posted 20 April 2013 - 04:29 PM

No. Good info about OpenGL pieline is here:

http://www.opengl.org/wiki/Rendering_Pipeline_Overview

### #7Shawn619  Members

Posted 20 April 2013 - 04:42 PM

If no, then when in the program in the fragment shader called? Can you elaborate

Thanks for the link, unfortunately all it tells me about the fragment shader is "The data for each fragment from the rasterization stage is processed by a fragment shader. The output from a fragment shader is a list of colors for each of the color buffers being written to, a depth value, and a stencil value. Fragment shaders are not able to set the stencil data for a fragment, but they do have control over the color and depth values."

### #8Brother Bob  Moderators

Posted 20 April 2013 - 04:46 PM

The fragment program is called once for each pixel. If you draw a triangle that covers 2000 pixels, then the fragmen program is called 2000 times, once for each pixel covered by the triangle.

### #9Shawn619  Members

Posted 20 April 2013 - 05:03 PM

Oh ok, so would it be fair say that the fragment shader is called for each pixel of a primitive(triangle,quad) at end of that primitive's draw?

ie:

glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 1, 0);
glEnd(); // fragment shader called here for each pixels of the triangle


### #10Brother Bob  Moderators

Posted 20 April 2013 - 05:08 PM

For the sake of explaining, yes, you can think of it like that. I just have to clarify myself a bit. If, for example, some pixels of the triangle are not drawn because they fail the stencil test or the depth test (the triangle is behind something that is already drawn) then those pixel may not have their fragment program called.

### #11Shawn619  Members

Posted 20 April 2013 - 05:16 PM

Ah, I understand now. I was thinking there was a 1:1 mapping of all vertices from the vertex to fragment shader, but I didn't account for those vertices lost as a result of failing those tests. Thanks!

### #12Brother Bob  Moderators

Posted 20 April 2013 - 05:17 PM

A vertex is never lost in the depth or stencil test; only fragments. It doesn't make sense to depth or stencil test a vertex.

### #13Shawn619  Members

Posted 20 April 2013 - 05:23 PM

Oh ok, thanks!

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.