Nooble
Nooble

Reputation: 572

Confused about GLSL and OpenGL

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

out vec4 fragColour;

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

gl_Position = vec4(vertex_position, 1.0)

I do

gl_Position = vec4(vertex_position, 2.0)

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.

Upvotes: 2

Views: 138

Answers (1)

A "vertex" whose w coordinate is 0 is considered to be infinitely far, so it won't be displayed. w of 0 is used to represent directions, not points.

Setting w equal to z will remove any sense of depth. The actual 3D coordinates of a point [x, y, z, w] are [ x/w, y/w, z/w ] (that's the principle of homogenous coordinates).

Normally, you keep w fixed at 1.0, and set the vertex position via x, y and z.

Upvotes: 4

Related Questions