Basic shader not rendering STL object correctly

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!

Where can I download this teapot test this out? It almost looks like the vertices are oddly setup. Also, you only have 4 colors you’re spitting out, you probably want something more like this http://myheroics.wordpress.com/2009/07/09/glsl-blinn-phong-gouraud-shader/ no?

Please find the file inf the link below, the reason for outputting just 4 colours is because I’m doing the lighthouse3d glsl tutorials, and the is the toon shader :wink:

https://dl.dropboxusercontent.com/u/26115638/OF-Share/teapot.stl

Hey Nardove,

I see the model is scaled by x5, the normals are probably scaling as well in the stl draw. Have you tried glEnable(GL_NORMALIZE)?

Hi Trent,

Did as suggested but same results, I even try not scaling the model and did not make any difference

I’m assuming that you’re not happy with the jagged edges on the side of the teapot? That’s an artifact of the model afaict, here’s your shaders running with a different teapot. Is that the problem that you’re talking about?

![](http://forum.openframeworks.cc/uploads/default/2992/Screen Shot 2013-08-10 at 6.20.20 PM.png)

Hi Josh,

I see, there most be something wrong with my implementation, the example I took it from uses the same teapot model that you have and it looks better, please have a look at -this link

Hi Josh,

I see, there most be something wrong with my implementation but I cant see where :frowning: the example I took it from uses the same teapot model that you have and it looks better, please have a look at -this link

Shouldn’t you be using:

  
  
	normal = gl_Normal;  
	gl_Position = ftransform();  
  

since your light isn’t specified in eye space but is rather in world space (that’s the default in OF)

Make the changes but unfortunately the same result

Since you’re calculating your light in the shader, try not using ofLight. That calls a GL_Light, which I don’t think you need at all and might be messing things up. The light is really just a vector that you’re calculating and I wonder if that’s making things a bit strange at the moment.