Page 1 of 4 1234 LastLast
Results 1 to 10 of 32

Thread: OpenGL Shadows and the Stencil Buffer

  1. #1
    Member Newbie
    Join Date
    May 2011
    Posts
    31

    OpenGL Shadows and the Stencil Buffer

    So, I've been following nehe's guide to creating shadows, and for whatever reason the shadows just aren't rendering. Not having an excellent understanding of things like the stencil-buffer means I'm sort of learning things as I go along. I hate outsourcing my debugging, but I'm kind of stumped here. does anybody know what's wrong?
    Code :
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT | GL_STENCIL_BUFFER_BIT );
    glDisable( GL_LIGHTING );
    glDepthMask( GL_FALSE );
    glDepthFunc( GL_LEQUAL );
    glEnable( GL_STENCIL_TEST );
    glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
    glStencilFunc( GL_ALWAYS, 1, 0xFFFFFFFFL );
     
    glFrontFace( GL_CCW );
    glStencilOp( GL_KEEP, GL_KEEP, GL_INCR );
     
    //shadow volume quad drawing code here
    //I've checked to make sure that the code to draw the volume is running
    //so I thought I'd spare you that
     
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
     
    glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
     
    glPushMatrix();
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, 1, 1, 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
     
    glBegin(GL_QUADS);
    	glVertex2i(0, 0);
    	glVertex2i(0, 1);
    	glVertex2i(1, 1);
    	glVertex2i(1, 0);
    glEnd();
     
    glEnable(GL_DEPTH_TEST);
    glPopMatrix();
    glPopAttrib();
     
    glEnable(GL_LIGHTING);
    glDisable(GL_STENCIL_TEST);
    glShadeModel(GL_SMOOTH);
    glDisable(GL_CULL_FACE);

    And I run that every frame. I have a feeling it's a problem in how I arranged the various gl*() commands. Only the large, screen-covering shadow is drawing with no parts being cut out.

  2. #2
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: OpenGL Shadows and the Stencil Buffer

    Hi there. I wanted to find out how to do shadows too and discovered how to do it with what's called a shadow matrix. I can give you the relevant code and at least partly explain how it works. I will admit though that it uses math from linear algebra which kind of above my head. I never took that course in college.
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  3. #3
    Member Newbie
    Join Date
    May 2011
    Posts
    31

    Re: OpenGL Shadows and the Stencil Buffer

    I'm going to take that course eventually, but yes I'd love to see it. I don't know any linear algebra but perhaps I can compare that to my code and see what's going on, or use it instead or something.

  4. #4
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    4

    Re: OpenGL Shadows and the Stencil Buffer

    I can help you but its first time I am using this page. give me you problem exactly I will help you
    Hoshang Kolivand
    shahinkey@yahoo.com

  5. #5
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: OpenGL Shadows and the Stencil Buffer

    Ok, put these two functions above your drawScene function:


    void calcNormal(float v[3][3], float out[3])
    {
    float v1[3], v2[3], length;
    static const int x = 0, y = 1, z = 2;

    v1[x] = v[0][x] - v[1][x];
    v1[y] = v[0][y] - v[1][y];
    v1[z] = v[0][z] - v[1][z];

    v2[x] = v[1][x] - v[2][x];
    v2[y] = v[1][y] - v[2][y];
    v2[z] = v[1][z] - v[2][z];

    out[x] = (v1[y] * v2[z]) - (v1[z] * v2[y]);
    out[y] = (v1[z] * v2[x]) - (v1[x] * v2[z]);
    out[z] = (v1[x] * v2[y]) - (v1[y] * v2[x]);

    length = (float)sqrt( (out[x] * out[x]) + (out[y] * out[y]) + (out[z] * out[z]) );

    out[x] = out[x] / length;
    out[y] = out[y] / length;
    out[z] = out[z] / length;
    }
    void MakeShadowMatrix(GLfloat points[3][3], GLfloat lightPos[4], GLfloat destMat[4][4])
    {
    GLfloat planeCoeff[4];
    GLfloat dot;

    calcNormal(points, planeCoeff);

    planeCoeff[3] = - ( (planeCoeff[0] * points[2][0]) + (planeCoeff[1] * points[2][1]) + (planeCoeff[2] * points[2][2]) );

    dot = (planeCoeff[0] * lightPos[0]) + (planeCoeff[1] * lightPos[1]) + (planeCoeff[2] * lightPos[2]) + (planeCoeff[3] * lightPos[3]);

    destMat[0][0] = dot - (lightPos[0] * planeCoeff[0]);
    destMat[1][0] = 0.0f - (lightPos[0] * planeCoeff[1]);
    destMat[2][0] = 0.0f - (lightPos[0] * planeCoeff[2]);
    destMat[3][0] = 0.0f - (lightPos[0] * planeCoeff[3]);

    destMat[0][1] = 0.0f - (lightPos[1] * planeCoeff[0]);
    destMat[1][1] = dot - (lightPos[1] * planeCoeff[1]);
    destMat[2][1] = 0.0f - (lightPos[1] * planeCoeff[2]);
    destMat[3][1] = 0.0f - (lightPos[1] * planeCoeff[3]);

    destMat[0][2] = 0.0f - (lightPos[2] * planeCoeff[0]);
    destMat[1][2] = 0.0f - (lightPos[2] * planeCoeff[1]);
    destMat[2][2] = dot - (lightPos[2] * planeCoeff[2]);
    destMat[3][2] = 0.0f - (lightPos[2] * planeCoeff[3]);

    destMat[0][3] = 0.0f - (lightPos[3] * planeCoeff[0]);
    destMat[1][3] = 0.0f - (lightPos[3] * planeCoeff[1]);
    destMat[2][3] = 0.0f - (lightPos[3] * planeCoeff[2]);
    destMat[3][3] = dot - (lightPos[3] * planeCoeff[3]);
    }

    Now declare these at begining of drawScene function:

    GLfloat shadowMat[4][4];
    GLfloat points[3][3];

    Set points to three points that lie on the plane where the shadow will be cast

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); //don't forget this


    And then when you're ready to draw a shadow put this code before it with a light source of your choosing:

    MakeShadowMatrix(points, lightPos0, shadowMat);

    glPushMatrix();

    glMultMatrixf((GLfloat *)shadowMat);

    glColor3f(0.0f, 0.0f, 0.0f);

    //draw the thing casting the shadow again

    glColor3f(1.0f, 1.0f, 1.0f);

    glPopMatrix();
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  6. #6
    Member Newbie
    Join Date
    May 2011
    Posts
    31

    Re: OpenGL Shadows and the Stencil Buffer


    Set points to three points that lie on the plane where the shadow will be cast
    Does the code rely on the fact that the shadow will be drawn on a single plane? Because it won't be.

  7. #7
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: OpenGL Shadows and the Stencil Buffer

    Umm, good question. I'm really not sure. Sorry. I thought I could be of help.
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  8. #8
    Member Newbie
    Join Date
    May 2011
    Posts
    31

    Re: OpenGL Shadows and the Stencil Buffer

    Lol. where'd you get the code then?

  9. #9
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: OpenGL Shadows and the Stencil Buffer

    OpenGL Super Bible Second Edition. Found the book at the main library. Its been incredibly helpful so far. I'm new to OpenGL.
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  10. #10
    Member Newbie
    Join Date
    May 2011
    Posts
    31

    Re: OpenGL Shadows and the Stencil Buffer

    Holy balls I need that book. Is there any code for projecting shadows onto something other than a plane?

Page 1 of 4 1234 LastLast

Similar Threads

  1. OpenGL, offscreen hard shadows calculation with stencil
    By elect in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-11-2013, 09:35 AM
  2. Replies: 2
    Last Post: 08-17-2003, 01:57 AM
  3. Stencil Buffer Shadows and Rotation.
    By MrShoe in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 10-18-2002, 01:42 AM
  4. Stencil Shadows
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-21-2002, 09:23 AM
  5. Volumetric Shadows and the Stencil Buffer!!!!!!!!!
    By Lloyd in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 05-02-2001, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean