forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallRenderer.cpp
More file actions
211 lines (170 loc) · 7.62 KB
/
Copy pathBallRenderer.cpp
File metadata and controls
211 lines (170 loc) · 7.62 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "BallRenderer.h"
void BallRenderer::renderBalls(Bengine::SpriteBatch& spriteBatch, const std::vector<Ball>& balls,
const glm::mat4& projectionMatrix) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Lazily initialize the program
if (m_program == nullptr) {
m_program = std::make_unique<Bengine::GLSLProgram>();
m_program->compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag");
m_program->addAttribute("vertexPosition");
m_program->addAttribute("vertexColor");
m_program->addAttribute("vertexUV");
m_program->linkShaders();
}
m_program->use();
spriteBatch.begin();
// Make sure the shader uses texture 0
glActiveTexture(GL_TEXTURE0);
GLint textureUniform = m_program->getUniformLocation("mySampler");
glUniform1i(textureUniform, 0);
// Grab the camera matrix
GLint pUniform = m_program->getUniformLocation("P");
glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]);
// Render all the balls
for (auto& ball : balls) {
const glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f);
const glm::vec4 destRect(ball.position.x - ball.radius, ball.position.y - ball.radius,
ball.radius * 2.0f, ball.radius * 2.0f);
spriteBatch.draw(destRect, uvRect, ball.textureId, 0.0f, ball.color);
}
spriteBatch.end();
spriteBatch.renderBatch();
m_program->unuse();
}
void MomentumBallRenderer::renderBalls(Bengine::SpriteBatch& spriteBatch, const std::vector<Ball>& balls,
const glm::mat4& projectionMatrix) {
glClearColor(0.0f, 0.1f, 0.5f, 1.0f);
// Lazily initialize the program
if (m_program == nullptr) {
m_program = std::make_unique<Bengine::GLSLProgram>();
m_program->compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag");
m_program->addAttribute("vertexPosition");
m_program->addAttribute("vertexColor");
m_program->addAttribute("vertexUV");
m_program->linkShaders();
}
m_program->use();
spriteBatch.begin();
// Make sure the shader uses texture 0
glActiveTexture(GL_TEXTURE0);
GLint textureUniform = m_program->getUniformLocation("mySampler");
glUniform1i(textureUniform, 0);
// Grab the camera matrix
GLint pUniform = m_program->getUniformLocation("P");
glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]);
// Render all the balls
for (auto& ball : balls) {
const glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f);
const glm::vec4 destRect(ball.position.x - ball.radius, ball.position.y - ball.radius,
ball.radius * 2.0f, ball.radius * 2.0f);
Bengine::ColorRGBA8 color;
GLubyte colorVal = (GLubyte)(glm::clamp(glm::length(ball.velocity) * ball.mass * 12.0f, 0.0f, 255.0f));
color.r = colorVal;
color.g = colorVal;
color.b = colorVal;
color.a = colorVal;
spriteBatch.draw(destRect, uvRect, ball.textureId, 0.0f, color);
}
spriteBatch.end();
spriteBatch.renderBatch();
m_program->unuse();
}
VelocityBallRenderer::VelocityBallRenderer(int screenWidth, int screenHeight) :
m_screenWidth(screenWidth),
m_screenHeight(screenHeight) {
// Empty
}
void VelocityBallRenderer::renderBalls(Bengine::SpriteBatch& spriteBatch, const std::vector<Ball>& balls,
const glm::mat4& projectionMatrix) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Lazily initialize the program
if (m_program == nullptr) {
m_program = std::make_unique<Bengine::GLSLProgram>();
m_program->compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag");
m_program->addAttribute("vertexPosition");
m_program->addAttribute("vertexColor");
m_program->addAttribute("vertexUV");
m_program->linkShaders();
}
m_program->use();
spriteBatch.begin();
// Make sure the shader uses texture 0
glActiveTexture(GL_TEXTURE0);
GLint textureUniform = m_program->getUniformLocation("mySampler");
glUniform1i(textureUniform, 0);
// Grab the camera matrix
GLint pUniform = m_program->getUniformLocation("P");
glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]);
// Render all the balls
for (auto& ball : balls) {
const glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f);
const glm::vec4 destRect(ball.position.x - ball.radius, ball.position.y - ball.radius,
ball.radius * 2.0f, ball.radius * 2.0f);
Bengine::ColorRGBA8 color;
float mult = 100.0f;
GLubyte colorVal = (GLubyte)(glm::clamp(ball.velocity.x * mult, 0.0f, 255.0f));
color.r = 128;
color.g = (GLubyte)((ball.position.x / m_screenWidth) * 255.0f);
color.b = (GLubyte)((ball.position.y / m_screenHeight) * 255.0f);
color.a = colorVal;
spriteBatch.draw(destRect, uvRect, ball.textureId, 0.0f, color);
}
spriteBatch.end();
spriteBatch.renderBatch();
m_program->unuse();
}
TrippyBallRenderer::TrippyBallRenderer(int screenWidth, int screenHeight) :
m_screenWidth(screenWidth),
m_screenHeight(screenHeight) {
// Empty
}
void TrippyBallRenderer::renderBalls(Bengine::SpriteBatch& spriteBatch, const std::vector<Ball>& balls, const glm::mat4& projectionMatrix) {
glClearColor(0.1f, 0.0f, 0.0f, 1.0f);
// Lazily initialize the program
if (m_program == nullptr) {
m_program = std::make_unique<Bengine::GLSLProgram>();
m_program->compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag");
m_program->addAttribute("vertexPosition");
m_program->addAttribute("vertexColor");
m_program->addAttribute("vertexUV");
m_program->linkShaders();
}
m_program->use();
spriteBatch.begin();
// Make sure the shader uses texture 0
glActiveTexture(GL_TEXTURE0);
GLint textureUniform = m_program->getUniformLocation("mySampler");
glUniform1i(textureUniform, 0);
// Grab the camera matrix
GLint pUniform = m_program->getUniformLocation("P");
glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]);
// Change these constants to get cool stuff
float TIME_SPEED = 0.01f;
float DIVISOR = 4.0f; // Increase to get more arms
float SPIRAL_INTENSITY = 10.0f; // Increase to make it spiral more
m_time += TIME_SPEED;
// Render all the balls
for (auto& ball : balls) {
const glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f);
const glm::vec4 destRect(ball.position.x - ball.radius, ball.position.y - ball.radius,
ball.radius * 2.0f, ball.radius * 2.0f);
Bengine::ColorRGBA8 color;
// Get vector from center point
glm::vec2 centerVec = ball.position - glm::vec2(m_screenWidth / 2, m_screenHeight / 2);
float centerDist = glm::length(centerVec);
// Get angle from the horizontal
float angle = atan2(centerVec.x, centerVec.y) / (3.1415926f / DIVISOR);
// Move with time
angle -= m_time;
// Add the spiral
angle += (centerDist / m_screenWidth) * SPIRAL_INTENSITY;
color.r = (GLubyte)(angle * 255.0f);
color.g = (GLubyte)(angle * 255.0f * cos(m_time));
color.b = (GLubyte)(angle * 255.0f * sin(m_time));
color.a = (GLubyte)(glm::clamp(1.0f - (centerDist / (m_screenWidth / 2.0f)), 0.0f, 1.0f) * 255.0f);
spriteBatch.draw(destRect, uvRect, ball.textureId, 0.0f, color);
}
spriteBatch.end();
spriteBatch.renderBatch();
m_program->unuse();
}