Skip to content

Commit c08f5ca

Browse files
committed
Generate random texture for each asteroid.
1 parent 6d30fcf commit c08f5ca

File tree

7 files changed

+32
-9
lines changed

7 files changed

+32
-9
lines changed

OpenRocket/OpenRocket/Asteroid.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ glm::vec4 Asteroid::circleCenter = glm::vec4(0.0f);
66

77
Asteroid::Asteroid(float radius = 40.0f, int nrOfVertices = 16, glm::vec4 coordinates = glm::vec4(0.0f,0.0f, 0.0f, 0.0f),
88
glm::vec4 colors = glm::vec4(0.0f, 0.0f,0.0f,0.0f)) : radius(radius), nrOfVertices(nrOfVertices), coordinates(coordinates), colors(colors), translatedDistance(0), currentZone(Constants::SAFE) {
9+
textureIndex = rand() % Constants::nrOfTextures;
910
};
1011
bool Asteroid::isInViewport() {
1112
return this->coordinates.y - this->translatedDistance < Constants::height && !this->belowViewport();
@@ -27,6 +28,9 @@ void Asteroid::updateState() {
2728
Asteroid::setCurrentZone(Constants::RED);
2829
}
2930
}
31+
int Asteroid::getTextureIndex() {
32+
return this->textureIndex;
33+
}
3034
float Asteroid::getX() { return this->coordinates.x; }
3135
float Asteroid::getRealY() { return this->coordinates.y - this->translatedDistance; }
3236
void Asteroid::setX(float newXOffset) { this->coordinates.x = newXOffset; }

OpenRocket/OpenRocket/Asteroid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Asteroid {
2020
float radius;
2121
float translatedDistance;
2222
string currentZone;
23+
int textureIndex;
2324
public:
2425
glm::mat4 asteroidMatrix;
2526
static glm::vec4 circlePoint;
@@ -40,6 +41,7 @@ class Asteroid {
4041
float getY();
4142
float getRadius();
4243
float getTranslatedDistance();
44+
int getTextureIndex();
4345
glm::mat4 getAsteroidMatrix() { return asteroidMatrix; }
4446

4547
void setX(float);

OpenRocket/OpenRocket/Constants.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ const float Constants::maxYDistance = 4 * Constants::height;
1818
const std::string Constants::xCoord = "x";
1919
const std::string Constants::yCoord = "y";
2020
const std::string Constants::RED = "Red Zone";
21-
const std::string Constants::SAFE= "Safe Zone";
21+
const std::string Constants::SAFE= "Safe Zone";
22+
const std::vector<const char*> Constants::textureImages{
23+
"blueAsteroid.png",
24+
"brightOrangeAsteroid.png",
25+
"orangeAsteroid.png",
26+
"turquoiseAsteroid.png"
27+
};
28+
const int Constants::nrOfTextures = 4;

OpenRocket/OpenRocket/Constants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <string>
3+
#include <vector>
34

45
class Constants
56
{
@@ -23,4 +24,6 @@ class Constants
2324
static const std::string yCoord;
2425
static const std::string RED;
2526
static const std::string SAFE;
27+
static const std::vector<const char*> textureImages;
28+
static const int nrOfTextures;
2629
};

OpenRocket/OpenRocket/Game.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ void Game::InitializeGame(const char* vertShader, const char* fragShader) {
105105
myMatrix = resizeMatrix * matrTransl;
106106

107107
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
108-
LoadTexture(texture, "blueAsteroid.png");
108+
Game::loadTextures();
109109
}
110-
111110
void Game::FireAnimation() {
112111
if (fireTail >= resetTailEvery) {
113112
fireTail = resetTailEvery;
@@ -199,9 +198,9 @@ void Game::RenderFunction(void) {
199198
myMatrixLocation = glGetUniformLocation(TextureProgramId, "myMatrix");
200199

201200
glActiveTexture(GL_TEXTURE0);
202-
glBindTexture(GL_TEXTURE_2D, texture);
201+
203202
for (auto& asteroid : asteroids) {
204-
203+
glBindTexture(GL_TEXTURE_2D, textures[asteroid->getTextureIndex()]);
205204
glm::mat4 asteroidMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(asteroid->getRadius(), asteroid->getRadius(), 1.0));
206205
glm::mat4 animateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, - asteroid->getTranslatedDistance(), 0.0)); // controleaza translatia de-a lungul lui Oy
207206
backgroundMatrix = backgroundScaleMatrix * backgroundTranslateMatrix;
@@ -346,7 +345,7 @@ void Game::CreateAsteroidBuffers() {
346345
Vertices[9 * k + 5] = 0.0f;
347346
Vertices[9 * k + 6] = 0.0f;
348347

349-
cout << (float)k / 18 << " ";
348+
//cout << (float)k / 18 << " ";
350349
Vertices[9 * k + 7] = (sin(theta) + 1) / 2;
351350
Vertices[9 * k + 8] = (cos(theta) + 1)/ 2;
352351
}
@@ -436,4 +435,11 @@ void Game::LoadTexture(GLuint &texture, const char* imageName)
436435

437436
SOIL_free_image_data(image);
438437
glBindTexture(GL_TEXTURE_2D, 0);
438+
}
439+
void Game::loadTextures() {
440+
for (auto& imageName : Constants::textureImages) {
441+
GLuint texture;
442+
LoadTexture(texture, imageName);
443+
textures.push_back(texture);
444+
}
439445
}

OpenRocket/OpenRocket/Game.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using namespace std;
1616

17-
static GLuint texture;
17+
static vector<GLuint> textures;
1818

1919
class Game {
2020
private:
@@ -119,4 +119,5 @@ class Game {
119119
void FireAnimation();
120120
void RocketAsteroidCollision();
121121
void LoadTexture(GLuint&, const char*);
122+
void loadTextures();
122123
};

OpenRocket/OpenRocket/Rocket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Rocket* Rocket::instance = nullptr;
99

1010
void displayMatrix(glm::mat4 matrix)
1111
{
12-
cout << "intra aici";
12+
//cout << "intra aici";
1313
for (int ii = 0; ii < 4; ii++)
1414
{
1515
for (int jj = 0; jj < 4; jj++)
@@ -222,7 +222,7 @@ void Rocket::RocketAsteroidsCollision(vector<Asteroid*> asteroids)
222222
if (condition1 || condition2 || condition3 || condition4 || condition5 || condition6 ||
223223
condition7 || condition8 || condition9 || condition10 || condition11 || condition12 )
224224
{
225-
cout << "coliziunee\n";
225+
//cout << "coliziunee\n";
226226
}
227227
//cout << "dist " << dist(currentCenter, currentFrontTriangle.left, currentFrontTriangle.right) << "\n";
228228

0 commit comments

Comments
 (0)