star and hearts positions update, and object Color shader update

This commit is contained in:
mxsgd 2024-02-07 23:01:55 +01:00
parent f110bd68f0
commit 9afb118663
4 changed files with 159 additions and 55 deletions

View File

@ -60,14 +60,14 @@
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings"> <ImportGroup Label="ExtensionSettings">

View File

@ -1,18 +1,38 @@
#version 430 core #version 430 core
float AMBIENT = 0.1; float AMBIENT = 0.2;
uniform vec3 color; uniform vec3 color;
uniform float metalness;
uniform float roughness;
uniform vec3 lightPos; uniform vec3 lightPos;
uniform vec3 lightColor;
in vec3 vecNormal; in vec3 viewDirTS;
in vec3 lightDirTS;
in vec3 worldPos; in vec3 worldPos;
out vec4 outColor; out vec4 outColor;
in mat3 TBN;
void main() void main()
{ {
vec3 lightDir = normalize(lightPos-worldPos); vec3 normal = normalize(TBN * vec3(0, 0, 1)); // Normal z TBN
vec3 normal = normalize(vecNormal); vec3 L = normalize(lightPos - worldPos);
float diffuse=max(0,dot(normal,lightDir)); vec3 V = normalize(viewDirTS);
outColor = vec4(color*min(1,AMBIENT+diffuse), 1.0); vec3 H = normalize(L + V);
float k = pow((roughness + 1), 2.0) / 8.0;
float D = (roughness * roughness) / (3.14159 * pow(pow(dot(normal, H), 2.0) * (roughness * roughness - 1.0) + 1.0, 2.0));
float G = dot(normal, L) / (dot(normal, L) * (1.0 - k) + k);
vec3 F0 = mix(vec3(0.04), color, metalness);
vec3 F = F0 + (1.0 - F0) * pow(1 - dot(V, H), 5.0);
vec3 specular = lightColor * (D * G * F) / (4 * dot(normal, L) * dot(normal, V) + 0.00001);
vec3 kD = vec3(1.0) - F;
vec3 diffuse = lightColor * kD * (color / 3.1458493);
vec3 finalColor = (diffuse + specular) * min(1.0, AMBIENT + max(dot(normal, L), 0.0));
outColor = vec4(finalColor, 1.0);
} }

View File

@ -2,17 +2,40 @@
layout(location = 0) in vec3 vertexPosition; layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal; 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 transformation;
uniform mat4 modelMatrix; uniform mat4 modelMatrix;
out vec3 vecNormal;
out vec3 worldPos; out vec3 worldPos;
out vec2 vecTex;
uniform vec3 lightPos;
uniform vec3 cameraPos;
out vec3 viewDirTS;
out vec3 lightDirTS;
out mat3 TBN;
void main() void main()
{ {
worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz; vec3 tangent = normalize(mat3(modelMatrix) * vertexTangent);
vecNormal = (modelMatrix* vec4(vertexNormal,0)).xyz; vec3 bitangent = normalize(mat3(modelMatrix) * vertexBitangent);
vec3 normal = normalize(mat3(modelMatrix) * vertexNormal);
mat3 TBN = transpose(mat3(tangent, bitangent, normal));
vec3 worldPos = (modelMatrix * vec4(vertexPosition, 1)).xyz;
vec3 viewDir = normalize(cameraPos - worldPos);
vec3 lightDir = normalize(lightPos - worldPos);
viewDirTS = vec3(TBN * viewDir);
lightDirTS = vec3(TBN * lightDir);
vecTex = vec2((worldPos.x + 10.0) / 20.0, 1.0 - (worldPos.y + 10.0) / 20.0);
gl_Position = transformation * vec4(vertexPosition, 1.0); gl_Position = transformation * vec4(vertexPosition, 1.0);
} }

View File

