Professional Documents
Culture Documents
C++ - Confused About GLSL and OpenGL - Stack Overflow
C++ - Confused About GLSL and OpenGL - Stack Overflow
Asked 9 years, 7 months ago Modified 9 years, 7 months ago Viewed 127 times
So I am fairly new to OpenGL, and I have been experimenting around with GLSL, and of course, I have
run into some problems. First of all, I should note that I am currently only drawing a singular triangle.
2 Second, here are my (really simple) shaders:
// Vertex Shader
#version 330
in vec3 vertex_position;
void main()
{
gl_Position = vec4(vertex_position, 1.0);
}
and
// Fragment Shader
#version 330
void main() {
fragColour = vec4(0.0, 1.0, 0.0, 1.0);
}
This works well enough to draw a triangle. Great. Now I thought of editing the Vertex Shader such that
instead of
I do
and sure enough, it magnifies my triangle. So I thought, maybe if I take the Z from my vertex shader and
set the W equal to it, I could get a sense of depth, so I did this:
// Vertex Shader
#version 330
in vec3 vertex_position;
void main()
{
gl_Position = vec4(vertex_position, vertex_position.z);
}
but alas, when I run the program, it shows nothing. In case people are wondering, here are my vertices:
float points[] = {
0.0f, 0.5f, 1.0f,
0.5f, -0.5f, 1.0f,
-0.5f, -0.5f, 1.0f
};
Also, I should say that I noticed that changing the z value in float points[] to anything other than 0
makes that part of the triangle invisible for some reason.
Share Improve this question Follow edited Sep 10, 2014 at 20:35 asked Sep 10, 2014 at 19:59
Nooble
562 7 17
Normally, you keep w fixed at 1.0, and set the vertex position via x , y and z .
Share Improve this answer Follow answered Sep 10, 2014 at 20:22
Angew is no longer
proud of SO
169k 18 363 464
Okay, then if I edit float points[] 's Z's to 1, it should set W = 1, but this still doesn't show anything. Isn't
vertex_position.z supposed to get the given vertex's Z value? And if that Z value is 1, then my code should
automatically set W to 1? – Nooble Sep 10, 2014 at 20:25
@Nooble: Well, Z=1 in NDC coordinates you're pushing the vertex out of the clip space, so all your vertices get
clipped. You should read up on how to do transforms properly. I.e. building a model and a projection matrix and
hwo those work. – datenwolf Sep 10, 2014 at 20:58
@Angew I do know how to use matrices, I was just curious about this. Any number at all besides zero seems to
push it out of Clip Space. For example, 0.5, which is well in between -1 to 1. Speaking of -1 to 1, is it inclusive?
– Nooble Sep 10, 2014 at 22:40
1 @Nooble Look at how you have written your vertex program: Zh = Z and W = Z. Then at perspective divide stage,
when the homogenous coordinates are transformed to cartesian coordinates, z = Zh/W = Z/Z = 1. So you got it
clipped and see nothing. – Amadeus Sep 11, 2014 at 0:53