-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (147 loc) · 5.72 KB
/
main.cpp
File metadata and controls
174 lines (147 loc) · 5.72 KB
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
//
// Created by Ayush Kumar on 27/09/19.
// Copyright © 2018 Ayush Kumar. All rights reserved.
//
#include <Headers/App.h>
//model loading library
#include <Headers/objloader.hpp>
#include <Headers/shader.h>
#include <Headers/Font.h>
#include <Headers/Gamestate.h>
#include <Headers/glm_includes.h>
#include <stb_image/stb_image.h>
#include <Headers/Environment.h>
#include <iostream>
#include <vector>
#include <cassert>
int main() {
App();
GameState* gamestate = new GameState();
Environment::loadVertexData();
Environment* env = new Environment();
Font* arial_font = new Font("Fonts/TimesNewRomanBold.ttf");
//App::gamestate = gamestate;
//building shaders
Shader pooltable_diffuse_shader("shaders/pooltable_diffuse.vs", "shaders/pooltable_diffuse.fs");
Shader ball_diffuse_shader("shaders/ball_diffuse.vs", "shaders/ball_diffuse.fs");
Shader axes_shader("shaders/axes.vs", "shaders/axes.fs");
Shader env_shader("shaders/cubemap.vs", "shaders/cubemap.fs");
Shader text_shader("shaders/text.vs", "shaders/text.fs");
App::updateFrame();
int iters = 0;
//gamestate->cuestick->model = glm::translate(glm::vec3(0.1, 0.5, 0.1)) * gamestate->cuestick->model;
while (!glfwWindowShouldClose(App::window)) {
//process input
App::updateFrame();
//if (gamestate->simulation_complete) {
App::processInput();
//}
App::clearColor();
iters++;
if (iters == 2) {
//takes arbitrarily long time in the 2nd iteration of the loop
//std::cout << App::DELTA_TIME << std::endl;
continue;
}
//std::cout << "entered main loop" << std::endl;
//std::cout << "Delta time : " << App::DELTA_TIME << std::endl;
//std::cout << "theta: " << App::CAMERA.theta << std::endl;
//create transform matrices
glm::mat4 view = App::getViewMatrix();
glm::mat4 ortho_projection = App::getOrthographicProjectionMatrix();
glm::mat4 persp_projection = App::getPerspectiveProjectionMatrix();
//remove translation to create a fake_view matrix
glm::mat4 fake_view = glm::mat4(glm::mat3(view));
env_shader.use();
env_shader.setMat4("model", env->getModelMatrix());
env_shader.setMat4("view", fake_view);
env_shader.setMat4("projection", persp_projection);
env->render(&env_shader);
axes_shader.use();
axes_shader.setMat4("view", view);
axes_shader.setMat4("projection", persp_projection);
//draw axes
//App::drawAxes();
gamestate->pooltable->drawBoundary();
if (gamestate->simulation_complete) {
gamestate->renderAim();
//render fakeball
}
pooltable_diffuse_shader.use();
pooltable_diffuse_shader.setMat4("model", gamestate->pooltable->getModelMatrix());
pooltable_diffuse_shader.setMat4("view", view);
pooltable_diffuse_shader.setMat4("projection", persp_projection);
pooltable_diffuse_shader.setVec3("glightpos", glm::vec3(3,4,5));
gamestate->pooltable->render(&pooltable_diffuse_shader);
if (gamestate->simulation_complete) {
gamestate->setCueStick();
glm::vec3 force_dirn = gamestate->cuestick->aim;
force_dirn[2] = 0.0f;
glm::vec3 force = 25.0f * (1.0f - gamestate->cuestick->animate_factor) * force_dirn;
if (App::HIT) {
//std::cout << "HIT : " << gamestate->cuestick->animate_factor << std::endl;
gamestate->simulation_complete = false;
assert(gamestate->rigidbodies[0]);
gamestate->rigidbodies[0]->setLinearVelocity(rp3d::Vector3(force[0], force[1], force[2]));
gamestate->rigidbodies[0]->setAngularVelocity(rp3d::Vector3(0.0, 0.0, 10.0));
App::HIT = false;
//Apply force to the cue
}
float radius = App::getMouseDrag();
if (radius != -1) gamestate->cuestick->animate(radius);
else gamestate->cuestick->unanimate();
pooltable_diffuse_shader.setMat4("model", gamestate->cuestick->getModelMatrix());
//renderAim();
//it should be line till the first intersection with a ball/wall and at that point render a small fake ball
gamestate->cuestick->render(&pooltable_diffuse_shader);
}
//after the simulation has completed for this frame, update the positions
//and transformations of the balls
gamestate->simulate();
if (!gamestate->simulation_complete) {
gamestate->updateState();
}
if (gamestate->simulation_complete && iters > 2) {
gamestate->performBallCheck();
}
ball_diffuse_shader.use();
ball_diffuse_shader.setMat4("view", view);
ball_diffuse_shader.setMat4("projection", persp_projection);
ball_diffuse_shader.setVec3("glightpos", glm::vec3(3,4,5));
for(int i = 0; i < 16; i++) {
ball_diffuse_shader.setMat4("model", gamestate->balls[i]->getModelMatrix());
if (!gamestate->balls[i]->scored) {
gamestate->balls[i]->render(&ball_diffuse_shader);
}
}
if (gamestate->simulation_complete) {
//render fakeball
ball_diffuse_shader.setMat4("model", gamestate->fakeball->getModelMatrix());
//switch to wireframe mode
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
gamestate->fakeball->render(&ball_diffuse_shader);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
//render scores
text_shader.use();
text_shader.setMat4("projection", ortho_projection);
//render players' scores
glm::vec3 color[2] = {glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f)};
std::cout << gamestate->turn << std::endl;
if (gamestate->turn) {
std::swap(color[0], color[1]);
}
text_shader.setVec3("text_color", color[0]);
arial_font->render(&text_shader, std::string("Player 0 : ") + std::to_string(gamestate->score[0]), 25.0f, 570.0f, 0.4f);
text_shader.setVec3("text_color", color[1]);
arial_font->render(&text_shader, std::string("Player 1 : ") + std::to_string(gamestate->score[1]), 540.0f, 570.0f, 0.4f);
//accesorial actions
App::endFrame();
}
glfwTerminate();
//deallocate everything that was dynamically created (using the new keyword);
delete gamestate;
delete env;
delete arial_font;
return 0;
}