From 04234b1f1cb3bef8fb089b2b1a0fe2ffbe5867be Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 14 Nov 2014 18:33:42 +0100 Subject: [PATCH] Made mouse control camera. (Stupid init of camera...) --- graphics.cc | 8 ++++++-- level.cc | 5 +++-- level.hh | 2 +- main.cc | 7 ++++++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/graphics.cc b/graphics.cc index 35513f5..651fe00 100644 --- a/graphics.cc +++ b/graphics.cc @@ -20,6 +20,10 @@ GLFWwindow* Graphics::getWindow() { return window; } +glm::uvec2 Graphics::getWindowSize() { + return windowSize; +} + void Graphics::setGLFWHintsForOpenGLVersion( unsigned int _version ) { #ifdef __APPLE__ @@ -159,8 +163,8 @@ glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, fl glm::mat4 Graphics::buildViewMatrix(Level* level) { glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, level->getCamera().getDistance(), 0.0f); // rotate vector - glm::mat4 rotationMatrix = glm::rotate(level->getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(level->getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate(level->getCamera().getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f)); + glm::mat4 rotationMatrix = + glm::rotate(level->getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) *glm::rotate(level->getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)); cameraVector = rotationMatrix * cameraVector; //construct lookAt (cameraPosition = cameraCenter + cameraVector return glm::lookAt(level->getCameraCenter()->getPosition() + glm::vec3(cameraVector), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); diff --git a/level.cc b/level.cc index f55a5d4..bb8cc9f 100644 --- a/level.cc +++ b/level.cc @@ -41,9 +41,10 @@ void Level::render() { this->terrain.render(); } -void Level::update(float runTime) { +void Level::update(float runTime, glm::vec2 mouseDelta) { // rotate bunny - cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); + //cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); + camera.updateRotation(mouseDelta/50.0f); } glm::vec3 Level::getAmbientLight() { diff --git a/level.hh b/level.hh index e472536..9af2592 100644 --- a/level.hh +++ b/level.hh @@ -15,7 +15,7 @@ class Level { Level(); ~Level(); void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning - void update(float runTime); + void update(float runTime, glm::vec2 mouseDelta); void render(); glm::vec3 getAmbientLight(); std::vector getLights(); diff --git a/main.cc b/main.cc index 6efddf7..4ae6e46 100644 --- a/main.cc +++ b/main.cc @@ -126,7 +126,12 @@ int main( int argc, char *argv[] ) frameCount = 0; } - app.getLevel()->update(now - startTimeInSeconds); + double xpos, ypos; + glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos); + glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2); + + app.getLevel()->update(now - startTimeInSeconds, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2, + (float)xpos-app.getGraphics()->getWindowSize().x/2)); app.getGraphics()->render(app.getLevel(), app.getShader()); openGLCriticalError();