PDA

View Full Version : Problem : My sphere is perforated

julio26
05-13-2005, 09:19 AM
Hello everybody,
I have an important question, I want to draw a sphere using glut and visual c++. Here is my code :

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <list>

using namespace std;

static float tx=0.0,ty=0.0,tz=0.0;
static float rx=0.0,ry=0.0,rz=0.0;
bool Tx,Ty,Tz,Rx,Ry,Rz;

float ambC[4] = {0.5,0.5,0.5,1};
float diffC[4] = {0.1,0.1,0.1,1.0};
float specC[4] = {0.2,0.2,0.2,1.0};
float ambH[4] = {0.0,0.0,1.0,1.0};
float diffH[4] = {0.1,0.1,0.1,1.0};
float specH[4] = {0.2,0.2,0.2,1.0};

double a=0;
int LightPos[4] = {0,0,3,1};
int MatSpec [4] = {1,1,1,1};

void redraw(){
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,-10,0,0,0,0,1,0);

glRotated(a,0,1,0);
glLightiv(GL_LIGHT0,GL_POSITION,LightPos);
if(Tx){
glTranslatef(tx,0,0);
}
else if(Ty){
glTranslatef(0,ty,0);
}
else if(Tz){
glTranslatef(0,0,tz);
}
else if(Rx){
glRotatef(rx,1.0,0.0,0.0);
}
else if(Ry)glRotatef(ry,0.0,1.0,0.0);
else if(Rz)glRotatef(rz,0.0,0.0,1.0);

glColor3f(0.0,0.0,1.0);
glMaterialfv(GL_FRONT, GL_SPECULAR, specC);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffC);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambC);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_D IFFUSE);

a=+1;

glutSwapBuffers();
glutPostRedisplay();
}

void processNormalKeys(unsigned char key, int x, int y) {
/*
Echap : sortir
x: translation on x
y: translation on y
z: translation on z
X: translation on - x
Y: translation on - y
Z: translation on - z
1: rotation x angle >0
2: rotation x angle <0
3: rotation y angle >0
4: rotation y angle <0
5: rotation z angle >0
6: rotation z angle <0
*/

if (key == 27)
exit(0);
else if(key==120){
tx=tx+0.1;
Tx=true;
Ty=false;
Tz=false;
Rx=false;
Ry=false;
Rz=false;
glutPostRedisplay();
}
else if(key==88){
tx=tx-0.1;
Tx=true;
Ty=false;
Tz=false;
Rx=false;
Ry=false;
Rz=false;
glutPostRedisplay();
}
else if(key==121){
ty=ty+0.1;
Tx=false;
Ty=true;
Tz=false;
Rx=false;
Ry=false;
Rz=false;
glutPostRedisplay();
}
else if(key==89){
ty=ty-0.1;
Tx=false;
Ty=true;
Tz=false;
Rx=false;
Ry=false;
Rz=false;
glutPostRedisplay();
}
else if(key==122){
tz=tz+0.1;
Tx=false;
Ty=false;
Tz=true;
Rx=false;
Ry=false;
Rz=false;
glutPostRedisplay();
}
else if(key==90){
tz=tz-0.1;
Tx=false;
Ty=false;
Tz=true;
Rx=false;
Ry=false;
Rz=false;
glutPostRedisplay();
}
else if(key==49){
rx=rx+0.5;
Tx=false;
Ty=false;
Tz=false;
Rx=true;
Ry=false;
Rz=false;
}
else if(key==50){
rx=rx-0.5;
Tx=false;
Ty=false;
Tz=false;
Rx=true;
Ry=false;
Rz=false;
}

else if(key==51){
ry=ry+0.5;
Tx=false;
Ty=false;
Tz=false;
Rx=false;
Ry=true;
Rz=false;
}
else if (key==52){
ry=ry-0.5;
Tx=false;
Ty=false;
Tz=false;
Rx=false;
Ry=true;
Rz=false;
}
else if (key==53){
rz=rz+1.0;
Tx=false;
Ty=false;
Tz=false;
Rx=false;
Ry=false;
Rz=true;
}
else if (key==54){
rz=rz-1.0;
Tx=false;
Ty=false;
Tz=false;
Rx=false;
Ry=false;
Rz=true;
}
}

void reshape(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
gluPerspective(45,float(width)/float(height),0,100); glMatrixMode(GL_MODELVIEW);
}

void initGL(){
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_CULL_FACE);
}

int main( int argc, char *argv[ ], char *envp[ ] ){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640,480); //Optionnel
glutCreateWindow("OpenGL");
glutReshapeFunc(reshape);
glutDisplayFunc(redraw);
glutKeyboardFunc(processNormalKeys);
initGL();
glutMainLoop();

return 1;

}

When i execute this code, my sphere is perforated, I don't know why !
Thanks

execom_rt
05-13-2005, 10:04 AM
and it is not deflating ? :D

T101
05-13-2005, 10:22 AM
A screenie would be good.

But:

gluPerspective(45,float(width)/float(height),0,100); You're using 0 as the zNear for the perspective view. Make it 0.1 or something.
Because of the way the z-buffer values are calculated from the depth, you'll have precision problems (due to a division by zero) when zNear is 0.

julio26
05-13-2005, 10:54 AM
Thanks it works !!!
I really really really appreciate your help!
I have another question if you don't mind.
You may have observed that when you translate on x then on y, the sphere is reinitialized in the center... I would like to avoid this thing. Is it possible ?
Thanks

Trahern
05-13-2005, 12:14 PM
Originally posted by julio26:
Thanks it works !!!
I really really really appreciate your help!
I have another question if you don't mind.
You may have observed that when you translate on x then on y, the sphere is reinitialized in the center... I would like to avoid this thing. Is it possible ?
Thankschange all your glTranslateg calls to:
glTranslatef(tx,ty,tz);
this should work

julio26
05-13-2005, 12:49 PM
Thanks, it works perfectly. :)
Do you have an idea for doing the same with rotation so that i will be able to mix rotations and translations?

Trahern
05-14-2005, 01:15 AM
Originally posted by julio26:
Thanks, it works perfectly. :)
Do you have an idea for doing the same with rotation so that i will be able to mix rotations and translations?well you can try to replace :

if(Tx){
glTranslatef(tx,0,0);
}
else if(Ty){
glTranslatef(0,ty,0);
}
else if(Tz){
glTranslatef(0,0,tz);
}
else if(Rx){
glRotatef(rx,1.0,0.0,0.0);
}
else if(Ry)glRotatef(ry,0.0,1.0,0.0);
else if(Rz)glRotatef(rz,0.0,0.0,1.0);with:

glTranslatef(tx,ty,tz); //as you have probably already done
glRotatef(rx,1,0,0);
glRotatef(ry,0,1,0);
glRotatef(rz,0,0,1);but the best thing you can do is to learn something about transformations in openGL ;)