Compare commits

...

2 Commits

Author SHA1 Message Date
Pawel Felcyn
90e88b0dc2 xd 2024-01-24 22:49:59 +01:00
Pawel Felcyn
7097be100e lower arrays sizes in shaders 2024-01-22 15:56:33 +01:00
24 changed files with 446 additions and 14 deletions

View File

@ -1,4 +1,5 @@
#include <list> #include <list>
#include <tuple>
#include "Sun.h" #include "Sun.h"
#include "Bullet.h" #include "Bullet.h"
#include "src/Shader_Loader.h" #include "src/Shader_Loader.h"
@ -12,10 +13,12 @@ private:
{ {
this->suns = new std::list<Sun*>(); this->suns = new std::list<Sun*>();
this->shaderLoader = new Core::Shader_Loader(); this->shaderLoader = new Core::Shader_Loader();
this->planetsTextures = new std::list<std::tuple<GLuint, GLuint>>();
} }
std::list<Sun*>* suns; std::list<Sun*>* suns;
std::list<Bullet*> bullets; std::list<Bullet*> bullets;
float aspectRatio; float aspectRatio;
std::list<std::tuple<GLuint, GLuint>>* planetsTextures;
public: public:
@ -35,6 +38,10 @@ public:
return suns; return suns;
} }
std::list<std::tuple<GLuint, GLuint>>* getPlanetsTextures() {
return planetsTextures;
}
static GameUtils* getInstance() static GameUtils* getInstance()
{ {
static GameUtils instance; // Jedna i jedyna instancja static GameUtils instance; // Jedna i jedyna instancja

View File

@ -2,6 +2,7 @@
#include "glm.hpp" #include "glm.hpp"
#include "ext.hpp" #include "ext.hpp"
#include "src/Render_Utils.h" #include "src/Render_Utils.h"
#include <tuple>
#pragma once #pragma once
class Planet : public GameObject class Planet : public GameObject
@ -14,15 +15,17 @@ private:
float scale; float scale;
glm::vec3 position; glm::vec3 position;
glm::mat4 positionMatrix; glm::mat4 positionMatrix;
std::tuple<GLuint, GLuint> texture;
public: public:
Planet(GameObject* center, float distanceFromCenter, float rotationSpeed, float scale, Core::RenderContext sphereContext) { Planet(GameObject* center, float distanceFromCenter, float rotationSpeed, float scale, Core::RenderContext sphereContext, std::tuple<GLuint, GLuint> texture) {
this->center = center; this->center = center;
this->distanceFromCenter = distanceFromCenter; this->distanceFromCenter = distanceFromCenter;
this->rotationSpeed = rotationSpeed; this->rotationSpeed = rotationSpeed;
this->sphereContext = sphereContext; this->sphereContext = sphereContext;
this->scale = scale; this->scale = scale;
this->position = glm::vec3(0); this->position = glm::vec3(0);
this->texture = texture;
} }
glm::mat4 getPositionMatrix() override { glm::mat4 getPositionMatrix() override {
@ -37,6 +40,6 @@ public:
float rotationAngle = glm::radians(time * rotationSpeed); float rotationAngle = glm::radians(time * rotationSpeed);
positionMatrix = center->getPositionMatrix() * glm::eulerAngleY(time * rotationSpeed) * glm::translate(glm::vec3(distanceFromCenter, 0, 0)); positionMatrix = center->getPositionMatrix() * glm::eulerAngleY(time * rotationSpeed) * glm::translate(glm::vec3(distanceFromCenter, 0, 0));
glm::mat4 modelMatrix = positionMatrix * glm::scale(glm::vec3(scale)); glm::mat4 modelMatrix = positionMatrix * glm::scale(glm::vec3(scale));
Core::drawObjectPBR(sphereContext, modelMatrix, glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); Core::drawObjectPBRTexture(sphereContext, modelMatrix, std::get<0>(texture), std::get<1>(texture), 0.3, 0.0, program);
} }
}; };

View File

@ -0,0 +1,123 @@
#version 430 core
float AMBIENT = 0.03;
float PI = 3.14;
uniform sampler2D depthMap;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
uniform vec3 lightPositions[100];
uniform vec3 lightColors[100];
uniform vec3 spotlightPos;
uniform vec3 spotlightColor;
uniform vec3 spotlightConeDir;
uniform vec3 spotlightPhi;
uniform float metallic;
uniform float roughness;
uniform float exposition;
in vec3 vecNormal;
in vec3 worldPos;
out vec4 outColor;
in vec3 viewDirTS;
in vec3 lightDirTS[4];
in vec3 spotlightDirTS;
in vec3 sunDirTS;
in vec2 vecTextCoord;
float DistributionGGX(vec3 normal, vec3 H, float roughness){
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(normal, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness){
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 normal, vec3 V, vec3 lightDir, float roughness){
float NdotV = max(dot(normal, V), 0.0);
float NdotL = max(dot(normal, lightDir), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0){
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V, vec3 colorTex){
float diffuse=max(0,dot(normal,lightDir));
vec3 F0 = vec3(0.04);
F0 = mix(F0, colorTex, metallic);
vec3 H = normalize(V + lightDir);
float NDF = DistributionGGX(normal, H, roughness);
float G = GeometrySmith(normal, V, lightDir, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
float NdotL = max(dot(normal, lightDir), 0.0);
return (kD * colorTex / PI + specular) * radiance * NdotL;
}
void main()
{
vec3 normal = normalize(vecNormal);
vec3 viewDir = normalize(cameraPos-worldPos);
vec3 lightDirs[100];
vec3 texColor = texture(colorTexture, vecTextCoord).rgb;
vec3 ambient = AMBIENT * texColor;
vec3 ilumination = ambient;
for (int i = 0; i < 100; ++i) {
lightDirs[i] = normalize(lightPositions[i] - worldPos);
vec3 attenuatedlightColor = lightColors[i] / pow(length(lightPositions[i] - worldPos), 2);
ilumination = ilumination+PBRLight(lightDirs[i], attenuatedlightColor * 300, normal, viewDir, texColor);
}
vec3 spotlightDir= normalize(spotlightPos-worldPos);
float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1);
vec3 attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2);
ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir, texColor);
outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1);
}

View File

@ -0,0 +1,46 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
layout(location = 3) in vec3 vertexTangent;
layout(location = 4) in vec3 vertexBitangent;
uniform mat4 transformation;
uniform mat4 modelMatrix;
out vec3 vecNormal;
out vec3 worldPos;
uniform vec3 lightsPositions[100];
uniform vec3 spotlightPos;
uniform vec3 cameraPos;
out vec3 viewDirTS;
out vec3 lightDirTS[100];
out vec3 spotlightDirTS;
out vec2 vecTextCoord;
void main()
{
worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz;
vecNormal = (modelMatrix* vec4(vertexNormal,0)).xyz;
gl_Position = transformation * vec4(vertexPosition, 1.0);
vec3 w_tangent = normalize(mat3(modelMatrix)*vertexTangent);
vec3 w_bitangent = normalize(mat3(modelMatrix)*vertexBitangent);
mat3 TBN = transpose(mat3(w_tangent, w_bitangent, vecNormal));
vec3 V = normalize(cameraPos-worldPos);
viewDirTS = TBN*V;
for (int i = 0; i < 100; ++i) {
vec3 L = normalize(lightsPositions[i]-worldPos);
lightDirTS[i] = TBN*L;
}
vec3 SL = normalize(spotlightPos-worldPos);
spotlightDirTS = TBN*SL;
vecTextCoord = vertexTexCoord;
}

View File

