void Camera::LookAt(glm::vec3 const & eye, glm::vec3 const & center) { m_scale = glm::vec3(1.0f); m_position = eye; glm::vec3 vdir = center-eye; float rotY = (vdir.x == 0.0f && vdir.z == 0.0f) ? 0.0f : AngleBetween(glm::vec2(0.0f,-1.0f),glm::vec2(vdir.x, vdir.z)); vdir = glm::vec3(glm::rotateY(glm::vec4(vdir,1.0f),-rotY)); float rotX = (vdir.x == 0.0f && vdir.y == 0.0f) ? 0.0f : AngleBetween(glm::vec2(0.0f, 1.0f),glm::vec2(vdir.y, vdir.z)); float rotZ = 0.0f; m_rotation = -glm::vec3(rotX,rotY,rotZ); UpdateModelMatrix(); if (GetUpDir().y < 0) m_rotation += glm::vec3(180.0f,0,0); }
void Hero::initialize() { objectType = CollidableModel::ObjectType::MD2Model; CMD2Model::LoadModel("data/models/SONIC/PLAY.md2"); animationState = this->StartAnimation(animType_t::STAND); CMD2Model::SetBoundingBox(CollidableModel::CalculateBoundingBox(CMD2Model::GetVertices())); minitialTransformation = glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(0.01f, 0.01f, 0.01f); auto tempBoundingBox = CMD2Model::GetBoundingBox(); tempBoundingBox.Scale(0.01f, 0.01f, 0.01f); tempBoundingBox.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); CMD2Model::SetBoundingBox(tempBoundingBox); UpdateModelMatrix(); initialCamera = glm::vec3(0.0f,1.0f,2.0f); camera = std::unique_ptr<ThirdPersonCamera>(new ThirdPersonCamera()); auto position = GameObject::getPosition(); camera->Reset(position+initialCamera,position,glm::vec3(0.0f,1.0f,0.0f)); camera->SetPerspectiveProjection(45.0f, 4.0f / 3.0f, 0.1f, 200.0f); }
int main() { // Initialize GLFW if (!glfwInit()) { std::cerr << "Failed to initialize GLFW!" << std::endl; return EXIT_FAILURE; } // Setup OpenGL context glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Try to create a window auto window = glfwCreateWindow( SIZE, SIZE, "OpenGL", NULL, NULL); if (window == NULL) { std::cerr << "Failed to open GLFW window, your graphics card is probably only capable of OpenGL 2.1" << std::endl; glfwTerminate(); return EXIT_FAILURE; } // Finalize window setup glfwMakeContextCurrent(window); // Initialize GLEW glewExperimental = GL_TRUE; glewInit(); if (!glewIsSupported("GL_VERSION_3_3")) { std::cerr << "Failed to initialize GLEW with OpenGL 3.3!" << std::endl; glfwTerminate(); return EXIT_FAILURE; } // Load shaders auto program_id = ShaderProgram("gl_projection.vert", "gl_projection.frag"); glUseProgram(program_id); InitializeGeometry(program_id); // Load and bind texture auto texture_id = LoadImage("lena.rgb", SIZE, SIZE); auto texture_attrib = glGetUniformLocation(program_id, "Texture"); glUniform1i(texture_attrib, 0); glActiveTexture(GL_TEXTURE0 + 0); glBindTexture(GL_TEXTURE_2D, texture_id); InitializeProjection(program_id); // Time counter float time = 0; // Main execution loop while (!glfwWindowShouldClose(window)) { // Update model matrix (model rotation) UpdateModelMatrix(program_id, time++); // Set gray background glClearColor(.5f,.5f,.5f,0); // Clear depth and color buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw triangles using the program glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 4, 4); // Display result glfwSwapBuffers(window); glfwPollEvents(); } // Clean up glfwTerminate(); return EXIT_SUCCESS; }