Example #1
0
int main(int argc, char** argv)
{
    GLFWwindow* window = initGLWindow();
    
    movement = new CameraMovement(window);
    
    initGL();
    
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    renderer.init(width, height);
    
    camera.init(width/(float)height, 60.f, .1f, 100.f);
    camera.setCameraProjection(PROJECTION_PERSPECTIVE);
    
    // Floor
    std::shared_ptr<Mesh> floorMesh = std::make_shared<Mesh>(UnitQuad::CreateUnitQuad());
    floorMesh->material.diffuse = glm::vec4(0.3f, 0.6f, 0.7f, 1.0f);
    floorMesh->material.ambient = glm::vec4(0.01f, 0.01f, 0.01f, 1.0f);
    floorMesh->material.specular = glm::vec4(0.1f, 0.1f, 0.1f, 1.0f);
    floorMesh->material.shininess = 100.0f;
    
    std::shared_ptr<SceneNode> floor(new SceneNode);
    floor->init(floorMesh);
    floor->position = glm::vec3(0.0,-1.0, 0.0);
    floor->rotation = glm::rotate(glm::mat4(1.0f),-90.0f, glm::vec3(1.0, 0.0, 0.0));
    floor->scale = glm::scale(glm::mat4(1.0), glm::vec3(100.0f));
    
    LightProperties lightProperties = LightFactory::Bright(glm::vec3(1.0, 1.0, 1.0));
    lightProperties.position = glm::vec4(-2.0f, 2.0f, -1.0f, 1.0);
    lightProperties.direction = glm::vec4(0.0, -0.1, -1.0, 0.0);
    std::shared_ptr<Light> light(new Light);
    light->properties = lightProperties;
    renderer.lights.push_back(light);
    
    LightProperties lightProperties1 = LightFactory::Bright(glm::vec3(1.0, 1.0, 1.0));
    lightProperties1.position = glm::vec4(4.0f, 2.0f, -3.0f, 1.0);
    lightProperties1.direction = glm::vec4(-1.0, -0.1, 0.0, 0.0);
    std::shared_ptr<Light> light1(new Light);
    light1->properties = lightProperties1;
    renderer.lights.push_back(light1);

    std::string path(MODEL_PATH);
    path.append("cooldragon.off");
    auto node = createSceneNode(path);
    node->position = glm::vec3(0.0f, 0.0f, -3.0f);
        
    renderer.nodes.push_back(node);
    
    renderer.nodes.push_back(floor);
    
    std::thread first (startGui, argc, argv);
    
    glfwSwapInterval(1); //0 to disable vsync, 1 to enable it
    
    while (!glfwWindowShouldClose(window))
    {
        if(shouldFlipNormals) {
            renderer.nodes[0]->mesh->flipNormals();
            shouldFlipNormals = false;
        }
        
        if(shouldLoadFile) {
            std::string path(MODEL_PATH);
            path.append(filename);
            renderer.nodes[0] = createSceneNode(path);
            renderer.nodes[0]->position = glm::vec3(-2.0f, -0.5f, -3.0f);
            
            gui->material = &renderer.nodes[0]->mesh->material;
            shouldLoadFile = false;
        }
        
        updateInput(window);
        
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glClearColor(0.0, 0.0, 0.0, 0.0);
        
        light->properties.position = glm::translate(glm::mat4(1.0f), glm::vec3(-2.5f +  cosf(glfwGetTime()), 0.5f, -0.0f)) * glm::vec4(1.0f);
        
        
        camera.position = movement->position;
        camera.target = camera.position + movement->lookatDirection;
        camera.update();
        
        renderer.proj = camera.getCameraProjectionTransform();
        renderer.view = camera.getCameraViewTransform();
        renderer.renderScene();
        
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    
    delete movement;
    
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}