Пример #1
0
void AppMain() {
    if(!glfwInit())
        throw std::runtime_error("glfwInit failed");
    
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_FALSE);
    
    if(!glfwOpenWindow(SCREEN_SIZE.x, SCREEN_SIZE.y, 8, 8, 8, 8, 0, 0, GLFW_WINDOW))
        throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
    
    glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
    if(glewInit() != GLEW_OK)
        throw std::runtime_error("glewInit failed");
    
    
    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
    std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
    std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;
    
    if(!GLEW_VERSION_3_2)
        throw std::runtime_error("OpenGL 3.2 API is not available.");
    
    glfwDisable(GLFW_MOUSE_CURSOR);
    glfwSetMousePos(0,0);
    glfwSetMouseWheel(0);
    
    LoadShaders();
    LoadTriangle();
    
    //intialise the Camera position
    gCamera.setPosition(glm::vec3(0,0,4));
    gCamera.setViewportAspectRatio(SCREEN_SIZE.x / SCREEN_SIZE.y);
    gCamera.setNearAndFarPlanes(0.5, 100.0f);
    Axis = glm::vec3(0,1,0);
    
    //intialise the Light attribute
    gLight.position = glm::vec3(0.0f,0.1f,-0.1f);
    gLight.intensities = glm::vec3(0.8,0.78,1); // white light
    gLight.attenuation = 0.2f;
    gLight.ambientCoefficient = 0.005f;
    
    double lastTime = glfwGetTime();
    while(glfwGetWindowParam(GLFW_OPENED)){
        
        double thisTime = glfwGetTime();
        Update(thisTime - lastTime);
        lastTime  = thisTime;
        Render();
        
    }
    
    glfwTerminate();
}
Пример #2
0
// update the scene based on the time elapsed since last update
static void Update(float secondsElapsed) {
    //rotate the first instance in `gInstances`
    const GLfloat degreesPerSecond = 180.0f;
    gDegreesRotated += secondsElapsed * degreesPerSecond;
    while(gDegreesRotated > 360.0f) gDegreesRotated -= 360.0f;
    gInstances.front().transform = glm::rotate(glm::mat4(), gDegreesRotated, glm::vec3(0,1,0));

    //move position of camera based on WASD keys, and XZ keys for up and down
    const float moveSpeed = 4.0; //units per second
    if(glfwGetKey('S')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.forward());
    } else if(glfwGetKey('W')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.forward());
    }
    if(glfwGetKey('A')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.right());
    } else if(glfwGetKey('D')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.right());
    }
    if(glfwGetKey('Z')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * -glm::vec3(0,1,0));
    } else if(glfwGetKey('X')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * glm::vec3(0,1,0));
    }

    //move light
    if(glfwGetKey('1'))
        gLight.position = gCamera.position();

    // change light color
    if(glfwGetKey('2'))
        gLight.intensities = glm::vec3(1,0,0); //red
    else if(glfwGetKey('3'))
        gLight.intensities = glm::vec3(0,1,0); //green
    else if(glfwGetKey('4'))
        gLight.intensities = glm::vec3(1,1,1); //white


    //rotate camera based on mouse movement
    const float mouseSensitivity = 0.1f;
    int mouseX, mouseY;
    glfwGetMousePos(&mouseX, &mouseY);
    gCamera.offsetOrientation(mouseSensitivity * mouseY, mouseSensitivity * mouseX);
    glfwSetMousePos(0, 0); //reset the mouse, so it doesn't go out of the window

    //increase or decrease field of view based on mouse wheel
    const float zoomSensitivity = -0.2f;
    float fieldOfView = gCamera.fieldOfView() + zoomSensitivity * (float)glfwGetMouseWheel();
    if(fieldOfView < 5.0f) fieldOfView = 5.0f;
    if(fieldOfView > 130.0f) fieldOfView = 130.0f;
    gCamera.setFieldOfView(fieldOfView);
    glfwSetMouseWheel(0);
}
Пример #3
0
void Input::init()
{
    glfwGetWindowSize(&m_mouse.x, &m_mouse.y);
    m_mouse.x /= 2;
    m_mouse.y /= 2;
    m_mouse.z = 0;

    m_mouseMiddleX = m_mouse.x;
    m_mouseMiddleY = m_mouse.y;

    glfwSetMousePos(m_mouse.x, m_mouse.y);
    glfwSetMouseWheel(0);
}
Пример #4
0
void TW_CALL ResetView(void * a)
{
	Rotation[0] = 0.0f;Rotation[1] = 0.0f;Rotation[2] = 0.0f;Rotation[3] = 1.0f;

	pos = glm::vec3( 5.0f, 3.0f , 18.0f );

	zNear = 0.1f;
	zFar  = 100.0f;
	FOV	  = 45.0f;
	glfwSetMouseWheel(0);

	horizAngle = 3.14f;
	verticAngle = 0.0f;
}
Пример #5
0
void init() {
	srand (time(NULL));

	if(!glfwInit()) {
        throw std::runtime_error("glfwInit failed");
		return;
	}
    
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_FALSE);
	if(!glfwOpenWindow(SCREEN_SIZE.x, SCREEN_SIZE.y, 8, 8, 8, 8, 16, 0, GLFW_WINDOW)) {
        throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
		return;
	}

	glfwSetWindowPos(400, 100);

    //GLFW settings.
	glfwDisable(GLFW_MOUSE_CURSOR);
    glfwSetMousePos(SCREEN_SIZE.x / 2, SCREEN_SIZE.y / 2);
    glfwSetMouseWheel(0);
	
	glewExperimental = GL_TRUE;
    if(glewInit() != GLEW_OK) {
        throw std::runtime_error("glewInit failed");
		return;
	}

	while(glGetError() != GL_NO_ERROR) {}    
	
	std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
    std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
    std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;

    if(!GLEW_VERSION_3_2) {
        throw std::runtime_error("OpenGL 3.2 API is not available.");
		return;
	}
}
Пример #6
0
// update the scene based on the time elapsed since last update
void Update(float secondsElapsed) {
    //rotate the cube
    const GLfloat degreesPerSecond = 180.0f;
    gDegreesRotated += secondsElapsed * degreesPerSecond;
    while(gDegreesRotated > 360.0f) gDegreesRotated -= 360.0f;

    //move position of camera based on WASD keys, and XZ keys for up and down
    const float moveSpeed = 2.0; //units per second
    if(glfwGetKey('S')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.forward());
    } else if(glfwGetKey('W')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.forward());
    }
    if(glfwGetKey('A')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.right());
    } else if(glfwGetKey('D')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.right());
    }
    if(glfwGetKey('Z')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * -glm::vec3(0,1,0));
    } else if(glfwGetKey('X')){
        gCamera.offsetPosition(secondsElapsed * moveSpeed * glm::vec3(0,1,0));
    }

    //rotate camera based on mouse movement
    const float mouseSensitivity = 0.1;
    int mouseX, mouseY;
    glfwGetMousePos(&mouseX, &mouseY);
    gCamera.offsetOrientation(mouseSensitivity * mouseY, mouseSensitivity * mouseX);
    glfwSetMousePos(0, 0); //reset the mouse, so it doesn't go out of the window

    //increase or decrease field of view based on mouse wheel
    const float zoomSensitivity = -0.2;
    float fieldOfView = gCamera.fieldOfView() + zoomSensitivity * (float)glfwGetMouseWheel();
    if(fieldOfView < 5.0f) fieldOfView = 5.0f;
    if(fieldOfView > 130.0f) fieldOfView = 130.0f;
    gCamera.setFieldOfView(fieldOfView);
    glfwSetMouseWheel(0);
}
Пример #7
0
void Input::update()
{
    m_mouse.z = glfwGetMouseWheel();
    glfwSetMouseWheel(0);
    if (m_mouseVisible == false)
    {
#ifdef __APPLE__
 		CGGetLastMouseDelta(&m_mouse.x, &m_mouse.y);
        GLFWvidmode mode;
        glfwGetDesktopMode(&mode);
	    CGPoint p;
		p.x = m_mouseMiddleX;
		p.y = m_mouseMiddleY;
		CGWarpMouseCursorPosition(p);
#else
        glfwGetMousePos(&m_mouse.x, &m_mouse.y);
        m_mouse.x -= m_mouseMiddleX;
        m_mouse.y -= m_mouseMiddleY;
        glfwSetMousePos(m_mouseMiddleX, m_mouseMiddleY);
#endif
    }
    else
    {
        glfwGetMousePos(&m_mouse.x, &m_mouse.y);
    }
    
    m_mouse.b = 0;
    if (glfwGetMouseButton(GLFW_MOUSE_BUTTON_1))
    {
        m_mouse.b |= 1;
    }
    if (glfwGetMouseButton(GLFW_MOUSE_BUTTON_2))
    {
        m_mouse.b |= 2;
    }
}
Пример #8
0
// the program starts here
void AppMain() {
    // initialise GLFW
    if(!glfwInit())
        throw std::runtime_error("glfwInit failed");

    // open a window with GLFW
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
    if(!glfwOpenWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, 8, 8, 8, 8, 16, 0, GLFW_WINDOW))
        throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");

    // GLFW settings
    glfwDisable(GLFW_MOUSE_CURSOR);
    glfwSetMousePos(0, 0);
    glfwSetMouseWheel(0);

    // initialise GLEW
    glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
    if(glewInit() != GLEW_OK)
        throw std::runtime_error("glewInit failed");

    // GLEW throws some errors, so discard all the errors so far
    while(glGetError() != GL_NO_ERROR) {}

    // print out some info about the graphics drivers
    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
    std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
    std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;

    // make sure OpenGL version 3.2 API is available
    if(!GLEW_VERSION_3_2)
        throw std::runtime_error("OpenGL 3.2 API is not available.");

    // OpenGL settings
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    // initialise the gWoodenCrate asset
    LoadWoodenCrateAsset();

    // create all the instances in the 3D scene based on the gWoodenCrate asset
    CreateInstances();

    // setup gCamera
    gCamera.setPosition(glm::vec3(-4,0,17));
    gCamera.setViewportAspectRatio(SCREEN_SIZE.x / SCREEN_SIZE.y);
    gCamera.setNearAndFarPlanes(0.5f, 100.0f);

    // setup gLight
    gLight.position = glm::vec3(-4,0,4);
    gLight.intensities = glm::vec3(1,1,1); //white
    gLight.attenuation = 0.2f;
    gLight.ambientCoefficient = 0.005f;

    // run while the window is open
    double lastTime = glfwGetTime();
    while(glfwGetWindowParam(GLFW_OPENED)){
        // update the scene based on the time elapsed since last update
        double thisTime = glfwGetTime();
        Update((float)(thisTime - lastTime));
        lastTime = thisTime;

        // draw one frame
        Render();

        // check for errors
        GLenum error = glGetError();
        if(error != GL_NO_ERROR)
            std::cerr << "OpenGL Error " << error << ": " << (const char*)gluErrorString(error) << std::endl;

        //exit program if escape key is pressed
        if(glfwGetKey(GLFW_KEY_ESC))
            glfwCloseWindow();
    }

    // clean up and exit
    glfwTerminate();
}
Пример #9
0
//rotated and add keyboard and mouse callback
void Update(float secondsElapsed) {
    //change the axis that rotated
    if (glfwGetKey('7')){
        Axis =glm::vec3(1,0,0);
    }
    if (glfwGetKey('8')){
        Axis =glm::vec3(0,0,1);
    }
    if (glfwGetKey('9')){
        Axis =glm::vec3(0,1,0);
    }
    //press "T" to stop rotate
    GLfloat degreesPerSecond = 20.0f;
    if (glfwGetKey('T')){
        degreesPerSecond = 0.0f;
    }

    gDegreesRotated += secondsElapsed * degreesPerSecond;
    while (gDegreesRotated > 360.0f) gDegreesRotated -= 360.0f;
    
    
    const float moveSpeed = 3.0;
    if (glfwGetKey('S')) {
        gCamera.offsetPositon(secondsElapsed * moveSpeed * -gCamera.forward());
    } else if (glfwGetKey('W')){
        gCamera.offsetPositon(secondsElapsed * moveSpeed * gCamera.forward());
    } else if (glfwGetKey('A')) {
        gCamera.offsetPositon(secondsElapsed * moveSpeed * -gCamera.right());
    } else if (glfwGetKey('D')) {
        gCamera.offsetPositon(secondsElapsed * moveSpeed * gCamera.right());
    }
    if (glfwGetKey('Z')) {
        gCamera.offsetPositon(secondsElapsed* moveSpeed * -glm::vec3(0,1,0));
    } else if (glfwGetKey('X')) {
        gCamera.offsetPositon(secondsElapsed * moveSpeed * glm::vec3(0,1,0));
    }
    
    // change light color
    if(glfwGetKey('1'))
        gLight.intensities = glm::vec3(0.8, 0.3, 0.0); //red
    else if(glfwGetKey('2'))
        gLight.intensities = glm::vec3(0.0, 0.8, 0.2); //green
    else if (glfwGetKey('3'))
        gLight.intensities = glm::vec3(0.1, 0.3, 0.8);
    else if(glfwGetKey('4'))
        gLight.intensities = glm::vec3(1,1,1); //white
    
    //mouse movement with camera
    const float mouseSensitivity = 0.1;
    int mouseX, mouseY;
    glfwGetMousePos(&mouseX, &mouseY);
    gCamera.offsetOrientation(mouseSensitivity* mouseY, mouseSensitivity* mouseX);
    glfwSetMousePos(0,0);
    
    
    //mouse wheel with field of view
    const float zoomSensitivity = -0.8;
    float fieldOfView = gCamera.fieldOfView() + zoomSensitivity *(float)glfwGetMouseWheel();
    if(fieldOfView < 5.0f) fieldOfView = 5.0f;
    if(fieldOfView > 130.0f) fieldOfView = 130.0f;
    gCamera.setFieldOfView(fieldOfView);
    if (glfwGetMouseButton(0))          //press left button to reset the Fov;
        gCamera.setFieldOfView(50.0f);
    glfwSetMouseWheel(0);
    
}
Пример #10
0
// the program starts here
void AppMain() {
    // initialise GLFW
    if(!glfwInit())
        throw std::runtime_error("glfwInit failed");
    
    // open a window with GLFW
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
    if(!glfwOpenWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, 8, 8, 8, 8, 16, 0, GLFW_WINDOW))
        throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");

    // GLFW settings
    glfwDisable(GLFW_MOUSE_CURSOR);
    glfwSetMousePos(0, 0);
    glfwSetMouseWheel(0);

    // initialise GLEW
    glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
    if(glewInit() != GLEW_OK)
        throw std::runtime_error("glewInit failed");
    
    // GLEW throws some errors, so discard all the errors so far
    while(glGetError() != GL_NO_ERROR) {}

    // print out some info about the graphics drivers
    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
    std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
    std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;

    // make sure OpenGL version 3.2 API is available
    if(!GLEW_VERSION_3_2)
        throw std::runtime_error("OpenGL 3.2 API is not available.");

    // OpenGL settings
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // load vertex and fragment shaders into opengl
    LoadShaders();

    // load the texture
    LoadTexture();

    // create buffer and fill it with the points of the triangle
    LoadCube();

    // setup gCamera
    gCamera.setPosition(glm::vec3(0,0,4));
    gCamera.setViewportAspectRatio(SCREEN_SIZE.x / SCREEN_SIZE.y);

    // run while the window is open
    double lastTime = glfwGetTime();
    while(glfwGetWindowParam(GLFW_OPENED)){
        // update the scene based on the time elapsed since last update
        double thisTime = glfwGetTime();
        Update(thisTime - lastTime);
        lastTime = thisTime;
        
        // draw one frame
        Render();

        // check for errors
        GLenum error = glGetError();
        if(error != GL_NO_ERROR)
            std::cerr << "OpenGL Error " << error << ": " << (const char*)gluErrorString(error) << std::endl;

        //exit program if escape key is pressed
        if(glfwGetKey(GLFW_KEY_ESC))
            glfwCloseWindow();
    }

    // clean up and exit
    glfwTerminate();
}
Пример #11
0
const InputData InputManager::GetInput() {
    InputData data = {
        false, // w
        false, // s
        false, // a
        false, // d
        /*
        false, // ctrl / mouse1
        false, // e    / mouse2
        false, // [    / mwheeldn
        false, // ]    / mwheelup
        false, // 1
        false, // 2
        false, // 3
        false, // 4
         */
        false, // esc
        0,     // mousey
        0      // mousex
    };
    glfwPollEvents();
    // Movement
    if (keypress('W') || keypress(GLFW_KEY_UP) || keypress(GLFW_KEY_KP_8))
        data.Forwards = true;
    if (keypress('S') || keypress(GLFW_KEY_DOWN) || keypress(GLFW_KEY_KP_5) || keypress(GLFW_KEY_KP_2))
        data.Backwards = true;
    if (keypress('A') || keypress(GLFW_KEY_LEFT) || keypress(GLFW_KEY_KP_4))
        data.Left = true;
    if (keypress('D') || keypress(GLFW_KEY_RIGHT) || keypress(GLFW_KEY_KP_6))
        data.Right = true;
    /*
    if (keypress(GLFW_KEY_LCTRL) || keypress(GLFW_KEY_RCTRL))
        data.Shoot = true;
    if (keypress('E') || keypress(GLFW_KEY_SPACE))
        data.Use = true;
    if (keypress('[') || keypress(GLFW_KEY_KP_7))
        data.InventoryPrev = true;
    if (keypress(']') || keypress(GLFW_KEY_KP_9))
        data.InventoryNext = true;
    if (keypress('1'))
        data.Slot1 = true;
    if (keypress('2'))
        data.Slot2 = true;
    if (keypress('3'))
        data.Slot3 = true;
    if (keypress('4'))
        data.Slot4 = true;
    */
    if (keypress(GLFW_KEY_ESC) || keypress('Q'))
        data.Exit = true;
    // Mouse events
    if (glfwGetWindowParam(GLFW_ACTIVE) == GL_TRUE) {
        if (lostFocus) {
            lostFocus = false;
            // Reset the mouse for the next call
        } else {
            /*
            if (mpress(GLFW_MOUSE_BUTTON_LEFT))
                data.Shoot = true;
            if (mpress(GLFW_MOUSE_BUTTON_RIGHT))
                data.Use = true;
            */
            int x = 0, y = 0;
            glfwGetMousePos(&x, &y);
            data.DeltaViewX = static_cast<float>(lastx - x);
            data.DeltaViewY = static_cast<float>(lasty - y);
            lastx = x;
            lasty = y;
            /*
            int mwheel = glfwGetMouseWheel();
            if (mwheel != 0) {
                if (mwheel > 0)
                    data.InventoryNext = true;
                else
                    data.InventoryPrev = true;
            }
            */
        }
        glfwSetMouseWheel(0);
    } else {
        // Ignore the mouse if the window's not in focus.
        lostFocus = true;
    }
    return data;
}
Пример #12
0
int main(int argc, char *argv[]){

    if(!glfwInit()){
        throw std::runtime_error("glfwInit failed");
    }
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    glfwOpenWindowHint(0, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 1);
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
    if(!glfwOpenWindow(800, 600, 0, 0, 0, 0, 32, 0, GLFW_WINDOW)){
        throw std::runtime_error("glfwOpenWindow failed. does this hardware work with 3.1");
    }

    glewExperimental = GL_TRUE;
    if(glewInit() != GLEW_OK){
        throw std::runtime_error("glewInit failed");
    }

    if(!GLEW_VERSION_3_1){
        throw std::runtime_error("OpenGL 3.1 API is not available");
    }

    LoadGeometry();
    LoadTexture();
    PrepVertexUniforms();
    init_font();
    printf("All data prepped\n");

    camera.setPosition(glm::vec3(0,0,8));
    camera.setViewportAspectRatio(screenwidth/screenheight);

    light.position = camera.position();
    light.intensities = glm::vec3(1,1,1);

    glfwDisable(GLFW_MOUSE_CURSOR);
    glfwSetMousePos(0, 0);
    glfwSetMouseWheel(0);

    printf("camera prepped\n");

    //turn on depth test so things dont look stupid
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_CULL_FACE);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    printf("starting main loop\n");
    while(glfwGetWindowParam(GLFW_OPENED)){

        update();
        display();

        //check for errors
        GLenum error = glGetError();
        if(error != GL_NO_ERROR){
            glPrintError();
        }

        //esc == close
        if(glfwGetKey(GLFW_KEY_ESC)){
            glfwCloseWindow();
        }
    }
    glfwTerminate();

    return 0;

}
Пример #13
0
void update(){
    //update time

    frames++;
    thisTime = glfwGetTime();
    diff = thisTime - lastTime;
    if(doRotate){
        degree += diff * 180.0f;
        if(degree > 360.0f){
            degree -= 360.0f;
        }
    }
    lastTime = thisTime;
    seconds += diff;
    if( seconds > 1.0){

        fps = frames;
        frames = 0;
        seconds = 0;
    }


    //handle keys
    //forward, back
    if(glfwGetKey('S')){
        camera.offsetPosition( diff * moveSpeed * -camera.forward());
    } else if(glfwGetKey('W')){
        camera.offsetPosition( diff * moveSpeed * camera.forward());
    }

    //left, right
    if(glfwGetKey('A')){
        camera.offsetPosition( diff * moveSpeed * -camera.right());
    } else if(glfwGetKey('D')){
        camera.offsetPosition( diff * moveSpeed * camera.right());
    }

    //up, down
    if(glfwGetKey('Z')){
        camera.offsetPosition( diff * moveSpeed * -glm::vec3(0,1,0));
    }else if(glfwGetKey('X')){
        camera.offsetPosition( diff * moveSpeed * glm::vec3(0,1,0));
    }

    if(glfwGetKey(GLFW_KEY_SPACE)){
        doRotate = false;
    }else{
        doRotate = true;
    }

    if(glfwGetKey('1')){
        light.intensities = glm::vec3(1,1,1);
    }

    if(glfwGetKey('2')){
        light.intensities = glm::vec3(1,0,0);
    }

    if(glfwGetKey('3')){
        light.intensities = glm::vec3(0,1,0);
    }

    if(glfwGetKey('4')){
        light.intensities = glm::vec3(0,0,1);
    }

    if(glfwGetKey('5')){
        light.intensities = glm::vec3(0,0,0);
    }

    const float mouseSensitivity = 0.1;
    int mouseX, mouseY;
    glfwGetMousePos(&mouseX, &mouseY);
    camera.offsetOrientation(mouseSensitivity * mouseY, mouseSensitivity * mouseX);
    glfwSetMousePos(0, 0);

    const float zoomSensitivity = -0.2;
    float fieldOfView = camera.fieldOfView() + zoomSensitivity * (float)glfwGetMouseWheel();
    if(fieldOfView < 5.0f) fieldOfView = 5.0f;
    if(fieldOfView > 130.0f) fieldOfView = 130.0f;
    camera.setFieldOfView(fieldOfView);
    glfwSetMouseWheel(0);

}