Skip to content

Commit 6d30fcf

Browse files
committed
Add functional draft of textured asteroids.
1 parent f9bae09 commit 6d30fcf

File tree

14 files changed

+835
-136
lines changed

14 files changed

+835
-136
lines changed

OpenRocket/OpenRocket/Game.cpp

Lines changed: 106 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "helpers.h"
44
#include "Constants.h"
55
#include "Asteroid.h"
6-
6+
#include "SOIL.h"
7+
#include "Rocket.h"
78
#include <GL/glew.h>
89
#include <GL/freeglut.h>
910

@@ -13,7 +14,6 @@ void Game::move(void)
1314
float translatedDistance = asteroid->getTranslatedDistance();
1415
asteroid->setTranslatedDistance(translatedDistance + Constants::asteroidMovingUnit);
1516
}
16-
glutPostRedisplay();
1717
}
1818
void Game::mouseHandler(int button, int state, int x, int y)
1919
{
@@ -50,28 +50,6 @@ Game::Game(int initial_pos_x, int initial_pos_y) :
5050
maxX = width / 2;
5151
maxY = height / 2;
5252

53-
//// Triunghiul de sus
54-
//775.f, 160.f, 0.f, 1.f,
55-
//825.f, 160.f, 0.f, 1.f,
56-
//800.f, 185.f, 0.f, 1.f,
57-
58-
//// Triunghiul de jos
59-
//775.f, 100.f, 0.f, 1.f,
60-
//825.f, 100.f, 0.f, 1.f,
61-
//800.f, 165.f, 0.f, 1.f,
62-
63-
//// Deptunghiul
64-
//790.f, 100.f, 0.f, 1.f, // stanga jos
65-
//810.f, 100.f, 0.f, 1.f, // dr jos
66-
//810.f, 175.f, 0.f, 1.f,
67-
//790.f, 175.f, 0.f, 1.f,
68-
69-
//// Triunghiul din varf
70-
//790.f, 175.f, 0.f, 1.f,
71-
//810.f, 175.f, 0.f, 1.f,
72-
//800.f, 210.f, 0.f, 1.f,
73-
74-
7553
InitializeGlew();
7654
CreateBackgroundBuffers();
7755
CreateRocketBuffers();
@@ -93,6 +71,7 @@ void Game::InitializeGlew() {
9371

9472
void Game::CreateShaders(const char* vertShader, const char* fragShader) {
9573
ProgramId = LoadShaders(vertShader, fragShader);
74+
TextureProgramId = LoadShaders("Texturare_Shader.vert", "Texturare_Shader.frag");
9675
glUseProgram(ProgramId);
9776
}
9877

@@ -126,6 +105,7 @@ void Game::InitializeGame(const char* vertShader, const char* fragShader) {
126105
myMatrix = resizeMatrix * matrTransl;
127106

128107
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
108+
LoadTexture(texture, "blueAsteroid.png");
129109
}
130110

131111
void Game::FireAnimation() {
@@ -156,12 +136,12 @@ void Game::FireAnimation() {
156136
float rightSideX = 775 + 50.f + fireSides;
157137
float rightSideY = 85.f - fireSides;
158138
float yellowTail = 70.f + fireTail;
159-
glNamedBufferSubData(rocketVbo, 15 * 4 * sizeof(GLfloat), sizeof(GLfloat), &rightSideX);
160-
glNamedBufferSubData(rocketVbo, (15 * 4 + 1) * sizeof(GLfloat), sizeof(GLfloat), &rightSideY);
161-
glNamedBufferSubData(rocketVbo, 16 * 4 * sizeof(GLfloat) + 1 * sizeof(GLfloat), sizeof(GLfloat), &orangeTail);
162-
glNamedBufferSubData(rocketVbo, 17 * 4 * sizeof(GLfloat), sizeof(GLfloat), &leftSideX);
163-
glNamedBufferSubData(rocketVbo, (17 * 4 + 1) * sizeof(GLfloat), sizeof(GLfloat), &leftSideY);
164-
glNamedBufferSubData(rocketVbo, 21 * 4 * sizeof(GLfloat) + 1 * sizeof(GLfloat), sizeof(GLfloat), &yellowTail);
139+
glNamedBufferSubData(rocketVbo, 15 * 7 * sizeof(GLfloat), sizeof(GLfloat), &rightSideX);
140+
glNamedBufferSubData(rocketVbo, (15 * 7 + 1) * sizeof(GLfloat), sizeof(GLfloat), &rightSideY);
141+
glNamedBufferSubData(rocketVbo, (16 * 7 + 1) * sizeof(GLfloat), sizeof(GLfloat), &orangeTail);
142+
glNamedBufferSubData(rocketVbo, 17 * 7 * sizeof(GLfloat), sizeof(GLfloat), &leftSideX);
143+
glNamedBufferSubData(rocketVbo, (17 * 7 + 1) * sizeof(GLfloat), sizeof(GLfloat), &leftSideY);
144+
glNamedBufferSubData(rocketVbo, (21 * 7 + 1) * sizeof(GLfloat), sizeof(GLfloat), &yellowTail);
165145
}
166146

167147
void Game::RenderFunction(void) {
@@ -172,6 +152,7 @@ void Game::RenderFunction(void) {
172152
if (rotationAngle > 360.f) {
173153
rotationAngle = 0.0f;
174154
}
155+
glUseProgram(ProgramId);
175156

176157
rotationAngle += rotationSpeed;
177158

@@ -182,7 +163,6 @@ void Game::RenderFunction(void) {
182163

183164
myMatrixLocation = glGetUniformLocation(ProgramId, "myMatrix");
184165
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &backgroundMatrix[0][0]);
185-
186166
glPointSize(2.0);
187167
glBindVertexArray(backgroundVao);
188168
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, backgroundEbo);
@@ -212,21 +192,27 @@ void Game::RenderFunction(void) {
212192
glDrawArrays(GL_POLYGON, 13, 5);
213193
glDrawArrays(GL_POLYGON, 18, 5);
214194

195+
196+
215197
Game::UpdateAsteroids();
198+
glUseProgram(TextureProgramId);
199+
myMatrixLocation = glGetUniformLocation(TextureProgramId, "myMatrix");
200+
201+
glActiveTexture(GL_TEXTURE0);
202+
glBindTexture(GL_TEXTURE_2D, texture);
216203
for (auto& asteroid : asteroids) {
217204

218-
219205
glm::mat4 asteroidMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(asteroid->getRadius(), asteroid->getRadius(), 1.0));
220206
glm::mat4 animateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, - asteroid->getTranslatedDistance(), 0.0)); // controleaza translatia de-a lungul lui Oy
221207
backgroundMatrix = backgroundScaleMatrix * backgroundTranslateMatrix;
222208
asteroidMatrix = backgroundMatrix * animateMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(asteroid->getX(), asteroid->getY(), 0.0)) * asteroidMatrix;
223209
asteroid->setAsteroidMatrix(asteroidMatrix);
224-
225210
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &asteroidMatrix[0][0]);
226211
glBindVertexArray(asteroidVao);
227212
glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle);
228213

