Jump to content
  • Advertisement
Sign in to follow this  
krienie

OpenGL normal transformation and rendering goes wrong...

This topic is 2477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

I have a problem with rendering out my vertex normals in GLSL. I have a simple scene with one point light, a cube and a low-poly sphere. Now I'm trying to render out the vertex normals of the objects, but somehow they render not as expected.

Here is the normal data I pass to the shader for the cube:
GLfloat tmpNorm[24] =
{ -1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f };


For the ball I used the Icosahedron sample from the OpenGL red book 7th edition, so the normal data should be right, but here is what I get:

[attachment=5736:renderComposition.jpg]

Not quite what you'd expect. The normals for the ball are all facing one direction and the normals of the cube are facing inwards.
Just to make sure it wasn't my awefull GLSL skills, I used this shader to render out the normals, as shown below (the shader from answer 2, with the bunny :)).

Vertex shader:
#version 150 core

in vec3 vPos;
in vec3 vNorm;

uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat4 normMatrix;

out Data
{
vec4 vPos;
vec4 vNorm;
mat4 mvp;
} vdata;

void main()
{
vdata.mvp = mvpMatrix;
vdata.vPos = vec4(vPos, 1.0f);
vdata.vNorm = normalize(normMatrix * vec4(vNorm, 1.0f));}


Geometry shader:
#version 150 core
layout(triangles) in;
layout(line_strip, max_vertices = 6) out;

in Data
{
vec4 vPos;
vec4 vNorm;
mat4 mvp;
} vdata[3];

out gData
{
vec4 color;
} gdata;

void main()
{
const vec4 green = vec4(0.0f, 1.0f, 0.0f, 1.0f);
const vec4 blue = vec4(0.0f, 0.0f, 1.0f, 1.0f);

for (int i = 0; i < 3; i++)
{
gl_Position = vdata.mvp * vdata.vPos;
gdata.color = green;
EmitVertex();

gl_Position = vdata.mvp * (vdata.vPos + vdata.vNorm);
gdata.color = blue;
EmitVertex();

EndPrimitive();
}
}


Fragment shader:
#version 150 core

in Data
{
vec4 color;
} gdata;

out vec4 outputColor;

void main()
{
outputColor = gdata.color;
}


So I then figured it had to be my normalmatrix. To calculate the normalmatrix for each object, I use the inverted and then transposed upper-left 3x3 matrix of the object's modelmatrix.
Because my rendering engine can only work with 4x4 matrices, I put that normal matrix in a 4x4 matrix like so:

[ n1, n2, n3, 0,
n4, n5, n6, 0,
n7, n8, n9, 0,
0, 0, 0, 1]


According to my research that should be correct, so now I'm really lost. I really don't know what I"m doing wrong here. The normal matrix seems right and so does the shader (although I'm not sure why the normals are rendered in black, instead of green and blue....), so I was wondering if you guys could help me figure out what I'm doing wrong here.

I hope I've given enough info for you guys to help me out.
Many thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!