Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glut.h>
- //Other libraries
- #include <iostream>
- #include <fstream>
- #include <math.h>
- #include <string.h>
- //---------------------------------------------------------------
- #define PI 3.141592654
- //Global
- float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0;
- float lastx, lasty;
- float xrotrad, yrotrad;
- const unsigned int MAP_SIZE = 30;
- const float MAX_DISTANCE = 275;
- const float MOVEMENT_SPEED = 0.05;
- GLuint Texture, paper;
- GLuint skybox[5];
- bool state[255];
- enum{ SKY_LEFT = 0,
- SKY_BACK = 1,
- SKY_RIGHT = 2,
- SKY_FRONT = 3,
- SKY_TOP = 4
- };
- typedef struct vertex{
- float x[8000];
- float y[8000];
- float z[8000];
- int count;
- }tVertex;
- typedef struct normal{
- float x[8000];
- float y[8000];
- float z[8000];
- int count;
- }tNormal;
- typedef struct texture{
- float s[8000];
- float t[8000];
- int count;
- }tTexture;
- typedef struct face{
- int v1, v2, v3;
- int n1, n2, n3;
- int t1, t2, t3;
- face *next;
- }tFace;
- typedef struct faceList{
- int count;
- face *head;
- }tFaceList;
- typedef struct object{
- tFaceList faces;
- tTexture textureCoordinates;
- tVertex vertexs;
- tNormal normals;
- GLuint textureID;
- }tObject;
- tObject slender, rock, tree, crate, house;
- //Alusion
- void keyboard_down(unsigned char key, int x, int y);
- void drawText(float x, float y, const char *string, void *font);
- void initskybox();
- void drawSkybox(float size);
- void moveWorld();
- void mouse(int x, int y);
- void free_mem(void);
- void drawGround(int w, int h);
- void getMouseAction(int button, int state, int x, int y);
- void displayScreen(void);
- void displayComands(void);
- void keyboard_up( unsigned char key, int x, int y);
- void light(void);
- void insertVertex(tObject *obj, float x, float y, float z);
- void insertNormal(tObject *obj, float x, float y, float z);
- void insertTexture(tObject *obj, float s, float t);
- void insertNormal(tObject *obj, float x, float y, float z);
- void insertFaceCount(tObject *obj, int v1, int t1, int n1, int v2, int t2, int n2, int v3, int t3, int n3);
- void loadObject(tObject *obj, const char *object, const char *texture);
- GLuint loadBMP_custom(const char * imagepath);
- unsigned int loadTexture(const char* imagepath);
- void free_mem(void){
- std::clog << "Exiting...\n";
- }
- void loadObject(tObject *obj, const char *object, const char *texture){
- obj->vertexs.count = 0;
- obj->normals.count = 0;
- obj->textureCoordinates.count = 0;
- obj->faces.count = 0;
- obj->faces.head = NULL;
- FILE *file = fopen(object, "r");
- if( file == NULL){
- printf("Impossible to open the file !\n");
- return ;
- }
- while(1){
- char line[128];
- int res = fscanf(file, "%s", line);
- if(res == EOF) break;
- if(strcmp(line, "v") == 0){
- float x, y, z;
- fscanf(file, "%f %f %f\n", &x, &y, &z);
- insertVertex(obj, x, y, z);
- }else if(strcmp(line, "vt") == 0){
- float s, t;
- fscanf(file, "%f %f\n", &s, &t);
- insertTexture(obj, s, t);
- }else if(strcmp(line, "f") == 0){
- int v1, t1, n1, v2, t2, n2, v3, t3, n3;
- fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &v1, &t1, &n1, &v2, &t2, &n2, &v3, &t3, &n3);
- insertFaceCount(obj, v1, t1, n1, v2, t2, n2, v3, t3, n3);
- }else if(strcmp(line, "vn") == 0){
- float nx, ny, nz;
- fscanf(file, "%f %f %f\n", &nx, &ny, &nz);
- insertNormal(obj, nx, ny, nz);
- }
- }
- obj->textureID = loadBMP_custom(texture);
- }
- void insertVertex(tObject *obj, float x, float y, float z){
- obj->vertexs.x[ obj->vertexs.count ] = x;
- obj->vertexs.y[ obj->vertexs.count ] = y;
- obj->vertexs.z[ obj->vertexs.count ] = z;
- obj->vertexs.count = obj->vertexs.count + 1;
- }
- void insertNormal(tObject *obj, float x, float y, float z){
- obj->normals.x[ obj->normals.count ] = x;
- obj->normals.y[ obj->normals.count ] = y;
- obj->normals.z[ obj->normals.count ] = z;
- obj->normals.count = obj->normals.count + 1;
- }
- void insertTexture(tObject *obj, float s, float t){
- obj->textureCoordinates.s[ obj->textureCoordinates.count ] = s;
- obj->textureCoordinates.t[ obj->textureCoordinates.count ] = t;
- obj->textureCoordinates.count = obj->textureCoordinates.count + 1;
- }
- void insertFaceCount(tObject *obj, int v1, int t1, int n1, int v2, int t2, int n2, int v3, int t3, int n3){
- face *newFace = new face;
- newFace->next = NULL;
- newFace->v1 = v1;
- newFace->v2 = v2;
- newFace->v3 = v3;
- newFace->n1 = n1;
- newFace->n2 = n2;
- newFace->n3 = n3;
- newFace->t1 = t1;
- newFace->t2 = t2;
- newFace->t3 = t3;
- if(obj->faces.head == NULL){
- obj->faces.head = newFace;
- }else{
- face *cur = obj->faces.head;
- while(cur->next != NULL){
- cur = cur->next;
- }
- cur->next = newFace;
- }
- obj->faces.count = obj->faces.count + 1;
- }
- //http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
- GLuint loadBMP_custom(const char * imagepath){
- unsigned char header[54];
- unsigned int dataPos;
- unsigned int width, height;
- unsigned int imageSize;
- unsigned char *data;
- FILE *file = fopen(imagepath, "rb");
- if(!file){
- std::clog << "Image could not be opened\n";
- return 0;
- }
- if( fread(header, 1, 54, file) != 54){
- std::clog << "Not a correct BMP file\n";
- return false;
- if(header[0] != 'B' || header[1]!= 'M'){
- std::clog << "Not a correct BMP file\n";
- return 0;
- }
- }
- dataPos = *(int*)&(header[0x0A]);
- imageSize = *(int*)&(header[0x22]);
- width = *(int*)&(header[0x12]);
- height = *(int*)&(header[0x16]);
- if(imageSize == 0) imageSize = width*height*3;
- if(dataPos == 0) dataPos = 54;
- data = new unsigned char [imageSize];
- fread(data, 1, imageSize, file);
- fclose(file);
- GLuint textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- return textureID;
- }
- unsigned int loadTexture(const char* imagepath){
- unsigned char header[54];
- unsigned int dataPos;
- unsigned int width, height;
- unsigned int imageSize;
- unsigned char *data;
- FILE *file = fopen(imagepath, "rb");
- if(!file){
- std::clog << "Image "<< imagepath <<" could not be opened\n";
- return 0;
- }
- if( fread(header, 1, 54, file) != 54){
- std::clog << "Not a correct BMP file\n";
- return false;
- if(header[0] != 'B' || header[1]!= 'M'){
- std::clog << "Not a correct BMP file\n";
- return 0;
- }
- }
- dataPos = *(int*)&(header[0x0A]);
- imageSize = *(int*)&(header[0x22]);
- width = *(int*)&(header[0x12]);
- height = *(int*)&(header[0x16]);
- if(imageSize == 0) imageSize = width * height * 3;
- if(dataPos == 0) dataPos = 54;
- data = new unsigned char [imageSize];
- fread(data, 1, imageSize, file);
- fclose(file);
- glEnable(GL_TEXTURE_2D);
- unsigned int num;
- glGenTextures(1,&num);
- glBindTexture(GL_TEXTURE_2D,num);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
- 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_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
- glDisable(GL_TEXTURE_2D);
- return num;
- }
- void initskybox(){
- skybox[SKY_LEFT] = loadTexture("nightsky_left.bmp");
- skybox[SKY_BACK] = loadTexture("nightsky_back.bmp");
- skybox[SKY_RIGHT] = loadTexture("nightsky_right.bmp");
- skybox[SKY_FRONT] = loadTexture("nightsky_front.bmp");
- skybox[SKY_TOP] = loadTexture("nightsky_top.bmp");
- }
- void drawSkybox(float size){
- bool L = glIsEnabled(GL_LIGHTING);
- bool T = glIsEnabled(GL_TEXTURE_2D);
- bool DT = glIsEnabled(GL_DEPTH_TEST);
- if( L ) glDisable(GL_LIGHTING);
- if( !T ) glEnable(GL_TEXTURE_2D);
- if( DT ) glDisable(GL_DEPTH_TEST);
- glBindTexture(GL_TEXTURE_2D, skybox[SKY_FRONT]);
- glBegin(GL_QUADS);
- glTexCoord2f(1,1); glVertex3f(size / 2, size - size / 2, size / 2);
- glTexCoord2f(0,1); glVertex3f(-size / 2, size - size / 2, size / 2);
- glTexCoord2f(0,0); glVertex3f(-size / 2, -size / 2, size / 2);
- glTexCoord2f(1,0); glVertex3f(size / 2, -size / 2, size / 2);
- glEnd();
- glBindTexture(GL_TEXTURE_2D,skybox[SKY_LEFT]);
- glBegin(GL_QUADS);
- glTexCoord2f(1,1); glVertex3f(-size / 2, size - size / 2, size / 2);
- glTexCoord2f(0,1); glVertex3f(-size / 2, size - size / 2, -size / 2);
- glTexCoord2f(0,0); glVertex3f(-size / 2, -size / 2, -size / 2);
- glTexCoord2f(1,0); glVertex3f(-size / 2, -size / 2, size / 2);
- glEnd();
- glBindTexture(GL_TEXTURE_2D,skybox[SKY_BACK]);
- glBegin(GL_QUADS);
- glTexCoord2f(0,1); glVertex3f(size / 2, size - size / 2, -size / 2);
- glTexCoord2f(1,1); glVertex3f(-size / 2, size - size / 2, -size / 2);
- glTexCoord2f(1,0); glVertex3f(-size / 2, -size / 2, -size / 2);
- glTexCoord2f(0,0); glVertex3f(size / 2, -size / 2, -size / 2);
- glEnd();
- glBindTexture(GL_TEXTURE_2D,skybox[SKY_RIGHT]);
- glBegin(GL_QUADS);
- glTexCoord2f(1,1); glVertex3f(size / 2, size - size / 2, -size / 2);
- glTexCoord2f(0,1); glVertex3f(size / 2, size - size / 2, size / 2);
- glTexCoord2f(0,0); glVertex3f(size / 2, -size / 2, size / 2);
- glTexCoord2f(1,0); glVertex3f(size / 2, -size / 2, -size / 2);
- glEnd();
- glBindTexture(GL_TEXTURE_2D,skybox[SKY_TOP]);
- glBegin(GL_QUADS);
- glTexCoord2f(1,0); glVertex3f(size / 2, size - size / 2, size / 2);
- glTexCoord2f(0,0); glVertex3f(-size / 2, size - size / 2, size / 2);
- glTexCoord2f(0,1); glVertex3f(-size / 2, size - size / 2, -size / 2);
- glTexCoord2f(1,1); glVertex3f(size / 2 , size - size / 2, -size / 2);
- glEnd();
- if( L ) glEnable(GL_LIGHTING);
- if( !T ) glDisable(GL_TEXTURE_2D);
- if( DT ) glEnable(GL_DEPTH_TEST);
- }
- void mouse(int x, int y) { //TODO: Keep the mouse on screen
- int diffx = x - lastx;
- int diffy = y - lasty;
- lastx = x;
- lasty = y;
- if( (xrot + (float) diffy) < -30.0f){
- xrot = -30.0f;
- }else if( (xrot + (float) diffy) > 20.0f){
- xrot = 20.0f;
- }else{
- xrot += (float)diffy;
- }
- yrot += (float) diffx;
- }
- //http://stackoverflow.com/questions/14925521/how-to-make-action-on-keyboard-input-be-instant
- void keyboard_up(unsigned char key, int x, int y){
- state[ key ] = false;
- }
- void keyboard_down(unsigned char key, int x, int y){
- state[ key ] = true;
- }
- //http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/
- //http://www.swiftless.com/tutorials/opengl/camera2.html
- void keyboard(void){
- if(state['w'] || state['W']){
- yrotrad = (yrot / 180 * PI);
- xrotrad = (xrot / 180 * PI);
- xpos += float(sin(yrotrad));
- zpos -= float(cos(yrotrad));
- }
- if(state['s'] || state['S']){
- yrotrad = (yrot / 180 * PI);
- xrotrad = (xrot / 180 * PI);
- xpos -= float(sin(yrotrad));
- zpos += float(cos(yrotrad));
- }
- if(state['d'] || state['D']){
- yrotrad = (yrot / 180 * PI);
- xpos += float(cos(yrotrad)) * 0.8;
- zpos += float(sin(yrotrad)) * 0.8;
- }
- if(state['a'] || state['A']){
- yrotrad = (yrot / 180 * PI);
- xpos -= float(cos(yrotrad)) * 0.8;
- zpos -= float(sin(yrotrad)) * 0.8;
- }
- if(state['q'] || state['Q']){
- exit(0);
- }
- }
- void resize(int w, int h){
- glViewport (0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(50.0f, (float)w/(float)h, 1.0f, 400.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- //---------------------------------------------------------------
- //Functions from end game
- bool result;
- void displayEnd(void){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1.0f, 1.0f, 1.0f);
- if(result){
- drawText(-0.18, 0.15, "VOCE GANHOU", GLUT_BITMAP_9_BY_15);
- }else{
- drawText(-0.18, 0.15, "VOCE PERDEU", GLUT_BITMAP_9_BY_15);
- }
- glColor3f(1.0f, 0.0f, 0.0f);
- drawText(-0.065, -0.15, "SAIR", GLUT_BITMAP_9_BY_15);
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void mouseEnd(int button, int state, int x, int y){
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
- if( (x >= 230) && (x <= 282) && (y >= 286) && (y <= 306) ){
- exit(0);
- }
- }
- }
- //---------------------------------------------------------------
- //Functions from the game screen
- void mouseGame(int button, int state, int x, int y){
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
- if(xpos < -224 && xpos > -275 && zpos < -32 && zpos > -67){
- glutDisplayFunc(displayEnd);
- glutMouseFunc(mouseEnd);
- glMatrixMode(GL_PROJECTION) ;
- glPushMatrix() ;
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW) ;
- glPushMatrix() ;
- glLoadIdentity() ;
- glDisable(GL_LIGHTING);
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_TEXTURE_2D);
- glutKeyboardFunc(NULL);
- glutPassiveMotionFunc(NULL);
- glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
- result = true;
- }
- }
- }
- void moveWorld(){
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(xrot, 1.0, 0.0, 0.0);
- glRotatef(yrot, 0.0, 1.0, 0.0);
- if(xpos > MAX_DISTANCE) xpos = MAX_DISTANCE;
- if(xpos < -MAX_DISTANCE) xpos = -MAX_DISTANCE;
- if(zpos > MAX_DISTANCE) zpos = MAX_DISTANCE;
- if(zpos < -MAX_DISTANCE) zpos = -MAX_DISTANCE;
- glTranslated(-xpos * MOVEMENT_SPEED, 0.0, -zpos * MOVEMENT_SPEED);
- if(xpos < 22 && xpos > -21 && zpos > 102 && zpos < 150){
- glutDisplayFunc(displayEnd);
- glutMouseFunc(mouseEnd);
- glMatrixMode(GL_PROJECTION) ;
- glPushMatrix() ;
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW) ;
- glPushMatrix() ;
- glLoadIdentity() ;
- glDisable(GL_LIGHTING);
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_TEXTURE_2D);
- glutKeyboardFunc(NULL);
- glutPassiveMotionFunc(NULL);
- glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
- result = false;
- }
- }
- void drawObjects(void){
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, slender.textureID);
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 1.0f, 1.0f);
- face *temp = slender.faces.head;
- for(int i = 0; i < slender.faces.count; i++){
- glNormal3f(slender.normals.x[temp->n1 - 1], slender.normals.y[temp->n1 - 1], slender.normals.z[temp->n1 - 1]);
- glTexCoord2f(slender.textureCoordinates.s[temp->t1 - 1], slender.textureCoordinates.t[temp->t1 - 1]);
- glVertex3f(slender.vertexs.x[temp->v1 - 1], slender.vertexs.y[temp->v1 - 1], slender.vertexs.z[temp->v1 - 1]);
- glNormal3f(slender.normals.x[temp->n2 - 1], slender.normals.y[temp->n2 - 1], slender.normals.z[temp->n2 - 1]);
- glTexCoord2f(slender.textureCoordinates.s[temp->t2 - 1], slender.textureCoordinates.t[temp->t2 - 1]);
- glVertex3f(slender.vertexs.x[temp->v2 - 1], slender.vertexs.y[temp->v2 - 1], slender.vertexs.z[temp->v2 - 1]);
- glNormal3f(slender.normals.x[temp->n3 - 1], slender.normals.y[temp->n3 - 1], slender.normals.z[temp->n3 - 1]);
- glTexCoord2f(slender.textureCoordinates.s[temp->t3 - 1], slender.textureCoordinates.t[temp->t3 - 1]);
- glVertex3f(slender.vertexs.x[temp->v3 - 1], slender.vertexs.y[temp->v3 - 1], slender.vertexs.z[temp->v3 - 1]);
- temp = temp->next;
- }
- glEnd();
- glBindTexture(GL_TEXTURE_2D, rock.textureID);
- glBegin(GL_TRIANGLES);
- temp = rock.faces.head;
- for(int i = 0; i < rock.faces.count; i++){
- glNormal3f(rock.normals.x[temp->n1 - 1], rock.normals.y[temp->n1 - 1], rock.normals.z[temp->n1 - 1]);
- glTexCoord2f(rock.textureCoordinates.s[temp->t1 - 1], rock.textureCoordinates.t[temp->t1 - 1]);
- glVertex3f(rock.vertexs.x[temp->v1 - 1], rock.vertexs.y[temp->v1 - 1], rock.vertexs.z[temp->v1 - 1]);
- glNormal3f(rock.normals.x[temp->n2 - 1], rock.normals.y[temp->n2 - 1], rock.normals.z[temp->n2 - 1]);
- glTexCoord2f(rock.textureCoordinates.s[temp->t2 - 1], rock.textureCoordinates.t[temp->t2 - 1]);
- glVertex3f(rock.vertexs.x[temp->v2 - 1], rock.vertexs.y[temp->v2 - 1], rock.vertexs.z[temp->v2 - 1]);
- glNormal3f(rock.normals.x[temp->n3 - 1], rock.normals.y[temp->n3 - 1], rock.normals.z[temp->n3 - 1]);
- glTexCoord2f(rock.textureCoordinates.s[temp->t3 - 1], rock.textureCoordinates.t[temp->t3 - 1]);
- glVertex3f(rock.vertexs.x[temp->v3 - 1], rock.vertexs.y[temp->v3 - 1], rock.vertexs.z[temp->v3 - 1]);
- temp = temp->next;
- }
- glEnd();
- glBindTexture(GL_TEXTURE_2D, tree.textureID);
- glBegin(GL_TRIANGLES);
- temp = tree.faces.head;
- for(int i = 0; i < tree.faces.count; i++){
- glNormal3f(tree.normals.x[temp->n1 - 1], tree.normals.y[temp->n1 - 1], tree.normals.z[temp->n1 - 1]);
- glTexCoord2f(tree.textureCoordinates.s[temp->t1 - 1], tree.textureCoordinates.t[temp->t1 - 1]);
- glVertex3f(tree.vertexs.x[temp->v1 - 1], tree.vertexs.y[temp->v1 - 1], tree.vertexs.z[temp->v1 - 1]);
- glNormal3f(tree.normals.x[temp->n2 - 1], tree.normals.y[temp->n2 - 1], tree.normals.z[temp->n2 - 1]);
- glTexCoord2f(tree.textureCoordinates.s[temp->t2 - 1], tree.textureCoordinates.t[temp->t2 - 1]);
- glVertex3f(tree.vertexs.x[temp->v2 - 1], tree.vertexs.y[temp->v2 - 1], tree.vertexs.z[temp->v2 - 1]);
- glNormal3f(tree.normals.x[temp->n3 - 1], tree.normals.y[temp->n3 - 1], tree.normals.z[temp->n3 - 1]);
- glTexCoord2f(tree.textureCoordinates.s[temp->t3 - 1], tree.textureCoordinates.t[temp->t3 - 1]);
- glVertex3f(tree.vertexs.x[temp->v3 - 1], tree.vertexs.y[temp->v3 - 1], tree.vertexs.z[temp->v3 - 1]);
- temp = temp->next;
- }
- glEnd();
- glBindTexture(GL_TEXTURE_2D, crate.textureID);
- glBegin(GL_TRIANGLES);
- temp = crate.faces.head;
- for(int i = 0; i < crate.faces.count; i++){
- glNormal3f(crate.normals.x[temp->n1 - 1], crate.normals.y[temp->n1 - 1], crate.normals.z[temp->n1 - 1]);
- glTexCoord2f(crate.textureCoordinates.s[temp->t1 - 1], crate.textureCoordinates.t[temp->t1 - 1]);
- glVertex3f(crate.vertexs.x[temp->v1 - 1], crate.vertexs.y[temp->v1 - 1], crate.vertexs.z[temp->v1 - 1]);
- glNormal3f(crate.normals.x[temp->n2 - 1], crate.normals.y[temp->n2 - 1], crate.normals.z[temp->n2 - 1]);
- glTexCoord2f(crate.textureCoordinates.s[temp->t2 - 1], crate.textureCoordinates.t[temp->t2 - 1]);
- glVertex3f(crate.vertexs.x[temp->v2 - 1], crate.vertexs.y[temp->v2 - 1], crate.vertexs.z[temp->v2 - 1]);
- glNormal3f(crate.normals.x[temp->n3 - 1], crate.normals.y[temp->n3 - 1], crate.normals.z[temp->n3 - 1]);
- glTexCoord2f(crate.textureCoordinates.s[temp->t3 - 1], crate.textureCoordinates.t[temp->t3 - 1]);
- glVertex3f(crate.vertexs.x[temp->v3 - 1], crate.vertexs.y[temp->v3 - 1], crate.vertexs.z[temp->v3 - 1]);
- temp = temp->next;
- }
- glEnd();
- glBindTexture(GL_TEXTURE_2D, house.textureID);
- glBegin(GL_TRIANGLES);
- temp = house.faces.head;
- for(int i = 0; i < house.faces.count; i++){
- glNormal3f(house.normals.x[temp->n1 - 1], house.normals.y[temp->n1 - 1], house.normals.z[temp->n1 - 1]);
- glTexCoord2f(house.textureCoordinates.s[temp->t1 - 1], house.textureCoordinates.t[temp->t1 - 1]);
- glVertex3f(house.vertexs.x[temp->v1 - 1], house.vertexs.y[temp->v1 - 1], house.vertexs.z[temp->v1 - 1]);
- glNormal3f(house.normals.x[temp->n2 - 1], house.normals.y[temp->n2 - 1], house.normals.z[temp->n2 - 1]);
- glTexCoord2f(house.textureCoordinates.s[temp->t2 - 1], house.textureCoordinates.t[temp->t2 - 1]);
- glVertex3f(house.vertexs.x[temp->v2 - 1], house.vertexs.y[temp->v2 - 1], house.vertexs.z[temp->v2 - 1]);
- glNormal3f(house.normals.x[temp->n3 - 1], house.normals.y[temp->n3 - 1], house.normals.z[temp->n3 - 1]);
- glTexCoord2f(house.textureCoordinates.s[temp->t3 - 1], house.textureCoordinates.t[temp->t3 - 1]);
- glVertex3f(house.vertexs.x[temp->v3 - 1], house.vertexs.y[temp->v3 - 1], house.vertexs.z[temp->v3 - 1]);
- temp = temp->next;
- }
- glEnd();
- }
- void drawGround(int w, int h){
- int i, j, r;
- glColor3f(1.0f, 1.0f, 1.0f);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, Texture);
- glNormal3f(0.0, 1.0, 0.0);
- for(i = -2*w; i < 2*w; i++){
- for(j = -2*h; j < 2*h; j++){
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0); glVertex3f(0.5 * i, -1.0f, j*0.5);
- glTexCoord2f(1, 0); glVertex3f(i*0.5 + 0.5, -1.0f, j*0.5);
- glTexCoord2f(1, 1); glVertex3f(i*0.5 + 0.5, -1.0f, j*0.5 + 0.5);
- glTexCoord2f(0, 1); glVertex3f(i*0.5, -1.0f, j*0.5 + 0.5);
- glEnd();
- }
- }
- }
- void drawText(float x, float y, const char *string, void *font){
- glRasterPos2f(x, y);
- int len, i;
- len = (int) strlen(string);
- for(i = 0; i < len; i++){
- glutBitmapCharacter(font, string[i]);
- }
- }
- void putPages(void){
- glPushMatrix();
- glTranslatef(-12.35, -0.80, -0.72);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, paper);
- glNormal3f(0.0, 0.0, -1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0); glVertex3f(0.5, 0.25, 0);
- glTexCoord2f(1, 0); glVertex3f(0, 0.25, 0);
- glTexCoord2f(1, 1); glVertex3f(0, 0.75, 0);
- glTexCoord2f(0, 1); glVertex3f(0.5, 0.75, 0);
- glEnd();
- glPopMatrix();
- }
- //http://www.glprogramming.com/red/chapter05.html
- //https://www.opengl.org/sdk/docs/man2/xhtml/glLight.xml
- void light(void){
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glEnable(GL_LIGHTING);
- float lightAmb[] = { 0.0, 0.0, 0.0, 1.0 };
- float lightDifAndSpec[] = { 20.0, 20.0, 20.0, 20.0 };
- float globAmb[] = { 0.6, 0.6, 0.6, 0.6 };
- glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDifAndSpec);
- glLightfv(GL_LIGHT0, GL_SPECULAR, lightDifAndSpec);
- //The intensity of light decreases as distance from the light increases.
- glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
- glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);
- glEnable(GL_LIGHT0);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globAmb);
- }
- void displayGame(void){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- float light_position[] = { xpos * MOVEMENT_SPEED, 0.0, zpos * MOVEMENT_SPEED, 1.0 }; //Positional Light
- float spotDirection[] = {cos (yrot * PI/180 + 11.0) , -sin(xrot * PI/ 180), sin(yrot * PI / 180.0 + 11.0)};
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20);
- drawSkybox(4*MAP_SIZE);
- drawGround(MAP_SIZE, MAP_SIZE);
- putPages();
- drawObjects();
- //Show light0
- /*glPushMatrix();
- glDisable(GL_LIGHTING);
- glTranslatef(xpos * MOVEMENT_SPEED, 0.0, zpos * MOVEMENT_SPEED);
- glRotatef(-90.0, 1.0, 0.0, 0.0);
- glColor3f(1.0f, 1.0f, 1.0f);
- glutWireCone(3.0 * tan( 45.0/180.0 * PI ), 4.0, 20, 20);
- glEnable(GL_LIGHTING);
- glPopMatrix();*/
- keyboard();
- moveWorld();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- //-------------------------------------
- //Functions from second screen
- void mouseComands(int button, int state, int x, int y){
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
- if( (x <= 275) && (x >= 257) && (y >= 423) && (y <= 437) ){
- glutKeyboardFunc(keyboard_down);
- glutKeyboardUpFunc(keyboard_up);
- glutIgnoreKeyRepeat(GL_TRUE);
- light();
- glutReshapeFunc(resize);
- glutPassiveMotionFunc(mouse);
- glutMouseFunc(mouseGame);
- glutSetCursor(GLUT_CURSOR_NONE); //Hide cursor
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective (50.0, 1.0f, 1.0f, 400.0f);
- loadObject(&slender, "slenderman.obj", "toonB.bmp");
- loadObject(&rock, "rockn.obj", "rock.bmp");
- loadObject(&tree, "tree.obj", "tree.bmp");
- loadObject(&crate, "craten.obj", "crate.bmp");
- loadObject(&house, "house.obj", "farmhouse.bmp");
- paper = loadBMP_custom("page.bmp");
- glutDisplayFunc(displayGame);
- //glutFullScreen();
- }
- }
- }
- void displayComands(void){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1.0f, 1.0f, 1.0f);
- drawText(-0.90f, 0.70f, "Comandos basicos:", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, 0.50f, "W: Mover-se para frente", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, 0.40f, "A: Mover-se para esquerda", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, 0.30f, "S: Mover-se para tras", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, 0.20f, "D: Mover-se para direita", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, 0.10f, "Movimento do mouse: Olhar ao seu redor", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, 0.0f, "Botao esquerdo do mouse: Pegar pagina", GLUT_BITMAP_9_BY_15);
- drawText(-0.75f, -0.10f, "Q: Sair", GLUT_BITMAP_9_BY_15);
- drawText(-0.0f, -0.70f, "OK", GLUT_BITMAP_9_BY_15);
- glutSwapBuffers();
- glutPostRedisplay();
- }
- //------------------------------------------------------------------------
- //Functions from the first screen
- void displayScreen(void){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1.0f, 1.0f, 1.0f);
- drawText(-0.20, 0.15, "SLENDER", GLUT_BITMAP_TIMES_ROMAN_24);
- glColor3f(0.0f, 1.0f, 0.0f);
- drawText(-0.08, -0.02, "JOGAR", GLUT_BITMAP_9_BY_15);
- glColor3f(1.0f, 0.0f, 0.0f);
- drawText(-0.065, -0.16, "SAIR", GLUT_BITMAP_9_BY_15);
- glColor3f(1.0f, 1.0f, 1.0f);
- drawText(-0.95, -0.95, "Desenvolvido por Thulio Guilherme - 2015", GLUT_BITMAP_8_BY_13);
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void mouseScreen(int button, int state, int x, int y){
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
- if( (x >= 225) && (x <= 287) && (y >= 241) && (y <= 261) ){
- glutMouseFunc(mouseComands);
- glutDisplayFunc(displayComands);
- }
- if( (x >= 230) && (x <= 282) && (y >= 286) && (y <= 306) ){
- exit(0);
- }
- }
- }
- //---------------------------------------------------------------
- int main(int argc, char **argv){
- std::clog << "Begin...\n";
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
- glutInitWindowSize(512, 512);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Slender Alpha");
- glutDisplayFunc(displayScreen);
- glutMouseFunc(mouseScreen);
- atexit(free_mem);
- initskybox();
- glClearColor(0.0, 0.0, 0.0, 1.0);
- Texture = loadBMP_custom("texture4.bmp");
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment