How to get gl_Position.w ?

Started by
3 comments, last by Sik_the_hedgehog 11 years, 2 months ago

I copied the OpenGL ES 2.0 style of specifying vertices with an "attribute vec4 position;" along with other things (is this how it's supposed to be done in OpenGL 4?).

But now my shadow mapping is messed up as the shadows aren't effected by the camera's rotation/movement.

I think it might be caused by the w component of "position" not matching what it would be in gl_Position.

What should I be setting the w component to?

Here is my shader code:

Vertex shader


attribute vec4 position;

uniform mat4 projection;
uniform mat4 model;
uniform mat4 view;
uniform mat4 modelViewInv;

uniform mat4 lightMatrix;
uniform vec3 lightPos;
uniform vec3 lightDir;

varying vec4 lpos;

attribute vec3 normalIn;
varying vec3 normalOut;

attribute vec2 texCoordIn0;
varying vec2 texCoordOut0;

void main(void)
{
  vec4 vpos = (view * (model * position));
  lpos = lightMatrix * vpos;
  gl_Position = projection * (view * (model * position));

  vec3 normalEyeSpace = vec3( modelViewInv * vec4(normalIn, 0.0) );
  normalOut = normalize(normalEyeSpace);

  texCoordOut0 = texCoordIn0;
}

Fragment shader



uniform vec4 color;

uniform sampler2D texture0;
uniform sampler2D shadowMap;

varying vec4 lpos;

varying vec2 texCoordOut0;

void main (void)
{
  vec3 smcoord = lpos.xyz / lpos.w;
  float shadow = max(0.5, float(smcoord.z <= texture2D(shadowMap, smcoord.xy).x));

  vec4 texColor = texture2D(texture0, texCoordOut0);

  gl_FragColor = vec4(color.xyz * texColor.xyz * shadow, color.w * texColor.w);
}

Advertisement

What should I be setting the w component to?

1 (the projection matrix will take care about the rest)

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Never mind, got it working.

I think varying is deprecated. Don't take my word for that though.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

I think varying is deprecated. Don't take my word for that though.

All uniform, varying and attribute were removed and instead in, out and inout are used. The problem is that GL < 3 only supports the former, while GL >= 3 only supports the latter (compatibility profile aside), unless I misunderstood something, so what keywords you use depends on how much old hardware do you pretend to support.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement