From 0b24cbc7ae76b3aa9f5cb167c8911074bdd03e5d Mon Sep 17 00:00:00 2001
From: xkamikoo <58092037+xkamikoo@users.noreply.github.com>
Date: Fri, 12 Feb 2021 03:43:11 +0100
Subject: [PATCH] Asteroids. TODO: find model with small amount of verticles
---
grk-project.vcxproj | 6 +-
grk-project.vcxproj.filters | 18 +-
shaders/shader_asteroid.frag | 62 +++++++
shaders/shader_asteroid.vert | 55 ++++++
shaders/shader_color.frag | 14 --
shaders/shader_color.vert | 16 --
shaders/shader_normal.frag | 62 +++++++
shaders/shader_normal.vert | 54 ++++++
shaders/shader_sun.frag | 3 +-
shaders/shader_tex.frag | 13 +-
shaders/shader_tex.vert | 2 +
src/main.cpp | 327 +++++++++++++++++++++++------------
src/mesh.h | 47 +++++
src/model.h | 7 +
14 files changed, 530 insertions(+), 156 deletions(-)
create mode 100644 shaders/shader_asteroid.frag
create mode 100644 shaders/shader_asteroid.vert
delete mode 100644 shaders/shader_color.frag
delete mode 100644 shaders/shader_color.vert
create mode 100644 shaders/shader_normal.frag
create mode 100644 shaders/shader_normal.vert
diff --git a/grk-project.vcxproj b/grk-project.vcxproj
index 5332a2d..8bda9b1 100644
--- a/grk-project.vcxproj
+++ b/grk-project.vcxproj
@@ -11,12 +11,14 @@
+
+
-
-
+
+
diff --git a/grk-project.vcxproj.filters b/grk-project.vcxproj.filters
index 7589c2e..70ea294 100644
--- a/grk-project.vcxproj.filters
+++ b/grk-project.vcxproj.filters
@@ -18,12 +18,6 @@
-
- Shader Files
-
-
- Shader Files
-
Shader Files
@@ -60,6 +54,18 @@
Shader Files
+
+ Shader Files
+
+
+ Shader Files
+
+
+ Shader Files
+
+
+ Shader Files
+
diff --git a/shaders/shader_asteroid.frag b/shaders/shader_asteroid.frag
new file mode 100644
index 0000000..8098f4a
--- /dev/null
+++ b/shaders/shader_asteroid.frag
@@ -0,0 +1,62 @@
+#version 430 core
+
+layout (location = 0) out vec4 FragColor;
+layout (location = 1) out vec4 BrightColor;
+
+struct PointLight {
+ vec3 position;
+ vec3 color;
+ float intensity;
+};
+
+#define MAX_POINT_LIGHTS 16
+
+uniform vec3 cameraPos;
+uniform sampler2D diffuseTexture;
+uniform sampler2D normalTexture;
+uniform PointLight pointLights[MAX_POINT_LIGHTS];
+uniform int LightsCount;
+
+in vec3 fragPos;
+in vec2 vTexCoord;
+in vec3 LightPosTS[MAX_POINT_LIGHTS];
+in vec3 CameraPosTS;
+in vec3 FragPosTS;
+
+
+
+void main()
+{
+ vec3 fragColor = vec3(0,0,0);
+ vec3 texture = texture2D(diffuseTexture, vTexCoord).rgb;
+ //vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z);
+ vec3 ambient = vec3(0.1, 0.1, 0.1) * texture;
+
+ vec3 normal = texture2D(normalTexture, vTexCoord).rgb;
+ normal = normalize(normal * 2.0 - 1.0);
+
+ vec3 V = normalize(CameraPosTS-FragPosTS);
+
+ for(int i = 0; i < LightsCount; i++)
+ {
+ vec3 lightDir = normalize(LightPosTS[i] - FragPosTS);
+
+
+ vec3 R = reflect(-lightDir,normal);
+
+ float dist = distance(fragPos, pointLights[i].position);
+ float distance = (1/dist) * (1/dist);
+
+ float spec = pow(max(0,dot(R,V)),2);
+ float diff = max(0,dot(normal,normalize(lightDir)));
+
+ vec3 diffuse = pointLights[i].color * diff * distance * pointLights[i].intensity;
+ vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist);
+
+
+ fragColor += texture*diffuse+specular;
+ }
+
+ BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
+ FragColor = vec4(fragColor+ambient,1.0);
+}
diff --git a/shaders/shader_asteroid.vert b/shaders/shader_asteroid.vert
new file mode 100644
index 0000000..efe5a64
--- /dev/null
+++ b/shaders/shader_asteroid.vert
@@ -0,0 +1,55 @@
+#version 430 core
+
+layout(location = 0) in vec3 vertexPosition;
+layout(location = 1) in vec2 vertexTexCoord;
+layout(location = 2) in vec3 vertexNormal;
+layout (location = 3) in vec3 aTangent;
+layout (location = 4) in vec3 aBitangent;
+layout (location = 5) in mat4 aInstanceMatrix;
+
+struct PointLight {
+ vec3 position;
+ vec3 color;
+ float intensity;
+};
+
+#define MAX_POINT_LIGHTS 16
+
+uniform mat4 projection;
+uniform mat4 view;
+uniform vec3 cameraPos;
+uniform PointLight pointLights[MAX_POINT_LIGHTS];
+uniform int LightsCount;
+
+
+out vec3 fragPos;
+out vec2 vTexCoord;
+out vec3 LightPosTS[MAX_POINT_LIGHTS];
+out vec3 CameraPosTS;
+out vec3 FragPosTS;
+
+void main()
+{
+ gl_Position = projection * view * aInstanceMatrix * vec4(vertexPosition, 1.0);
+
+ mat3 normalMatrix = transpose(inverse(mat3(aInstanceMatrix)));
+
+ vec3 T = normalize(normalMatrix * aTangent);
+ vec3 N = normalize(normalMatrix * vertexNormal);
+ // re-orthogonalize T with respect to N
+ T = normalize(T - dot(T, N) * N);
+ // then retrieve perpendicular vector B with the cross product of T and N
+ vec3 B = cross(N, T);
+
+ mat3 TBN = mat3(T, B, N);
+
+ for(int i=0; i cube;
std::shared_ptr sphere;
@@ -68,16 +69,17 @@ std::shared_ptr asteroid;
//std::vector corvetteMeshes;
std::shared_ptr crewmate;
+
+//cameraPos
float cameraAngle = 0;
glm::vec3 cameraPos = glm::vec3(-6, 0, 0);
glm::vec3 cameraDir;
glm::vec3 cameraSide;
-
-
glm::mat4 cameraMatrix, perspectiveMatrix;
glm::vec3 sunPos = glm::vec3(10.0f, 0.0f, -5.0f);
glm::vec3 sunPos2 = glm::vec3(25.0f, -1.0f, 10.0f);
+
//particlepart
struct Particle {
glm::vec3 pos, speed;
@@ -90,6 +92,16 @@ struct Particle {
return this->cameradistance > that.cameradistance;
}
};
+
+struct Object
+{
+ glm::mat4 modelM;
+ glm::mat4 invModelM;
+ std::shared_ptr modelParent;
+ GLuint textureID;
+ GLuint shaderID;
+ glm::vec3 color;
+};
const int MaxParticles = 1000;
Particle ParticlesContainer[MaxParticles];
@@ -138,8 +150,9 @@ std::vector faces
"skybox/back.jpg"
};
-
+std::vector