Archived

This topic is now archived and is closed to further replies.

chbfiv

Can see inside box faces, not outside

Recommended Posts

Im working on my Mesh class atm for my win32 open engine. my current problem is that when i render a cube from my Mesh class, I only see in inside of the box. I don''t have any lighting set up, but do I need to fix my normals even when im not using lighting?

Share this post


Link to post
Share on other sites
I did just turned it on, lol. I'm using OpenGL. bah

Edit:
glEnable(GL_CULL_FACE);

I took that out and it still does the same thing. Does it have todo with the order I draw the triangles that make the box?

[edited by - chbfiv on October 6, 2003 10:41:59 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by chbfiv
Edit:
glEnable(GL_CULL_FACE);

I took that out and it still does the same thing. Does it have todo with the order I draw the triangles that make the box?


Just getting rid of that line won''t necessarily take off culling. In order to make sure it''s turned off you need to do:

glDisable(GL_CULL_FACE);

The order you define the triangle vertices in is important. OpenGL determines what to cull based on whether you define your vertices in a clockwise or counterclockwise manner. You can change this by doing:

glCullFace(GL_CW);

for clockwise or:

glCullFace(GL_CCW);

for counterclockwise. You may want to try both of these culling modes, it doesn''t really matter what winding you use as long as everything you''re rendering is consistent.

Share this post


Link to post
Share on other sites
I don't really have an order to fillinf in my box, is that the problem?

opengl.cpp redraw funtion

Mesh tmpMesh; //create mesh object

tmpMesh.box(25); //for testing, fill in a box


void OpenGL::redraw(void) {
GLfloat w = (GLfloat)glutGet(GLUT_WINDOW_WIDTH);
GLfloat h = (GLfloat)glutGet(GLUT_WINDOW_HEIGHT);
glLoadIdentity();
gluPerspective(100.0,w/h,0,1000);
static float rotateBy=0;
rotateBy+=0.5;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0,0,-50);
glRotatef(rotateBy,1,1,0);
push();
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
void OpenGL::st_redraw(void) {
OpenGLPtr->redraw();
}
int OpenGL::push() {

tmpMesh.draw();

return(0);
}

mesh.cpp

void Mesh::draw() {
for(int i=0; i < num_faces; i++) {
glBegin(GL_TRIANGLES);
glVertex3f(pFaces[i].a.x,pFaces[i].a.y,pFaces[i].a.z);
glVertex3f(pFaces[i].b.x,pFaces[i].b.y,pFaces[i].b.z);
glVertex3f(pFaces[i].c.x,pFaces[i].c.y,pFaces[i].c.z);
glEnd();
}
}

void Mesh::box(GLfloat size) {

// fill vertices

pVertices = new vertex[8];
num_vertices = 8;

for(int i=0;i < num_vertices;i++) {
pVertices[i].x = 0.0f;
pVertices[i].y = 0.0f;
pVertices[i].z = 0.0f;
pVertices[i].r = 1.0f;
pVertices[i].g = 1.0f;
pVertices[i].b = 1.0f;

}

pVertices[1].x = size;
pVertices[2].x = size;
pVertices[2].z = size;
pVertices[3].z = size;
pVertices[4].y = size;
pVertices[5].x = size;
pVertices[5].y = size;
pVertices[6].x = size;
pVertices[6].z = size;
pVertices[6].y = size;
pVertices[7].z = size;
pVertices[7].y = size;
// fill faces

pFaces = new face[12];
num_faces = 12;
pFaces[0].a = pVertices[0];
pFaces[0].b = pVertices[1];
pFaces[0].c = pVertices[2];
pFaces[1].a = pVertices[0];
pFaces[1].b = pVertices[2];
pFaces[1].c = pVertices[3];
pFaces[2].a = pVertices[4];
pFaces[2].b = pVertices[5];
pFaces[2].c = pVertices[6];
pFaces[3].a = pVertices[4];
pFaces[3].b = pVertices[6];
pFaces[3].c = pVertices[7];
pFaces[4].a = pVertices[0];
pFaces[4].b = pVertices[3];
pFaces[4].c = pVertices[4];
pFaces[5].a = pVertices[3];
pFaces[5].b = pVertices[7];
pFaces[5].c = pVertices[4];
pFaces[6].a = pVertices[0];
pFaces[6].b = pVertices[4];
pFaces[6].c = pVertices[1];
pFaces[7].a = pVertices[1];
pFaces[7].b = pVertices[4];
pFaces[7].c = pVertices[5];
pFaces[8].a = pVertices[1];
pFaces[8].b = pVertices[5];
pFaces[8].c = pVertices[2];
pFaces[9].a = pVertices[2];
pFaces[9].b = pVertices[5];
pFaces[9].c = pVertices[6];
pFaces[10].a = pVertices[2];
pFaces[10].b = pVertices[3];
pFaces[10].c = pVertices[6];
pFaces[11].a = pVertices[6];
pFaces[11].b = pVertices[3];
pFaces[11].c = pVertices[7];
}




mesh.h

struct vertex {
GLfloat x,y,z,r,g,b;
};
struct face {
vertex a,b,c;
};
class Mesh {
public:
Mesh() : num_vertices(0),num_faces(0),render(true) {}
~Mesh() {
if(pVertices)
delete [] pVertices;
if(pFaces)
delete [] pFaces;
}
virtual void draw();
void box(GLfloat size);
private:
vertex* pVertices;
face* pFaces;
int num_vertices;
int num_faces;
bool bRender;
};


Edit: took out color stuff that was not needed

[edited by - chbfiv on October 6, 2003 11:25:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by chbfiv
I don''t really have an order to fillinf in my box, is that the problem?



Oh yes you do, the order in which you feed the vertices to triangles.
Change the vertex order per face to opposite to fix the culling.

Relevant code section:


void Mesh::draw() {
for(int i=0; i < num_faces; i++) {
glBegin(GL_TRIANGLES);
glVertex3f(pFaces[i].a.x,pFaces[i].a.y,pFaces[i].a.z);
glVertex3f(pFaces[i].b.x,pFaces[i].b.y,pFaces[i].b.z);
glVertex3f(pFaces[i].c.x,pFaces[i].c.y,pFaces[i].c.z);
glEnd();
}
}


-Nik

Share this post


Link to post
Share on other sites
Man I''m losing it; Though none of my books explain what your talking about, nor did my google search help, I''m doing my best to understand.

If I''m drawing a simple cube, these are my steps.

1-fill in a vertex[8] struct''s xyz, using one value for size.
IE.

vertex[0].x = 0;vertex[0].y = 0;vertex[0].z = 0;
vertex[1].x = cubesize;vertex[1].y = 0;vertex[1].z = 0;
vertex[2].x = cubesize;vertex[2].y = 0;vertex[2].z = cubesize;
vertex[3].x = 0;vertex[3].y = 0;vertex[3].z = cubesize;

vertex[4].x = 0;vertex[4].y = cubesize;vertex[4].z = 0;
vertex[5].x = cubesize;vertex[5].y = cubesize;vertex[5].z = 0;
vertex[6].x = cubesize;vertex[6].y = cubesize;vertex[6].z = cubesize;
vertex[7].x = 0;vertex[7].y = cubesize;vertex[7].z = cubesize;

a real simple positive space box.
2-fill in a face[12] struct''s abc(a=vertex,b=vertex,c=vertex)

pFaces[0].a = pVertices[0];pFaces[0].b = pVertices[1];pFaces[0].c = pVertices[2];
pFaces[1].a = pVertices[0];pFaces[1].b = pVertices[2];pFaces[1].c = pVertices[3];
pFaces[2].a = pVertices[4];pFaces[2].b = pVertices[5];pFaces[2].c = pVertices[6];
pFaces[3].a = pVertices[4];pFaces[3].b = pVertices[6];pFaces[3].c = pVertices[7];
pFaces[4].a = pVertices[0];pFaces[4].b = pVertices[3];pFaces[4].c = pVertices[4];
pFaces[5].a = pVertices[3];pFaces[5].b = pVertices[7];pFaces[5].c = pVertices[4];
pFaces[6].a = pVertices[0];pFaces[6].b = pVertices[4];pFaces[6].c = pVertices[1];
pFaces[7].a = pVertices[1];pFaces[7].b = pVertices[4];pFaces[7].c = pVertices[5];
pFaces[8].a = pVertices[1];pFaces[8].b = pVertices[5];pFaces[8].c = pVertices[2];
pFaces[9].a = pVertices[2];pFaces[9].b = pVertices[5];pFaces[9].c = pVertices[6];
pFaces[10].a = pVertices[2];pFaces[10].b = pVertices[3];pFaces[10].c = pVertices[6];
pFaces[11].a = pVertices[6];pFaces[11].b = pVertices[3];pFaces[11].c = pVertices[7];

all the faces of the Box are connected, though with no order.
-now that you have everything in the face struct, thats all you need. Loop through each face, and render each triangle.

glPushMatrix();

for(int i=0; i < num_faces; i++)
{
glBegin(GL_TRIANGLES);
glVertex3f(pFaces[i].a.x,pFaces[i].a.y,pFaces[i].a.z);
glVertex3f(pFaces[i].b.x,pFaces[i].b.y,pFaces[i].b.z);
glVertex3f(pFaces[i].c.x,pFaces[i].c.y,pFaces[i].c.z);
glEnd();
}
glPopMatrix();


I''ve done the following to try and fix my problem.
-glDisable(GL_CULL_FACE)
-glEnable(GL_CULL_FACE)
-glCullFace(GL_BACK)
-glCullFace(GL_FRONT)
-glCullFace(GL_FRONT_AND_BACK)

I don''t understand,
quote:

Oh yes you do, the order in which you feed the vertices to triangles.

Do you mean when I fill them into a face struct, or when I render them? And Why? Clockwise? I really dont understand that, this is a 3D space, that could be any direction in my eyes.

Just to explain what is happening one more time. I render the Box on the screen, but it seems that the face that should be behind the face closer to the sceen moves itself to the front.
It makes it look like the faces that are truly closer to the viewer seem clear so you can see in the Box.

Maybe i have a bad mindset about all this atm. It seems like a simple task.

Share this post


Link to post
Share on other sites
quote:
Original post by chbfiv
Do you mean when I fill them into a face struct, or when I render them? And Why? Clockwise? I really dont understand that, this is a 3D space, that could be any direction in my eyes.


The order in which they get sent to GL is the important one. This may or may not be the same as your face struct.

The vertex winding order defines which is the front face of your polys. If you look at the front of your poly, the vertices will go either clockwise or anti-clockwise. You need to make sure that all of your faces are defined correctly to face outwards.

The order you choose to represent the front face doesn''t matter, as long as you''re consistant. You then set that with:
glFrontFace(GL_CW);
glFrontFace(GL_CCW); // Counter/Anti clockwise

Then, assuming thats all correct you typically do
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); // Remove back facing (invisible) polys

The other two options for glCullFace are rarely used so don''t worry about them. Also you may need to remember that using a mirror transformation (like a negative scale for a mirror) will mean you need to flip the front face from one to the other.

Share this post


Link to post
Share on other sites
Oh, and looking from your arrays and such (ugh, bad variable names and too much indirection ) your first two faces are defined anti-clockwise (assuming a right handed coord system, I think it flips if you''re using a left handed).

Right handed system:

y
|
|
0---x
/
/
z


I didn''t check any further though, so I don''t know if they''re consistant.

Share this post


Link to post
Share on other sites
ok, this is what I did now.

I turned on CW

and I filled in each triangle in a CW manner.
then I left them in the CW manner and turned on CCW, with the same result. On top of that, after I turned on glEnable(GL_CULL_FACE) and glCullFace(GL_BACK) and I still see both sides of each triangle.


Edit:

I even cut and pasted a Nehe demo code that creates a pyramid. Still the same problem, it cant be my vertex order, or my draw order because Its from a nehe! gerrr.

not that you need to see the pyramid code, maybe someone will get it.

glBegin(GL_TRIANGLES); // Start Drawing The Pyramid
glColor3f(15.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 15.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,15.0f,0.0f); // Green
glVertex3f(-15.0f,-15.0f, 15.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,15.0f); // Blue
glVertex3f( 15.0f,-15.0f, 15.0f); // Right Of Triangle (Front)
glColor3f(15.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 15.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,15.0f); // Blue
glVertex3f( 15.0f,-15.0f, 15.0f); // Left Of Triangle (Right)
glColor3f(0.0f,15.0f,0.0f); // Green
glVertex3f( 15.0f,-15.0f, -15.0f); // Right Of Triangle (Right)
glColor3f(15.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 15.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f,15.0f,0.0f); // Green
glVertex3f( 15.0f,-15.0f, -15.0f); // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,15.0f); // Blue
glVertex3f(-15.0f,-15.0f, -15.0f); // Right Of Triangle (Back)
glColor3f(15.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 15.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,15.0f); // Blue
glVertex3f(-15.0f,-15.0f,-15.0f); // Left Of Triangle (Left)
glColor3f(0.0f,15.0f,0.0f); // Green
glVertex3f(-15.0f,-15.0f, 15.0f); // Right Of Triangle (Left)
glEnd(); // Done Drawing The Pyramid


[edited by - chbfiv on October 7, 2003 8:46:03 PM]

Share this post


Link to post
Share on other sites