Add asteroids (something is wrong with them, but it compiles)

This commit is contained in:
xkamikoo 2021-02-15 23:15:35 +01:00
parent 0b24cbc7ae
commit 19d7977344
10 changed files with 3874 additions and 76 deletions

14
models/Asteroid_X.mtl Normal file
View File

@ -0,0 +1,14 @@
# Blender MTL File: 'Asteroid_Small_6X.blend'
# Material Count: 1
newmtl Aster_Small_5_
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd textures\\Aster_Small_5_Color.png
map_Bump textures\\Aster_Small_5_NM.png

3743
models/Asteroid_X.obj Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 MiB

View File

@ -54,7 +54,7 @@ void main()
vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist); vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist);
fragColor += texture*diffuse+specular; fragColor += texture*(diffuse+specular);
} }
BrightColor = vec4(0.0, 0.0, 0.0, 1.0); BrightColor = vec4(0.0, 0.0, 0.0, 1.0);

View File

@ -6,6 +6,7 @@ layout(location = 2) in vec3 vertexNormal;
layout (location = 3) in vec3 aTangent; layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent; layout (location = 4) in vec3 aBitangent;
layout (location = 5) in mat4 aInstanceMatrix; layout (location = 5) in mat4 aInstanceMatrix;
layout (location = 9) in mat3 normalMatrix;
struct PointLight { struct PointLight {
vec3 position; vec3 position;
@ -21,7 +22,6 @@ uniform vec3 cameraPos;
uniform PointLight pointLights[MAX_POINT_LIGHTS]; uniform PointLight pointLights[MAX_POINT_LIGHTS];
uniform int LightsCount; uniform int LightsCount;
out vec3 fragPos; out vec3 fragPos;
out vec2 vTexCoord; out vec2 vTexCoord;
out vec3 LightPosTS[MAX_POINT_LIGHTS]; out vec3 LightPosTS[MAX_POINT_LIGHTS];
@ -32,7 +32,7 @@ void main()
{ {
gl_Position = projection * view * aInstanceMatrix * vec4(vertexPosition, 1.0); gl_Position = projection * view * aInstanceMatrix * vec4(vertexPosition, 1.0);
mat3 normalMatrix = transpose(inverse(mat3(aInstanceMatrix))); //mat3 normalMatrix = transpose(inverse(mat3(aInstanceMatrix)));
vec3 T = normalize(normalMatrix * aTangent); vec3 T = normalize(normalMatrix * aTangent);
vec3 N = normalize(normalMatrix * vertexNormal); vec3 N = normalize(normalMatrix * vertexNormal);

View File

@ -54,7 +54,7 @@ void main()
vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist); vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist);
fragColor += texture*diffuse+specular; fragColor += texture*(diffuse+specular);
} }
BrightColor = vec4(0.0, 0.0, 0.0, 1.0); BrightColor = vec4(0.0, 0.0, 0.0, 1.0);

View File

@ -26,14 +26,14 @@ in vec2 vTexCoord;
void main() void main()
{ {
vec3 fragColor = vec3(0,0,0); vec3 fragColor = vec3(0,0,0);
vec4 textureColor = texture2D(diffuseTexture, vTexCoord); vec3 texture = texture2D(diffuseTexture, vTexCoord).rgb;
vec3 ambient = vec3(0.2) * textureColor.xyz; vec3 ambient = vec3(0.2) * texture;
vec3 normal = normalize(interpNormal); vec3 normal = normalize(interpNormal);
vec3 V = normalize(cameraPos-fragPos);
for(int i = 0; i < LightsCount; i++) for(int i = 0; i < LightsCount; i++)
{ {
vec3 lightDir = normalize(pointLights[i].position - fragPos); vec3 lightDir = normalize(pointLights[i].position - fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 R = reflect(-lightDir,normal); vec3 R = reflect(-lightDir,normal);
float dist = distance(fragPos, pointLights[i].position); float dist = distance(fragPos, pointLights[i].position);
@ -43,10 +43,9 @@ void main()
float diff = max(0,dot(normal,normalize(lightDir))); float diff = max(0,dot(normal,normalize(lightDir)));
vec3 diffuse = pointLights[i].color * diff * distance * pointLights[i].intensity; vec3 diffuse = pointLights[i].color * diff * distance * pointLights[i].intensity;
vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/(dist*2)); vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/(dist*5));
vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z); // * pointLights[i].color; fragColor += texture*(diffuse+specular);
fragColor += texture*diffuse+vec3(1)*specular;
} }
BrightColor = vec4(0.0, 0.0, 0.0, 1.0); BrightColor = vec4(0.0, 0.0, 0.0, 1.0);

View File

