OpenGL API Specifications
Documents and Learning ResourcesSupport Libraries and FrameworksFunction Libraries and GeneratorsThis list contains some basic resources for getting started with, and developing for, OpenGL 3.0 and onwards. Contact your forum moderator if you want to contribute to the list or have any comments.Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 07 October 2004 - 12:12 PM
Posted 07 October 2004 - 01:34 PM
Posted 07 October 2004 - 06:55 PM
Quote:
Original post by dimensionX
What is the word on using gl_NormalMatrix inside a fragment shader in OpenGL 2.0 ?
Posted 08 October 2004 - 02:20 AM
Posted 08 October 2004 - 03:12 AM
Posted 08 October 2004 - 04:12 AM
Posted 09 October 2004 - 04:43 AM
/* Author: James D. Trotter
29/09-2004
Phong illumination program
--------------------------
http://en.wikipedia.org/wiki/Phong_shading
The vertex shader transforms the
vertex-normal by the inverse
transpose of the modelview
matrix, so that the normal
is in world-space.
The eye-space vector from the
light to the vertex is trans-
formed into world-space, and
sent to the fragment shader.
The eye-space vector from the
vertex to the camera, (the
negative vertex coordinate,
since the camera is at the
origin in eye-space), is
transformed into world-space,
so that it can be used to
calculate the specular light
component in the fragment shader.
*/
// Position of the point-light
uniform vec4 lightPosition;
// Values interpolated across the polyogon
varying vec3 normal; // Vertex-normal
varying vec3 viewVec; // World-space view vector
varying vec3 lightVec; // World-space light vector
void main(void) {
// Get the vertex normal, transformed
// by the inverse transpose of the
// modelview-matrix
normal = gl_NormalMatrix * gl_Normal;
// Get the world-space vector
// from the light to the vertex.
vec4 lightToVertex = lightPosition - gl_Vertex;
lightVec = (gl_ModelViewMatrix * lightToVertex).xyz;
// Get the world-space vector from
// the vertex to the camera.
viewVec = (gl_ModelViewMatrix * gl_Vertex).xyz;
// Perform vertex transformations.
gl_Position = ftransform();
}
/* Author: James D. Trotter
29/09-2004
Phong illumination program:
---------------------------
http://en.wikipedia.org/wiki/Phong_shading
Using the interpolated vertex normal,
lighting is calculated according to
the phong illumination or reflection
model. All three lighting components -
diffuse, ambient, and specular - are
taken into account:
Diffuse: L.N
where L is the tangent-space
vector from the light to the
vertex. And N is the
(interpolated) fragment normal.
Specular: pow(R.V, n)
where R is the reflected light
vector around the normal N.
V is the tangent-space vector
from the vertex to the viewer.
n indicates the surface reflectivity.
Ambient: The ambient term is added to
give general illumination. This
is needed because diffuse and
specular only accurately give
local lighting conditions.
*/
// Values interpolated across the polyogon
varying vec3 normal; // Vertex-normal
varying vec3 viewVec; // World-space view vector
varying vec3 lightVec; // World-space light vector
// Light properties
uniform vec4 Ii; // Light brightness
uniform vec4 Ia; // Ambient light term
// Surface properties
uniform vec4 Kd; // Reflection coefficient
uniform vec4 Ks; // Specular coefficient
uniform vec4 Ka; // Ambient coefficient
uniform float n; // Surface reflectivity
void main(void) {
// Normalize incoming vectors
vec3 L = normalize(lightVec);
vec3 V = normalize(viewVec);
// Calculate the diffuse component
vec4 Id = Kd * max(dot(L, normal), 0.0);
// Calculate reflection vector
vec3 R = normalize(reflect(L, normal));
// Calculate the specular component
vec4 Is = Ks * pow(max(dot(R, V), 0.0), n);
// Add the components together
gl_FragColor = Ia * Ka + Ii * (Id + Is);
}
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.