-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cpp
executable file
·179 lines (143 loc) · 4.05 KB
/
Application.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Application.cpp
// ogl4
//
// Created by Philipp Lensing on 16.09.16.
// Copyright © 2016 Philipp Lensing. All rights reserved.
//
#include "Application.h"
#ifdef WIN32
#include <GL/glew.h>
#include <glfw/glfw3.h>
#else
#define GLFW_INCLUDE_GLCOREARB
#define GLFW_INCLUDE_GLEXT
#include <glfw/glfw3.h>
#endif
#include "lineplanemodel.h"
#include "triangleplanemodel.h"
#include "trianglespheremodel.h"
#include "lineboxmodel.h"
#include "triangleboxmodel.h"
#include "model.h"
#include "scene.h"
#ifdef WIN32
#define ASSET_DIRECTORY "../../assets/"
#else
#define ASSET_DIRECTORY "../assets/"
#endif
Application::Application(GLFWwindow* pWin) : pWindow(pWin), Cam(pWin)
{
BaseModel* pModel;
ConstantShader* pConstShader;
PhongShader* pPhongShader;
// create LineGrid model with constant color shader
pModel = new LinePlaneModel(10, 10, 10, 10);
pConstShader = new ConstantShader();
pConstShader->color( Color(1,1,1));
pModel->shader(pConstShader, true);
Models.push_back( pModel );
/*
// Exercise 1
pTankBot = new Model(ASSET_DIRECTORY "tank_bottom.dae", false);
pTankBot->shader(new PhongShader(), true);
Models.push_back(pTankBot);
pTankTop = new Model(ASSET_DIRECTORY "tank_top.dae", false);
pTankTop->shader(new PhongShader(), true);
Models.push_back(pTankTop);
*/
// Exercise 2
pPhongShader = new PhongShader();
pTank = new Tank();
pTank->shader(pPhongShader, true);
pTank->loadModels(ASSET_DIRECTORY "tank_bottom.dae", ASSET_DIRECTORY "tank_top.dae");
Models.push_back( pTank );
// Exercise 3
/*
Scene* pScene = new Scene();
pScene->shader(new PhongShader(), true);
pScene->addSceneFile(ASSET_DIRECTORY "scene.osh");
Models.push_back(pScene);
*/
}
void Application::start()
{
glEnable (GL_DEPTH_TEST); // enable depth-testing
glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void Application::update()
{
/*
// Exercise 1
Matrix TM;
TM.translation(4,0,0);
Matrix RM_1;
Matrix RM_2;
Matrix RM_3;
RM_1.rotationY(-glfwGetTime());
RM_2.rotationY(-0.5f*3.14);
RM_3.rotationY(glfwGetTime());
Matrix RTM_bot = RM_1 * TM * RM_2;
Matrix TRM = RTM_bot * RM_3;
pTankBot->transform(RTM_bot);
pTankTop->transform(TRM);
*/
// Exercise 2
// TODO: add your code here
float fb = 0, lr = 0;
if (glfwGetKey(pWindow, GLFW_KEY_UP) == GLFW_PRESS) fb = 1.0f;
if (glfwGetKey(pWindow, GLFW_KEY_DOWN) == GLFW_PRESS) fb = -1.0f;
if (glfwGetKey(pWindow, GLFW_KEY_LEFT) == GLFW_PRESS) lr = 1.0f;
if (glfwGetKey(pWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) lr = -1.0f;
pTank->steer(fb, lr);
double xpos, ypos;
glfwGetCursorPos(pWindow, &xpos, &ypos);
Vector temp = Vector();
pTank->aim(
calc3DRay(xpos, ypos, temp)
);
pTank->update(glfwGetTime());
Cam.update();
}
Vector Application::calc3DRay( float x, float y, Vector& Pos)
{
int width, height;
glfwGetWindowSize(pWindow, &width, &height);
double xnorm = 2.0 / (width);
double ynorm = 2.0 / (height);
x = 1 - x * xnorm;
y = 1 - y * ynorm;
Matrix pMatrix = Cam.getProjectionMatrix();
Pos = Vector(x, y, 0);
Pos = pMatrix.invert() * Pos;
Matrix vMatrix = Cam.getViewMatrix();
vMatrix.transpose();
Pos = vMatrix * Pos;
float s = 0;
Cam.position().triangleIntersection(Pos, Vector(1, 0, 0), Vector(0, 0, 0), Vector(0, 0, 1), s);
Pos = Cam.position() + Pos * s;
return Pos; // dummy return value
}
void Application::draw()
{
// 1. clear screen
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 2. setup shaders and draw models
for( ModelList::iterator it = Models.begin(); it != Models.end(); ++it )
{
(*it)->draw(Cam);
}
// 3. check once per frame for opengl errors
GLenum Error = glGetError();
assert(Error==0);
}
void Application::end()
{
for( ModelList::iterator it = Models.begin(); it != Models.end(); ++it )
delete *it;
Models.clear();
}