Ehy, i tried your solution end finally worked! I continued to work with light but now i do a jump back because i notice a problem in this basic example.
I write here my new code that it’s very similar to your:
#include "testApp.h"
void testApp::setup()
{
ofSetVerticalSync(true);
ofSetFrameRate(60);
ofBackground(10, 10, 10);
ofEnableDepthTest();
shader.load( "shader.vert", "shader.frag");
plane.set(100, 100, 10, 10);
cube.set(10, 10, 10);
sphere.set(10, 100);
sphere.enableNormals();
cube.enableNormals();
plane.enableNormals();
light.setPosition(0, -100, 0);
}
void testApp::update()
{
updateNormalMatrix();
shader.load( "shader.vert", "shader.frag");
}
void testApp::draw()
{
glEnable(GL_DEPTH_TEST);
cam.begin();
shader.begin();
ofVec3f cameraSpaceLightPos = light.getPosition() * cam.getModelViewMatrix();
shader.setUniform3f("cameraSpaceLightPos", cameraSpaceLightPos.x, cameraSpaceLightPos.y, cameraSpaceLightPos.z);
shader.setUniform3f("firstRow", normalMatrix.a, normalMatrix.b, normalMatrix.c);
shader.setUniform3f("secondRow", normalMatrix.d, normalMatrix.e, normalMatrix.f);
shader.setUniform3f("thirdRow", normalMatrix.g, normalMatrix.h, normalMatrix.i);
plane.draw();
ofPushMatrix();
ofTranslate(0,-20,10);
sphere.draw();
ofPopMatrix();
ofPushMatrix();
ofTranslate(0,0,10);
ofRotateZ(ofGetElapsedTimeMicros() * .00004);
cube.draw();
ofPopMatrix();
shader.end();
light.draw();
cam.end();
glDisable(GL_DEPTH_TEST);
ofDrawBitmapString(ofToString(ofGetFrameRate()), ofPoint(10,10));
}
ofMatrix3x3 testApp::mat4ToMat3(ofMatrix4x4 mat4)
{
return ofMatrix3x3(mat4._mat[0][0], mat4._mat[0][1], mat4._mat[0][2], mat4._mat[1][0], mat4._mat[1][1], mat4._mat[1][2], mat4._mat[2][0], mat4._mat[2][1], mat4._mat[2][2]);
}
void testApp::updateNormalMatrix()
{
ofMatrixStack matrixStack(*ofGetWindowPtr());
ofMatrix4x4 modelViewMatrix = matrixStack.getModelViewMatrix();
normalMatrix = mat4ToMat3(modelViewMatrix);
normalMatrix.transpose();
normalMatrix.invert();
}
vertex shader:
#version 150
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelViewMatrix;
uniform vec3 cameraSpaceLightPos;
uniform vec3 firstRow;
uniform vec3 secondRow;
uniform vec3 thirdRow;
in vec4 position;
in vec2 texcoord;
in vec3 normal;
const vec4 diffuseColor = vec4(1,0,1,1);
out vec4 colorOut;
void main(){
mat3 normaMatrix;
normaMatrix[0][0] = firstRow.x;
normaMatrix[0][1] = firstRow.y;
normaMatrix[0][2] = firstRow.z;
normaMatrix[1][0] = secondRow.x;
normaMatrix[1][1] = secondRow.y;
normaMatrix[1][2] = secondRow.z;
normaMatrix[2][0] = thirdRow.x;
normaMatrix[2][1] = thirdRow.y;
normaMatrix[2][2] = thirdRow.z;
vec3 vertexNormal = normalize(normaMatrix * normal);
vec3 cameraSpaceVertexPos = vec3(modelViewMatrix * position);
vec3 lightDir = normalize(cameraSpaceLightPos - cameraSpaceVertexPos);
float intensity = max(dot(vertexNormal,lightDir), 0.0);
colorOut = diffuseColor * intensity;
colorOut.w = 1.0;
gl_Position = modelViewProjectionMatrix * position;
}
Now, my doubts and problems are two.
First, when i launch the app, i can see some black area and i think it’s normal. But i think it’s not normal that if i move the camera, i can see lit area everywhere. I mean… there are not area not reached by light. Isn’t it strange?
Second problem is if i rotate the cube, i see always the same light effects on the face, also if the face it’s in opposite side of the light.
I link here a video:
www.mauroferrario.com/file/simpleLight.mov