@ -45,6 +45,8 @@
<ClInclude Include="Sun.h" /> <ClInclude Include="Sun.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="draw_obj_text_pbr.frag" />
<None Include="draw_obj_text_pbr.vert" />
<None Include="fireball.frag" /> <None Include="fireball.frag" />
<None Include="fireball.vert" /> <None Include="fireball.vert" />
<None Include="shaders\shader_9_1.frag" /> <None Include="shaders\shader_9_1.frag" />
@ -55,6 +57,8 @@
<None Include="shaders\shader_skybox.vert" /> <None Include="shaders\shader_skybox.vert" />
<None Include="shaders\test.frag" /> <None Include="shaders\test.frag" />
<None Include="shaders\test.vert" /> <None Include="shaders\test.vert" />
<None Include="tex.frag" />
<None Include="tex.vert" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{5BACD057-4B83-4CB6-A367-40A10BCE2149}</ProjectGuid> <ProjectGuid>{5BACD057-4B83-4CB6-A367-40A10BCE2149}</ProjectGuid>

View File

@ -139,5 +139,19 @@
<None Include="shaders\shader_skybox.vert"> <None Include="shaders\shader_skybox.vert">
<Filter>Shader Files</Filter> <Filter>Shader Files</Filter>
</None> </None>
<None Include="fireball.frag" />
<None Include="fireball.vert" />
<None Include="draw_obj_text_pbr.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="draw_obj_text_pbr.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="tex.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="tex.frag">
<Filter>Shader Files</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,123 @@
#version 430 core
float AMBIENT = 0.03;
float PI = 3.14;
uniform sampler2D depthMap;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
uniform vec3 lightPositions[100];
uniform vec3 lightColors[100];
uniform vec3 spotlightPos;
uniform vec3 spotlightColor;
uniform vec3 spotlightConeDir;
uniform vec3 spotlightPhi;
uniform float metallic;
uniform float roughness;
uniform float exposition;
in vec3 vecNormal;
in vec3 worldPos;
out vec4 outColor;
in vec3 viewDirTS;
in vec3 lightDirTS[4];
in vec3 spotlightDirTS;
in vec3 sunDirTS;
in vec3 test;
float DistributionGGX(vec3 normal, vec3 H, float roughness){
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(normal, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness){
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 normal, vec3 V, vec3 lightDir, float roughness){
float NdotV = max(dot(normal, V), 0.0);
float NdotL = max(dot(normal, lightDir), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0){
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){
float diffuse=max(0,dot(normal,lightDir));
vec3 F0 = vec3(0.04);
F0 = mix(F0, texture(colorTexture, vec2(0.5, 0.5)).rgb, metallic);
vec3 H = normalize(V + lightDir);
float NDF = DistributionGGX(normal, H, roughness);
float G = GeometrySmith(normal, V, lightDir, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
float NdotL = max(dot(normal, lightDir), 0.0);
return (kD * texture(colorTexture, vec2(0.5, 0.5)).rgb / PI + specular) * radiance * NdotL;
}
void main()
{
vec3 normal = normalize(vecNormal);
vec3 viewDir = normalize(cameraPos-worldPos);
vec3 lightDirs[4];
vec3 ambient = AMBIENT * texture(colorTexture, vec2(0.5, 0.5)).rgb;
vec3 ilumination = ambient;
for (int i = 0; i < 100; ++i) {
lightDirs[i] = normalize(lightPositions[i] - worldPos);
vec3 attenuatedlightColor = lightColors[i] / pow(length(lightPositions[i] - worldPos), 2);
ilumination = ilumination+PBRLight(lightDirs[i], attenuatedlightColor * 300, normal, viewDir);
}
vec3 spotlightDir= normalize(spotlightPos-worldPos);
float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1);
vec3 attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2);
ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir);
outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1);
}

View File