@ -25,7 +25,9 @@ int SCR_HEIGHT = 720;
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
int winId; int winId;
Core::Shader_Loader shaderLoader;
//shader programs
GLuint programTex; GLuint programTex;
GLuint programSkybox; GLuint programSkybox;
GLuint programSun; GLuint programSun;
@ -51,12 +53,10 @@ GLuint particles_position_buffer;
GLuint particles_color_buffer; GLuint particles_color_buffer;
bool bothEngines = true; bool bothEngines = true;
Core::Shader_Loader shaderLoader; //textures
int asteroidAmount = 100;
GLuint sunTexture; GLuint sunTexture;
GLuint earthTexture; GLuint earthTexture;
GLuint marsTexture;
GLuint moonTexture; GLuint moonTexture;
GLuint skyboxTexture; GLuint skyboxTexture;
GLuint particleTexture; GLuint particleTexture;
@ -66,11 +66,16 @@ std::shared_ptr<Model> cube;
std::shared_ptr<Model> sphere; std::shared_ptr<Model> sphere;
std::shared_ptr<Model> corvette; std::shared_ptr<Model> corvette;
std::shared_ptr<Model> asteroid; std::shared_ptr<Model> asteroid;
//std::vector<Core::RenderContext> corvetteMeshes;
std::shared_ptr<Model> crewmate; std::shared_ptr<Model> crewmate;
//asteroids
GLuint bufferAsteroids;
int asteroidAmount = 100;
//cameraPos int engineLightTimer = 50;
float frustumScale = 1.f;
//camera
float cameraAngle = 0; float cameraAngle = 0;
glm::vec3 cameraPos = glm::vec3(-6, 0, 0); glm::vec3 cameraPos = glm::vec3(-6, 0, 0);
glm::vec3 cameraDir; glm::vec3 cameraDir;
@ -92,16 +97,6 @@ struct Particle {
return this->cameradistance > that.cameradistance; return this->cameradistance > that.cameradistance;
} }
}; };
struct Object
{
glm::mat4 modelM;
glm::mat4 invModelM;
std::shared_ptr<Model> modelParent;
GLuint textureID;
GLuint shaderID;
glm::vec3 color;
};
const int MaxParticles = 1000; const int MaxParticles = 1000;
Particle ParticlesContainer[MaxParticles]; Particle ParticlesContainer[MaxParticles];
@ -130,6 +125,17 @@ int FindUnusedParticle() {
return 0; // All particles are taken, override the first one return 0; // All particles are taken, override the first one
} }
struct Object
{
glm::mat4 modelM;
glm::mat4 invModelM;
std::shared_ptr<Model> modelParent;
GLuint textureID;
GLuint shaderID;
glm::vec3 color;
};
//Light //Light
struct Light { struct Light {
glm::vec3 position; glm::vec3 position;
@ -137,8 +143,17 @@ struct Light {
float intensity; float intensity;
}; };
int engineLightTimer = 50; struct Asteroid
{
glm::mat4 model;
glm::mat3 inv;
};
//vectors
std::vector<Object> objects;
std::vector<Light> lights;
std::vector<Asteroid> asteroids;
std::vector<glm::mat4> asteroidsMatrixes;
//wczytywanie skyboxa (musi byc jpg!) //wczytywanie skyboxa (musi byc jpg!)
std::vector<std::string> faces std::vector<std::string> faces
{ {
@ -150,9 +165,7 @@ std::vector<std::string> faces
"skybox/back.jpg" "skybox/back.jpg"
}; };
std::vector<Object> objects;
std::vector<Light> lights;
std::vector<glm::mat4> asteroids;
void keyboard(unsigned char key, int x, int y) void keyboard(unsigned char key, int x, int y)
{ {
@ -221,6 +234,53 @@ void renderQuad()
glBindVertexArray(0); glBindVertexArray(0);
} }
glm::mat4 orbitAsteroids(float time, glm::vec3 objectPos, glm::mat4 asteroidModelMatrix, glm::vec3 orbit);
void updateAsteroid()
{
for (int i=0; i<asteroids.size();i++)
{
asteroids[i].model = orbitAsteroids(lastTime, sunPos, asteroidsMatrixes[i], glm::vec3(0.0f, 1.0f, 0.0f));
asteroids[i].inv = glm::transpose(glm::inverse(glm::mat3(asteroids[i].model)));
}
glBindBuffer(GL_ARRAY_BUFFER, bufferAsteroids);
glBufferData(GL_ARRAY_BUFFER, asteroidAmount * sizeof(Asteroid), &asteroids[0], GL_DYNAMIC_DRAW);
for (unsigned int i = 0; i < asteroid->meshes.size(); i++)
{
unsigned int VAO = asteroid->meshes[i].VAO;
glBindVertexArray(VAO);
// set attribute pointers for matrix (4 times vec4)
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)0);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)(sizeof(glm::vec4)));
glEnableVertexAttribArray(7);
glVertexAttribPointer(7, 4, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)(2 * sizeof(glm::vec4)));
glEnableVertexAttribArray(8);
glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)(3 * sizeof(glm::vec4)));
glEnableVertexAttribArray(9);
glVertexAttribPointer(9, 3, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)(4 * sizeof(glm::vec4)));
glEnableVertexAttribArray(10);
glVertexAttribPointer(10, 3, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)(4 * sizeof(glm::vec4) + sizeof(glm::vec3)));
glEnableVertexAttribArray(11);
glVertexAttribPointer(11, 3, GL_FLOAT, GL_FALSE, sizeof(Asteroid), (void*)(4 * sizeof(glm::vec4) + 2 * sizeof(glm::vec3)));
glVertexAttribDivisor(5, 1);
glVertexAttribDivisor(6, 1);
glVertexAttribDivisor(7, 1);
glVertexAttribDivisor(8, 1);
glVertexAttribDivisor(9, 1);
glVertexAttribDivisor(10, 1);
glVertexAttribDivisor(11, 1);
glBindVertexArray(0);
}
}
glm::mat4 createCameraMatrix() glm::mat4 createCameraMatrix()
{ {
// Obliczanie kierunku patrzenia kamery (w plaszczyznie x-z) przy uzyciu zmiennej cameraAngle kontrolowanej przez klawisze. // Obliczanie kierunku patrzenia kamery (w plaszczyznie x-z) przy uzyciu zmiennej cameraAngle kontrolowanej przez klawisze.
@ -231,7 +291,7 @@ glm::mat4 createCameraMatrix()
return Core::createViewMatrix(cameraPos, cameraDir, up); return Core::createViewMatrix(cameraPos, cameraDir, up);
} }
float frustumScale = 1.f;
//funkcja rysujaca modele za pomoca assimpa //funkcja rysujaca modele za pomoca assimpa
void drawFromAssimpModel(GLuint program, std::shared_ptr<Model> model, glm::mat4 modelMatrix) void drawFromAssimpModel(GLuint program, std::shared_ptr<Model> model, glm::mat4 modelMatrix)
@ -248,6 +308,7 @@ void drawFromAssimpModel(GLuint program, std::shared_ptr<Model> model, glm::mat4
glUseProgram(0); glUseProgram(0);
} }
//funkcja rysujaca modele, ktore nie maja wlasnej tekstury za pomoca assimpa
void drawFromAssimpTexture(GLuint program, std::shared_ptr<Model> model, glm::mat4 modelMatrix, GLuint texID) void drawFromAssimpTexture(GLuint program, std::shared_ptr<Model> model, glm::mat4 modelMatrix, GLuint texID)
{ {
glUseProgram(program); glUseProgram(program);
@ -288,6 +349,15 @@ void drawAsteroids()
asteroid->DrawInstances(programAsteroid, asteroidAmount); asteroid->DrawInstances(programAsteroid, asteroidAmount);
} }
glm::mat4 orbitAsteroids(float time, glm::vec3 objectPos, glm::mat4 asteroidModelMatrix, glm::vec3 orbit)
{
glm::mat4 orbitModelMatrix = glm::translate(objectPos);
orbitModelMatrix = glm::rotate(orbitModelMatrix, time / 100, orbit);
//orbitModelMatrix = glm::translate(asteroidModelMatrix, -objectPos) * orbitModelMatrix;
return orbitModelMatrix * asteroidModelMatrix;
}
//Skybox //Skybox
unsigned int loadCubemap(std::vector<std::string> faces) unsigned int loadCubemap(std::vector<std::string> faces)
{ {
@ -489,12 +559,10 @@ void renderScene()
glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
drawFromAssimpModel(programTex, crewmate, crewmateModelMatrix); drawFromAssimpModel(programTex, crewmate, crewmateModelMatrix);
//rysowanie Ziemi z ksiezycem
drawFromAssimpTexture(programTex, sphere, earth, earthTexture); drawFromAssimpTexture(programTex, sphere, earth, earthTexture);
drawFromAssimpTexture(programTex, sphere, moon, moonTexture); drawFromAssimpTexture(programTex, sphere, moon, moonTexture);
drawFromAssimpTexture(programTex, sphere, planet1, moonTexture); drawFromAssimpTexture(programTex, sphere, planet1, marsTexture);
glUseProgram(programNormal); glUseProgram(programNormal);
@ -535,6 +603,7 @@ void renderScene()
} }
glUniform3f(glGetUniformLocation(programAsteroid, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); glUniform3f(glGetUniformLocation(programAsteroid, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
updateAsteroid();
drawAsteroids(); drawAsteroids();
//particlepart //particlepart
@ -545,10 +614,8 @@ void renderScene()
lastTime = time; lastTime = time;
glm::mat4 transformation = perspectiveMatrix * cameraMatrix; glm::mat4 transformation = perspectiveMatrix * cameraMatrix;
int newparticles = 0; int newparticles = 0;
if (engineLightTimer < 30) if (engineLightTimer < 30)
{ {
engineLightTimer++; engineLightTimer++;
@ -654,8 +721,8 @@ void renderScene()
void initAsteroids() void initAsteroids()
{ {
int amount = asteroidAmount; int amount = asteroidAmount;
float radius = 15.0; float radius = 7.0;
float offset = 25.0f; float offset = 2.0f;
for (int i=0; i < amount; i++) for (int i=0; i < amount; i++)
{ {
@ -666,7 +733,7 @@ void initAsteroids()
float x = sin(angle) * radius + displacement; float x = sin(angle) * radius + displacement;
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset; displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
float y = displacement * 0.4f; float y = displacement * 0.1f;
displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset; displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
float z = cos(angle) * radius + displacement; float z = cos(angle) * radius + displacement;
@ -677,36 +744,15 @@ void initAsteroids()
float rotAngle = (rand() % 360); float rotAngle = (rand() % 360);
model = glm::rotate(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f)); model = glm::rotate(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f));
Asteroid obj;
asteroids.push_back(model); obj.model = model;
obj.inv = glm::transpose(glm::inverse(glm::mat3(model)));
asteroidsMatrixes.push_back(model);
asteroids.push_back(obj);
} }
unsigned int buffer; glGenBuffers(1, &bufferAsteroids);
glGenBuffers(1, &buffer); updateAsteroid();
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &asteroids[0], GL_STATIC_DRAW);
for (unsigned int i = 0; i < asteroid->meshes.size(); i++)
{
unsigned int VAO = asteroid->meshes[i].VAO;
glBindVertexArray(VAO);
// set attribute pointers for matrix (4 times vec4)
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(glm::vec4)));
glEnableVertexAttribArray(7);
glVertexAttribPointer(7, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(2 * sizeof(glm::vec4)));
glEnableVertexAttribArray(8);
glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(3 * sizeof(glm::vec4)));
glVertexAttribDivisor(5, 1);
glVertexAttribDivisor(6, 1);
glVertexAttribDivisor(7, 1);
glVertexAttribDivisor(8, 1);
glBindVertexArray(0);
}
} }
void initParticles() void initParticles()
@ -839,19 +885,15 @@ void init()
corvette = std::make_shared<Model>("models/Corvette-F3.obj"); corvette = std::make_shared<Model>("models/Corvette-F3.obj");
crewmate = std::make_shared<Model>("models/space_humster.obj"); crewmate = std::make_shared<Model>("models/space_humster.obj");
asteroid = std::make_shared<Model>("models/Asteroid_4_LOW_MODEL_.obj"); asteroid = std::make_shared<Model>("models/Asteroid_X.obj");
//shipModel = obj::loadModelFromFile("models/spaceship.obj");
//sphereModel = obj::loadModelFromFile("models/sphere.obj");
sphere = std::make_shared<Model>("models/sphere.obj"); sphere = std::make_shared<Model>("models/sphere.obj");
cube = std::make_shared<Model>("models/cube.obj"); cube = std::make_shared<Model>("models/cube.obj");
//sphereContext.initFromOBJ(sphereModel);
//cubeContext.initFromOBJ(cubeModel);
//shipContext.initFromOBJ(shipModel);
sunTexture = Core::LoadTexture("textures/sun.png"); sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png"); earthTexture = Core::LoadTexture("textures/earth2.png");
moonTexture = Core::LoadTexture("textures/moon.png"); moonTexture = Core::LoadTexture("textures/moon.png");
particleTexture = Core::LoadTexture("textures/sun.png"); particleTexture = Core::LoadTexture("textures/sun.png");
marsTexture = Core::LoadTexture("models/textures/Mars/2k_mars.png");
skyboxTexture = loadCubemap(faces); skyboxTexture = loadCubemap(faces);
@ -863,13 +905,13 @@ void init()
Light l1; Light l1;
l1.position = sunPos; l1.position = sunPos;
l1.color = glm::vec3(0.8f, 0.8f, 0.7f); l1.color = glm::vec3(0.8f, 0.8f, 0.7f);
l1.intensity = 25; l1.intensity = 105;
lights.push_back(l1); lights.push_back(l1);
Light l2; Light l2;
l2.position = sunPos2; l2.position = sunPos2;
l2.color = glm::vec3(0.5f, 0.5f, 0.5f); l2.color = glm::vec3(0.5f, 0.5f, 0.5f);
l2.intensity = 15; l2.intensity = 55;
lights.push_back(l2); lights.push_back(l2);
Light l3; Light l3;