229214
}
215+
glDisable(GL_TEXTURE_2D);
230216

231217
rocket->RocketAsteroidsCollision(asteroids);
232218
glutPostRedisplay();
@@ -237,9 +223,6 @@ bool colliding(Rocket rocket, Asteroid asteroid)
237223
{
238224
return false;
239225
}
240-
241-
242-
243226
void Game::CreateBackgroundBuffers() {
244227

245228
GLfloat Vertices[10000];
@@ -285,140 +268,113 @@ void Game::CreateRocketBuffers() {
285268
Rocket* rocket = Rocket::getInstance();
286269
GLfloat Vertices[] = {
287270
// Triunghiul de sus
288-
rocket->topWingsTriangle.left[0], rocket->topWingsTriangle.left[1], 0.f, 1.f,
289-
rocket->topWingsTriangle.right[0], rocket->topWingsTriangle.right[1], 0.f, 1.f,
290-
rocket->topWingsTriangle.top[0], rocket->topWingsTriangle.top[1], 0.f, 1.f,
271+
rocket->topWingsTriangle.left[0], rocket->topWingsTriangle.left[1], 0.f, 1.f, 1.0f, 0.0f, 0.0f,
272+
rocket->topWingsTriangle.right[0], rocket->topWingsTriangle.right[1], 0.f, 1.f, 1.0f, 0.0f, 0.0f,
273+
rocket->topWingsTriangle.top[0], rocket->topWingsTriangle.top[1], 0.f, 1.f, 1.0f, 0.0f, 0.0f,
291274

292275
// Triunghiul de jos
293-
rocket->bottomWingsTriangle.left[0], rocket->bottomWingsTriangle.left[1], 0.f, 1.f,
294-
rocket->bottomWingsTriangle.right[0], rocket->bottomWingsTriangle.right[1], 0.f, 1.f,
295-
rocket->bottomWingsTriangle.top[0], rocket->bottomWingsTriangle.top[1], 0.f, 1.f,
276+
rocket->bottomWingsTriangle.left[0], rocket->bottomWingsTriangle.left[1], 0.f, 1.f, 1.0f, 0.0f, 0.0f,
277+
rocket->bottomWingsTriangle.right[0], rocket->bottomWingsTriangle.right[1], 0.f, 1.f, 1.0f, 0.0f, 0.0f,
278+
rocket->bottomWingsTriangle.top[0], rocket->bottomWingsTriangle.top[1], 0.f, 1.f, 1.0f, 0.0f, 0.0f,
296279

297280
// Dreptunghiul
298-
rocket->body.bottomLeft[0], rocket->body.bottomLeft[1], 0.f, 1.f,
299-
rocket->body.bottomRight[0], rocket->body.bottomRight[1], 0.f, 1.f,
300-
rocket->body.topRight[0], rocket->body.topRight[1], 0.f, 1.f,
301-
rocket->body.topLeft[0], rocket->body.topLeft[1], 0.f, 1.f,
281+
rocket->body.bottomLeft[0], rocket->body.bottomLeft[1], 0.f, 1.f, 0.8f, 0.5f, 0.2f,
282+
rocket->body.bottomRight[0], rocket->body.bottomRight[1], 0.f, 1.f, 0.8f, 0.5f, 0.2f,
283+
rocket->body.topRight[0], rocket->body.topRight[1], 0.f, 1.f, 0.8f, 0.5f, 0.2f,
284+
rocket->body.topLeft[0], rocket->body.topLeft[1], 0.f, 1.f, 0.8f, 0.5f, 0.2f,
302285

303286
// Triunghiul din varf
304-
rocket->frontTriangle.left[0], rocket->frontTriangle.left[1], 0.f, 1.f,
305-
rocket->frontTriangle.right[0], rocket->frontTriangle.right[1], 0.f, 1.f,
306-
rocket->frontTriangle.top[0], rocket->frontTriangle.top[1], 0.f, 1.f,
307-
// Focul portocaliu
308-
775 + 15.f, 100.f, 0.f, 1.f,
309-
775 + 35.f, 100.f, 0.f, 1.f,
310-
775 + 50.f, 85.f, 0.f, 1.f, // dreapta
311-
775 + 25.f, 50.f, 0.f, 1.f, // varful de jos
312-
775 + 0.f, 85.f, 0.f, 1.f, // stanga
313-
314-
// Focul galben
315-
775 + 15.f, 100.f, 0.f, 1.f,
316-
775 + 35.f, 100.f, 0.f, 1.f,
317-
775 + 40.f, 90.f, 0.f, 1.f,
318-
775 + 25.f, 70.f, 0.f, 1.f, // varful de jos
319-
775 + 10.f, 90.f, 0.f, 1.f,
320-
321-
};
322-
323-
GLfloat Colors[] = {
324-
// Triunghiul de sus
325-
1.0f, 0.0f, 0.0f, 0.f,
326-
1.0f, 0.0f, 0.0f, 0.f,
327-
1.0f, 0.0f, 0.0f, 0.f,
328-
329-
// Triunghiul de jos
330-
1.0f, 0.0f, 0.0f, 0.f,
331-
1.0f, 0.0f, 0.0f, 0.f,
332-
1.0f, 0.0f, 0.0f, 0.f,
333-
334-
// Dreptunghiul
335-
0.8f, 0.5f, 0.2f, 0.f,
336-
0.8f, 0.5f, 0.2f, 0.f,
337-
0.8f, 0.5f, 0.2f, 0.f,
338-
0.8f, 0.5f, 0.2f, 0.f,
339-
340-
// Triunghiul din varf
341-
1.0f, 1.0f, 1.0f, 0.f,
342-
1.0f, 1.0f, 1.0f, 0.f,
343-
1.0f, 1.0f, 1.0f, 0.f,
344-
287+
rocket->frontTriangle.left[0], rocket->frontTriangle.left[1], 0.f, 1.f, 1.0f, 1.0f, 1.0f,
288+
rocket->frontTriangle.right[0], rocket->frontTriangle.right[1], 0.f, 1.f, 1.0f, 1.0f, 1.0f,
289+
rocket->frontTriangle.top[0], rocket->frontTriangle.top[1], 0.f, 1.f, 1.0f, 1.0f, 1.0f,
290+
345291
// Focul portocaliu
346-
1.f, 0.25f, 0.0f, 1.0f,
347-
1.f, 0.25f, 0.0f, 1.0f,
348-
1.f, 0.25f, 0.0f, 1.0f,
349-
1.f, 0.25f, 0.0f, 1.0f,
350-
1.f, 0.25f, 0.0f, 1.0f,
292+
775 + 15.f, 100.f, 0.f, 1.f, 1.f, 0.25f, 0.0f,
293+
775 + 35.f, 100.f, 0.f, 1.f, 1.f, 0.25f, 0.0f,
294+
775 + 50.f, 85.f, 0.f, 1.f, 1.f, 0.25f, 0.0f,
295+
775 + 25.f, 50.f, 0.f, 1.f, 1.f, 0.25f, 0.0f,
296+
775 + 0.f, 85.f, 0.f, 1.f, 1.f, 0.25f, 0.0f,
351297

352298
// Focul galben
353-
1.0f, 0.8f, 0.0f, 1.0f,
354-
1.0f, 0.8f, 0.0f, 1.0f,
355-
1.0f, 0.8f, 0.0f, 1.0f,
356-
1.0f, 0.8f, 0.0f, 1.0f,
357-
1.0f, 0.8f, 0.0f, 1.0f
299+
775 + 15.f, 100.f, 0.f, 1.f, 1.0f, 0.8f, 0.0f,
300+
775 + 35.f, 100.f, 0.f, 1.f, 1.0f, 0.8f, 0.0f,
301+
775 + 40.f, 90.f, 0.f, 1.f, 1.0f, 0.8f, 0.0f,
302+
775 + 25.f, 70.f, 0.f, 1.f, 1.0f, 0.8f, 0.0f,
303+
775 + 10.f, 90.f, 0.f, 1.f, 1.0f, 0.8f, 0.0f,
358304
};
359305

360-
int verticesCount = sizeof(Vertices) / sizeof(GLfloat);
361-
306+
// se creeaza un VAO (Vertex Array Object) - util cand se utilizeaza mai multe VBO
307+
glGenVertexArrays(1, &rocketVao);
308+
// se creeaza un buffer nou (atribute)
362309
glGenBuffers(1, &rocketVbo);
310+
311+
// legarea VAO
312+
glBindVertexArray(rocketVao);
313+
314+
// legarea buffer-ului "Array"
363315
glBindBuffer(GL_ARRAY_BUFFER, rocketVbo);
316+
// punctele sunt "copiate" in bufferul curent
364317
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
365318

366-
glGenVertexArrays(1, &rocketVao);
367-
glBindVertexArray(rocketVao);
368-
369-
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
319+
// se activeaza lucrul cu atribute; atributul 0 = pozitie
370320
glEnableVertexAttribArray(0);
321+
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (GLvoid*)0);
371322

372-
glGenBuffers(1, &rocketColorBufferId);
373-
glBindBuffer(GL_ARRAY_BUFFER, rocketColorBufferId);
374-
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
375-
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
323+
// se activeaza lucrul cu atribute; atributul 1 = culoare
376324
glEnableVertexAttribArray(1);
325+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (GLvoid*)(4 * sizeof(GLfloat)));
326+
377327
}
378328
void Game::CreateAsteroidBuffers() {
379329

380330
GLfloat Vertices[1000];
381-
GLfloat Colors[1000];
382331
for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) {
383332
float theta = Constants::TWO_PI * k / Constants::nrOfVerticesPerCircle;
384-
cout << "angle " << float(Constants::TWO_PI * float(k)) / float(Constants::nrOfVerticesPerCircle) << "\n";
333+
//cout << "angle " << float(Constants::TWO_PI * float(k)) / float(Constants::nrOfVerticesPerCircle) << "\n";
385334
float x = cos(theta);
386335
float y = sin(theta);
387336
Asteroid::circleCenter = { 0.0f, 0.0f, 0.0f, 1.0f };
388337
Asteroid::circlePoint = { cos(theta), sin(theta), 0.0f, 1.0f };
389338
// varfurile corespunzatoare cercului
390-
Vertices[4 * k] = x;
391-
Vertices[4 * k + 1] = y;
392-
Vertices[4 * k + 2] = 0.0f;
393-
Vertices[4 * k + 3] = 1.0f;
394-
cout << 4*k << " " << Vertices[4 * k] << " " << Vertices[4 * k + 1] << " " << Vertices[4 * k + 2] << " " << Vertices[4 * k + 3] << "\n";
395-
396-
Colors[4 * k] = 1.0f;
397-
Colors[4 * k + 1] = 0.0f;
398-
Colors[4 * k + 2] = 0.0f;
399-
Colors[4 * k + 3] = 1.0f;
339+
Vertices[9 * k] = x;
340+
Vertices[9 * k + 1] = y;
341+
Vertices[9 * k + 2] = 0.0f;
342+
Vertices[9 * k + 3] = 1.0f;
343+
//cout << 4*k << " " << Vertices[4 * k] << " " << Vertices[4 * k + 1] << " " << Vertices[4 * k + 2] << " " << Vertices[4 * k + 3] << "\n";
344+
345+
Vertices[9 * k + 4] = 1.0f;
346+
Vertices[9 * k + 5] = 0.0f;
347+
Vertices[9 * k + 6] = 0.0f;
348+
349+
cout << (float)k / 18 << " ";
350+
Vertices[9 * k + 7] = (sin(theta) + 1) / 2;
351+
Vertices[9 * k + 8] = (cos(theta) + 1)/ 2;
400352
}
401-
for (int i = 0; i < Constants::nrOfVerticesPerCircle * 4 - 4; i+=4) {
402-
//cout << Vertices[i] << " " << Vertices[i + 1] << " " << Vertices[i + 2] << " " << Vertices[i + 3] << "\n";
403-
}
404-
int verticesCount = sizeof(Vertices) / sizeof(GLfloat);
405-
353+
354+
// se creeaza un VAO (Vertex Array Object) - util cand se utilizeaza mai multe VBO
355+
glGenVertexArrays(1, &asteroidVao);
356+
// se creeaza un buffer nou (atribute)
406357
glGenBuffers(1, &asteroidVbo);
407-
glBindBuffer(GL_ARRAY_BUFFER, asteroidVbo);
408-
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
409358

