GLSL Shaders

Started by
-1 comments, last by Neutrinohunter 16 years, 1 month ago
I'm having trouble with any form of shader at the moment, it seems I get weird artefacts when I move around, And this happens with any form of shader so I'm wondering whether there is a problem with my matrices or in the way I'm using them. I should also mention if I slip someone else's shader in for Shadow Volumes/Shadow Mapping it also does the same thing, yet works perfectly in their respective projects. I'm following the good ol' Open GL Shading Language as a guide but its not helping I'm finding. The following is someone else's shader which works fine in their project, but not mine so I am wondering what could be the problem (obviously my code doesn't work :P)

uniform vec3 uLightPos;

#define EXTRUSION_FACTOR 100.0

void main()
{
	vec4 Position = gl_Vertex;
	vec3 LightDir = normalize(uLightPos - Position.xyz);
  	float DiffuseLight = max(dot(gl_Normal, LightDir), 0.0);

	//Check if the vertex is facing the light.
	if(DiffuseLight <= 0.0)
	{
		//Extrude!
		Position = Position + (Position - vec4(uLightPos, 1.0)) * EXTRUSION_FACTOR;

		//Place the extruded vector at infinity.
      	Position.w = 0.0;
	}

	//Transform position to clip space.
	gl_Position = ftransform();
}

My Shader Algorithm:

void JShadowVolumeShaderAlgorithm::render(GLfloat x, GLfloat y, GLfloat z) {

   	//Save all attributes.
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45.0f, 1.0f, 0.1f, 1000.0f);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   scene->updateCameraView();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
   
   int noOfLights = scene->getLightManager()->getNumberOfLightsUsed();
   //JModelManager * modelManager = scene->getModelManager();
   //for each light
   for(int i = 0; i < noOfLights ; i++) {
      JVector3D lightPos = scene->getLightManager()->getLight(i)->getPosition();
      GLfloat lPos [4] = {lightPos.x, lightPos.y, lightPos.z, 1.0};
      glLightfv(GL_LIGHT0, GL_POSITION, lPos);

	  //Fill the depth buffer.
      glDisable(GL_LIGHTING);
    
      scene->renderInternalProperties();
      scene->renderSceneModels();

	  //Prevents z-fighting.
      if (clipPlaneBehindLight) {
         glEnable(GL_POLYGON_OFFSET_FILL);
         //glPolygonOffset(0.0f, 100.0f);
         glPolygonOffset(bias, offset);
      }
      glDepthMask(GL_FALSE);
      glDepthFunc(GL_LESS);
      glEnable(GL_STENCIL_TEST);
      glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT); //Use the two side stencil buffer.
	
      glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
   
      glActiveStencilFaceEXT(GL_FRONT);
      glStencilOp(GL_KEEP, GL_INCR_WRAP_EXT, GL_KEEP);
      glStencilFunc(GL_ALWAYS, 0, 0);

	//Decrement the stencil buffer.
   	glActiveStencilFaceEXT(GL_BACK);
	   glStencilOp(GL_KEEP, GL_DECR_WRAP_EXT, GL_KEEP);
   	glStencilFunc(GL_ALWAYS, 0, 0);

      vp->Use();
         glUniform3fARB(m_glslLightPos, lightPos.x, lightPos.y, lightPos.z);
         scene->renderSceneModels();      
   	vp->Disable();
	
   	glDisable(GL_POLYGON_OFFSET_FILL);
      glDepthFunc(GL_LEQUAL);
   	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	
   	glActiveStencilFaceEXT(GL_FRONT);
      glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
   	glStencilFunc(GL_EQUAL, 0, 0xFFFFFFFF);

      glActiveStencilFaceEXT(GL_BACK);
      glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
      glStencilFunc(GL_EQUAL, 0, 0xFFFFFFFF);

	  //The light pass.
      glEnable(GL_LIGHTING);
      scene->renderInternalProperties();
      scene->renderSceneModels();
	
      glDisable(GL_BLEND);
   }
}

I'm wondering whether its something to do with incorrect normals, but can't be sure. Neutrinohunter

This topic is closed to new replies.

Advertisement