summaryrefslogtreecommitdiff
path: root/shared_cpp
diff options
context:
space:
mode:
Diffstat (limited to 'shared_cpp')
-rw-r--r--shared_cpp/Renderer3d.cpp24
-rw-r--r--shared_cpp/mathlib.cpp54
-rw-r--r--shared_cpp/mathlib.h18
3 files changed, 90 insertions, 6 deletions
diff --git a/shared_cpp/Renderer3d.cpp b/shared_cpp/Renderer3d.cpp
index 4ec5504..d6be3cb 100644
--- a/shared_cpp/Renderer3d.cpp
+++ b/shared_cpp/Renderer3d.cpp
@@ -15,14 +15,29 @@ const char* vertexSrc =
" vec4 fragmentPosition = projection * view * model * vec4(position, 1);"
" gl_Position = fragmentPosition;\n"
" VertexColor = color;\n"
- " VertexNormal = normal;\n"
+ " mat3 normalMatrix = mat3(model);\n"
+ " VertexNormal = normalize(normalMatrix * normal);\n"
"}\n";
const char* fragmentSrc =
+ "precision mediump float;\n"
"varying lowp vec4 VertexColor; \n"
"varying lowp vec3 VertexNormal; \n"
+ "vec4 ambientColor = vec4(0.2, 0.2, 0.2, 1.0);\n"
+ "vec3 lightDirection = vec3(0.0, 1.0, 0.0);\n"
+ "vec4 lightColor = vec4(0.7, 0.7, 0.7, 1.0);\n"
"void main() { \n"
- " gl_FragColor = VertexColor; \n"
+ " vec4 finalColor = VertexColor * ambientColor;\n"
+ " float normalDotDir = max(0.0, dot(VertexNormal, lightDirection));\n"
+ " if (normalDotDir <= 0.0001) {"
+ " finalColor += vec4(0, 0, 0, 0);"
+ " }\n"
+ " else {\n"
+ " float cosTheta = clamp(normalDotDir, 0.0, 1.0);\n"
+ " vec4 diffuseFactor = VertexColor * cosTheta;\n"
+ " finalColor += lightColor * diffuseFactor; \n"
+ " }\n"
+ " gl_FragColor = finalColor;\n"
"}";
@@ -80,9 +95,8 @@ void Mesh3d::load(Vertex3d* inVertices, uint32 inNumVertices, uint32* inIndices,
glEnableVertexAttribArray(renderer->attributes.position);
glVertexAttribPointer(renderer->attributes.position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)0);
- // @TODO: Show normal once it's actually implemented
- //glEnableVertexAttribArray(renderer->attributes.normal);
- //glVertexAttribPointer(renderer->attributes.normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, normal));
+ glEnableVertexAttribArray(renderer->attributes.normal);
+ glVertexAttribPointer(renderer->attributes.normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, normal));
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, color));
diff --git a/shared_cpp/mathlib.cpp b/shared_cpp/mathlib.cpp
index fb09cd9..3402f0e 100644
--- a/shared_cpp/mathlib.cpp
+++ b/shared_cpp/mathlib.cpp
@@ -521,6 +521,14 @@ void Mat4x4::print() {
// ***************************************
// Quaternion
+Quaternion::Quaternion() { };
+
+Quaternion::Quaternion(float inW, float inX, float inY, float inZ) {
+ w = inW;
+ x = inX;
+ y = inY;
+ z = inZ;
+}
float Quaternion::operator [](int index) {
switch (index) {
@@ -612,6 +620,32 @@ Quaternion Quaternion::normalize() const {
};
}
+/*Mat4x4 Quaternion::toMatrix() const {
+ return {
+ {
+ 1 - 2 * (y * y - z * z),
+ 2 * (x * y - z * w),
+ 2 * (x * z + w * y),
+ 0,
+
+ 2 * (x * y + w * z),
+ 1 - 2 * (x * x - z * z),
+ 2 * (y * z - w * x),
+ 0,
+
+ 2 * (x * z - w * y),
+ 2 * (y * z + w * x),
+ 1 - 2 * (x * x - y * y),
+ 0,
+
+ 0,
+ 0,
+ 0,
+ 1
+ }
+ };
+}*/
+
Mat4x4 Quaternion::toMatrix() const {
return {
{
@@ -651,3 +685,23 @@ Quaternion quaternionFromRotation(Vector3 axis, float angleRadians) {
axis.z * sinHalfAngRad
};
}
+
+Quaternion quaternionFromEulerAngle(float yaw, float pitch, float roll) {
+ float cy = cosf(yaw * 0.5f);
+ float sy = sinf(yaw * 0.5f);
+ float cp = cosf(pitch * 0.5f);
+ float sp = sinf(pitch * 0.5f);
+ float cr = cosf(roll * 0.5f);
+ float sr = sinf(roll * 0.5f);
+
+ return {
+ cr * cp * cy + sr * sp * sy,
+ sr * cp * cy - cr * sp * sy,
+ cr * sp * cy + sr * cp * sy,
+ cr * cp * sy - sr * sp * cy
+ };
+}
+
+void Quaternion::printDebug(const char* name) {
+ printf("%s=Quaternion(%f, %f, %f, %f)\n", name, x, y, z, w);
+}
diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h
index e3c6875..5f9abff 100644
--- a/shared_cpp/mathlib.h
+++ b/shared_cpp/mathlib.h
@@ -21,6 +21,17 @@
#define DEG_TO_RAD(x) (x * (PI / 180.f))
#define RAD_TO_DEG(x) (x * (180.f / PI))
+
+// -- Random
+inline float randomFloatBetween(float min, float max) {
+ float random = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
+ return (max - min) * random + min;
+}
+
+inline int randomIntBetween(int min, int max) {
+ return static_cast<int>(randomFloatBetween(min, max));
+}
+
struct Vector2 {
float x = 0;
float y = 0;
@@ -130,11 +141,13 @@ struct Mat4x4 {
};
struct Quaternion {
- float w = 0;
+ float w = 1;
float x = 0;
float y = 0;
float z = 0;
+ Quaternion();
+ Quaternion(float inW, float inX, float inY, float inZ);
float operator [](int index);
Quaternion operator*(const Quaternion& other) const;
Quaternion operator*(const float& scale) const;
@@ -145,6 +158,9 @@ struct Quaternion {
Quaternion normalize() const;
float length() const;
float dot(const Quaternion& other) const;
+
+ void printDebug(const char* name);
};
Quaternion quaternionFromRotation(Vector3 axis, float angleRadians);
+Quaternion quaternionFromEulerAngle(float yaw, float pitch, float roll);