#include #include #include "UI.h" #include "Axis.h" #include "Box.h" #include "Object.h" #include "OffObject.h" #include "Land.h" #include "CameraClass.h" #include "Errors.h" using namespace std; #include "vgl.h" #include "mat.h" #include "vec.h" #include "LoadShaders.h" // globals Axis axis_G; Box box_G; OffObject lizard_G("offs/lizard.off"); OffObject head_G("offs/HolmesShapes/head.off"); Land land_G(50,50); GLuint program_G; bool ortho_G = false; CameraClass camera_G; float theta_G =0; GLfloat worldRotateX_G, worldRotateY_G, worldRotateZ_G; void DoProjection(); void reset(void); void init(void); void display(void); void resize(int w, int h); void CheckForErrors(void); int main(int argc, char * argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(512, 512); glutInitContextVersion(3,0); glutInitContextProfile(GLUT_CORE_PROFILE | GLUT_DEBUG ); glutCreateWindow(argv[0]); if (glewInit()) { cerr<< "Unable to initialize GLEW " << endl; return -1; } init(); // input like things glutKeyboardFunc(Keypress); glutSpecialFunc(SpecialKeypress); glutKeyboardUpFunc(KeyUp); glutSpecialUpFunc(SpecialUp); KeyHelp(); // display like things glutDisplayFunc(display); glutReshapeFunc(resize); glutMainLoop(); return 0; } void reset(void) { // rotations worldRotateX_G = 0; worldRotateY_G = 0; worldRotateZ_G = 0; ortho_G = false; camera_G.Reset(); return; } void HeadSetup() { mat4 R,T, S; R = S = T= mat4(1.0); T = Translate(0.1, 0.4, 0.2); head_G.SetTransformation(S*T*R); head_G.SetColor(1, 0, 0); head_G.Bind(program_G); return; } void LandSetup() { land_G.SetTransformation(Translate(-25,0,-25)); land_G.SetColor(.12, .8, .5); land_G.Bind(program_G); return; } void LizardSetup() { mat4 R,T, S; R = RotateY(40)*RotateX(-90)*RotateZ(-10); T = Translate(.2, .5, 0); S = Scale(6,6,6); lizard_G.SetTransformation(S*T*R); lizard_G.SetColor(.1, .5, .2); lizard_G.Bind(program_G); return; } void init(void) { reset(); InitErrors(); glClearColor(1,1,1,1); ShaderInfo shaders [] = { { GL_VERTEX_SHADER, "vertex.glsl"}, { GL_FRAGMENT_SHADER, "fragment.glsl"}, { GL_NONE, NULL} }; program_G = LoadShaders(shaders); glUseProgram(program_G); axis_G.Bind(program_G); box_G.Bind(program_G); LizardSetup(); LandSetup(); HeadSetup(); return; } void resize(int w, int h) { int small = min(w,h); glViewport(0,0,small,small); return; } void DoProjection() { mat4 projection=mat4(1.0); static GLint projectionLoc = glGetUniformLocation(program_G,"projectionM"); if (ortho_G) { projection = Ortho(-1.5, 1.5, -1.5, 1.5, 2.0, 30.0); } else { projection = Frustum(-1.5, 1.5, -1.5, 1.5, 2.0, 30.0); } glUniformMatrix4fv(projectionLoc, 1, GL_TRUE, projection); return; } void display(void){ mat4 model; mat4 world; mat4 T,S,R; mat4 I = mat4(1.0); T = S = I; static int mLoc = glGetUniformLocation(program_G, "modelM"); static int wLoc = glGetUniformLocation(program_G, "worldM"); static int cameraLoc = glGetUniformLocation(program_G, "cameraM"); DoProjection(); camera_G.Display(cameraLoc); glClear(GL_COLOR_BUFFER_BIT); // set up the world transformation world = RotateX(worldRotateX_G) * RotateY(worldRotateY_G) * RotateZ(worldRotateZ_G); // reset all matricies to be identity glUniformMatrix4fv(wLoc, 1, true, world); glUniformMatrix4fv(mLoc, 1, true, I); axis_G.Display(); lizard_G.Display(); land_G.Display(); for(int i =1; i < 5; i++) { switch(i) { case 1: T = Translate(10,0,-10); break; case 2: T = Translate(-10,0,10); break; case 3: T = Translate(10,0,10); break; case 4: T = Translate(-10,0,-10); break; } R = RotateY(theta_G*i); S = Scale(i*1.5, i*1.5, i*1.5); model = T*S*R; glUniformMatrix4fv(mLoc, 1, true, model); head_G.Display(); } glutSwapBuffers(); return; }