Sign in to follow this  
bbuerger

GLSL Shader

Recommended Posts

Hi there,

I tried to write some simple phong shader but the result looks a bit weird.
[sharedmedia=core:attachments:8477]

I guess that the shader codes are correct and think that the problem is because
of the normals. I calculate them my myself and pass them as vertex normals.

[CODE]
QVector<float> WavefrontObj::calculateNormals(const float *vertices, unsigned int numVertices, const unsigned int *indices, unsigned int numIndices)
{
// Foreach vertice we calculate a normal
QVector<float> vecNormals(numVertices);
for(unsigned int i=0; i<numIndices; )
{
hduVector3Df firstvec = hduVector3Df(&vertices[indices[i+1]*3])-hduVector3Df(&vertices[indices[i]*3]);
hduVector3Df secondvec = hduVector3Df(&vertices[indices[i]*3])-hduVector3Df(&vertices[indices[i+2]*3]);
hduVector3Df normal = crossProduct(firstvec, secondvec);
for(int v=0; v<3; ++v, ++i)
{
unsigned int index = indices[i]*3;
assert(index<numVertices && "Error in vertices");
vecNormals[index]-=normal[0];
vecNormals[index+1]-=normal[1];
vecNormals[index+2]-=normal[2];
}
}
// Normalize all normals
for(int i=0; i<vecNormals.size(); i+=3)
{
hduVector3Df normal(&vecNormals[i]);
normal.normalize();
vecNormals[i]=-normal[0];
vecNormals[i+1]=-normal[1];
vecNormals[i+2]=-normal[2];
}
return vecNormals;
}
[/CODE]

The model looks like this
[sharedmedia=core:attachments:8478]

Is there a better way to calculate the normals? I tried calculating them using the geometry shader but this gives the image a facet look.

Thanks for any hints,
bbuerger

Share this post


Link to post
Share on other sites
Are you sharing vertices between edges? If you are, you'll have problems because the normal on the outside flat surface needs to be different to the normal on the inside circular surface.

Share this post


Link to post
Share on other sites
Flat edges should NEVER share vertices. You need to manually in a 3d program separate the inner cylinder from the rest of the model, then separate all flat sides of the remaining cube.

Share this post


Link to post
Share on other sites
Yes I have no duplicate edges. Thanks for the hints then I will recreate my model or search for a program that create duplicate vertices if the angle is too big.

Share this post


Link to post
Share on other sites
Ok this solved the problem.
Blender has a modifier edge split that can reinsert the missing vertices automatically.
Thanks for the good advice.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this