@ -73,7 +73,6 @@ glm::vec3 spaceshipDir = glm::vec3(1.f, 0.f, 0.f);
bool fire = false; bool fire = false;
float a = 3; float a = 3;
std::random_device rd; std::random_device rd;
std::mt19937 gen(rd()); std::mt19937 gen(rd());
//std::uniform_real_distribution<float> distribution(-0.5f, 0.5f); //std::uniform_real_distribution<float> distribution(-0.5f, 0.5f);
@ -89,7 +88,10 @@ glm::vec3 distance = asteroid_Pos - spaceshipPos;
double step = 0.0000001; double step = 0.0000001;
GLuint VAO,VBO; GLuint VAO,VBO;
float lastAsteroidTime = 0; float lastFiretime = 0;
float ammoReloadTime = 3.0f; // Czas ładowania amunicji w sekundach
float ammoReloadProgress = 0.0f;
float aspectRatio = 1.f; float aspectRatio = 1.f;
glm::vec3 ammoPos; glm::vec3 ammoPos;
unsigned int textureID; unsigned int textureID;
@ -99,13 +101,20 @@ float tiltAngleUpDown;
int colission = 3; int colission = 3;
int star = 0; int star = 0;
int star_counter = 0; int star_counter = 1;
float starMetalness = 0.8;
float starRoughness = 0.1;
glm::vec3 lightPos = glm::vec3(-8, 4, 2);
glm::vec3 lightColor = glm::vec3(0.9, 0.7, 0.8) * 100;
glm::vec3 lightDir = glm::vec3(0, 0, 0);
float spotlightPhi = 3.14 / 3;
double easeInExpo(double x) { double easeInExpo(double x) {
return pow(2, 10 * x - 10); return pow(2, 10 * x - 10);
} }
glm::mat4 createCameraMatrix() glm::mat4 createCameraMatrix()
{ {
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f))); glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f)));
@ -143,17 +152,25 @@ glm::mat4 createPerspectiveMatrix()
return perspectiveMatrix; return perspectiveMatrix;
} }
void drawObjectColor(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color) { void drawObjectColor(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float metalness, float roughness, glm::vec3 lightstarPos) {
glUseProgram(program); glUseProgram(program);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z); glUniform3f(glGetUniformLocation(program, "lightPos"), lightstarPos.x, lightstarPos.y, lightstarPos.z);
glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0);
Core::DrawContext(context);
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "lightColor"), lightColor.r, lightColor.g, lightColor.b);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUniform1f(glGetUniformLocation(program, "metalness"), metalness);
glUniform1f(glGetUniformLocation(program, "roughness"), roughness);
// Przesyłanie informacji o widoku (view) do shadera
Core::DrawContext(context);
} }
void drawObjectTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint normalMapId, GLuint metalnessTexture, GLuint roughnessTexture) { void drawObjectTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint normalMapId, GLuint metalnessTexture, GLuint roughnessTexture) {
@ -196,6 +213,44 @@ void drawObjectSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
} }
void updateAmmoReload() {
float deltaTime = static_cast<float>(glfwGetTime() - lastFiretime);
if (fire) {
ammoReloadProgress += 1.0f / ammoReloadTime * deltaTime;
if (ammoReloadProgress >= 1.0f) {
ammoReloadProgress = 1.0f;
fire = false;
}
}
else {
ammoReloadProgress = fmaxf(0.0f, ammoReloadProgress - 1.0f / ammoReloadTime * deltaTime);
}
}
void drawAmmoReloadBar() {
glUseProgram(program);
float barWidth = 0.01f;
float barHeight = 0.02f;
float barOffsetX = spaceshipPos.x-0.1f;
float barOffsetY = spaceshipPos.y -0.1f;
float barOffsetZ = spaceshipPos.z;
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 barModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(barOffsetX, barOffsetY, barOffsetZ));
barModelMatrix = glm::scale(barModelMatrix, glm::vec3(barWidth, barHeight, ammoReloadProgress/5));
glm::mat4 transformation = viewProjectionMatrix * barModelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&barModelMatrix);
glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 1.0f, 0.0f);
glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0);
Core::DrawContext(cubeContext);
glUseProgram(0);
}
void generateAsteroids(glm::vec3 asteroid_Pos, glm::vec3 distance, double step) { void generateAsteroids(glm::vec3 asteroid_Pos, glm::vec3 distance, double step) {
glm::vec3 normalizedDir = glm::normalize(distance); glm::vec3 normalizedDir = glm::normalize(distance);
@ -225,16 +280,21 @@ void generatePlanetoidBelt() {
float pScale = planetoidsArray[i][2]; float pScale = planetoidsArray[i][2];
bool collision = false; bool collision = false;
planetoidsArray[i][3] -= speed; planetoidsArray[i][3] -= speed;
float x = planetoidsArray[i][3];
if (planetoidsArray[i][3] < -3.f) {
planetoidsArray[i][0] += spaceshipPos.z; // Płynne przesunięcie na nową pozycję
planetoidsArray[i][1] += spaceshipPos.y;
planetoidsArray[i][3] = 10.f;
planetoidsArray[i][4] == 0;
}
if (planetoidsArray[i][4] == 1) { if (planetoidsArray[i][4] == 1) {
// Planeta ju¿ uczestniczy³a w kolizji, przejdŸ do kolejnej iteracji // Planeta ju<EFBFBD> uczestniczy<7A>a w kolizji, przejd<6A> do kolejnej iteracji
continue; continue;
} }
if (planetoidsArray[i][3] < -3.f) {
planetoidsArray[i][3] = 10.f;
}
float x = planetoidsArray[i][3];
for (int j = 0; j < i; ++j) { for (int j = 0; j < i; ++j) {
float prevZ = planetoidsArray[j][0]; float prevZ = planetoidsArray[j][0];
@ -256,8 +316,8 @@ void generatePlanetoidBelt() {
if (!collision) { if (!collision) {
if( fmod(i, starind) == 0) { if( fmod(i, starind) == 0) {
if (checkCollision(glm::vec3(x, y, z), 0.1f, spaceshipPos, 0.025f,true)) { if (checkCollision(glm::vec3(x, y, z), 0.1f, spaceshipPos, 0.025f,true)) {
// Kolizja z gwiazd¹ // Kolizja z gwiazd<EFBFBD>
std::cout << "Collision with star " << i << std::endl; //std::cout << "Collision with star " << i << std::endl;
planetoidsArray[i][4] = 1; planetoidsArray[i][4] = 1;
star_counter++; star_counter++;
if (star_counter == 3){ if (star_counter == 3){
@ -290,7 +350,7 @@ void generatePlanetoidBelt() {
if (fmod(i, starind) == 0) { if (fmod(i, starind) == 0) {
float time = glfwGetTime(); float time = glfwGetTime();
glm::mat4 modelMatrix = glm::translate(glm::vec3(x, y, z)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 1.0f)); glm::mat4 modelMatrix = glm::translate(glm::vec3(x, y, z)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 1.0f));
drawObjectColor(starContext, modelMatrix * glm::eulerAngleX(time) * glm::scale(glm::vec3(0.03f)), glm::vec3(1, 1, 0.7)); drawObjectColor(starContext, modelMatrix * glm::eulerAngleX(time) * glm::scale(glm::vec3(0.03f)), glm::vec3(1, 1, 0.7), starMetalness, starRoughness, spaceshipPos);
if (star == 0) if (star == 0)
{ {
star++; star++;
@ -327,27 +387,27 @@ glm::mat4 specshipCameraRotrationMatrix = glm::mat4({
}); });
void drawStars(int star_number) { void drawStars(int star_number) {
float yOffset = 2.0f; float yOffset = 0.55f + spaceshipPos.y;
float zOffset = 12.0f; float zOffset =5.10f + spaceshipPos.z;
float scaleFactor = 0.03f; float scaleFactor = 0.03f;
for (int i = 0; i < star_number; ++i) { for (int i = 0; i < star_number; ++i) {
drawObjectColor(starContext, glm::translate(glm::vec3(10.0f, yOffset, zOffset - i * 0.5f)) drawObjectColor(starContext, glm::translate(glm::vec3(2.3f, yOffset, (zOffset) - i * 0.5f ))
* glm::rotate(glm::mat4(), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f))
* glm::scale(glm::vec3(scaleFactor)), glm::vec3(0, 1, 0)); * glm::scale(glm::vec3(scaleFactor)), glm::vec3(1, 1, 0.7), starMetalness, starRoughness,glm::vec3(1.0f, yOffset, zOffset));
} }
} }
void drawHearts(int collision_number) { void drawHearts(int collision_number) {
float yOffset = 1.0f; float yOffset = -0.5f + spaceshipPos.y;
float zOffset = 12.0f; float zOffset = 6.15 + spaceshipPos.z;
for (int i = 0; i < 5; ++i) { for (int i = 0; i < 5; ++i) {
if (collision_number > i) { if (collision_number > i) {
drawObjectColor(heartContext, glm::translate(glm::vec3(10.0f, yOffset, zOffset - i * 0.5f)) drawObjectColor(heartContext, glm::translate(glm::vec3(3.5f, yOffset, (zOffset) - i * 0.5f))
* glm::rotate(glm::mat4(), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f))
* glm::scale(glm::vec3(0.025f)), glm::vec3(1, 0, 0)); * glm::scale(glm::vec3(0.025f)), glm::vec3(1, 0, 0), starMetalness, starRoughness, glm::vec3(1.0f, yOffset, zOffset));
} }
} }
} }
@ -404,18 +464,17 @@ void renderScene(GLFWwindow* window)
//); //);
generatePlanetoidBelt(); generatePlanetoidBelt();
lastAsteroidTime = glfwGetTime();
if (fire == true){ if (fire == true){
ammoPos = ammoPos + glm::vec3(0.025f, 0.f, 0.f); ammoPos = ammoPos + glm::vec3(0.025f, 0.f, 0.f);
std::cout << ammoPos.x; glm::mat4 modelMatrix = glm::translate(ammoPos) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(ammoPos) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 0.0f)); drawObjectColor(saberContext, modelMatrix * glm::scale(glm::vec3(0.003f)), glm::vec3(0.1, 0.1, 0.7), starMetalness, starRoughness, spaceshipPos);
drawObjectColor(saberContext, glm::translate(ammoPos) * glm::scale(glm::vec3(0.005f)), glm::vec3(1, 0.2, 0.6));
} }
drawStars(star_counter); drawStars(star_counter);
drawHearts(colission); drawHearts(colission);
//updateAmmoReload();
//drawAmmoReloadBar();
glUseProgram(0); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -540,6 +599,8 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
fire = true; fire = true;
ammoPos = spaceshipPos; ammoPos = spaceshipPos;
lastFiretime = glfwGetTime();
} }
} }
@ -560,15 +621,6 @@ void processInput(GLFWwindow* window)
} }
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
spaceshipPos += glm::vec3(0.f,moveSpeed,0.f); spaceshipPos += glm::vec3(0.f,moveSpeed,0.f);
tiltAngleUpDown += easeInExpo(x);
}
else {
if (tiltAngleUpDown > 0) {
tiltAngleUpDown -= 0.003;
}
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS){
spaceshipPos -= glm::vec3(0.f, moveSpeed, 0.f);
tiltAngleUpDown -= easeInExpo(x); tiltAngleUpDown -= easeInExpo(x);
} }
else { else {
@ -576,6 +628,15 @@ void processInput(GLFWwindow* window)
tiltAngleUpDown += 0.003; tiltAngleUpDown += 0.003;
} }
} }
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS){
spaceshipPos -= glm::vec3(0.f, moveSpeed, 0.f);
tiltAngleUpDown += easeInExpo(x);
}
else {
if (tiltAngleUpDown > 0) {
tiltAngleUpDown -= 0.003;
}
}
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
spaceshipPos += spaceshipSide * moveSpeed; spaceshipPos += spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)