PDA

View Full Version : Help with shaders



Magnificence
04-30-2011, 02:39 PM
So I constructed my engine with OpenGL 1.1+. I did this to make it a little easier for me to get my matrices straight. Now that I think I have, I'm working on the OpenGL 3.0+ implementation, but apparantly my scene remains black (background color). I want to know what's wrong with my code. First, lets take a look at my vertex and fragment shaders:

Vertex:

#version 130

uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 projection_matrix;

in vec3 a_Vertex;
out vec4 color_output;

void main()
{
vec4 pos = model_matrix * vec4( a_Vertex, 1.0 );
pos = view_matrix * pos;

gl_Position = projection_matrix * pos;
color_output = vec4( 1.0, 1.0, 1.0, 1.0 );
}
I'm passing the color white to the fragment shader because I want to implement the textures a little later, for now, I just want to be able to load models.
So model_matrix is the matrix that positions every model to be drawn and view_matrix positions the 'camera'. In my OpenGL 1.1 implementation, I would:
Push the view matrix For each model:
Push the model matrix
Pop the model matrix
Pop the view matrix
I hope I'm getting the same effect with what I'm doing right now in my vertex shader.

Fragment:

#version 130

in vec4 color_input;
out vec4 color_output;

void main()
{
color_output = color_input;
}


There are now compile errors and I'm convinced I don't do anything else that should fail because I don't get any access violations (segmentation fault).

Any help would be appreciated, thanks.

Yomboprime
04-30-2011, 03:58 PM
I think you should write the variable gl_Position in the vertex shader with a value that is transformed to homogeneous coordinates( using projection and view matrices?), and in the fragment shader you should write the colour to gl_FragColor.

Also, the varyings you declare are wrong. It should be

out vec4 color_output;
in the vertex shader, and

in vec4 color_output;
in the fragment shader

Alfonse Reinheart
04-30-2011, 04:27 PM
There are now compile errors and I'm convinced I don't do anything else that should fail because I don't get any access violations (segmentation fault).

Yomboprime's correct as to your error, which is why you generally shouldn't put words like "output" or "input" in your interface variable names, since what is an output on one side must have the same name as the input on the other.

However, OpenGL shouldn't crash. Unless you pass OpenGL a bad pointer, OpenGL isn't supposed to crash, ever. OpenGL shows errors by, well, providing errors. A GLSL compiler error should not crash; it should give a compiler error log. That log should explain the problem, and possibly even the line number on which it happened.

Magnificence
05-03-2011, 03:36 PM
ok, so now I have the following shaders:

Vertex:


#version 130

uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 projection_matrix;

in vec3 a_Vertex;
out vec4 color;

void main()
{
vec4 pos = model_matrix * vec4( a_Vertex, 1.0 );
pos = view_matrix * pos;

gl_Position = projection_matrix * pos;
color = vec4( 1.0, 1.0, 1.0, 1.0 );
}


Fragment:


#version 130

in vec4 color;

void main()
{
gl_FragColor = color;
}


The scene is still black, but are the shaders ok now?
Also, in my book, they use the following fragment shader:



#version 130

in vec4 color;
out vec4 outColor;

void main(void)
{
outColor = color;
}


Why do I have to use gl_FragColor? Or is that just an other way of doing it, maybe forward compatible?

arekkusu
05-03-2011, 04:42 PM
gl_FragColor is deprecated, the opposite of forward compatible.