PDA

View Full Version : 3D Pacman: Unintentional Transparency, + other stuff.



Faze
03-17-2005, 08:47 AM
Hi,
As my first openGL project I'm trying to make a 3D pacman game, improving it as I learn more. Currently I'm early on, working on creating pacman himself, using a vertex array. The array contains vertices which specify a number of quadlists, each of which is a band around the sphere that is pacman.
I open and close Pacman's mouth by not drawing a number (which increases and decreases according to a timer) of strips extending in both directions from the first strip. I want to put semi-circle polygons at the top and bottom of his mouth, and i'm drawing them successfully, but there are some odd issues with the visibility of those polygons and the transparency of the sphere I'm creating. It seems like I can see the bottom part of his mouth (which I've temporarily colored green) through any other part of his body. Also, I can't see most of the top mouth section, which is colored blue, and I don't know why. However, when I don't draw the body of pacman, the mouth polys draw correctly.

Here is the code I'm using to draw it all. I've also provided a download link to the current code ANY help would be greatly appreciated, as I'm really not sure what could be wrong! I will gladly answer any questions about how I'm doing things.

Here is the entire source file. (http://mail.rochester.edu/~mr001m/main.cpp)

Here is the executable. (http://mail.rochester.edu/~mr001m/main.exe)
KEYS:
Arrow keys to move pacman around the screen
Middle mouse button to pause pacman's motion and mouth
Left mouse button to resume motion
F1 to draw filled polygons
F2 to draw outlined polygons (wireframe)
F3 to draw point-defined polygons

CODE:


GLfloat red = 1.0;
GLfloat green = 1.0;
GLfloat blue = 0.0;

//loop through all of the quad strips which define the sphere
for (GLint i = 0; i < numStrips; i++) {

//if we are not inside the mouth, draw the strip and color it yellow
if (i > mouthSize &amp;&amp; i < (numStrips - mouthSize)) {
glBegin(GL_QUAD_STRIP);
{
glColor3f(red, green, blue);

//precision is the number of faces per quad strip, so there are (precision + 1) * 2 vertices per strip
//the vertices are stored in this order:
//first leading edge vertex
//first back edge vertex
//second leading edge vertex
//etc etc
for (GLint j = 0; j < (precision + 1) * 2; j++) {
glArrayElement((i * (precision + 1) * 2) + j);
}
}
glEnd();
}

//if we are inside the mouth, draw a polygon defined by the same points which would've defined one edge of the quad strip
if (i == (numStrips - mouthSize) - 1) {
glBegin(GL_POLYGON);
{
glColor3f(0.0, 1.0, 0.0);

for (GLint j = 0; j < (precision + 1) * 2; j += 2) {
glArrayElement((i * (precision + 1) * 2) + j);
}
}
glEnd();
}
if ( i == mouthSize + 1) {
glBegin(GL_POLYGON);
{
glColor3f(0.0, 0.0, 1.0);

for (GLint j = ((precision + 1) * 2) - 1; j >= 0; j -= 2) {
glArrayElement((i * (precision + 1) * 2) + j);
}

}
glEnd();
}

}

Overmind
03-17-2005, 09:37 AM
You have to enable depth test.

In your main function, request a depth buffer:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

In your init function, enable depth test:
glEnable(GL_DEPTH_TEST);

And in your drawing function, you have to clear the depth buffer, too:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Faze
03-18-2005, 02:55 PM
Thanks a ton, that fixed it completely!