410-
glGenVertexArrays(1, &asteroidVao);
359+
// legarea VAO
411360
glBindVertexArray(asteroidVao);
412361

413-
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
362+
// legarea buffer-ului "Array"
363+
glBindBuffer(GL_ARRAY_BUFFER, asteroidVbo);
364+
// punctele sunt "copiate" in bufferul curent
365+
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
366+
367+
// se activeaza lucrul cu atribute; atributul 0 = pozitie
414368
glEnableVertexAttribArray(0);
369+
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)0);
415370

416-
glGenBuffers(1, &asteroidColorBufferId);
417-
glBindBuffer(GL_ARRAY_BUFFER, asteroidColorBufferId);
418-
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
419-
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
371+
// se activeaza lucrul cu atribute; atributul 1 = culoare
420372
glEnableVertexAttribArray(1);
373+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)(4 * sizeof(GLfloat)));
421374

375+
// se activeaza lucrul cu atribute; atributul 2 = coordonate de texturare
376+
glEnableVertexAttribArray(2);
377+
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)(7 * sizeof(GLfloat)));
422378
}
423379
void Game::UpdateAsteroids() {
424380
auto end = std::remove_if(asteroids.begin(),
@@ -462,4 +418,22 @@ void Game::GenerateAsteroids(int nrOfAsteroids) {
462418
for (int i = 0; i < nrOfAsteroids; i++) {
463419
this->asteroids.push_back(Game::GenerateSingleAsteroid());
464420
}
421+
}
422+
void Game::LoadTexture(GLuint &texture, const char* imageName)
423+
{
424+
glGenTextures(1, &texture);
425+
glBindTexture(GL_TEXTURE_2D, texture);
426+
427+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
428+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
429+
430+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
431+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
432+
int width, height;
433+
unsigned char* image = SOIL_load_image(imageName, &width, &height, 0, SOIL_LOAD_RGB);
434+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
435+
glGenerateMipmap(GL_TEXTURE_2D);
436+
437+
SOIL_free_image_data(image);
438+
glBindTexture(GL_TEXTURE_2D, 0);
465439
}

0 commit comments

Comments
 (0)