aboutsummaryrefslogtreecommitdiffstats
path: root/dev/gfxtest/gfx_test.cc
blob: f96d80e3cd17659a24d25264b5252b826bf01c11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "gfx_test.h"

#include <iostream>
#include <sstream>

GfxTest::GfxTest() : GraphicsApp(1024,768, "GfxTest"), playing_(true) {
}
    
    
GfxTest::~GfxTest() {
}

    
void GfxTest::InitNanoGUI() {
    // Setup the GUI window
    nanogui::Window *window = new nanogui::Window(screen(), "Controls");
    window->setPosition(Eigen::Vector2i(10, 10));
    window->setSize(Eigen::Vector2i(400,200));
    window->setLayout(new nanogui::GroupLayout());
    
    pauseBtn_ = new nanogui::Button(window, "Pause");
    pauseBtn_->setCallback(std::bind(&GfxTest::OnPauseBtnPressed, this));
    pauseBtn_->setTooltip("Toggle playback.");
        
    screen()->performLayout();
}



void GfxTest::OnPauseBtnPressed() {
    playing_ = !playing_;
    if (playing_) {
        pauseBtn_->setCaption("Pause");
    }
    else {
        pauseBtn_->setCaption("Play");
    }
}


void GfxTest::UpdateSimulation(double dt)  {
    static const float radians_per_sec = GfxMath::ToRadians(25.0);
    if (playing_) {
        rot_matrix_ = rot_matrix_ * Matrix4::RotationY((float)dt * radians_per_sec);
    }
}


void GfxTest::InitOpenGL() {
    // Set up the camera in a good position to see the entire field
    proj_matrix_ = Matrix4::Perspective(60.0f, aspect_ratio(), 0.01f, 100.0f);
    view_matrix_ = Matrix4::LookAt(Point3(0,0,4), Point3(0,0,0), Vector3(0,1,0));
    glClearColor(0.0, 0.0, 0.0, 1);
    
    teapot_.LoadFromOBJ(Platform::FindMinGfxDataFile("teapot.obj"));
    shader_.Init();
}


void GfxTest::DrawUsingOpenGL() {
    for (int r=-2; r<3; r++) {
        for (int c=-2; c<3; c++) {
            Matrix4 model_matrix = Matrix4::Translation(Vector3((float)r, (float)c, 0.0f)) * rot_matrix_;
            shader_.Draw(model_matrix, view_matrix_, proj_matrix_, &teapot_, DefaultShader::MaterialProperties());
        }
    }
}