Basic Gouraud Shader

Hello forum,

I am trying to implement the basic gouraud shader. If i run with the shader , i do not see anything on the screen and without shader i can see the primitive i am trying to render. Any hint is appreciated to debug this issue. Here goes the snippet for the shaders:

#version 150 core

uniform mat4 modelViewProjectionMatrix;
uniform mat4 projectionMatrix;

in vec4 position;
in vec4 normal;

//light and material properties
uniform vec3 light_pos = vec3(100.0,100.0,100.0);
uniform vec3 diffuse_albedo = vec3(0.5,0.2,0.7);
uniform vec3 specular_albedo = vec3(0.7);
uniform float specular_power = 128.0;
uniform vec3 ambient = vec3(0.1,0.1,0.1);

out vec3 color;

void main()
{
	//calculate the view-space coordinate
	vec4 P = modelViewProjectionMatrix * position;

	//calculate the normal in view space
	vec3 N = mat3(modelViewProjectionMatrix) * vec3(normal);

	//calculate the view-space light vector
	vec3 L = light_pos - P.xyz;

	//calculate view vector
	//simply the negative of the
	//view-space position
	vec3 V = -P.xyz;

	//normalize all the vectors
	N = normalize(N);
	L = normalize(L);
	V = normalize(V);

	//calculate R  by reflecting -L around the plane defined by N
	vec3 R = reflect(-L,N);

	//calculate the diffuse and specular contributions
	vec3 diffuse = max(dot(N,L),0.0) * diffuse_albedo;

	vec3 specular = pow(max(dot(R,V),0.0),specular_power) * specular_albedo;

	//send the color output to the fragment shader
	color = ambient + diffuse + specular;

	//calculate the clip-space position of each vertex
	gl_Position = projectionMatrix * P;
}

And the fragment shader:

#version 150

out vec4 outputcolor;

in vec3 color;

void main()
{
	outputcolor = vec4(color,1.0);
}

The above shaders are already tested with the raw opengl code and it run fine with it. I may be missing something from the application part .

//--------------------------------------------------------------
void ofApp::setup()
{
   if(ofIsGLProgrammableRenderer())
      perVertexShader.load("shader/shader-gauraud-vertex");
   else
   {
      std::cout << "Programmable Shading is not supported. " << std::endl;
      exit();
   }

   ofEnableDepthTest();
   //set the radius of the sphere
   sphere.setRadius(ofGetWidth() * .15);
}

And the rendering happens in the following snippet:

void ofApp::draw()
{
   float spin = sin(ofGetElapsedTimef()*.35f);

   //ofNoFill();
   ofSetColor(255,128,128);

   perVertexShader.begin();

   perVertexShader.setUniform3f("specular_albedo",1.0f,1.0f,1.0f);
   perVertexShader.setUniform1f("specular_power",30.0f);
   
   sphere.setPosition(ofGetWidth() * 0.5,
		      ofGetHeight() * 0.5,
		      0);

   sphere.rotate(spin,0,1,0);

   sphere.draw();
   

   perVertexShader.end();
}

Thanks

Thanks folks,

The issue is solved!

One more thing by the way.

Is there any example where the color is set as the floating point value between 0.0 and 1.0.

I am used it more than the 255.

Thanks

In what context, GLSL or openFrameworks? There’s ofFloatColor: ofFloatColor c(1.0, 0.0, 0.0, 1.0)

What was the error?

I was not multiplying the properly in the vertex shader. it should be as follows

//calculate the view-space coordinate
vec4 P = modelViewMatrix * position;

//calculate the normal in view space
vec3 N = mat3(modelViewMatrix) * vec3(normal);