mardi 2 décembre 2014

OpenGL per pixel shading


Vote count:

0




I'm having some trouble understanding per pixel shading. I have the code below in my vertex shader which works out ambient, diffuse and specular lighting, the problem now lies trying to split it up among the vertex/fragment shader.


I am trying to follow this tutorial


http://ift.tt/1wFqPIn


I see a varying variable which to my understanding allows communication between two shaders, although I am using in and out to achieve this, which I've read is possible, the problem is when I try modify my Position input, the program tells me I am not allowed to modify an input variable.


I'm having an awful time with this an would appreciate any help.



#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;

out vec4 Colour0;

// Transforms
uniform mat4 gModelToWorldTransform;
uniform mat4 gWorldToViewToProjectionTransform;

// Ambient light parameters
uniform vec3 gAmbientLightIntensity;

// Directional light parameters
uniform vec3 gDirectionalLightIntensity;
uniform vec3 gDirectionalLightDirection;

// Material constants
uniform float gKa;
uniform float gKd;
uniform float gKs;
uniform float gKsStrength;


void main()
{
// Transform the vertex from local space to homogeneous clip space
vec4 vertexPositionInModelSpace = vec4(Position, 1);
vec4 vertexInWorldSpace = gModelToWorldTransform * vertexPositionInModelSpace;
vec4 vertexInHomogeneousClipSpace = gWorldToViewToProjectionTransform * vertexInWorldSpace;
gl_Position = vertexInHomogeneousClipSpace;

// Calculate the directional light intensity at the vertex
// Find the normal in world space and normalise it
vec3 normalInWorldSpace = (gModelToWorldTransform * vec4(Normal, 0.0)).xyz;
normalInWorldSpace = normalize(normalInWorldSpace);

// Calculate the ambient light intensity at the vertex
// Ia = Ka * ambientLightIntensity
vec4 ambientLightIntensity = gKa * vec4(gAmbientLightIntensity, 1.0);

// Setup the light direction and normalise it
vec3 lightDirection = normalize(-gDirectionalLightDirection);

//lightDirection = normalize(gDirectionalLightDirection);
// Id = kd * lightItensity * N.L
// Calculate N.L
float diffuseFactor = dot(normalInWorldSpace, lightDirection);
diffuseFactor = clamp(diffuseFactor, 0.0, 1.0);

// N.L * light source colour * intensity
vec4 diffuseLightIntensity = gKd * vec4(gDirectionalLightIntensity, 1.0f) * diffuseFactor;

vec3 lightReflect = normalize(reflect(gDirectionalLightDirection, Normal));

//Calculate the specular light intensity at the vertex
float specularFactor = dot(normalInWorldSpace, lightReflect);
specularFactor = pow(specularFactor, gKsStrength);
vec4 specularLightIntensity = gKs * vec4(gDirectionalLightIntensity, 0.5f) * specularFactor;

// Final vertex colour is the product of the vertex colour
// and the total light intensity at the vertex

vec4 colour = vec4(1.0, 1.0, 1.0, 1.0);
Colour0 = colour * (ambientLightIntensity + diffuseLightIntensity + specularLightIntensity);
}


asked 34 secs ago







OpenGL per pixel shading

Aucun commentaire:

Enregistrer un commentaire