Hi,
I’m learning myself shaders using the tutorials form lighthouse3d website, I was using ofSphere for my examples, I had not problems at all, then I decided to test with a different 3d object, so I download a teapot from the grabcad website unfortunately the shader does not render the teapot the same way it renders a ofSphere, please see image attached as reference.
Here is my code
testApp.cpp
#include "testApp.h"
void testApp::setup(){
ofSetVerticalSync(true);
camera.setDistance(400);
shader.load("shaders/shader.vert", "shaders/shader.frag");
teapot.loadSTL(ofToDataPath("teapot.stl"));
teapot.scaleModel(5);
// teapot.centerModel();
glEnable(GL_DEPTH_TEST);
}
void testApp::draw(){
ofBackground( ofColor::black );
ofEnableLighting();
camera.begin();
shader.begin();
float posX = ofGetMouseX() - ofGetWidth() * 0.5;
float posY = ofGetMouseY() - ofGetHeight() * 0.5;
ofVec3f lightPos(posX, -posY, 200);
lightPos.normalize();
shader.setUniform3fv("lightPos", lightPos.getPtr());
ofSphere( 80, 0, 0, 30 );
ofPushMatrix();
ofTranslate(-80, -20);
teapot.draw();
ofPopMatrix();
shader.end();
camera.end();
}
shader.vert
varying vec3 normal;
void main() {
normal = gl_NormalMatrix * gl_Normal;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
shader.frag
uniform vec3 lightPos;
varying vec3 normal;
void main() {
float intensity;
vec4 color;
intensity = dot(lightPos, normalize(normal));
if (intensity > 0.95) {
color = vec4(1.0, 0.5, 0.5, 1.0);
}
else if (intensity > 0.5) {
color = vec4(0.6, 0.3, 0.3, 1.0);
}
else if (intensity > 0.25) {
color = vec4(0.4, 0.2, 0.2, 1.0);
}
else {
color = vec4(0.2, 0.1, 0.1, 1.0);
}
gl_FragColor = color;
}
Any help will be much appreciated!