PDA

View Full Version : Need help drawing coplanar polygons

dmats
11-07-2008, 11:54 AM
Hello,
I'm new to OpenGL and I'm hoping someone can help me out with this.

I have a red rectangle and I want to be able to draw triangles on either side of it. I have a set of data points that give me the vertices of the triangles that I want to draw. The problem is that they lie on the same plane as the red rectangle.

I've read up on z-fighting and I understand that, but I don't think it fixes my problem. I want to draw the triangles on one side of the rectangle and have the other side remain the red rectangle. Basically, it's like having a piece of paper and I want to write on one side of it but not be able to see that writing from the other side.

I'm not sure what kind of problem I'm dealing with. Is it a blending problem, a depth problem, or maybe even a texturing problem? Maybe it's a simple as just adding a slight offset to my triangles so they don't reside in the same plane (the problem is, when I zoom in, I don't want to see a gap between the triangles and rectangle)? I'd appreciate any help anyone could give me.

Thank you!

trinitrotoluene
11-07-2008, 01:01 PM
If your triangles is entirely inside your rectangle, you can use the stencil buffer to do that. I will try it and if it works, I will post the algorithm.

trinitrotoluene
11-07-2008, 02:14 PM
There is a first solution. When I draw the triangles over the rectangle I use the counter clock wise winding to view it on recto and clock wise winding on verso.

#include <iostream>

#include <GL/glut.h>
#include <GL/glu.h>

static float angle = 0.0;

static void draw(void);
static void init(void);
static void redraw(void);
static void reshape(int w,int h);

int main(int argc,char **argv)
{
glutInit(&amp;argc,argv);

glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_STEN CIL|GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("Ortho");

glutReshapeFunc(reshape);
glutDisplayFunc(draw);
glutIdleFunc(redraw);

init();
glutMainLoop();

return(EXIT_SUCCESS);
}

void draw(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL _STENCIL_BUFFER_BIT);
angle+=1.0f;
glRotatef(angle,0.0,1.0,0.0);

glutSwapBuffers();
}

{

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

glColor4f(1.0,1.0,1.0,1.0);

glVertex4f(-100.0,-100.0,0.0,1.0);
glVertex4f(100.0,-100.0,0.0,1.0);
glVertex4f(100.0,100.0,0.0,1.0);
glVertex4f(-100.0,100.0,0.0,1.0);
glEnd();

glStencilFunc(GL_EQUAL, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glEnable(GL_CULL_FACE);
glDepthFunc(GL_ALWAYS);

glColor4f(1.0,0.0,0.0,1.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex4f(-50.0,-50.0,0.0,1.0);
glVertex4f(50.0,-50.0,0.0,1.0);
glVertex4f(0.0,50.0,0.0,1.0);
glEnd();

glColor4f(0.0,1.0,0.0,1.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex4f(-50.0,-50.0,0.0,1.0);
glVertex4f(0.0,50.0,0.0,1.0);
glVertex4f(50.0,-50.0,0.0,1.0);
glEnd();

glDepthFunc(GL_LESS);
glDisable(GL_CULL_FACE);
glDisable(GL_STENCIL_TEST);

}

void init(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}

void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
// gluPerspective(65.0,(GLfloat)width/(GLfloat)height,1.0,100.0);
glOrtho(-200,200,-200,200,-1000.0,1000.0);
// glOrtho(0.0,(GLdouble)width,0,(GLdouble)height,-1.0,100.0);
glMatrixMode(GL_MODELVIEW);
}

void redraw(void)
{
glutPostRedisplay();
}

So the next thing to do is try to reset the stencil buffer where I draw the rectangle without the need of clearing the entire stencil buffer and make it work in a complex scene.

trinitrotoluene
11-07-2008, 03:02 PM
This is my final solution that seem to work but is unoptimized.

{

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

glColor4f(1.0,1.0,1.0,1.0);
//Draw "paper"
glVertex4f(-100.0,-100.0,0.0,1.0);
glVertex4f(100.0,-100.0,0.0,1.0);
glVertex4f(100.0,100.0,0.0,1.0);
glVertex4f(-100.0,100.0,0.0,1.0);
glEnd();

glStencilFunc(GL_EQUAL, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glEnable(GL_CULL_FACE);
glDepthFunc(GL_ALWAYS);

//Draw recto triangle
glColor4f(1.0,0.0,0.0,1.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex4f(-50.0,-50.0,0.0,1.0);
glVertex4f(50.0,-50.0,0.0,1.0);
glVertex4f(0.0,50.0,0.0,1.0);
glEnd();

//Draw verso triangle
glColor4f(0.0,1.0,0.0,1.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex4f(-50.0,-50.0,0.0,1.0);
glVertex4f(0.0,50.0,0.0,1.0);
glVertex4f(50.0,-50.0,0.0,1.0);
glEnd();

glDepthFunc(GL_LESS);
glDisable(GL_CULL_FACE);
// glClear(GL_STENCIL_BUFFER_BIT);
//clear the portion of stencil buffer we just write
glDepthFunc(GL_EQUAL);
glStencilOp(GL_KEEP,GL_KEEP,GL_ZERO);

glVertex4f(-100.0,-100.0,0.0,1.0);
glVertex4f(100.0,-100.0,0.0,1.0);
glVertex4f(100.0,100.0,0.0,1.0);
glVertex4f(-100.0,100.0,0.0,1.0);
glEnd();

glDepthFunc(GL_LESS);

glDisable(GL_STENCIL_TEST);

}

IneQuation.pl
11-08-2008, 10:00 AM