OpenGL questions

Started by
11 comments, last by Shawn619 10 years, 12 months ago

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?

I appreciate any answers, thanks!

Advertisement

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?

I appreciate any answers, thanks!

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.

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.

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

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

No. Good info about OpenGL pieline is here:

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

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."

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.

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

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.

This topic is closed to new replies.

Advertisement