Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <windows.h>
- #include<math.h>
- const int width = 500;
- const int height = 500;
- const float rat = 1.0 * width / height;
- GLfloat eX = 0;
- GLfloat eY = 15;
- GLfloat eZ = 20;
- GLfloat lX = 0;
- GLfloat lY = 0;
- GLfloat lZ = 0;
- float l_height = 15;
- float spt_cutoff = 30;
- float rot = 0;
- bool l_on = true;
- static GLfloat v_cube[8][3] =
- {
- {0,0,0},
- {0,0,1},
- {0,1,0},
- {0,1,1},
- {1,0,0},
- {1,0,1},
- {1,1,0},
- {1,1,1}
- };
- static GLubyte c_ind[6][4] =
- {
- {0,2,6,4},
- {1,5,7,3},
- {0,4,5,1},
- {2,3,7,6},
- {0,1,3,2},
- {4,6,7,5}
- };
- static void getNormal3p(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3)
- {
- GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
- Ux = x2-x1;
- Uy = y2-y1;
- Uz = z2-z1;
- Vx = x3-x1;
- Vy = y3-y1;
- Vz = z3-z1;
- Nx = Uy*Vz - Uz*Vy;
- Ny = Uz*Vx - Ux*Vz;
- Nz = Ux*Vy - Uy*Vx;
- glNormal3f(Nx,Ny,Nz);
- }
- void cube(float R=0.5, float G=0.5, float B=0.5, bool e=false, float alpha=1)
- {
- GLfloat m_no[] = {0, 0, 0, 1.0};
- GLfloat m_amb[] = {R,G,B,1};
- GLfloat m_diff[] = {R,G,B,1};
- GLfloat m_spec[] = {1,1,1,1};
- GLfloat m_sh[] = {30};
- GLfloat m_em[] = {1,1,1,1};
- glMaterialfv(GL_FRONT, GL_AMBIENT, m_amb);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, m_diff);
- glMaterialfv(GL_FRONT, GL_SPECULAR, m_spec);
- glMaterialfv(GL_FRONT, GL_SHININESS, m_sh);
- if(e & l_on) glMaterialfv(GL_FRONT, GL_EMISSION, m_em);
- else glMaterialfv(GL_FRONT, GL_EMISSION, m_no);
- glBegin(GL_QUADS);
- for (GLint i = 0; i <6; i++)
- {
- getNormal3p(v_cube[c_ind[i][0]][0], v_cube[c_ind[i][0]][1], v_cube[c_ind[i][0]][2],
- v_cube[c_ind[i][1]][0], v_cube[c_ind[i][1]][1], v_cube[c_ind[i][1]][2],
- v_cube[c_ind[i][2]][0], v_cube[c_ind[i][2]][1], v_cube[c_ind[i][2]][2]);
- for (GLint j=0; j<4; j++)
- {
- glVertex3fv(&v_cube[c_ind[i][j]][0]);
- }
- }
- glEnd();
- }
- void axes()
- {
- float length = 10;
- float width = 0.3;
- // X-axis
- glPushMatrix();
- glTranslatef(length/2,0,0);
- glScalef(length,width,width);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(0.8,0.1,0.1);
- glPopMatrix();
- // Y-axis
- glPushMatrix();
- glTranslatef(0,length/2,0);
- glScalef(width,length,width);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(0.1,0.8,0.1);
- glPopMatrix();
- // Z-axis
- glPushMatrix();
- glTranslatef(0,0,length/2);
- glScalef(width,width,length);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(0.1,0.1,0.8);
- glPopMatrix();
- }
- void flr()
- {
- glPushMatrix();
- // glTranslatef(0,-0.5,0);
- glScalef(60,1,60);
- glTranslatef(-0.5,-1,-0.5);
- cube();
- glPopMatrix();
- }
- void table()
- {
- float height=6;
- float width=10;
- float length=5;
- float base_height=1;
- float leg_height=height-base_height;
- float leg_width=0.5;
- // whole table
- glPushMatrix();
- glTranslatef(0,leg_height,0);
- // base
- glPushMatrix();
- glScalef(width,base_height,length);
- glTranslatef(-0.5,0,-0.5);
- cube(0.53,0.39,0.28);
- glPopMatrix();
- // legs
- glPushMatrix();
- glTranslatef((width/2-leg_width/2),0,(length/2-leg_width/2));
- glScalef(leg_width,leg_height,leg_width);
- glTranslatef(-0.5,-1,-0.5);
- cube(0.53,0.39,0.28);
- glPopMatrix();
- glPushMatrix();
- glTranslatef((width/2-leg_width/2),0,-(length/2-leg_width/2));
- glScalef(leg_width,leg_height,leg_width);
- glTranslatef(-0.5,-1,-0.5);
- cube(0.53,0.39,0.28);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-(width/2-leg_width/2),0,(length/2-leg_width/2));
- glScalef(leg_width,leg_height,leg_width);
- glTranslatef(-0.5,-1,-0.5);
- cube(0.53,0.39,0.28);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-(width/2-leg_width/2),0,-(length/2-leg_width/2));
- glScalef(leg_width,leg_height,leg_width);
- glTranslatef(-0.5,-1,-0.5);
- cube(0.53,0.39,0.28);
- glPopMatrix();
- glPopMatrix();
- }
- static void res(int width, int height)
- {
- glViewport(0, 0, width, width/rat);
- }
- void light()
- {
- //light
- GLfloat l_no[] = {0, 0, 0, 1.0};
- GLfloat l_amb[] = {0.5, 0.5, 0.5, 1.0};
- GLfloat l_dif[] = {1,1,1,1};
- GLfloat l_spec[] = {1,1,1,1};
- GLfloat l_pos[] = {0,l_height,0,1.0};
- glEnable(GL_LIGHT0);
- if(l_on) glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb);
- else glLightfv(GL_LIGHT0, GL_AMBIENT, l_no);
- if(l_on) glLightfv(GL_LIGHT0, GL_DIFFUSE, l_dif);
- else glLightfv(GL_LIGHT0, GL_DIFFUSE, l_no);
- if(l_on) glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec);
- else glLightfv(GL_LIGHT0, GL_SPECULAR, l_no);
- glLightfv(GL_LIGHT0, GL_POSITION, l_pos);
- // spot light extra
- GLfloat l_spt[] = {0,-1,0,1};
- GLfloat spt_ct[] = {spt_cutoff};
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, l_spt);
- glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, spt_ct);
- }
- static void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-3, 3, -3, 3, 3.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(eX,eY,eZ, lX,lY,lZ, 0,1,0);
- glRotatef(rot, 0,1,0);
- axes();
- table();
- flr();
- glPushMatrix();
- glRotatef(45, 0,1,0);
- light();
- glPopMatrix();
- glTranslatef(0,l_height+1,0);
- glScalef(5,1,1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,0,0,true);
- glutSwapBuffers();
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27 :
- case 'q':
- exit(0);
- break;
- case 't':
- l_on=1-l_on;
- break;
- case ',':
- rot+=2;
- break;
- case '.':
- rot-=2;
- break;
- case 's':
- eZ--;
- lZ--;
- break;
- case 'p':
- eZ++;
- lZ++;
- break;
- case 'n':
- eY--;
- break;
- case 'u':
- eY++;
- break;
- case '1':
- l_height++;
- break;
- case '2':
- l_height--;
- break;
- case '3':
- spt_cutoff++;
- break;
- case '4':
- spt_cutoff--;
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- /* Program entry point */
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(width,height);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutIdleFunc(idle);
- glutReshapeFunc(res);
- // glClearColor(1,1,1,1);
- glEnable(GL_DEPTH_TEST);
- glShadeModel( GL_SMOOTH );
- glEnable(GL_NORMALIZE);
- glEnable(GL_BLEND);
- glEnable(GL_LIGHTING);
- printf("########################################################################################\n");
- printf("########################################################################################\n");
- printf("############## #################\n");
- printf("############## PLEASE FOLLOW THE BELOW INSTRUCTIONS #################\n");
- printf("############## #################\n");
- printf("########################################################################################\n");
- printf("########################################################################################\n\n\n");
- printf("Use 'w' to look up, 's' to look down, 'd' to look right, and 'a' to look left.\n");
- printf("Use 'i' to move camera up, 'k' to move camera down, 'l' to move camera right, and 'j' to move camera left with the look at point fixed.\n");
- printf("Use '+' to zoom in and '-' to zoom out.\n\n\n");
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement