0

I am having some problems understanding how lighting works in OpenGL (especially understanding the pre-defined variables. is there a list of them somewhere?). I have been messing around in my Vertex Shader file to shade my shpere. So I managed to shade it but I don't know what I did. Here is the code:

Vertex Shader:

#version 330
precision highp float;

in vec3 in_Position; //declare position
in vec3 in_Color; //color passed from the cpp file

//mycode
in vec3 in_Normal;

// mvpmatrix is the result of multiplying the model, view, and projection matrices */
uniform mat4 MVP_matrix;

vec3 ambient;

out vec3 ex_Color;

void main(void) {

// Multiply the MVP_ matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)

gl_Position = MVP_matrix * vec4(in_Position, 1.0);

//mycode
ambient = vec3(0.0f,0.1f,1.0f); // just a test vector

ambient = ambient * in_Position ; // HERE IS WHAT I DONT GET!

ex_Color = ambient;

}

I am multiplying each point on the sphere with a MVP matrix, and then with an ambient variable.

Fragment Shader:

#version 330
precision highp float;

in vec3 ex_Color;
out vec4 gl_FragColor;

void main(void) {

gl_FragColor = vec4(ex_Color,1.0);

}

What does "precision highp float;" does? I know that the vertex file passes the color to the fragment shader and then that fragment gets "interpolated". Ok how does interpolation works? How does Opengl know where to stop the interpolation ?

Trt Trt
  • 5,330
  • 13
  • 53
  • 86
  • 5
    "is there a list of them somewhere" Yes. [The OpenGL specification.](http://www.opengl.org/registry/) Really, you need to stop asking these questions piecemeal and just go [look up a good tutorial already](http://www.opengl.org/wiki/Getting_started#Tutorials_and_How_To_Guides). These kinds of questions aren't doing anyone any good. You're basically asking, "How does this entire part of OpenGL work?" – Nicol Bolas Nov 11 '11 at 02:28
  • @NicolBolas I am aware of the tutorials, I just don't have time, and most of them use custom libraries. SO that's why I asked this. – Trt Trt Nov 11 '11 at 08:43
  • 1
    @TrtTrt: You don't want to take the time to learn correctly, so instead you ask us to basically write tutorials for you. I'm not sure I understand your logic. And what's wrong with using "custom libraries"? Or do you believe that you gain something by reinventing the wheel? Learning something as complicated as graphics programming requires being able to cut right to the actual issue at hand. And that means using libraries to deal with things that _don't matter_. – Nicol Bolas Nov 11 '11 at 08:49
  • 1
    @NicolBolas: This "I don't have time" argument, and the whole lot of questions asked by Trt Trt remind me awfully of Skybuck Flying over at the comp.graphics.api.opengl newsgroup http://goo.gl/UTDik – datenwolf Nov 11 '11 at 12:51

1 Answers1

5
ambient = ambient * in_Position ; // HERE IS WHAT I DONT GET!

That makes two of us. I have no idea what it is that you intend to accomplish with this. That will certainly produce a color. But it won't be meaningful in any real way. It certainly doesn't qualify as "lighting".

What does "precision highp float;" does?

Nothing. Not in desktop OpenGL. I really have no idea why someone put that there; the precision highp stuff is for GL ES compatibility, but version 3.30 shaders are fundamentally incompatible with GL ES 2.0, since ES 2.0 doesn't use in/out qualifiers and desktop GLSL 3.30 does.

Ok how does interpolation works?

Way too broad to answer here. This is better answered here, which is part of my much larger tutorial series.

How does Opengl know where to stop the interpolation ?

The edge of your triangle.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982