@ -0,0 +1,43 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
layout(location = 3) in vec3 vertexTangent;
layout(location = 4) in vec3 vertexBitangent;
uniform mat4 transformation;
uniform mat4 modelMatrix;
out vec3 vecNormal;
out vec3 worldPos;
uniform vec3 lightsPositions[100];
uniform vec3 spotlightPos;
uniform vec3 cameraPos;
out vec3 viewDirTS;
out vec3 lightDirTS[100];
out vec3 spotlightDirTS;
void main()
{
worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz;
vecNormal = (modelMatrix* vec4(vertexNormal,0)).xyz;
gl_Position = transformation * vec4(vertexPosition, 1.0);
vec3 w_tangent = normalize(mat3(modelMatrix)*vertexTangent);
vec3 w_bitangent = normalize(mat3(modelMatrix)*vertexBitangent);
mat3 TBN = transpose(mat3(w_tangent, w_bitangent, vecNormal));
vec3 V = normalize(cameraPos-worldPos);
viewDirTS = TBN*V;
for (int i = 0; i < 100; ++i) {
vec3 L = normalize(lightsPositions[i]-worldPos);
lightDirTS[i] = TBN*L;
}
vec3 SL = normalize(spotlightPos-worldPos);
spotlightDirTS = TBN*SL;
}

View File

@ -9,8 +9,8 @@ uniform vec3 cameraPos;
uniform vec3 color; uniform vec3 color;
uniform vec3 lightPositions[100]; uniform vec3 lightPositions[8];
uniform vec3 lightColors[100]; uniform vec3 lightColors[8];
uniform vec3 spotlightPos; uniform vec3 spotlightPos;
uniform vec3 spotlightColor; uniform vec3 spotlightColor;
@ -29,7 +29,7 @@ out vec4 outColor;
in vec3 viewDirTS; in vec3 viewDirTS;
in vec3 lightDirTS[4]; in vec3 lightDirTS[8];
in vec3 spotlightDirTS; in vec3 spotlightDirTS;
in vec3 sunDirTS; in vec3 sunDirTS;
@ -99,11 +99,11 @@ void main()
vec3 viewDir = normalize(cameraPos-worldPos); vec3 viewDir = normalize(cameraPos-worldPos);
vec3 lightDirs[4]; vec3 lightDirs[8];
vec3 ambient = AMBIENT * color; vec3 ambient = AMBIENT * color;
vec3 ilumination = ambient; vec3 ilumination = ambient;
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 8; ++i) {
lightDirs[i] = normalize(lightPositions[i] - worldPos); lightDirs[i] = normalize(lightPositions[i] - worldPos);
vec3 attenuatedlightColor = lightColors[i] / pow(length(lightPositions[i] - worldPos), 2); vec3 attenuatedlightColor = lightColors[i] / pow(length(lightPositions[i] - worldPos), 2);
ilumination = ilumination+PBRLight(lightDirs[i], attenuatedlightColor * 300, normal, viewDir); ilumination = ilumination+PBRLight(lightDirs[i], attenuatedlightColor * 300, normal, viewDir);

View File

@ -12,12 +12,12 @@ uniform mat4 modelMatrix;
out vec3 vecNormal; out vec3 vecNormal;
out vec3 worldPos; out vec3 worldPos;
uniform vec3 lightsPositions[100]; uniform vec3 lightsPositions[8];
uniform vec3 spotlightPos; uniform vec3 spotlightPos;
uniform vec3 cameraPos; uniform vec3 cameraPos;
out vec3 viewDirTS; out vec3 viewDirTS;
out vec3 lightDirTS[100]; out vec3 lightDirTS[8];
out vec3 spotlightDirTS; out vec3 spotlightDirTS;
void main() void main()
@ -33,7 +33,7 @@ void main()
vec3 V = normalize(cameraPos-worldPos); vec3 V = normalize(cameraPos-worldPos);
viewDirTS = TBN*V; viewDirTS = TBN*V;
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 8; ++i) {
vec3 L = normalize(lightsPositions[i]-worldPos); vec3 L = normalize(lightsPositions[i]-worldPos);
lightDirTS[i] = TBN*L; lightDirTS[i] = TBN*L;
} }

View File

