Separate tranform code into geometry

master
outfrost 2020-05-27 01:31:50 +02:00
parent edfc39dc5c
commit f8c6c26ece
6 changed files with 52 additions and 46 deletions

View File

@ -14,6 +14,7 @@ LDLIBS ::= -lGL -lGLEW -lglut -lassimp $(LDLIBS)
sources ::= main.c \
engine/asset.c \
engine/geometry.c \
engine/logger.c \
engine/performance.c \
engine/render.c \

36
src/engine/geometry.c Normal file
View File

@ -0,0 +1,36 @@
#include <stddef.h>
#include "geometry.h"
Transform identity() {
return (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = 0.0f,
.b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = 0.0f,
.c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = 0.0f,
.d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f };
}
void multiply(Transform* transform, Transform by) {
GLfloat* a = (GLfloat*) &by;
GLfloat* b = (GLfloat*) transform;
for (size_t row = 0; row < 4; ++row) {
for (size_t col = 0; col < 4; ++col) {
b[(row * 4) + col] =
a[(row * 4) + 0] * b[(0 * 4) + col]
+ a[(row * 4) + 1] * b[(1 * 4) + col]
+ a[(row * 4) + 2] * b[(2 * 4) + col]
+ a[(row * 4) + 3] * b[(3 * 4) + col];
}
}
}
void translate(Transform* transform, Vector3D vec) {
multiply(transform, (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = vec.x,
.b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = vec.y,
.c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = vec.z,
.d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f });
}
Vector3D translationOf(Transform transform) {
return (Vector3D) { transform.a4, transform.b4, transform.c4 };
}

View File

@ -1,7 +1,10 @@
#ifndef GEOMETRY_H_
#define GEOMETRY_H_
#include <GL/gl.h>
typedef struct Vector3D Vector3D;
typedef struct Transform Transform;
struct Vector3D {
float x;
@ -9,4 +12,15 @@ struct Vector3D {
float z;
};
struct Transform {
GLfloat a1, a2, a3, a4;
GLfloat b1, b2, b3, b4;
GLfloat c1, c2, c3, c4;
GLfloat d1, d2, d3, d4;
};
Transform identity();
void translate(Transform* transform, Vector3D vec);
Vector3D translationOf(Transform transform);
#endif

View File

@ -2,45 +2,13 @@
#include "engine/logger.h"
#include "geometry.h"
#include "scene.h"
Scene* currentScene = NULL;
Transform identity() {
return (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = 0.0f,
.b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = 0.0f,
.c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = 0.0f,
.d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f };
}
void multiply(Transform* transform, Transform by) {
GLfloat* a = (GLfloat*) &by;
GLfloat* b = (GLfloat*) transform;
for (size_t row = 0; row < 4; ++row) {
for (size_t col = 0; col < 4; ++col) {
b[(row * 4) + col] =
a[(row * 4) + 0] * b[(0 * 4) + col]
+ a[(row * 4) + 1] * b[(1 * 4) + col]
+ a[(row * 4) + 2] * b[(2 * 4) + col]
+ a[(row * 4) + 3] * b[(3 * 4) + col];
}
}
}
void translate(Transform* transform, Vector3D vec) {
multiply(transform, (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = vec.x,
.b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = vec.y,
.c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = vec.z,
.d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f });
}
Vector3D translationOf(Transform transform) {
return (Vector3D) { transform.a4, transform.b4, transform.c4 };
}
Scene* newScene() {
Scene* scene = malloc(sizeof(Scene));
*scene = (Scene) { .parent = NULL,

View File

@ -4,14 +4,6 @@
#include "asset.h"
typedef struct Scene Scene;
typedef struct Transform Transform;
struct Transform {
GLfloat a1, a2, a3, a4;
GLfloat b1, b2, b3, b4;
GLfloat c1, c2, c3, c4;
GLfloat d1, d2, d3, d4;
};
struct Scene {
Scene* parent;
@ -23,10 +15,6 @@ struct Scene {
Scene* currentScene;
Transform identity();
void translate(Transform* transform, Vector3D vec);
Vector3D translationOf(Transform transform);
Scene* newScene();
void insertChildScene(Scene* scene, Scene* newChild);
void reparentScene(Scene* scene, Scene* newParent);

View File

@ -3,7 +3,6 @@
#include <GL/gl.h>
#include "engine/geometry.h"
#include "engine/scene.h"
Scene* playerCharacter;