Jump to content
  • Advertisement
Sign in to follow this  

OpenGL am I missing something? (shaders)

This topic is 4369 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, Trying to make my first glsl app. A simple per fragment lighting on a sphere. I'm using the GLSLShader and GLSLProgram classes from "More OpenGL Game Programming". Here's the code: InitScene
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	quadric = gluNewQuadric();
	gluQuadricDrawStyle(quadric, GLU_FILL);
	gluQuadricNormals(quadric, GLU_SMOOTH);
	gluQuadricTexture(quadric, GL_TRUE);

	GLSLShader vert("pplight.vs");
	GLSLShader frag("pplight.ps", GL_FRAGMENT_SHADER);

	GLfloat lightpos[] = { 1.0, 0.0, 0.0, 0.0 };
	glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
	GLfloat lightcolor[] = { 1.0, 0.0, 0.0, 1.0 };
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor);
	GLfloat lightspeccolor[] = { 1.0, 1.0, 1.0, 1.0 };
	glLightfv(GL_LIGHT0, GL_SPECULAR, lightspeccolor);


	gluLookAt(0.0f, 0.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ,0.0f);

	gluSphere(quadric, 1.0, 60, 60);


#version 110
varying vec3 normal;
varying vec3 lightVec;
varying vec3 viewVec;

void main(void)
	vec4 ecPos = gl_ModelViewMatrix * gl_Vertex;
	normal = normalize(gl_NormalMatrix * gl_Normal);
	lightVec = vec3(normalize(gl_LightSource[0].position - ecPos));
	viewVec = vec3(normalize(-ecPos));
	gl_Position = ftransform();

#version 110
varying vec3 normal;
varying vec3 lightVec;
varying vec3 viewVec;

void main(void)
	vec3 nnormal = normalize(normal);
	vec3 nlightVec = normalize(lightVec);
	vec3 nviewVec = normalize(viewVec);
	vec3 reflectVec = reflect(-nlightVec, nnormal);
	float specValue = clamp(dot(reflectVec, nviewVec), 0.0, 1.0);
	specValue = pow(specValue, 6.0);
	gl_FragColor = vec4(gl_LightSource[0].diffuse * diffuseAmount * max(dot(nlightVec, nnormal), 0.0)) +
	(gl_LightSource[0].specular * specAmount * specValue).rgb, 1.0);

run it and... no lights! The sphere appears unlit. :( What am I missing? Thanks

Share this post

Link to post
Share on other sites
I have no idea, but have you tried enabling lighting? glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0).

and i guess you have made sure there are no compile-errors or linking problems..

Share this post

Link to post
Share on other sites
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!