Sign in to follow this  
k0d3r

OpenGL Shadow volume rendering for md5meshes

Recommended Posts

k0d3r    122
I have been trying to write a doom 3 model viewer in openGL and I am having trouble with shadow volumes. :( description of your image As you can see, the shadow is not rendered right. I'd appreciate any help on this.

Share this post


Link to post
Share on other sites
umbrae    308
I don't think anyone can help without further information. What technique are you using? What's your pseudo code?

This seems like a debugging problem, which is hard to analyse over the web, but possible. Perhaps post some of the code you have?

Share this post


Link to post
Share on other sites
k0d3r    122
I am sorry about that.

I am using stencil shadow volumes. I calculate the silhouette edges of the md5mesh. I use the following edge structure to store the information.

class Edge
{
public:
int tri1;
int tri2;
int vert1;
int vert2;

// is it a silhouette edge ?
bool silhouette;

// 1st triangle front facing ?
bool facing1;
// 2nd triangle front facing ?
bool facing2;
};



This is how the edges are detected.

//==============================================
//
//==============================================
void MD5Model::appendEdge(int meshnumber, int v1, int v2, int tri)
{
meshes[meshnumber]->edges.resize(meshes[meshnumber]->edges.size() + 1);
Edge* newedge = &meshes[meshnumber]->edges[meshes[meshnumber]->edges.size() - 1];
newedge->silhouette = 0;
// set the triangle index
newedge->tri1 = tri;
// set the edge vertices
newedge->vert1 = v1;
newedge->vert2 = v2;
// we don't have the second triangle yet !
newedge->tri2 = -1;
}


char s[1024];
//==============================================
//
//==============================================
void MD5Model::findCreateMatchingEdge(int meshnumber, int v1, int v2, int tri)
{
for(unsigned int i = 0 ; i < meshes[meshnumber]->edges.size() ; i++)
{
if(meshes[meshnumber]->edges[i].vert1 == v2 && meshes[meshnumber]->edges[i].vert2 == v1)
{
if(meshes[meshnumber]->edges[i].tri2 == -1)
{
meshes[meshnumber]->edges[i].tri2 = tri;
return;
}
else
{
sprintf(s, "Edge with more than two tris: meshnumber, v1, v2, tri: %i %i %i %i\n", meshnumber, v1, v2, tri);
MessageBox(NULL, s, "Uh ??", MB_OK);
}
}
}
//we haven´t found a match for this edge, so we create it as a new one
appendEdge(meshnumber, v1, v2, tri);
}

//==============================================
//
//==============================================
void MD5Model::createEdgeList(int meshnumber)
{
for (int i = 0 ; i < meshes[meshnumber]->tris.size() ; i++)
{
Tri* tri = &meshes[meshnumber]->tris[i];

if(tri->v[0] < tri->v[1])
appendEdge(meshnumber, tri->v[0], tri->v[1], i);

if(tri->v[1] < tri->v[2])
appendEdge(meshnumber, tri->v[1], tri->v[2], i);

if(tri->v[2] < tri->v[0])
appendEdge(meshnumber, tri->v[2], tri->v[0], i);
}

for(int i = 0 ; i < meshes[meshnumber]->tris.size() ; i++)
{
Tri* tri = &meshes[meshnumber]->tris[i];

if(tri->v[0] > tri->v[1])
findCreateMatchingEdge(meshnumber, tri->v[0], tri->v[1], i);

if(tri->v[1] > tri->v[2])
findCreateMatchingEdge(meshnumber, tri->v[1], tri->v[2], i);

if(tri->v[2] > tri->v[0])
findCreateMatchingEdge(meshnumber, tri->v[2], tri->v[0], i);
}
}



For each frame I calculate whether the triangles associated with an edge are facing the light or not.

//==============================================
//
//==============================================
void MD5Model::calculateSilhouetteEdges(float light_x, float light_y, float light_z, float light_w)
{
vecf4 light = vecf4(light_x, light_y, light_z, light_w);

for(size_t i = 0 ; i < 2 ; i++)
{
Mesh* mesh = meshes[i];

for(int j = 0 ; j < mesh->edges.size() ; j++)
{
mesh->edges[j].silhouette = 0;
mesh->edges[j].facing1 = 0;
mesh->edges[j].facing2 = 0;

// Extract normal of the first triangle connected to the edge
vecf4 n1 = vecf4(mesh->verts[mesh->tris[mesh->edges[j].tri1].v[0]].n[0],
mesh->verts[mesh->tris[mesh->edges[j].tri1].v[0]].n[1],
mesh->verts[mesh->tris[mesh->edges[j].tri1].v[0]].n[2],
0.0f);

vecf4 n2;

if(mesh->edges[j].tri2 != -1)
{
n2 = vecf4(mesh->verts[mesh->tris[mesh->edges[j].tri2].v[0]].n[0],
mesh->verts[mesh->tris[mesh->edges[j].tri2].v[0]].n[1],
mesh->verts[mesh->tris[mesh->edges[j].tri2].v[0]].n[2],
0.0f);
}

// bad naming !
// this normalizes the normals.
n1.normal();
n2.normal();

vecf4 pos = vecf4((mesh->verts[mesh->edges[j].vert1].pos[0] + mesh->verts[mesh->edges[j].vert2].pos[0])/2.0f,
(mesh->verts[mesh->edges[j].vert1].pos[1] + mesh->verts[mesh->edges[j].vert2].pos[1])/2.0f,
(mesh->verts[mesh->edges[j].vert1].pos[2] + mesh->verts[mesh->edges[j].vert2].pos[2])/2.0f,
1.0f);

vecf4 lVec = pos - light;
lVec.normal();

float dot1 = lVec.dot3(n1);

if(mesh->edges[j].tri2 != -1)
{
float dot2 = lVec.dot3(n2);

if(dot1 >= 0.0f)
mesh->edges[j].facing1 = 1;
if(dot2 >= 0.0f)
mesh->edges[j].facing2 = 1;

if(mesh->edges[j].facing1 != mesh->edges[j].facing2)
mesh->edges[j].silhouette = 1;
}
else
{
if(dot1 >= 0.0f)
{
mesh->edges[j].silhouette = 1;
mesh->edges[j].facing1 = 1;
}
}
}
}
}



I have been having trouble orienting the extruded quads in the right way. So I just use a flag (bool x) and render the quads based on the flag. I disable culling before rendering the quads.

//==============================================
//
//==============================================
void MD5Model::renderShadowVolumeQuads(float light_x, float light_y, float light_z, float light_w, bool x)
{
calculateSilhouetteEdges(light_x, light_y, light_z, light_w);
glDisable(GL_LIGHTING);
glColor3f(1.0f, 1.0f, 1.0f);

for(size_t i = 0 ; i < 2 ; i++)
{
const Mesh* mesh = meshes[i];

for(int j = 0 ; j < mesh->edges.size() ; j++)
{

if(mesh->edges[j].facing1 != mesh->edges[j].facing2)
{
if(mesh->edges[j].facing1 != x)
{
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.5f);

glVertex3f(mesh->verts[mesh->edges[j].vert2].pos[0],
mesh->verts[mesh->edges[j].vert2].pos[1],
mesh->verts[mesh->edges[j].vert2].pos[2]);

glVertex3f(mesh->verts[mesh->edges[j].vert1].pos[0],
mesh->verts[mesh->edges[j].vert1].pos[1],
mesh->verts[mesh->edges[j].vert1].pos[2]);

glVertex4f(mesh->verts[mesh->edges[j].vert1].pos[0],
mesh->verts[mesh->edges[j].vert1].pos[1],
mesh->verts[mesh->edges[j].vert1].pos[2],
0.0f);

glVertex4f(mesh->verts[mesh->edges[j].vert2].pos[0],
mesh->verts[mesh->edges[j].vert2].pos[1],
mesh->verts[mesh->edges[j].vert2].pos[2],
0.0f);
glEnd();
}
else
{
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.5f);

glVertex3f(mesh->verts[mesh->edges[j].vert1].pos[0],
mesh->verts[mesh->edges[j].vert1].pos[1],
mesh->verts[mesh->edges[j].vert1].pos[2]);

glVertex3f(mesh->verts[mesh->edges[j].vert2].pos[0],
mesh->verts[mesh->edges[j].vert2].pos[1],
mesh->verts[mesh->edges[j].vert2].pos[2]);

glVertex4f(mesh->verts[mesh->edges[j].vert2].pos[0],
mesh->verts[mesh->edges[j].vert2].pos[1],
mesh->verts[mesh->edges[j].vert2].pos[2],
0.2f);

glVertex4f(mesh->verts[mesh->edges[j].vert1].pos[0],
mesh->verts[mesh->edges[j].vert1].pos[1],
mesh->verts[mesh->edges[j].vert1].pos[2],
0.2f);

glEnd();
}
}
}
}
glEnable(GL_LIGHTING);
}

Share this post


Link to post
Share on other sites
ma_hty    100
I'm not quite sure what is the direct causes of your problem.

However, there are something obviously wrong about your implementation.

Firstly, normalizing a four component vector not necessary give you the same thing as normalizing a three component vector. Therefore, these two lines are probably wrong
> vecf4 lVec = pos - light;
> lVec.normal();

Also, the silhouette determination algorithm work on face normal instead of vertex normal. If you give it vertex normal, it will simply break down.

Lastly, although you haven't reach that far yet, the rendering of shadow volume to stencil buffer can have problem due to capping. Please carefully check that you did the rendering to stencil buffer right.

I strongly recommend you to try the implementation in http://www.gamedev.net/reference/articles/article1990.asp, which is far better than what you are doing right now.

Share this post


Link to post
Share on other sites
k0d3r    122
Thanks for replying.

> lVec.normal();

This function ignores the w-component. The result is a three component normalization.

I do use the normals of the faces for silhouette determination. I am still not sure what the problem is. Here is a picture of the silhouette rendered from the camera's view.

description of your image

Share this post


Link to post
Share on other sites
ma_hty    100
Quote:
Original post by k0d3r
Thanks for replying.
> lVec.normal();
This function ignores the w-component. The result is a three component normalization.


Mmmm...... as the comments in your source code, it is really a "bad naming".


Quote:
Original post by k0d3r
I do use the faces of normals. I am still not sure what the problem is. The md5mesh contains winged edges, which I think is another problem.



Are you sure the following lines give you face normal?
> vecf4 n1 = vecf4(
> mesh->verts[mesh->tris[mesh->edges[j].tri1].v[0]].n[0],
> mesh->verts[mesh->tris[mesh->edges[j].tri1].v[0]].n[1],
> mesh->verts[mesh->tris[mesh->edges[j].tri1].v[0]].n[2],
> 0.0f);

By the way, although shadow volume algorithm can work on all triangular meshes, the optimization you used requires an extra assumption beside a triangular mesh, i.e. your 3D model must be closed. Therefore, each edge is shared by EXACTLY two triangles. Please make sure your mesh meet this requirement.

[Edited by - ma_hty on April 22, 2008 1:28:36 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now