OpenGL is weird

Started by
0 comments, last by _the_phantom_ 16 years, 5 months ago
Ok, for my assignment i have to draw a logo. But the display is weird. http://img249.imageshack.us/img249/7718/screenbo8.png

// The program draws 3D logo: MLU
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
void draw3DSolid(double[],double[],int,double);
void draw(){
	double PI=3.141593;
	// Lighting settings
	GLfloat mat_ambient[]={0.8f,0.8f,0.8f,1.0f};		
	GLfloat mat_diffuse[]={0.6f,0.6f,0.6f,1.0f};		
	GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
	GLfloat mat_shininess[]={50.0f};
	glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
	glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
	glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
	glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
	GLfloat lightIntensity[]={0.5f,0.5f,0.5f,1.0f};
	GLfloat light_position[]={2.0f,6.0f,3.0f,0.0f};
	glLightfv(GL_LIGHT0,GL_POSITION,light_position);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
	//
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-2.0*64/48.0,2.0*64/48.0,-2.0,2.0,0.1,100);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.5f,0.0f,0.0f);
	glTranslatef(-1.0f,0.0f,0.0f);
	double xarr[30],yarr[30];
	// M
	xarr[0]=0.0f;yarr[0]=0.0f;
	xarr[1]=0.2f;yarr[1]=0.0f;
	xarr[2]=0.2f;yarr[2]=0.7f;
	xarr[3]=0.5f;yarr[3]=0.262f;
	xarr[4]=0.8f;yarr[4]=0.7f;
	xarr[5]=0.8f;yarr[5]=0.0f;
	xarr[6]=1.0f;yarr[6]=0.0f;
	xarr[7]=1.0f;yarr[7]=1.0f;
	xarr[8]=0.8f;yarr[8]=1.0f;
	xarr[9]=0.5f;yarr[9]=0.562f;
	xarr[10]=0.2f;yarr[10]=1.0f;
	xarr[11]=0.0f;yarr[11]=1.0f;
	draw3DSolid(xarr,yarr,12,0.3);
	// draw face polygons
	for(double z=0;z<=0.3;z+=0.3){
		glBegin(GL_POLYGON);		// left "leg" of M		
			glVertex3f(xarr[0],yarr[0],z);
			glVertex3f(xarr[1],yarr[1],z);
			glVertex3f(xarr[10],yarr[10],z);
			glVertex3f(xarr[11],yarr[11],z);
		glEnd();
		glBegin(GL_POLYGON);		// left "arm" of M
			glVertex3f(xarr[2],yarr[2],z);
			glVertex3f(xarr[3],yarr[3],z);
			glVertex3f(xarr[9],yarr[9],z);
			glVertex3f(xarr[10],yarr[10],z);
		glEnd();
		glBegin(GL_POLYGON);		// right "arm" of M
			glVertex3f(xarr[3],yarr[3],z);
			glVertex3f(xarr[4],yarr[4],z);
			glVertex3f(xarr[8],yarr[8],z);
			glVertex3f(xarr[9],yarr[9],z);
		glEnd();
		glBegin(GL_POLYGON);		// right "leg" of M
			glVertex3f(xarr[5],yarr[5],z);
			glVertex3f(xarr[6],yarr[6],z);
			glVertex3f(xarr[7],yarr[7],z);
			glVertex3f(xarr[8],yarr[8],z);
		glEnd();
	}	
	// L
	xarr[0]=0.0f;yarr[0]=0.0f;
	xarr[1]=0.8f;yarr[1]=0.0f;
	xarr[2]=0.8f;yarr[2]=0.2f;
	xarr[3]=0.2f;yarr[3]=0.2f;
	xarr[4]=0.2f;yarr[4]=1.0f;
	xarr[5]=0.0f;yarr[5]=1.0f;
	glTranslatef(1.3f,0.0f,0.0f);		// shift coordinates
	draw3DSolid(xarr,yarr,6,0.3);
	// draw face polygons
	for(double z=0;z<=0.3;z+=0.3){
		glBegin(GL_POLYGON);		// the "leg" of L
			glVertex3f(xarr[0],yarr[0],z);
			glVertex3f(xarr[3],yarr[3],z);
			glVertex3f(xarr[4],yarr[4],z);
			glVertex3f(xarr[5],yarr[5],z);
		glEnd();
		glBegin(GL_POLYGON);		// the "foot" of L
			glVertex3f(xarr[0],yarr[0],z);
			glVertex3f(xarr[1],yarr[1],z);
			glVertex3f(xarr[2],yarr[2],z);
			glVertex3f(xarr[3],yarr[3],z);
		glEnd();
	}
	// U
	int pts=0,arc0start,arc0end,arc1start,arc1end;
	arc0start=0;
	for(double i=PI;i<=2.0*PI;i+=PI/10){	// inner arc centered at (0.4,0.4) r=0.2
		xarr[pts]=0.4+0.2*cos(i);
		yarr[pts]=0.4+0.2*sin(i);
		pts++;
	}
	arc0end=pts-1;	
	xarr[pts]=0.6f;yarr[pts]=1.0f;pts++;	// top points of right "leg"	
	xarr[pts]=0.8f;yarr[pts]=1.0f;pts++;
	arc1start=pts;
	for(double i=0;i>=-PI;i-=PI/10.0){		// outer arc centered at (0.4,0.4) r=0.4
		xarr[pts]=0.4+0.4*cos(i);
		yarr[pts]=0.4+0.4*sin(i);
		pts++;
	}
	arc1end=pts-1;
	xarr[pts]=0.0f;yarr[pts]=1.0f;pts++;	// top points of left "leg"
	xarr[pts]=0.2f;yarr[pts]=1.0f;pts++;
	glTranslatef(1.0f,0.0f,0.0f);			// shift coordinates
	draw3DSolid(xarr,yarr,pts,0.3f);
	// draw face polygons
	for(double z=0;z<=0.3;z+=0.3){
		// draw arc polygons
		int pt2=arc1end;		// last point of each polygon
		for(int i=arc0start;i<arc0end;i++){		// loop through the inner arc
			glBegin(GL_POLYGON);
				glVertex3f(xarr,yarr,z);
				glVertex3f(xarr[i+1],yarr[i+1],z);
				glVertex3f(xarr[pt2-1],yarr[pt2-1],z);
				glVertex3f(xarr[pt2],yarr[pt2],z);
				pt2--;			// shift last point
			glEnd();
		}
		// draw regular polygons
		glBegin(GL_POLYGON);			// right "leg" of U
			glVertex3f(xarr[arc0end],yarr[arc0end],z);
			glVertex3f(xarr[arc0end+1],yarr[arc0end+1],z);
			glVertex3f(xarr[arc0end+2],yarr[arc0end+2],z);
			glVertex3f(xarr[arc0end+3],yarr[arc0end+3],z);
		glEnd();
		glBegin(GL_POLYGON);			// left "leg" of U
			glVertex3f(xarr[arc1end],yarr[arc1end],z);
			glVertex3f(xarr[arc1end+1],yarr[arc1end+1],z);
			glVertex3f(xarr[arc1end+2],yarr[arc1end+2],z);
			glVertex3f(xarr[arc0start],yarr[arc0start],z);
		glEnd();
	}
	glFlush();
}
int main(int argc, char** argv){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,100);
	glutCreateWindow("3D Logo");
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
	glClearColor(0.8f,0.8f,0.8f,0.0f);
	glViewport(0,0,640,480);
	glutDisplayFunc(draw);
	glutMainLoop();
}
// draw3DSolid draws an extruded outline of a figure of the specified thickness 
void draw3DSolid(double xarr[],double arry[],int points,double thickness){
	for(int i=0;i<points-1;i++){
		glBegin(GL_POLYGON);
			glVertex3f(xarr,arry,0.0f);
			glVertex3f(xarr[i+1],arry[i+1],0.0f);
			glVertex3f(xarr[i+1],arry[i+1],thickness);
			glVertex3f(xarr,arry,thickness);
		glEnd();
	}
	glBegin(GL_POLYGON);		// connect first point with last point
		glVertex3f(xarr[0],arry[0],0.0f);
		glVertex3f(xarr[points-1],arry[points-1],0.0f);
		glVertex3f(xarr[points-1],arry[points-1],thickness);
		glVertex3f(xarr[0],arry[0],thickness);
	glEnd();
}

Advertisement
1) Home work help is frowned upon here.
2) Just dumpping code with no question is just annoying; people aren't going to fix your code for you

This topic is closed to new replies.

Advertisement