Jump to content
• Advertisement

# OpenGL Some polygons are not drawn

This topic is 3336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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: Code:
#include &lt;GL/glut.h&gt;
#include &lt;math.h&gt;

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 &lt; 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){
float radius = 0.05;
int i;

glBegin(GL_POLYGON);
glColor3f (1.0, 1.0, 0.0);
glNormal3f(0,0,-1);
for (i=0; i &lt; 360; i++)
{
float degInRad = i*3.14159/180;
glVertex3f(cos(degInRad)*radius,sin(degInRad)*radius,zcoor);
}
glEnd();
}

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

glFrontFace(GL_CW);

glColor3f(1,1,1);
glBegin(GL_QUAD_STRIP);
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);
glBegin(GL_QUADS);
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();

glBegin(GL_QUADS);
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();

glBegin(GL_QUADS);
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();

glBegin(GL_QUADS);
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 &lt; 0.70; i +=0.05){
for(j = 0.30 ; j &lt; 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 &lt; 0.70; i +=0.05){
for(j = 0.30 ; j &lt; 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);
glLoadIdentity();

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);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

glShadeModel(GL_SMOOTH);
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(&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$&lt;
$(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

#### Share this post

##### Share on other sites
Advertisement
I havent look at the code very thoroughly but you are playing alot with the
winding (order of specifying vertices (clock wise or counter clock wise), with glFrontFace, the winding is the way opengl gl detects if a polygon is front facing or back facing in relation to the viewpoint, try disabling back face culling to see if that's your problem.

glDisable(GL_CULL_FACE);

#### Share this post

##### Share on other sites
Use [source]code[/source] tags to post code, or it will be much too hard to read. It will look like this:
void function() {}

Anyway, for your question, my guess is that you have enabled face-culling, to only display front-faces. Usually triangles have front- and back-faces. To disable that, making the faces visible from both sides, use glDisable(GL_CULL_FACE). If you don't want that, reverse the order of the vertices in your triangles, which will make the other side the front.

#### Share this post

##### Share on other sites
I tried with glDisable(GL_CULL_FACE) in the init() function, but the result is the same.

#### Share this post

##### Share on other sites
Try it in the draw-function. I would suspect your triangle-fans though.. when drawing in a double-loop. Try putting glBegin/glEnd in the outer loop instead of outside both loops.. it seems strange to draw a triangle-fan like that.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
• Advertisement

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633728
• Total Posts
3013575
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!