void processMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { xf = (float)x/((float)window::width/2.0) - 1; yf = 1 - (float)y/((float)window::height/2.0); pressedLeft = true; prevX = xf; prevY = yf; prevZ = findZ(prevX,prevY); base.getMatrix().setMatrix(world.getMatrix()); } else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { xf = (float)x/((float)window::width/2.0) - 1; yf = 1 - (float)y/((float)window::height/2.0); pressedMiddle = true; prevX = xf; prevY = yf; prevZ = findZ(prevX,prevY); base.getMatrix().setMatrix(world.getMatrix()); } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { xf = (float)x/((float)window::width/2.0) - 1; yf = 1 - (float)y/((float)window::height/2.0); pressedRight = true; prevX = xf; prevY = yf; prevZ = findZ(prevX,prevY); base.getMatrix().setMatrix(world.getMatrix()); } else { base.getMatrix().setMatrix(world.getMatrix()); temp.getMatrix().identity(); pressedLeft = false; pressedMiddle = false; pressedRight = false; } }
void IsometricCubeWidget::setCube(Cube c) { color** mat = c.getMatrix(); for(int x = 0; x < 18; x++) { for(int y = 0; y < 3; y++) { displayCube[x][y] = mat[x][y]; } delete [] mat[x]; } delete [] mat; mat = 0; this->update(); }
//---------------------------------------------------------------------------- // Callback method called when window readraw is necessary or // when glutPostRedisplay() was called. void window::displayCallback(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers //cout << *(world.getMatrix().getPointer() + 12) << " and " << *(world.getMatrix().getPointer() + 13) << " and " << *(world.getMatrix().getPointer() + 14) << endl; if (toggle_rage && !insideWorld(world.getMatrix()) && !jump) { world.getMatrix().identity(); } else { processMovement(); } camera.inverseCamera(); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse_character); // CHARACTER glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); cube.getMatrix().identity(); all.getMatrix().setMatrix(cube.getMatrix().rotateY(PI/2)); all.getMatrix().setMatrix(all.getMatrix().multiply(camera.getMatrix())); // YOU glLoadMatrixf(all.getMatrix().getPointer()); if (toggle_rage) glColor3f(1.0,0.0,0.0); else glColor3f(0.0,0.0,1.0); glutSolidSphere(2,30,30); if (debug) { glColor3f(1.0,0.0,0.0); glutWireCube(3.6); } drawFPS(); glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); //glLoadIdentity(); //updateParticles(); //drawParticles(); glLoadMatrixf(world.getMatrix().getPointer()); Draw_Skybox(0,150,0,800,800,800); if (!toggle_rage) glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); else glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse_rage); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glPushMatrix(); glTranslatef(0, -CURRENT_TERRAIN_HEIGHT, 0); glCallList(CURRENT_TERRAIN_ID); glPopMatrix(); glDisable(GL_LIGHTING); light0.getMatrix().identity(); light0.getMatrix().setMatrix(light0.getMatrix().translate(light0_position[0],light0_position[1],light0_position[2])); all.getMatrix().setMatrix(light0.getMatrix()); all.getMatrix().setMatrix(all.getMatrix().multiply(world.getMatrix())); glLoadMatrixf(all.getMatrix().getPointer()); glColor3f(1.0,1.0,1.0); glutSolidSphere(1.0,20,20); glEnable(GL_LIGHTING); // WORLD glEnable(GL_LIGHTING); if (toggle_shader) shader->bind(); g_world->draw(world.getMatrix()); if (toggle_shader) shader->unbind(); glDisable(GL_LIGHTING); glutSwapBuffers(); }
void processMovement() { float y; if (!toggle_rage) { if (keys[W]) { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,1.1)); } if (keys[S]) { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,-1.1)); } } else { if (keys[W]) { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,1.1)); toggle_rage = false; } if (keys[S]) { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,-1.1)); toggle_rage = false; } else { //world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,1.1)); selectRandomPath(); } } if (keys[A]) { rotation.getMatrix().setMatrix(world.getMatrix().rotateY(-0.05)); world.getMatrix().setMatrix(world.getMatrix().multiply(rotation.getMatrix())); } if (keys[D]) { rotation.getMatrix().setMatrix(world.getMatrix().rotateY(0.05)); world.getMatrix().setMatrix(world.getMatrix().multiply(rotation.getMatrix())); } if (jump) { j_x += 0.1; y = -(j_x*j_x); if (j_x >= 0) y = y*-1; if (j_x > -j_start) jump = false; else world.getMatrix().setMatrix(world.getMatrix().translate(0.0,y,0.0)); } }
void keyDown(unsigned char key, int x, int y) { Vector3 v; switch (key) { case ' ': if (!jump) { j_x = j_start; jump = true; } break; case 'w': keys[W] = true; break; case 's': keys[S] = true; break; case 'a': keys[A] = true; break; case 'd': keys[D] = true; break; case 'v': toggle_view = !toggle_view; if (toggle_view) { v = Vector3(0,10,5); camera.lookAt(v,l,up); camera.getE().print(); camera.getMatrix().print(); } else { camera.lookAt(p,l,up); camera.getE().print(); camera.getMatrix().print(); } break; /*case '1': light0Toggle = !light0Toggle; if (light0Toggle) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0); break;*/ case 'r': toggle_rage = !toggle_rage; rage_count = 0; break; case 'p': world.getMatrix().identity(); break; case 'h': toggle_shader = !toggle_shader; break; case 't': toggle_texture = !toggle_texture; if (toggle_texture) glEnable(GL_TEXTURE_2D); else glDisable(GL_TEXTURE_2D); break; case 'f': is_fullscreen = !is_fullscreen; if (is_fullscreen) glutFullScreen(); else glutReshapeWindow(512, 512); break; case 'u': debug = !debug; break; case '1': CURRENT_TERRAIN_ID = TERRAIN_ONE_ID; //CURRENT_TERRAIN_HEIGHT = 2; break; case '2': CURRENT_TERRAIN_ID = TERRAIN_TWO_ID; //CURRENT_TERRAIN_HEIGHT = 2; break; case '3': CURRENT_TERRAIN_ID = TERRAIN_THREE_ID; //CURRENT_TERRAIN_HEIGHT = 2; break; case 27: //if (shader) // delete shader; //glDeleteLists(terrainListID, 1); exit(0); break; } }
void selectRandomPath() { if (rage_count == 0) // select random path { rage_path = rand()%6; rage_count += 1; world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,2.5)); } else // continue executing code { rage_count++; if (rage_count < 10 || jump) { if (rage_path == 4) { jump = true; } if (rage_path == 0 || rage_path == 1 || rage_path == 4) { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,2.5)); rotation.getMatrix().setMatrix(world.getMatrix().rotateY(-0.07)); world.getMatrix().setMatrix(world.getMatrix().multiply(rotation.getMatrix())); } else if (rage_path == 2 || rage_path == 3 || rage_path == 4) { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,2.5)); rotation.getMatrix().setMatrix(world.getMatrix().rotateY(0.07)); world.getMatrix().setMatrix(world.getMatrix().multiply(rotation.getMatrix())); } else { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,2.5)); } if (jump) { j_x += 0.1; float y = -(j_x*j_x); if (j_x >= 0) y = y*-1; if (j_x > -j_start) { jump = false; j_x = j_start; } else world.getMatrix().setMatrix(world.getMatrix().translate(0.0,y,0.0)); } } else { world.getMatrix().setMatrix(world.getMatrix().translate(0.0,0,2.5)); rage_count = 0; } } }
void processMotion(int x, int y) { xf = (float)x/((float)window::width/2.0) - 1; yf = 1 - (float)y/((float)window::height/2.0); if (pressedLeft) { zf = findZ(xf,yf); Vector3 prev = Vector3(prevX,prevY,prevZ); Vector3 curr = Vector3(xf,yf,zf); Vector3 p = prev.normalize(); Vector3 c = curr.normalize(); prev.set(p.get(0), p.get(1), p.get(2)); curr.set(c.get(0), c.get(1), c.get(2)); theta = getTheta(prev, curr); Vector3 axis3 = prev.cross(prev, curr); Vector3 a = axis3.normalize(); axis3.set(a.get(0), a.get(1), a.get(2)); Vector4 axis4 = Vector4(axis3.get(0), axis3.get(1), axis3.get(2), 0); temp.getMatrix().setMatrix(temp.getMatrix().rotateAA(axis4,theta)); } else if (pressedMiddle) { temp.getMatrix().identity(); temp.getMatrix().setMatrix(temp.getMatrix().translate((xf-prevX)*15,(yf-prevY)*15,0)); } else if (pressedRight) { float s; if (prevY < yf) { s = 1-(yf-prevY)*-2; temp.getMatrix().identity(); temp.getMatrix().setMatrix(temp.getMatrix().scale(s,s,s)); } else { float s = 1+(yf-prevY)*0.5; if (s < 0) { s = 0.0001; } temp.getMatrix().identity(); temp.getMatrix().setMatrix(temp.getMatrix().scale(s,s,s)); } } }
int main(int argc, char *argv[]) { GLfloat ambient[] = {0.5,0.5,0.5,1.0}; // default ambient color for all light sources glutInit(&argc, argv); // initialize GLUT glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // open an OpenGL context with double buffering, RGB colors, and depth buffering glutInitWindowSize(512, 512); // set initial window size glutCreateWindow("Final Project"); // open window and set window title if (is_fullscreen) glutFullScreen(); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); // enable z-buffer glClearColor(0.0, 0.0, 0.0, 0.0); // set clear color to black glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); // set the default ambient color glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); // allow glColor to set ambient and diffuse colors of geometry // Generate light source: glEnable(GL_LIGHTING); // enables lighting; this changes the behavior of glColor glEnable(GL_LIGHT0); // enable a light source; otherwise you will only see ambient light glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_TEXTURE_2D); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); glLightfv(GL_LIGHT0, GL_SHININESS, light0_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); // Install callback functions: glutDisplayFunc(window::displayCallback); glutReshapeFunc(window::reshapeCallback); glutIdleFunc(window::idleCallback); glutMouseFunc(processMouse); glutMotionFunc(processMotion); glutKeyboardFunc(keyDown); glutKeyboardUpFunc(keyUp); // Initialize cube matrix: character.getMatrix().identity(); all.getMatrix().identity(); cube.getMatrix().identity(); cube1.getMatrix().identity(); cube2.getMatrix().identity(); dragon.getMatrix().identity(); base.getMatrix().identity(); temp.getMatrix().identity(); world.getMatrix().identity(); terrain.getMatrix().identity(); rotation.getMatrix().identity(); // Shaders shader = new Shader(toon_vert,toon_frag,true); camera.lookAt(p,l,up); cout << "Loading .obj files" << endl; objReader.readObj("cow.obj", nVerts, &vertices, &normals, &texcoords, nIndices, &indices); cout << "Loading texture files" << endl; loadTexture(texture_array,"desert_front.ppm", SKYFRONT); loadTexture(texture_array,"desert_back.ppm", SKYBACK); loadTexture(texture_array,"desert_left.ppm", SKYLEFT); loadTexture(texture_array,"desert_right.ppm", SKYRIGHT); loadTexture(texture_array,"desert_top.ppm", SKYUP); cout << "Generating terrain1" << endl; TerrainHelper th1; th1.terrainLoad(800,800,1); th1.terrainScale(0,TERRAIN_ONE_HEIGHT); TERRAIN_ONE_ID = th1.terrainCreateDL(0,0,0); cout << "Generating terrain2" << endl; TerrainHelper th2; th2.terrainLoad(800,800,1); th2.terrainScale(0, TERRAIN_TWO_HEIGHT); TERRAIN_TWO_ID = th2.terrainCreateDL(0,0,0); cout << "Generating terrain3" << endl; TerrainHelper th3; th3.terrainLoad(800, 800,1); th3.terrainScale(0,TERRAIN_THREE_HEIGHT); TERRAIN_THREE_ID = th3.terrainCreateDL(0,0,0); CURRENT_TERRAIN_ID = TERRAIN_ONE_ID; cout << "Creating world" << endl; createWorld(); //cout << "initializing particles" << endl; //initParticles(0.0,0.0,0.0); glutMainLoop(); return 0; }
int WaveObjLoader::loadObject(const char* filename, Cube cubeForObject) { std::vector<std::string*> coord; //read every single line of the obj file as a string std::vector<coordinate*> vertex; std::vector<face*> faces; std::vector<coordinate*> normals; //normal vectors for every face std::ifstream in(filename); //open the .obj file if(!in.is_open()) //if not opened, exit with -1 { std::cout << "Nor oepened" << std::endl; return -1; } char buf[256]; //read in every line to coord while(!in.eof()) { in.getline(buf,256); coord.push_back(new std::string(buf)); } //go through all of the elements of coord, and decide what kind of element is that for(int i=0;i<coord.size();i++) { if(coord[i]->c_str()[0]=='#') //if it is a comment (the first character is #) continue; //we don't care about that else if(coord[i]->c_str()[0]=='v' && coord[i]->c_str()[1]==' ') //if vector { float tmpx,tmpy,tmpz; sscanf(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz); //read in the 3 float coordinate to tmpx,tmpy,tmpz vertex.push_back(new coordinate(tmpx,tmpy,tmpz)); //and then add it to the end of our vertex list }else if(coord[i]->c_str()[0]=='v' && coord[i]->c_str()[1]=='n') //if normal vector { float tmpx,tmpy,tmpz; //do the same thing sscanf(coord[i]->c_str(),"vn %f %f %f",&tmpx,&tmpy,&tmpz); normals.push_back(new coordinate(tmpx,tmpy,tmpz)); }else if(coord[i]->c_str()[0]=='f') //if face { int a,b,c,d,e; if(count(coord[i]->begin(),coord[i]->end(),' ')==3) //if it is a triangle (it has 3 space in it) { sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b); faces.push_back(new face(b,a,c,d)); //read in, and add to the end of the face list }else{ sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b); faces.push_back(new face(b,a,c,d,e)); //do the same, except we call another constructor, and we use different pattern } } } //raw glLoadMatrixd(cubeForObject.getMatrix().getPointer()); int num; //the id for the list num=glGenLists(1); //generate a uniqe glNewList(num,GL_COMPILE); //and create it for(int i=0;i<faces.size();i++) { if(faces[i]->four) //if it's a quad draw a quad { glBegin(GL_QUADS); //basically all I do here, is use the facenum (so the number of the face) as an index for the normal, so the 1st normal owe to the first face //I subtract 1 because the index start from 0 in C++ glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z); //draw the faces glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z); glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z); glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z); glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z); glEnd(); }else{ glBegin(GL_TRIANGLES); glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z); glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z); glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z); glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z); glEnd(); } } glEndList(); //delete everything to avoid memory leaks for(int i=0;i<coord.size();i++) delete coord[i]; for(int i=0;i<faces.size();i++) delete faces[i]; for(int i=0;i<normals.size();i++) delete normals[i]; for(int i=0;i<vertex.size();i++) delete vertex[i]; return num; //return with the id }