Вы находитесь на странице: 1из 5

#include

#include
#include
#include

<GL/glut.h>
<math.h>
<stdio.h>
<string.h>

#define X 1
#define Y 2
#define Z 3
float iden[4][4]={0};
int xinc=3, yinc=3,x=0,y=0;
typedef struct point // will store the lowest
{
float x, y, z,h; //x , y,z coordinates
}point;

point in the cube

// it is expected that the first4 i.e. x[0]..x[3] points represent one


face of the cube
// and x[4]..x[7] represent the opposite face
typedef struct cube
{
point x[8];
}cube;
typedef struct rect
{
point x[2];
}rect;
GLint w=600,h=400;
GLfloat x0=00,ye=20,z0=60;
//GLfloat x0=100,ye=0,z0=100;
GLfloat xref=0,yref=0,zref=0;
GLfloat vx=0,vy=1,vz=0;
GLfloat xwMin=-300, ywMin=-200, xwMax = 300, ywMax=200;
GLfloat dnear = -200, dfar=400;
void timer(int unused) {
glutPostRedisplay();
glutTimerFunc(20,timer,0);
}
void init(void)
{
glClearColor(1.0,1,1,0.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(x0,ye,z0,xref,yref,zref,vx,vy,vz);
glMatrixMode(GL_PROJECTION);
glOrtho(xwMin, xwMax,ywMin, ywMax,dnear,dfar);
//gluPerspective(120, 1, 0, 400);
}
void rotate3D(point p1, point p2, float theta)
{
float vx = p2.x - p1.x, vy = p2.y - p1.y, vz = p2.z - p1.z;

glTranslatef(p1.x,p1.y,p1.z);
glRotatef(theta, vx,vy,vz);
glTranslatef(-p1.x,-p1.y,-p1.z);
}
void drawCube1(cube c)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen
And Depth Buffer
glBegin(GL_QUADS);
// Draw A Quad
glColor3f(0.0f,1.0f,0.0f);
Set The Color To Green
glVertex3f( 50.0f, 50.0f,-50.0f);
Top Right Of The Quad (Top)
glVertex3f(-50.0f, 50.0f,-50.0f);
Top Left Of The Quad (Top)
glVertex3f(-50.0f, 50.0f, 50.0f);
Bottom Left Of The Quad (Top)
glVertex3f( 50.0f, 50.0f, 50.0f);
Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f);
Set The Color To Orange
glVertex3f( 50.0f,-50.0f, 50.0f);
Top Right Of The Quad (Bottom)
glVertex3f(-50.0f,-50.0f, 50.0f);
Top Left Of The Quad (Bottom)
glVertex3f(-50.0f,-50.0f,-50.0f);
Bottom Left Of The Quad (Bottom)
glVertex3f( 50.0f,-50.0f,-50.0f);
Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f);
Set The Color To Red
glVertex3f( 50.0f, 50.0f, 50.0f);
Top Right Of The Quad (Front)
glVertex3f(-50.0f, 50.0f, 50.0f);
Top Left Of The Quad (Front)
glVertex3f(-50.0f,-50.0f, 50.0f);
Bottom Left Of The Quad (Front)
glVertex3f( 50.0f,-50.0f, 50.0f);
Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f);
Set The Color To Yellow
glVertex3f( 50.0f,-50.0f,-50.0f);
Top Right Of The Quad (Back)
glVertex3f(-50.0f,-50.0f,-50.0f);
Top Left Of The Quad (Back)
glVertex3f(-50.0f, 50.0f,-50.0f);
Bottom Left Of The Quad (Back)
glVertex3f( 50.0f, 50.0f,-50.0f);
Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f);
Set The Color To Blue
glVertex3f(-50.0f, 50.0f, 50.0f);
Top Right Of The Quad (Left)

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

glVertex3f(-50.0f, 50.0f,-50.0f);
Top Left Of The Quad (Left)
glVertex3f(-50.0f,-50.0f,-50.0f);
Bottom Left Of The Quad (Left)
glVertex3f(-50.0f,-50.0f, 50.0f);
Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f);
Set The Color To Violet
glVertex3f( 50.0f, 50.0f,-50.0f);
Top Right Of The Quad (Right)
glVertex3f( 50.0f, 50.0f, 50.0f);
Top Left Of The Quad (Right)
glVertex3f( 50.0f,-50.0f, 50.0f);
Bottom Left Of The Quad (Right)
glVertex3f( 50.0f,-50.0f,-50.0f);
Bottom Right Of The Quad (Right)
glEnd();
// Done Drawing The Quad
}
void drawCube(cube c)
{
//back surface of sube
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_LINE_LOOP);
glVertex3f(c.x[0].x,c.x[0].y,c.x[0].z);
glVertex3f(c.x[1].x,c.x[1].y,c.x[1].z);
glVertex3f(c.x[2].x,c.x[2].y,c.x[2].z);
glVertex3f(c.x[3].x,c.x[3].y,c.x[3].z);
glEnd();
// front surface
glBegin(GL_LINE_LOOP);
glVertex3f(c.x[4].x,c.x[4].y,c.x[4].z);
glVertex3f(c.x[5].x,c.x[5].y,c.x[5].z);
glVertex3f(c.x[6].x,c.x[6].y,c.x[6].z);
glVertex3f(c.x[7].x,c.x[7].y,c.x[7].z);
glEnd();
// Connect two surfaces with lines to form side surfaces
glBegin(GL_LINES);
glVertex3f(c.x[0].x,c.x[0].y,c.x[0].z);
glVertex3f(c.x[4].x,c.x[4].y,c.x[4].z);
glVertex3f(c.x[1].x,c.x[1].y,c.x[1].z);
glVertex3f(c.x[5].x,c.x[5].y,c.x[5].z);
glVertex3f(c.x[2].x,c.x[2].y,c.x[2].z);
glVertex3f(c.x[6].x,c.x[6].y,c.x[6].z);
glVertex3f(c.x[3].x,c.x[3].y,c.x[3].z);
glVertex3f(c.x[7].x,c.x[7].y,c.x[7].z);
glEnd();
glFinish();
glViewport(0,0,w,h);
}
void drawSphere(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (50, 30, 20);

//
//
//
//
//
//
//
//

glFlush ();
}
void drawRect(rect r)
{

Set

Set

Set

Set

glBegin(GL_QUADS);
// Draw A Quad
glColor3f(0.0f,1.0f,0.0f);
The Color To Blue
glVertex3f( r.x[0].x, r.x[0].y,r.x[0].z);
// Top Right Of The Quad (Top)
glColor3f(1.0f,0.0f,0.0f);
The Color To Blue
glVertex3f(r.x[1].x, r.x[0].y,r.x[0].z);
// Top Left Of The Quad (Top)
glColor3f(0.0f,0.0f,1.0f);
The Color To Blue
glVertex3f(r.x[1].x, r.x[1].y,r.x[0].z);
// Bottom Left Of The Quad (Top)
glColor3f(1.0f,1.0f,0.0f);
The Color To Blue
glVertex3f( r.x[0].x, r.x[1].y,r.x[0].z);
// Bottom Right Of The Quad (Top)
glEnd();

//

//

//

//

}
void display(void)
{
cube c;
rect r;
int i,l=50;
point p1={25,0,0}, p2={25,10,0};
float mat[4][4]={0},mat1[4][4],mat2[4][4];
r.x[0].x=r.x[0].y=r.x[0].z=-l/2;
r.x[1].x=l=r.x[1].y=r.x[1].z=l/2;
r.x[0].z=r.x[1].z=0;
c.x[0].x=c.x[0].y=c.x[0].z=0;
c.x[1].x=l;c.x[1].y=c.x[1].z=0;
c.x[2].x=c.x[2].y=l;c.x[2].z=0;
c.x[3].x=0;c.x[3].y=l;c.x[3].z=0;
c.x[4].x=c.x[4].y=0;c.x[4].z=l;
c.x[5].x=l;c.x[5].y=0,c.x[5].z=l;
c.x[6].x=c.x[6].y=c.x[6].z=l;
c.x[7].x=0;c.x[7].y=c.x[7].z=l;
c.x[0].h=c.x[1].h=c.x[2].h=c.x[4].h=c.x[5].h=c.x[6].h=c.x[7].h=c.x[
3].h=1; // CHANGE
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen
And Depth Buffer
glColor3f(0.0,0.0,0.0) ;

// draw axes
glBegin(GL_LINES);
glVertex3i(-400,0,0);
glVertex3i(400,0,0);
glVertex3i(0,-400,0);
glVertex3i(0,400,0);
glVertex3i(0,0,-400);
glVertex3i(0,0,400);
glEnd();
glColor3f(0.0,1.0,0.0) ;
glBegin(GL_LINES);
glVertex3i(0,0,0);
glVertex3i(50,50,50);
glEnd();
glutSwapBuffers();
}

int main(int argc,char *argv[])


{
iden[0][0]=iden[1][1]=iden[2][2]=iden[3][3]=1;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(50,50);
glutInitWindowSize(w,h);
glutCreateWindow("My First Window");
init();
glutDisplayFunc(display);
//glutTimerFunc(20,timer,10);
glutMainLoop();
return 0;
}

Вам также может понравиться