PDA

View Full Version : Not all polygons are drawn

MiteT
10-27-2009, 02:59 PM
I am a beginner with this library and have a problem that puzzles me a lot.
I am trying to make a 3D flag which I will set a light on and be able to rotate it. I do that by drawing 6 polygons, so the flag can have depth and two sides. But, whenever I rotate it, I see that only 4 of the 6 sides are drawn, and I can see the inside of the flag.

I have got this far, looking from examples:

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

int angle=0;
int angle2 = 0;
int angle3 = 0;
float svetloZ = 0.1;
float svetloY = 0.1;
float svetloX = 0.1;
float flagFrontZ = 0.6;
GLfloat gold[] = { 0.7f, 0.8f, 0.2f, 1.0f };
GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // ESC e pritisnat
exit(0);
break;
case 'A':
angle+=5;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'S':
angle-=5;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'H':
angle2-=5;
glutPostRedisplay();
break;
case 'J':
angle2+=5;
glutPostRedisplay();
break;
case 'U':
angle3+=5;
glutPostRedisplay();
break;
case 'I':
angle3-=5;
glutPostRedisplay();
break;
case 'M':
if(svetloZ == 0.1) break;
else {
svetloZ-=0.05;
glutPostRedisplay();
break;
}
case 'N':
if(svetloZ == 0.9) break;
else {
svetloZ+=0.05;
glutPostRedisplay();
break;
}
case 'V':
if(svetloY == 0.9) break;
else {
svetloY+=0.05;
glutPostRedisplay();
break;
}
case 'B':
if(svetloY == 0.1) break;
else {
svetloY-=0.05;
glutPostRedisplay();
break;
}
case 'X':
if(svetloX == 0.9) break;
else {
svetloX+=0.05;
glutPostRedisplay();
break;
}
case 'C':
if(svetloX == 0.1) break;
else {
svetloX-=0.05;
glutPostRedisplay();
break;
}
}
}

void star(float zcoor){
int i = 0;

for(i ; i < 12 ; i++){
glRotatef(30,0,0,1);

glBegin(GL_POLYGON);
glColor3f (1.0, 1.0, 0.0);
glNormal3f(0,0,-1);

glVertex3f(0.05,0,zcoor);
glVertex3f(0,0.05,zcoor);
glVertex3f(0.1,0.1,zcoor);
glEnd();
}
}

void circle(float zcoor){
int i;

glBegin(GL_POLYGON);
glColor3f (1.0, 1.0, 0.0);
glNormal3f(0,0,-1);
for (i=0; i < 360; i++)
{
}
glEnd();
}

void kocka (void) // ova e objektot koj go pretstavuva svetloto
{
int i, j;

glFrontFace(GL_CW);

glColor3f(1,1,1);
glNormal3f(0,0,1);
glVertex3f(0.5,0.5,0);
glVertex3f(0.5,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.5,0);

glVertex3f(0.5,0.5,0.1);
glVertex3f(0.5,0.6,0.1);
glVertex3f(0.6,0.6,0.1);
glVertex3f(0.6,0.5,0.1);

glVertex3f(0.5,0.5,0);
glVertex3f(0.5,0.5,0.1);
glVertex3f(0.6,0.6,0.1);
glVertex3f(0.6,0.6,0);

glEnd();
}

void flag(void){
float i; float j;

glColor3f(0,1,0);
glNormal3f(1,0,0);
glVertex3f(0.7,0.3,0.6);
glVertex3f(0.7,0.3,0.7);
glVertex3f(0.7,0.7,0.7);
glVertex3f(0.7,0.7,0.6);
glEnd();

glNormal3f(-1,0,0);
glVertex3f(0.3,0.3,0.6);
glVertex3f(0.3,0.3,0.7);
glVertex3f(0.3,0.7,0.7);
glVertex3f(0.3,0.7,0.6);
glEnd();

glNormal3f(0,1,0);
glVertex3f(0.3,0.7,0.6);
glVertex3f(0.3,0.7,0.7);
glVertex3f(0.7,0.7,0.7);
glVertex3f(0.7,0.7,0.6);
glEnd();

glNormal3f(0,-1,0);
glVertex3f(0.3,0.3,0.6);
glVertex3f(0.3,0.3,0.7);
glVertex3f(0.7,0.3,0.7);
glVertex3f(0.7,0.3,0.6);
glEnd();

glBegin(GL_TRIANGLE_FAN);
for(i = 0.30; i < 0.70; i +=0.05){
for(j = 0.30 ; j < 0.70; j +=0.05){

glColor3f(1,0,0);
glNormal3f(0,0,1);

glVertex3f(i,j,0.7);
glVertex3f(i+0.05,j,0.7);
glVertex3f(i+0.05,j+0.05,0.7);
glVertex3f(i,j+0.05,0.7);

}
}
glEnd();

glBegin(GL_TRIANGLE_FAN);
for(i = 0.30; i < 0.70; i +=0.05){
for(j = 0.30 ; j < 0.70; j +=0.05){

glNormal3f(0,0,-1);
glColor3f(1,0,0);

glVertex3f(i,j,flagFrontZ);
glVertex3f(i+0.05,j,flagFrontZ);
glVertex3f(i+0.05,j+0.05,flagFrontZ);
glVertex3f(i,j+0.05,flagFrontZ);

}
}
glEnd();

glPushMatrix();

glTranslatef(0.5,0.5,0);
circle(flagFrontZ);
star(flagFrontZ);

glPopMatrix();

}

void display(void)
{
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);

glScalef(0.5,0.5,0.5);
glFrontFace(GL_CCW);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glTranslatef(0.5, 0.5, 0);
glRotatef(angle3,1,0,0);
glRotatef(angle2,0,1,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.5, -0.5, 0);

GLfloat lightPos[] = { svetloX, svetloY, svetloZ, 1.0f };
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

glPushMatrix();
glTranslatef(svetloX,svetloY,svetloZ);
kocka();
glPopMatrix();

flag();

glFlush ();
glutSwapBuffers();
}

void init (void)
{
glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);

GLfloat ambientLight[] = { 0.2f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};

// Setup and enable light 0
GLfloat lightPos[] = { 0.f, 0.0f, svetloZ, 1.0f };
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

glEnable(GL_LIGHT0);
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize (450, 350);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Здраво");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Also, I compile it with this make file.

TARGETS = primer
LLDLIBS = -lglut32 -lopengl32 -lglu32
default: \$(TARGETS)
all: default
.c.o:
gcc -c \$<
\$(TARGETS): \$(TARGETS).o
gcc \$@.o -L . \$(LLDLIBS) -o \$@
clean:
rm -f *.o \$(TARGETS)

Does anyone has an idea where or what I am doing wrong?

Thank you,
Mike

marshats
10-27-2009, 04:41 PM