Jump to content
  • Advertisement
Sign in to follow this  
Tommato

OpenGL OpenGL pixel shader (Phong)

This topic is 2622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=Helvetica][size=2]Hi All[/font][font=Helvetica][size=2]
[/font][font=Helvetica][size=2]
[/font][font=Helvetica][size=2]AFAIK I need to create OpenGL pixel shader to have a OpenGL phong preview.[/font][font=Helvetica][size=2]
[/font][font=Helvetica][size=2]I'm familiar with phong render but never did it in OpenGL. My task is [/font][font=Helvetica][size=2]quite standard - I need just a lighting with surface normal [/font][font=Helvetica][size=2]interpolated for every pixel. Is there a standard pixel shader(s) [/font][font=Helvetica][size=2](or other ways to do this) that could be used?[/font][font=Helvetica][size=2]
[/font][font=Helvetica][size=2]
[/font][font=Helvetica][size=2]Thanks[/font][font=Helvetica][size=2]
[/font][font=Helvetica][size=2]Tom[/font]

Share this post


Link to post
Share on other sites
Advertisement

[font="Helvetica"]Hi All[/font] [font="Helvetica"]AFAIK I need to create OpenGL pixel shader to have a OpenGL phong preview.[/font] [font="Helvetica"]I'm familiar with phong render but never did it in OpenGL. My task is [/font][font="Helvetica"]quite standard - I need just a lighting with surface normal [/font][font="Helvetica"]interpolated for every pixel. Is there a standard pixel shader(s) [/font][font="Helvetica"](or other ways to do this) that could be used?[/font] [font="Helvetica"]Thanks[/font] [font="Helvetica"]Tom[/font]


Here's one way of doing it (I used OpenGL 4.1). Send the smooth normals to the vertex shader as an attribute. I'm passing the light position vector, MVP, MV, and normal matrices as uniforms. The normal interpolation is handled by the vertex shader.


#version 410

// Incoming per vertex and normal
in vec4 vVertex;
in vec3 vNormal;

// from the application
uniform vec3 vLightPosition;
uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;

// pass the normals and light direction to the fragment program
out vec3 vVaryingLightDir;
out vec3 vVaryingNormal;

void main(void)
{
// surface normal in eye space
vVaryingNormal = normalMatrix * vNormal;

// vertex position in eye space
vec4 vPosition4 = mvMatrix * vVertex;

// convert from homogenous
vec3 vPosition3 = vPosition4.xyz / vPosition4.w;

// vector to the light source
vVaryingLightDir = normalize(vLightPosition - vPosition3);

// transform geometry
gl_Position = mvpMatrix * vVertex;
}


The fragment shader needs to normalize the light direction and the vertex normal because of the interpolation. The shiny constant can be almost anything depending on what material properties you want.


#version 410

out vec4 vFragColor;

uniform vec4 diffuseColor;
uniform vec4 ambientColor;
uniform vec4 specularColor;
uniform float shiny;

in vec3 vVaryingNormal;
in vec3 vVaryingLightDir;


void main(void)
{

vec3 fragNormal = normalize(vVaryingNormal);
vec3 fragLighDir = normalize(vVaryingLightDir);

// Dot product to compute diffuse intensity
float diff = max(0.0, dot(fragNormal, fragLightDir));

// Multiply the intensity by diffuse color
vFragColor = diff * diffuseColor;

//add ambient
vFragColor += ambientColor;

// specular light
vec3 vReflection = normalize(reflect(-fragLightDir,fragNormal));
float spec = max(0.0, dot(fragNormal,vReflection));
if(diff !=0) {
float fragSpec = pow(spec,shiny);
vFragColor.rgb += vec3(fragSpec, fragSpec, fragSpec);
}
}


Here's a screenshot of what this Phong shader looks like on a typical model:

[attachment=5375:phong.png]

Share this post


Link to post
Share on other sites
Ola Jesse7 and dpadam450,

We are learning and trying, as soon we figured out, I will reply with news!

Thanks a lot

Tom

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!