Computing.Net > Forums > Programming > Opengl-Hidden surface removal

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Opengl-Hidden surface removal

Reply to Message Icon

Name: y2jtalukder
Date: April 28, 2004 at 10:38:32 Pacific
OS: Win 98se
CPU/Ram: N/A
Comment:

I am trying to create a simple program that displays complex objects made of polygons. The problem that I am having is that the faces of objects dont appear to be drawn in the correct way, in that the faces that are behind front faces show through. I understand that the solution to this problem is Hidden surface removal, but I am having difficulty incorporating it into my program. Also if you know how, how would you orientate the camera via keys pressed by user-more specifically "gluLookat" I have tried to use the array at the top, but I am having difficulty. The code follows. Please feel free to modify the code, but I would appreciate "//" comments to indicate any changes. Thanks in advance for any help! Note that I am trying to create a closed cylinder, and that you have to rotate the model to see the problem.

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

static left = 0, right = 0, up = 0, down = 0, spin =0, zoom = 0, shift = 0, turn = 0;
static GLdouble viewer[]= {0.0, 0.0, 5.0};
GLUquadricObj *quadric;

void Display ( void )
{
quadric = gluNewQuadric();
gluQuadricNormals(quadric, GL_SMOOTH);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();

//Rotate whole model
glRotatef((GLfloat) spin,0.0,1.0,0.0);
glRotatef((GLfloat) turn, 1.0, 0.0, 0.0);

//Zomm in on model
glTranslatef(0.0,0.0,(GLfloat) zoom);

//Move whole model to left and right
glTranslatef((GLfloat) shift,0.0,0.0);

glColor3f(1.0,0.0,0.0);
gluCylinder(quadric, 0.50, 0.50, 1, 35, 25);
gluDisk(quadric, 0.0, 0.5, 35, 35);
glPopMatrix();
glFlush();
glutSwapBuffers();

}

void MyInit ( void )
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
//glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
///////////////////////////////LIGHTING///////////////////////////
//variables
GLfloat LightModelAmbient[] = {0.2, 0.2, 0.2, 1.0};

GLfloat light_position[] = {-2.0, 2.0, 2.0, 1.0};
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_spec[] = {1.0, 1.0, 1.0, 1.0};

GLfloat mat_ambi [] = {0.11, 0.06, 0.11, 1.0};
GLfloat mat_diff [] = {0.43, 0.47, 0.54, 1.0};
GLfloat mat_spec [] = {0.33, 0.33, 0.52, 1.0};
GLfloat mat_emis [] = {0.0, 0.0, 0.0, 0.0};
GLfloat mat_shin = 10.0;

//lighting model commands
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, LightModelAmbient);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0.0);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 0.0);

//lighting commands
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_spec);

//material commands
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambi);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_spec);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emis);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shin);
}

void Keys (unsigned char key, int xx, int yy)
{
if(key == 'r')
{
spin += -2;
glutPostRedisplay();
}

else if(key == 'R')
{
spin += 2;
glutPostRedisplay();
}
else if(key == 'z')
{
zoom += 1;
glutPostRedisplay();
}
else if(key == 'Z')
{
zoom += -1;
glutPostRedisplay();
}
else if(key == 'm')
{
shift += -2;
glutPostRedisplay();
}
else if(key == 'M')
{
shift += 2;
glutPostRedisplay();
}

else if (key == 'd')
{
zoom = 0;
spin = 0;
shift = 0;
turn = 0;
glutPostRedisplay();
}

else if (key == 't')
{
turn += 2;
glutPostRedisplay();
}

else if (key == 'T')
{
turn -= 2;
glutPostRedisplay();
}
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 0.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0,0.0, 1.0, 0.0);
}

int main(int argc, char **argv)
{
glutInit ( &argc, argv ) ;
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ) ;
glutInitWindowSize ( 500,500 ) ; // window size
glutInitWindowPosition ( 50, 50 ) ; // window position
glutCreateWindow ( "Display sphere" );

MyInit ( ) ;
glutDisplayFunc( Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(Keys);
glutMainLoop ( ) ;
}




Sponsored Link
Ads by Google

Response Number 1
Name: LeHKN
Date: April 28, 2004 at 23:20:19 Pacific
Reply:

"Also if you know how, how would you orientate the camera via keys pressed by user- more specifically gLuLookat."

reply:

Tampering with gluLookat will significantly alter your lighting. I think the following method will better suit your purpose. It's an alternative to manipulating gluLookat.
What you want to do is wrap this around your objects, then use the keyboard function to allow the user to manipulate the settings to view the scene from different angles and distance.

TRY THIS!

float scalex=1, scaley=1, scalez=1;
int transx=0, transy=0, transz=0;
int degreez=0, degreex=0, degreey=0, degree=0;

//inside display function
glPushMatrix();
glTranslatef(transx, transy, transz);
glScalef(scalex, scaley, 1);
glRotatef(degree, degreex, degreey, degreez);

//draw your objects here

glPopMatrix();

void keyboard (unsigned char key, int x, int y)
{

switch (key) {
case 'x':
scalex/=2;
break;
case 'X':
scalex*=2;
break;
case 'y':
scaley/=2;
break;
case 'Y':
scaley*=2;
break;
case 'z':
scalez/=2;
break;
case 'Z':
scalez*=2;
break;
case 'u':
transz-=1;
break;
case 'U':
transz+=1;
break;
case 'l':
transx-=2;
break;
case 'L':
transx+=2;
break;
case 'r':
transy-=3;
break;
case 'R':
transy+=3;
break;
case 'A':
degree+=10;
degreez=1;
degreey=0;
degreex=0;
break;
case 'B':
degree+=5;
degreex=1;
degreey=0;
degreez=0;
break;
case 'C':
degree+=20;
degreey=1;
degreex=0;
degreez=0;
break;
default:
break;

}


glutPostRedisplay();

}


//This should work. If you're still interested in manipulating gluLookat, let me know. gluLookat is very sensitive.

RadarSync-We drive you happy!
(www.driversuneed.com)


0

Response Number 2
Name: y2jtalukder
Date: April 29, 2004 at 03:44:58 Pacific
Reply:

Thankyou for your help, but I would prefer to use gluLookAt in conjunction with keys to alter the view.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Opengl-Hidden surface removal

Undocumented DOS commands www.computing.net/answers/programming/undocumented-dos-commands/9094.html

If or conditions and hidden system files www.computing.net/answers/programming/if-or-conditions-and-hidden-system-files/20087.html

Script to remove lines from a file www.computing.net/answers/programming/script-to-remove-lines-from-a-file/7090.html