# OpenGL OpenGL questions

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

## Recommended Posts

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!

##### Share on other sites

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.

##### Share on other sites

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.

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

##### Share on other sites

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

##### Share on other sites
No. Good info about OpenGL pieline is here:

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

##### Share on other sites

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

##### Share on other sites

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.

##### Share on other sites

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


##### Share on other sites

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.

##### Share on other sites

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!

##### Share on other sites

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.

Oh ok, thanks!

• ### Similar Content

• By _OskaR
Hi,
I have an OpenGL application but without possibility to wite own shaders.
I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
• By xhcao
Does sync be needed to read texture content after access texture image in compute shader?
My simple code is as below,
glUseProgram(program.get());
glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
glDispatchCompute(1, 1, 1);
// Does sync be needed here?
glUseProgram(0);
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);

Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?

• My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:

• I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.

#define NUM_LIGHTS 2
struct Light
{
vec3 position;
vec3 diffuse;
float attenuation;
};
uniform Light Lights[NUM_LIGHTS];

• By pr033r
Hello,
I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things.
I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help.
My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
Exe file (if you want to look) and models folder (for those who will download the sources):
http://leteckaposta.cz/367190436
Thanks for any help...

• 14
• 16
• 10
• 17
• 9