Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


bbuerger

Member Since 26 Apr 2012
Offline Last Active Apr 27 2012 04:55 AM

Topics I've Started

GLSL Shader

26 April 2012 - 02:19 AM

Hi there,

I tried to write some simple phong shader but the result looks a bit weird.
Wrong.png

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.

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;
}

The model looks like this
WireView.png

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

PARTNERS