@ -9,6 +9,7 @@
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include "../GameUtils.h" #include "../GameUtils.h"
#include "../Spaceship.h" #include "../Spaceship.h"
#include "./Texture.h"
@ -168,6 +169,44 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl
std::list<Sun*>* suns = GameUtils::getInstance()->getSuns(); std::list<Sun*>* suns = GameUtils::getInstance()->getSuns();
glm::vec3 lightsPositions[8];
glm::vec3 lightsColors[8];
int i = 0;
for (Sun* sun : *suns) {
lightsPositions[i] = sun->getPosition();
lightsColors[i] = sun->getColor();
++i;
}
glUniform3fv(glGetUniformLocation(program, "lightPositions"), 8, glm::value_ptr(lightsPositions[0]));
glUniform3fv(glGetUniformLocation(program, "lightColors"), 8, glm::value_ptr(lightsColors[0]));
glUniform3f(glGetUniformLocation(program, "spotlightConeDir"), spaceship->spotlightConeDir.x, spaceship->spotlightConeDir.y, spaceship->spotlightConeDir.z);
glUniform3f(glGetUniformLocation(program, "spotlightPos"), spaceship->spotlightPos.x, spaceship->spotlightPos.y, spaceship->spotlightPos.z);
glUniform3f(glGetUniformLocation(program, "spotlightColor"), spaceship->spotlightColor.x, spaceship->spotlightColor.y, spaceship->spotlightColor.z);
glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship->spotlightPhi);
Core::DrawContext(context);
}
void Core::drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, GLuint normal, float roughness, float metallic, GLuint program) {
Spaceship* spaceship = Spaceship::getInstance();
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
Core::SetActiveTexture(texture, "colorTexture", program, 0);
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform1f(glGetUniformLocation(program, "exposition"), 1.f);
glUniform1f(glGetUniformLocation(program, "roughness"), roughness);
glUniform1f(glGetUniformLocation(program, "metallic"), metallic);
glUniform1i(glGetUniformLocation(program, "colorTexture"), 0);
glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship->cameraPos.x, spaceship->cameraPos.y, spaceship->cameraPos.z);
std::list<Sun*>* suns = GameUtils::getInstance()->getSuns();
glm::vec3 lightsPositions[100]; glm::vec3 lightsPositions[100];
glm::vec3 lightsColors[100]; glm::vec3 lightsColors[100];

View File

@ -73,5 +73,6 @@ namespace Core
glm::mat4 createPerspectiveMatrix(); glm::mat4 createPerspectiveMatrix();
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program);
void drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, GLuint normal, float roughness, float metallic, GLuint program);
void loadModelToContext(std::string path, Core::RenderContext& context); void loadModelToContext(std::string path, Core::RenderContext& context);
} }

View File

@ -68,7 +68,8 @@ GLuint Core::LoadCubemap(const std::vector<std::string>& faces)
void Core::SetActiveTexture(GLuint textureID, const char * shaderVariableName, GLuint programID, int textureUnit) void Core::SetActiveTexture(GLuint textureID, const char * shaderVariableName, GLuint programID, int textureUnit)
{ {
glUniform1i(glGetUniformLocation(programID, shaderVariableName), textureUnit); int location = glGetUniformLocation(programID, shaderVariableName);
glUniform1i(location, textureUnit);
glActiveTexture(GL_TEXTURE0 + textureUnit); glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, textureID); glBindTexture(GL_TEXTURE_2D, textureID);
} }

View File

