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>
- #include "BmpLoader.h"
- const int width = 1000;
- const int height = 1000;
- GLfloat eyeX = 0;
- GLfloat eyeY = 6;
- GLfloat eyeZ = -13;
- GLfloat lookX = 0;
- GLfloat lookY = 5;
- GLfloat lookZ = 0;
- GLfloat alpha = 0.0, theta = 0.0, axis_x=0.0, axis_y=0.0 , f_spin=0.0;
- GLboolean bRotate = false, uRotate = false;
- bool press1 = false;
- bool press2 = false;
- bool press4 = false;
- bool flagRot = false;
- float rot = 0;
- unsigned int ID;
- float px=0,py=30,pz=30,spot_cutoff = 30;
- 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] =
- {
- {3,1,5,7}, //front
- {6,4,0,2}, //back
- {2,3,7,6}, //top
- {1,0,4,5}, //bottom
- {7,5,4,6}, //right
- {2,0,1,3} //left
- };
- 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 set_material(float R=0.5, float G=0.5, float B=0.5, bool e=false , bool l = false)
- {
- GLfloat mat_no[] = {0,0,0, 1.0};
- GLfloat mat_amb[] = {R,G,B,1};
- GLfloat mat_diff[] = {R,G,B,1};
- GLfloat mat_spec[] = {1,1,1,1};
- GLfloat mat_sh[] = {30};
- GLfloat mat_em[] = {1,1,1,1};
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_amb);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diff);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_spec);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_sh);
- if(e && l)
- {
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_em);
- }
- else
- {
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_no);
- }
- }
- void cube(float R=0.5, float G=0.5, float B=0.5, bool e=false, bool l = false)
- {
- set_material(R,G,B,l);
- 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]);
- glTexCoord2f(0,1);
- glVertex3fv(&v_cube[c_ind[i][0]][0]);
- glTexCoord2f(0,0);
- glVertex3fv(&v_cube[c_ind[i][1]][0]);
- glTexCoord2f(1,0);
- glVertex3fv(&v_cube[c_ind[i][2]][0]);
- glTexCoord2f(1,1);
- glVertex3fv(&v_cube[c_ind[i][3]][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 rodshape1()
- {
- float t_length = 6;
- float t_width = 0.5;
- glPushMatrix();
- //glTranslatef(-14.5,7,0);
- glTranslatef(-14.5,7,0);
- glPushMatrix();
- glScalef(t_width,t_width,t_length);
- glTranslatef(-0.5,0,-0.5);
- cube(1,1,1,true,press1);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,0,t_length/2+t_width/2);
- glScalef(t_width,t_width,t_width);
- glTranslatef(-0.5,0,-0.5);
- cube(0.2,0.2,0.2);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,0,-t_length/2-t_width/2);
- glScalef(t_width,t_width,t_width);
- glTranslatef(-0.5,0,-0.5);
- cube(0.2,0.2,0.2);
- glPopMatrix();
- glPopMatrix();
- }
- void rodshape2()
- {
- float t_length = 6;
- float t_width = 0.5;
- glPushMatrix();
- //glTranslatef(-14.5,7,0);
- glTranslatef(14,7,10);
- glPushMatrix();
- glScalef(t_width,t_width,t_length);
- glTranslatef(-0.5,0,-0.5);
- cube(1,1,1,true,press2);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,0,t_length/2+t_width/2);
- glScalef(t_width,t_width,t_width);
- glTranslatef(-0.5,0,-0.5);
- cube(0.2,0.2,0.2);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,0,-t_length/2-t_width/2);
- glScalef(t_width,t_width,t_width);
- glTranslatef(-0.5,0,-0.5);
- cube(0.2,0.2,0.2);
- glPopMatrix();
- glPopMatrix();
- }
- void spotlightshape()
- {
- float s_height = 0.25;
- float s_width = 2;
- float s_length = 2;
- glPushMatrix();
- glTranslatef(7,9,7);
- /*
- glPushMatrix();
- glScalef(s_width,s_height,s_length);
- glTranslatef(-0.5,0,-0.5);
- cube(1,1,1);
- glPopMatrix();
- */
- glPushMatrix();
- glScalef(s_width,s_height,s_length);
- glTranslatef(-0.5,0,-0.5);
- cube(1,1,1,true,press4);
- glPopMatrix();
- glPopMatrix();
- }
- void light1()
- {
- //light
- GLfloat l1_no[] = {0, 0, 0, 1.0};
- GLfloat l1_amb[] = {0.1, 0.1, 0.1, 1.0};
- GLfloat l1_dif[] = {1,1,1,1};
- GLfloat l1_spec[] = {1,1,1,1};
- GLfloat l1_pos[] = {-14.5,7,0,1.0};
- //GLfloat l_pos[] = {-14.5,-7,10,1.0};
- glEnable(GL_LIGHT0);
- if(press1)
- {
- glLightfv(GL_LIGHT0, GL_AMBIENT, l1_amb);
- }
- else
- {
- glLightfv(GL_LIGHT0, GL_AMBIENT, l1_no);
- }
- if(press1)
- {
- glLightfv(GL_LIGHT0, GL_DIFFUSE, l1_dif);
- }
- else
- {
- glLightfv(GL_LIGHT0, GL_DIFFUSE, l1_no);
- }
- if(press1)
- {
- glLightfv(GL_LIGHT0, GL_SPECULAR, l1_spec);
- }
- else
- {
- glLightfv(GL_LIGHT0, GL_SPECULAR, l1_no);
- }
- glLightfv(GL_LIGHT0, GL_POSITION, l1_pos);
- }
- void light2()
- {
- //light
- GLfloat l2_no[] = {0, 0, 0, 1.0};
- GLfloat l2_amb[] = {0.1, 0.1, 0.1, 1.0};
- GLfloat l2_dif[] = {1,1,1,1};
- GLfloat l2_spec[] = {1,1,1,1};
- //GLfloat l_pos[] = {-14.5,7,0,1.0};
- GLfloat l2_pos[] = {14,-7,10,1.0};
- glEnable(GL_LIGHT1);
- if(press2)
- {
- glLightfv(GL_LIGHT1, GL_AMBIENT, l2_amb);
- }
- else
- {
- glLightfv(GL_LIGHT1, GL_AMBIENT, l2_no);
- }
- if(press2)
- {
- glLightfv(GL_LIGHT1, GL_DIFFUSE, l2_dif);
- }
- else
- {
- glLightfv(GL_LIGHT1, GL_DIFFUSE, l2_no);
- }
- if(press2)
- {
- glLightfv(GL_LIGHT1, GL_SPECULAR, l2_spec);
- }
- else
- {
- glLightfv(GL_LIGHT1, GL_SPECULAR, l2_no);
- }
- glLightfv(GL_LIGHT1, GL_POSITION, l2_pos);
- }
- void light3()
- {
- //light
- GLfloat l3_no[] = {0, 0, 0, 1.0};
- GLfloat l3_amb[] = {0.5, 0.5, 0.5, 1.0};
- GLfloat l3_dif[] = {1,1,1,1};
- GLfloat l3_spec[] = {1,1,1,1};
- GLfloat l3_pos[] = {px,py,pz,1.0};
- //GLfloat l_pos[] = {14,-7,10,1.0};
- glEnable(GL_LIGHT2);
- if(press4)
- {
- glLightfv(GL_LIGHT2, GL_AMBIENT, l3_amb);
- }
- else
- {
- glLightfv(GL_LIGHT2, GL_AMBIENT, l3_no);
- }
- if(press4)
- {
- glLightfv(GL_LIGHT2, GL_DIFFUSE, l3_dif);
- }
- else
- {
- glLightfv(GL_LIGHT2, GL_DIFFUSE, l3_no);
- }
- if(press4)
- {
- glLightfv(GL_LIGHT2, GL_SPECULAR, l3_spec);
- }
- else
- {
- glLightfv(GL_LIGHT2, GL_SPECULAR, l3_no);
- }
- glLightfv(GL_LIGHT2, GL_POSITION, l3_pos);
- // spot light extra
- GLfloat l3_spt[] = {0,0,-1,1};
- GLfloat spt_ct[] = {spot_cutoff};
- glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, l3_spt);
- glLightfv(GL_LIGHT2, GL_SPOT_CUTOFF, spt_ct);
- }
- void LoadTexture(const char*filename)
- {
- glGenTextures(1, &ID);
- glBindTexture(GL_TEXTURE_2D, ID);
- glPixelStorei(GL_UNPACK_ALIGNMENT, ID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- BmpLoader bl(filename);
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bl.iWidth, bl.iHeight, GL_RGB, GL_UNSIGNED_BYTE, bl.textureData );
- }
- static void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-3, 3, -3, 3, 2.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(eyeX,eyeY,eyeZ, lookX,lookY,lookZ, 0,1,0);
- //glViewport(0, 0, width, height);
- glRotatef(rot, 0,1,0);
- axes();
- ///Main Road
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,1);
- glPushMatrix();
- glScalef(10,0.2,50);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Grass
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,2);
- glPushMatrix();
- glScalef(1000,0.1,1000);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Fence Left
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,3);
- glPushMatrix();
- glTranslatef(30,0,25);
- glScalef(50,5,0.1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Fence Right
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,3);
- glPushMatrix();
- glTranslatef(-30,0,25);
- glScalef(50,5,0.1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Building Front Right
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,4);
- glPushMatrix();
- glTranslatef(-25,0,35);
- glScalef(40,40,0.1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Building Front Left
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,4);
- glPushMatrix();
- glTranslatef(25,0,35);
- glScalef(40,40,0.1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Building Right
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,4);
- glPushMatrix();
- glTranslatef(-45,0,65);
- glScalef(0.1,40,60);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Building Left
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,4);
- glPushMatrix();
- glTranslatef(45,0,65);
- glScalef(0.1,40,60);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Building Back Left
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,4);
- glPushMatrix();
- glTranslatef(25,0,95);
- glScalef(40,40,0.1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- ///Building Back Right
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,4);
- glPushMatrix();
- glTranslatef(-25,0,95);
- glScalef(40,40,0.1);
- glTranslatef(-0.5,-0.5,-0.5);
- cube(1,1,1);
- glPopMatrix();
- glDisable(GL_TEXTURE_2D);
- glutSwapBuffers();
- }
- void animate()
- {
- glutPostRedisplay();
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27 :
- case 'q':
- exit(0);
- break;
- case 'w':
- eyeZ++;
- lookZ++;
- break;
- case 's':
- eyeZ--;
- lookZ--;
- break;
- case 'a':
- eyeX++;
- lookX++;
- break;
- case 'd':
- eyeX--;
- lookX--;
- break;
- case 'u':
- eyeY++;
- lookY++;
- break;
- case 'j':
- eyeY--;
- lookY--;
- break;
- case '.':
- rot--;
- break;
- case ',':
- rot++;
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(width,height);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glEnable(GL_LIGHTING);
- glutCreateWindow("1607006");
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutIdleFunc(animate);
- //glutSpecialFunc(collision);
- //glutIdleFunc(idle);
- //glutReshapeFunc(res);
- // glClearColor(1,1,1,1);
- glEnable(GL_DEPTH_TEST);
- glShadeModel( GL_SMOOTH );
- glEnable(GL_NORMALIZE);
- glEnable(GL_BLEND);
- printf(" Use 'w' to look up, 's' to look down, 'd' to look right, and 'a' to look left.\n");
- printf(" Press '1' for right light turn on,\n Press '2' for left light turn on, \n Press '3' to turn on both light, \n Press '4' to turn on spotlight.\n");
- LoadTexture("C:\\Users\\road.bmp");
- LoadTexture("C:\\Users\\grass.bmp");
- LoadTexture("C:\\Users\\fence.bmp");
- LoadTexture("C:\\Users\\window.bmp");
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement