iltuf

04-12-2012, 10:32 PM

Hi all. I've been attempting to teach myself glsl shading using vertex and fragment shaders. I've made some decent progress using sphere code I found online along with the shading model described in a textbook. However, I am getting a very strange effects with my finished product. I've attached a picture.

I believe my error is with the sphere normals, but I'm not sure what I'm doing wrong or how I can fix it. Here is the code that generates my sphere.

for ( i = 0; i < numParallels + 1; i++ )

{

for ( j = 0; j < numSlices + 1; j++ )

{

int vertex = ( i * (numSlices + 1) + j ) * 3;

if ( vertices )

{

(*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) *

sinf ( angleStep * (float)j );

(*vertices)[vertex + 1] = radius * cosf ( angleStep * (float)i );

(*vertices)[vertex + 2] = radius * sinf ( angleStep * (float)i ) *

cosf ( angleStep * (float)j );

}

if ( normals )

{

(*normals)[vertex + 0] = (*vertices)[vertex + 0];

(*normals)[vertex + 1] = (*vertices)[vertex + 1];

(*normals)[vertex + 2] = (*vertices)[vertex + 2];

}

I then load up the normals and vertex information in some buffers.

Here are my shaders - I apologize for the random unused variables. I've been trying to figure out why the heck I get these alternating ambient-color triangles.

#version 400

layout(location=0) in vec3 in_Position;

layout(location=1) in vec4 in_Color;

layout(location=2) in vec3 in_Normals;

out vec4 ex_Color;

out vec3 position;

out vec3 fN;

out vec3 fE;

out vec3 fL;

out float specularExponent;

uniform mat4 ModelMatrix;

uniform mat4 ViewMatrix;

uniform mat4 ProjectionMatrix;

uniform vec4 light_position;

void main(void)

{

fN = in_Normals;

fE = in_Position;

fL = light_position.xyz;

if(light_position.w != 0.0)

{

fL = light_position.xyz - in_Position.xyz;

}

gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * vec4(in_Position.x,in_Position.y,in_Position.z, 1);

position = gl_Position.xyz;

ex_Color = vec4(.5,.3,.5,1);

specularExponent = 100.0;

}

#version 400

in vec4 ex_Color;

in vec3 position;

in float specularExponent;

in vec3 fN;

in vec3 fL;

in vec3 fE;

out vec4 fColor;

void main()

{

vec4 light_color = vec4(1.0,1.0,1.0,1.0);

vec4 ambient_light = vec4(.5,.5,.5,1.0);

vec4 SpecularColor = vec4(1.0, 1.0, 1.0, 1.0);

vec3 N = normalize(fN);

vec3 E = normalize(fE);

vec3 L = normalize(fL);

vec3 H = normalize(L+E);

vec4 amb = ex_Color * ambient_light;

vec4 diff = max(dot(L,N), 0.0) * ex_Color * light_color;

vec4 spec = pow( max (dot(N,H), 0.0), specularExponent) * SpecularColor * light_color ;

if(dot(L,N) < 0.0){

spec = vec4(0,0,0,1);

}

fColor = amb + diff + spec;

//fColor = vec4(1,1,1,1);

}

I appreciate the help!

Joe

I believe my error is with the sphere normals, but I'm not sure what I'm doing wrong or how I can fix it. Here is the code that generates my sphere.

for ( i = 0; i < numParallels + 1; i++ )

{

for ( j = 0; j < numSlices + 1; j++ )

{

int vertex = ( i * (numSlices + 1) + j ) * 3;

if ( vertices )

{

(*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) *

sinf ( angleStep * (float)j );

(*vertices)[vertex + 1] = radius * cosf ( angleStep * (float)i );

(*vertices)[vertex + 2] = radius * sinf ( angleStep * (float)i ) *

cosf ( angleStep * (float)j );

}

if ( normals )

{

(*normals)[vertex + 0] = (*vertices)[vertex + 0];

(*normals)[vertex + 1] = (*vertices)[vertex + 1];

(*normals)[vertex + 2] = (*vertices)[vertex + 2];

}

I then load up the normals and vertex information in some buffers.

Here are my shaders - I apologize for the random unused variables. I've been trying to figure out why the heck I get these alternating ambient-color triangles.

#version 400

layout(location=0) in vec3 in_Position;

layout(location=1) in vec4 in_Color;

layout(location=2) in vec3 in_Normals;

out vec4 ex_Color;

out vec3 position;

out vec3 fN;

out vec3 fE;

out vec3 fL;

out float specularExponent;

uniform mat4 ModelMatrix;

uniform mat4 ViewMatrix;

uniform mat4 ProjectionMatrix;

uniform vec4 light_position;

void main(void)

{

fN = in_Normals;

fE = in_Position;

fL = light_position.xyz;

if(light_position.w != 0.0)

{

fL = light_position.xyz - in_Position.xyz;

}

gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * vec4(in_Position.x,in_Position.y,in_Position.z, 1);

position = gl_Position.xyz;

ex_Color = vec4(.5,.3,.5,1);

specularExponent = 100.0;

}

#version 400

in vec4 ex_Color;

in vec3 position;

in float specularExponent;

in vec3 fN;

in vec3 fL;

in vec3 fE;

out vec4 fColor;

void main()

{

vec4 light_color = vec4(1.0,1.0,1.0,1.0);

vec4 ambient_light = vec4(.5,.5,.5,1.0);

vec4 SpecularColor = vec4(1.0, 1.0, 1.0, 1.0);

vec3 N = normalize(fN);

vec3 E = normalize(fE);

vec3 L = normalize(fL);

vec3 H = normalize(L+E);

vec4 amb = ex_Color * ambient_light;

vec4 diff = max(dot(L,N), 0.0) * ex_Color * light_color;

vec4 spec = pow( max (dot(N,H), 0.0), specularExponent) * SpecularColor * light_color ;

if(dot(L,N) < 0.0){

spec = vec4(0,0,0,1);

}

fColor = amb + diff + spec;

//fColor = vec4(1,1,1,1);

}

I appreciate the help!

Joe