Skip to main content
GameDev.net gamedev.net
🔒 Locked

Multiple GLSL Shaders Question

Started by donny dont Mar 29, 2007 at 6:48 PM 2 replies 1.5k views
Original Post
donny dont
donny dont
I'm relatively new to creating shaders in GLSL and in the Orange Book there is a chapter on general purpose shaders, such as texturing and lighting. However there isn't an example in the book on how you would texture a geometry in one shader and then apply lighting to the geometry in another, and combine the results of the two separate shaders. Does anyone have an example of texturing a cube with one shader (procedural or otherwise) and then applying a light to it in another shader?
Kalidor
Kalidor
This isn't specific to using shaders, it's just general multipass rendering. You can use OpenGL's blending modes for combining multiple passes together in various ways, usually just adding them together.
donny dont
donny dont
Thanks for the reply.

I was a little lost as the shaders in the Orange book sometimes contained lighting equations within the code such as the brick shader example.

brick.vert
//// Vertex shader for procedural bricks//// Authors: Dave Baldwin, Steve Koren, Randi Rost//          based on a shader by Darwyn Peachey//// Copyright (c) 2002-2004 3Dlabs Inc. Ltd. //// See 3Dlabs-License.txt for license information//uniform vec3 LightPosition;const float SpecularContribution = 0.3;const float DiffuseContribution  = 1.0 - SpecularContribution;varying float LightIntensity;varying vec2  MCposition;void main(void){    vec3 ecPosition = vec3 (gl_ModelViewMatrix * gl_Vertex);    vec3 tnorm      = normalize(gl_NormalMatrix * gl_Normal);    vec3 lightVec   = normalize(LightPosition - ecPosition);    vec3 reflectVec = reflect(-lightVec, tnorm);    vec3 viewVec    = normalize(-ecPosition);    float diffuse   = max(dot(lightVec, tnorm), 0.0);    float spec      = 0.0;    if (diffuse > 0.0)    {        spec = max(dot(reflectVec, viewVec), 0.0);        spec = pow(spec, 16.0);    }    LightIntensity  = DiffuseContribution * diffuse +                      SpecularContribution * spec;    MCposition      = gl_Vertex.xy;    gl_Position     = ftransform();}


brick.frag
//// Fragment shader for procedural bricks//// Authors: Dave Baldwin, Steve Koren, Randi Rost//          based on a shader by Darwyn Peachey//// Copyright (c) 2002-2004 3Dlabs Inc. Ltd.//// See 3Dlabs-License.txt for license information//  uniform vec3  BrickColor, MortarColor;uniform vec2  BrickSize;uniform vec2  BrickPct;varying vec2  MCposition;varying float LightIntensity;void main(void){    vec3  color;    vec2  position, useBrick;        position = MCposition / BrickSize;    if (fract(position.y * 0.5) > 0.5)        position.x += 0.5;    position = fract(position);    useBrick = step(position, BrickPct);    color  = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);    color *= LightIntensity;    gl_FragColor = vec4 (color, 1.0);}


So all that needs to be done is rip out the lighting calculations in the code and then just do this in pseudo code.

glEnable(GL_BLEND);glBlendFunc(someSourceFactor, someDestinationFactor);glUseProgram(brickProgram);// DRAW GEOMETRYglUseProgram(lightProgram);// DRAW GEOMETRY AGAINglUseProgram(0);


MARS_999
MARS_999
Sure, looks ok if that's what you want to do. Remember to shutdown the blending when your done with it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.