@ -46,6 +46,7 @@ GLuint programSun;
GLuint programTest; GLuint programTest;
GLuint programTex; GLuint programTex;
GLuint programCubemap; GLuint programCubemap;
GLuint programTextPBR;
std::list<Planet*> planets; std::list<Planet*> planets;
Sun* sun; Sun* sun;
@ -61,6 +62,7 @@ float deltaTime = 0.f;
Spaceship* spaceship = Spaceship::getInstance(); Spaceship* spaceship = Spaceship::getInstance();
void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef); void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef);
void loadPlanetsTexturesWithNormals();
void updateDeltaTime(float time) { void updateDeltaTime(float time) {
if (lastTime < 0) { if (lastTime < 0) {
lastTime = time; lastTime = time;
@ -99,12 +101,14 @@ void renderScene(GLFWwindow* window)
sun->draw(); sun->draw();
} }
glUseProgram(program); glUseProgram(programTextPBR);
for (Planet* p : planets) { for (Planet* p : planets) {
p->draw(time, program); p->draw(time, programTextPBR);
} }
glUseProgram(program);
spaceship->renderBullets(glfwGetTime(), program); spaceship->renderBullets(glfwGetTime(), program);
//drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program);
@ -147,13 +151,21 @@ void createGalaxy(glm::vec3 galaxyPosition) {
createSolarSystem(galaxyPosition + glm::vec3(100, 20, -50), 5, planetsSizes3, 2, 25.f, 0.2f); createSolarSystem(galaxyPosition + glm::vec3(100, 20, -50), 5, planetsSizes3, 2, 25.f, 0.2f);
} }
std::tuple<GLuint, GLuint> getRandomPlanetTexture() {
std::list<std::tuple<GLuint, GLuint>>* planetsTextures = GameUtils::getInstance()->getPlanetsTextures();
std::srand(static_cast<unsigned int>(std::time(nullptr)));
int index = std::rand() % planetsTextures->size();
auto iterator = std::next(planetsTextures->begin(), 0);
return *iterator;
}
void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef) { void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef) {
GameUtils* gu = GameUtils::getInstance(); GameUtils* gu = GameUtils::getInstance();
sun = new Sun(programSun, models::sphereContext, sunPos, glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, sunScale); sun = new Sun(programSun, models::sphereContext, sunPos, glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, sunScale);
gu->getSuns()->push_back(sun); gu->getSuns()->push_back(sun);
for (int i = 0; i < numberOfPlanets; i++) { for (int i = 0; i < numberOfPlanets; i++) {
float distanceFromSum = (i + 1) * planetsDistance; float distanceFromSum = (i + 1) * planetsDistance;
Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext); Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext, getRandomPlanetTexture());
planets.push_back(planet); planets.push_back(planet);
} }
} }
@ -169,6 +181,7 @@ void init(GLFWwindow* window)
programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag"); programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag");
programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag");
programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programTextPBR = gameUtils->shaderLoader->CreateProgram("shaders/draw_obj_text_pbr.vert", "shaders/draw_obj_text_pbr.frag");
loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); loadModelToContext("./models/marbleBust.obj", models::marbleBustContext);
loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext);
@ -186,10 +199,25 @@ void init(GLFWwindow* window)
}; };
texture::cubemapTexture = Core::LoadCubemap(cubeFaces); texture::cubemapTexture = Core::LoadCubemap(cubeFaces);
loadPlanetsTexturesWithNormals();
createSuns(); createSuns();
} }
void loadPlanetsTexturesWithNormals()
{
std::list<std::tuple<GLuint, GLuint>>* planetsTextures = GameUtils::getInstance()->getPlanetsTextures();
std::tuple<GLuint, GLuint> p1 = std::tuple<GLuint, GLuint>(Core::LoadTexture("./textures/planets/lol.png"), Core::LoadTexture("./textures/planets/planet1normal.png"));
std::tuple<GLuint, GLuint> p2 = std::tuple<GLuint, GLuint>(Core::LoadTexture("./textures/planets/planet2.png"), Core::LoadTexture("./textures/planets/planet2normal.png"));
std::tuple<GLuint, GLuint> p3 = std::tuple<GLuint, GLuint>(Core::LoadTexture("./textures/planets/planet3.png"), Core::LoadTexture("./textures/planets/planet3normal.png"));
std::tuple<GLuint, GLuint> p4 = std::tuple<GLuint, GLuint>(Core::LoadTexture("./textures/planets/planet4.png"), Core::LoadTexture("./textures/planets/planet4normal.png"));
planetsTextures->push_back(p1);
planetsTextures->push_back(p2);
planetsTextures->push_back(p3);
planetsTextures->push_back(p4);
}
void shutdown(GLFWwindow* window) void shutdown(GLFWwindow* window)
{ {
GameUtils::getInstance()->shaderLoader->DeleteProgram(program); GameUtils::getInstance()->shaderLoader->DeleteProgram(program);

Binary file not shown.

After

Width:  |  Height:  |  Size: 452 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB