Advertisement

Light position/rotation

Started by March 12, 2017 01:41 AM
-1 comments, last by ta0soft 7 years, 8 months ago

So I'm a complete noob when it comes to OpenGL and GLSL but I have experience with DirectX. I'm developing a simple 3D application with a rotating sphere. The problem I'm facing is the rotation effects the position of the light, I'd like the light position to remain fixed while the sphere rotates. I know this is a shader issue because the view matrix and light position are stored in uniform values.

I tried creating a new uniform and multiplying by that instead of viewMatrix, I also tried resetting the view matrix to identity state before/after drawing the sphere but neither solution worked. I'm using Juce framework to develop the application but I don't think that makes a difference. Can someone point me in the right direction? Tom


if (p_Uniforms->projectionMatrix != nullptr) p_Uniforms->projectionMatrix->setMatrix4(CreateProjectionMatrix().mat, 1, false);
if (p_Uniforms->texture != nullptr) p_Uniforms->texture->set((GLint)0);
if (p_Uniforms->lightPosition != nullptr) p_Uniforms->lightPosition->set(-15.0f, 10.0f, 15.0f, 0.0f);
if (p_Uniforms->viewMatrix != nullptr) p_Uniforms->viewMatrix->setMatrix4(CreateViewMatrix().mat, 1, false);

attribute vec4 position;
attribute vec4 normal;
attribute vec4 sourceColour;
attribute vec2 texureCoordIn;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec4 lightPosition;

varying vec4 destinationColour;
varying vec2 textureCoordOut;
varying float lightIntensity;

void main()
{
	destinationColour = sourceColour;
	textureCoordOut = texureCoordIn;
	
	vec4 light = viewMatrix * lightPosition;
	lightIntensity = dot(light, normal);
	
	gl_Position = projectionMatrix * viewMatrix * position;
}

varying vec4 destinationColour;
varying vec2 textureCoordOut;
varying float lightIntensity;

uniform sampler2D demoTexture;

void main()
{
	float l = max(0.3, lightIntensity * 0.3);
	vec4 colour = vec4(l, l, l, 1.0);
	
	gl_FragColor = colour * texture2D(demoTexture, textureCoordOut);
}

ss1.jpg

ss2.jpg

This topic is closed to new replies.

Advertisement