Rename `Vector3D` to `Vector`

master
outfrost 2020-07-15 00:02:18 +02:00
parent 9562d38144
commit d259a0da9a
7 changed files with 85 additions and 85 deletions

View File

@ -16,8 +16,8 @@
static const float smoothingThresholdAngle = TAU / 14.0f; static const float smoothingThresholdAngle = TAU / 14.0f;
static const struct aiScene* importScene(const char* path); static const struct aiScene* importScene(const char* path);
static Vector3D triangleNormal(Vector3D v1, Vector3D v2, Vector3D v3); static Vector triangleNormal(Vector v1, Vector v2, Vector v3);
static Vector3D convertAiVector3D(struct aiVector3D vect); static Vector convertAiVector3D(struct aiVector3D vect);
static const char* replaceFileExtension(const struct aiString path, const char* ext); static const char* replaceFileExtension(const struct aiString path, const char* ext);
@ -111,7 +111,7 @@ const Solid* importSolid(const char* path) {
const unsigned int numFaces = aiMesh->mNumFaces; const unsigned int numFaces = aiMesh->mNumFaces;
Mesh mesh = { .numVertices = numVertices, Mesh mesh = { .numVertices = numVertices,
.vertices = malloc(numVertices * sizeof(Vector3D)), .vertices = malloc(numVertices * sizeof(Vector)),
.normals = NULL, .normals = NULL,
.textureCoords = NULL, .textureCoords = NULL,
.numFaces = numFaces, .numFaces = numFaces,
@ -124,14 +124,14 @@ const Solid* importSolid(const char* path) {
} }
if (aiMesh->mNormals != NULL) { if (aiMesh->mNormals != NULL) {
mesh.normals = malloc(numVertices * sizeof(Vector3D)); mesh.normals = malloc(numVertices * sizeof(Vector));
for (unsigned int normIndex = 0; normIndex < numVertices; ++normIndex) { for (unsigned int normIndex = 0; normIndex < numVertices; ++normIndex) {
mesh.normals[normIndex] = convertAiVector3D( mesh.normals[normIndex] = convertAiVector3D(
aiMesh->mNormals[normIndex]); aiMesh->mNormals[normIndex]);
} }
} }
mesh.textureCoords = malloc(numVertices * sizeof(Vector3D)); mesh.textureCoords = malloc(numVertices * sizeof(Vector));
for (unsigned int texcIndex = 0; texcIndex < numVertices; ++texcIndex) { for (unsigned int texcIndex = 0; texcIndex < numVertices; ++texcIndex) {
mesh.textureCoords[texcIndex] = convertAiVector3D( mesh.textureCoords[texcIndex] = convertAiVector3D(
aiMesh->mTextureCoords[0][texcIndex]); aiMesh->mTextureCoords[0][texcIndex]);
@ -143,14 +143,14 @@ const Solid* importSolid(const char* path) {
Face face = { .numIndices = numIndices, Face face = { .numIndices = numIndices,
.indices = malloc(numIndices * sizeof(size_t)), .indices = malloc(numIndices * sizeof(size_t)),
.normals = malloc(numIndices * sizeof(Vector3D)) }; .normals = malloc(numIndices * sizeof(Vector)) };
for (unsigned int i = 0; i < numIndices; ++i) { for (unsigned int i = 0; i < numIndices; ++i) {
face.indices[i] = aiFace.mIndices[i]; face.indices[i] = aiFace.mIndices[i];
} }
if (numIndices == 3) { if (numIndices == 3) {
Vector3D normal = triangleNormal(mesh.vertices[face.indices[0]], Vector normal = triangleNormal(mesh.vertices[face.indices[0]],
mesh.vertices[face.indices[1]], mesh.vertices[face.indices[1]],
mesh.vertices[face.indices[2]]); mesh.vertices[face.indices[2]]);
for (size_t i = 0; i < numIndices; ++i) { for (size_t i = 0; i < numIndices; ++i) {
@ -178,12 +178,12 @@ const Solid* importSolid(const char* path) {
Face face = mesh.faces[faceIndex]; Face face = mesh.faces[faceIndex];
if (face.normals) { if (face.normals) {
face.normals = memcpy(malloc(face.numIndices * sizeof(Vector3D)), face.normals = memcpy(malloc(face.numIndices * sizeof(Vector)),
face.normals, face.normals,
face.numIndices * sizeof(Vector3D)); face.numIndices * sizeof(Vector));
for (size_t indexIndex = 0; indexIndex < face.numIndices; ++indexIndex) { for (size_t indexIndex = 0; indexIndex < face.numIndices; ++indexIndex) {
Vector3D smoothedNormal = face.normals[indexIndex]; Vector smoothedNormal = face.normals[indexIndex];
for (size_t i = 0; i < mesh.numFaces; ++i) { for (size_t i = 0; i < mesh.numFaces; ++i) {
if (i == faceIndex || !mesh.faces[i].normals) { if (i == faceIndex || !mesh.faces[i].normals) {
@ -287,12 +287,12 @@ static const struct aiScene* importScene(const char* path) {
return scene; return scene;
} }
static Vector3D triangleNormal(Vector3D v1, Vector3D v2, Vector3D v3) { static Vector triangleNormal(Vector v1, Vector v2, Vector v3) {
return normalized(crossProduct(subtractVectors(v2, v1), subtractVectors(v3, v1))); return normalized(crossProduct(subtractVectors(v2, v1), subtractVectors(v3, v1)));
} }
static Vector3D convertAiVector3D(struct aiVector3D vect) { static Vector convertAiVector3D(struct aiVector3D vect) {
return (Vector3D) { .x = vect.x, return (Vector) { .x = vect.x,
.y = vect.y, .y = vect.y,
.z = vect.z }; .z = vect.z };
} }

View File

@ -20,9 +20,9 @@ struct Solid {
struct Mesh { struct Mesh {
size_t numVertices; size_t numVertices;
Vector3D* vertices; Vector* vertices;
Vector3D* normals; Vector* normals;
Vector3D* textureCoords; Vector* textureCoords;
size_t numFaces; size_t numFaces;
Face* faces; Face* faces;
size_t materialIndex; size_t materialIndex;
@ -31,7 +31,7 @@ struct Mesh {
struct Face { struct Face {
size_t numIndices; size_t numIndices;
size_t* indices; size_t* indices;
Vector3D* normals; Vector* normals;
}; };
struct Material { struct Material {

View File

@ -28,7 +28,7 @@ Transform multiply(Transform t1, Transform t2) {
return result; return result;
} }
void translate(Transform* transform, Vector3D vec) { void translate(Transform* transform, Vector vec) {
*transform = multiply( *transform = multiply(
(Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = vec.x, (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, .b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = vec.y,
@ -37,7 +37,7 @@ void translate(Transform* transform, Vector3D vec) {
*transform); *transform);
} }
void rotate(Transform* transform, Vector3D axis, float angle) { void rotate(Transform* transform, Vector axis, float angle) {
axis = normalized(axis); axis = normalized(axis);
float l = axis.x; float l = axis.x;
float m = axis.y; float m = axis.y;
@ -54,31 +54,31 @@ void rotate(Transform* transform, Vector3D axis, float angle) {
*transform); *transform);
} }
Vector3D addVectors(Vector3D v1, Vector3D v2){ Vector addVectors(Vector v1, Vector v2){
return (Vector3D) { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; return (Vector) { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
} }
Vector3D subtractVectors(Vector3D v1, Vector3D v2) { Vector subtractVectors(Vector v1, Vector v2) {
return (Vector3D) { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; return (Vector) { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
} }
Vector3D crossProduct(Vector3D v1, Vector3D v2) { Vector crossProduct(Vector v1, Vector v2) {
return (Vector3D) { .x = (v1.y * v2.z) - (v1.z * v2.y), return (Vector) { .x = (v1.y * v2.z) - (v1.z * v2.y),
.y = (v1.z * v2.x) - (v1.x * v2.z), .y = (v1.z * v2.x) - (v1.x * v2.z),
.z = (v1.x * v2.y) - (v1.y * v2.x) }; .z = (v1.x * v2.y) - (v1.y * v2.x) };
} }
float dotProduct(Vector3D v1, Vector3D v2) { float dotProduct(Vector v1, Vector v2) {
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
} }
Vector3D scaleVector(Vector3D vec, float scale) { Vector scaleVector(Vector vec, float scale) {
return (Vector3D) { vec.x * scale, return (Vector) { vec.x * scale,
vec.y * scale, vec.y * scale,
vec.z * scale }; vec.z * scale };
} }
Vector3D clampMagnitude(Vector3D vec, float maxMagnitude) { Vector clampMagnitude(Vector vec, float maxMagnitude) {
float m = magnitude(vec); float m = magnitude(vec);
if (m > maxMagnitude) { if (m > maxMagnitude) {
vec = scaleVector(vec, maxMagnitude / m); vec = scaleVector(vec, maxMagnitude / m);
@ -86,11 +86,11 @@ Vector3D clampMagnitude(Vector3D vec, float maxMagnitude) {
return vec; return vec;
} }
float magnitude(Vector3D vec) { float magnitude(Vector vec) {
return sqrtf(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); return sqrtf(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
} }
Vector3D applyTransform(Transform transform, Vector3D vec) { Vector applyTransform(Transform transform, Vector vec) {
GLfloat* a = (GLfloat*) &transform; GLfloat* a = (GLfloat*) &transform;
GLfloat b[4] = { vec.x, vec.y, vec.z, 1.0f }; GLfloat b[4] = { vec.x, vec.y, vec.z, 1.0f };
GLfloat c[4]; GLfloat c[4];
@ -102,14 +102,14 @@ Vector3D applyTransform(Transform transform, Vector3D vec) {
+ a[(row * 4) + 2] * b[2] + a[(row * 4) + 2] * b[2]
+ a[(row * 4) + 3] * b[3]; + a[(row * 4) + 3] * b[3];
} }
return (Vector3D) { c[0], c[1], c[2] }; return (Vector) { c[0], c[1], c[2] };
} }
Vector3D translationOf(Transform transform) { Vector translationOf(Transform transform) {
return (Vector3D) { transform.a4, transform.b4, transform.c4 }; return (Vector) { transform.a4, transform.b4, transform.c4 };
} }
Vector3D normalized(Vector3D vec) { Vector normalized(Vector vec) {
float m = magnitude(vec); float m = magnitude(vec);
return (Vector3D) { vec.x / m, vec.y / m, vec.z / m }; return (Vector) { vec.x / m, vec.y / m, vec.z / m };
} }

View File

@ -3,10 +3,10 @@
#include <GL/gl.h> #include <GL/gl.h>
typedef struct Vector3D Vector3D; typedef struct Vector Vector;
typedef struct Transform Transform; typedef struct Transform Transform;
struct Vector3D { struct Vector {
float x; float x;
float y; float y;
float z; float z;
@ -23,17 +23,17 @@ static const float TAU = 6.28318530718f;
Transform identity(); Transform identity();
Transform multiply(Transform t1, Transform t2); Transform multiply(Transform t1, Transform t2);
void translate(Transform* transform, Vector3D vec); void translate(Transform* transform, Vector vec);
void rotate(Transform* transform, Vector3D axis, float angle); void rotate(Transform* transform, Vector axis, float angle);
Vector3D addVectors(Vector3D v1, Vector3D v2); Vector addVectors(Vector v1, Vector v2);
Vector3D subtractVectors(Vector3D v1, Vector3D v2); Vector subtractVectors(Vector v1, Vector v2);
Vector3D crossProduct(Vector3D v1, Vector3D v2); Vector crossProduct(Vector v1, Vector v2);
float dotProduct(Vector3D v1, Vector3D v2); float dotProduct(Vector v1, Vector v2);
Vector3D scaleVector(Vector3D vec, float scale); Vector scaleVector(Vector vec, float scale);
Vector3D clampMagnitude(Vector3D vec, float maxMagnitude); Vector clampMagnitude(Vector vec, float maxMagnitude);
float magnitude(Vector3D vec); float magnitude(Vector vec);
Vector3D applyTransform(Transform transform, Vector3D vec); Vector applyTransform(Transform transform, Vector vec);
Vector3D translationOf(Transform transform); Vector translationOf(Transform transform);
Vector3D normalized(Vector3D vec); Vector normalized(Vector vec);
#endif // ENGINE_GEOMETRY_H_ #endif // ENGINE_GEOMETRY_H_

View File

@ -101,7 +101,7 @@ static void setupCamera() {
static void moveCameraTo(const Scene* anchor) { static void moveCameraTo(const Scene* anchor) {
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
Vector3D pos = translationOf(worldTransform(anchor)); Vector pos = translationOf(worldTransform(anchor));
glTranslatef(-pos.x, -pos.y, -pos.z); glTranslatef(-pos.x, -pos.y, -pos.z);
} }
@ -153,8 +153,8 @@ static void drawSolid(const Solid* solid) {
glBegin(GL_LINES); glBegin(GL_LINES);
for (size_t i = 0; i < face.numIndices; ++i) { for (size_t i = 0; i < face.numIndices; ++i) {
size_t vertIndex = face.indices[i]; size_t vertIndex = face.indices[i];
Vector3D vertex = mesh.vertices[vertIndex]; Vector vertex = mesh.vertices[vertIndex];
Vector3D normal = face.normals[i]; Vector normal = face.normals[i];
glColor3f(absolute(normal.x), absolute(normal.y), absolute(normal.z)); glColor3f(absolute(normal.x), absolute(normal.y), absolute(normal.z));
glVertex3f(vertex.x, vertex.y, vertex.z); glVertex3f(vertex.x, vertex.y, vertex.z);
glVertex3f(vertex.x + normal.x, vertex.y + normal.y, vertex.z + normal.z); glVertex3f(vertex.x + normal.x, vertex.y + normal.y, vertex.z + normal.z);
@ -178,14 +178,14 @@ static void drawSolid(const Solid* solid) {
size_t vertIndex = face.indices[i]; size_t vertIndex = face.indices[i];
if (face.normals) { if (face.normals) {
if (mesh.textureCoords) { if (mesh.textureCoords) {
Vector3D coords = mesh.textureCoords[vertIndex]; Vector coords = mesh.textureCoords[vertIndex];
glTexCoord2f(coords.x, coords.y); glTexCoord2f(coords.x, coords.y);
} }
Vector3D normal = face.normals[i]; Vector normal = face.normals[i];
glNormal3f(normal.x, normal.y, normal.z); glNormal3f(normal.x, normal.y, normal.z);
} }
Vector3D vertex = mesh.vertices[vertIndex]; Vector vertex = mesh.vertices[vertIndex];
glVertex3f(vertex.x, vertex.y, vertex.z); glVertex3f(vertex.x, vertex.y, vertex.z);
} }

View File

@ -28,7 +28,7 @@ static inline void setBlockInGrid(BlockGrid grid, size_t x, size_t z, Block* blo
void initLevel() { void initLevel() {
playerSpawnTransform = identity(); playerSpawnTransform = identity();
translate(&playerSpawnTransform, (Vector3D) { .x = -BLOCKGRID_CELL_SIZE, translate(&playerSpawnTransform, (Vector) { .x = -BLOCKGRID_CELL_SIZE,
.y = 0.0f, .y = 0.0f,
.z = -BLOCKGRID_CELL_SIZE }); .z = -BLOCKGRID_CELL_SIZE });
@ -41,7 +41,7 @@ void initLevel() {
for (size_t z = 0; z < levelGrid.depth; ++z) { for (size_t z = 0; z < levelGrid.depth; ++z) {
for (size_t x = 0; x < levelGrid.width; ++x) { for (size_t x = 0; x < levelGrid.width; ++x) {
Scene* blockScene = newScene(); Scene* blockScene = newScene();
translate(&blockScene->transform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE, translate(&blockScene->transform, (Vector) { .x = x * BLOCKGRID_CELL_SIZE,
.y = 0.0f, .y = 0.0f,
.z = z * BLOCKGRID_CELL_SIZE }); .z = z * BLOCKGRID_CELL_SIZE });
blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid; blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
@ -87,7 +87,7 @@ static void buildLevelFromImage(const TgaImage* image) {
case 0xFF00FFFF: case 0xFF00FFFF:
block = &blockEmpty; block = &blockEmpty;
playerSpawnTransform = identity(); playerSpawnTransform = identity();
translate(&playerSpawnTransform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE, translate(&playerSpawnTransform, (Vector) { .x = x * BLOCKGRID_CELL_SIZE,
.y = 0.0f, .y = 0.0f,
.z = z * BLOCKGRID_CELL_SIZE }); .z = z * BLOCKGRID_CELL_SIZE });
break; break;

View File

@ -7,20 +7,20 @@ static const float movementSpeed = 2.5f;
Scene* playerCharacter; Scene* playerCharacter;
static Transform screenToWorldMovementTransform; static Transform screenToWorldMovementTransform;
static Vector3D worldMovementUp; static Vector worldMovementUp;
static Vector3D worldMovementDown; static Vector worldMovementDown;
static Vector3D worldMovementLeft; static Vector worldMovementLeft;
static Vector3D worldMovementRight; static Vector worldMovementRight;
static Direction movementDirection; static Direction movementDirection;
static void movePlayer(Vector3D direction, float delta); static void movePlayer(Vector direction, float delta);
static Vector3D worldMovementDirection(float x, float y); static Vector worldMovementDirection(float x, float y);
void initPlayer() { void initPlayer() {
screenToWorldMovementTransform = identity(); screenToWorldMovementTransform = identity();
rotate(&screenToWorldMovementTransform, (Vector3D) { 0.0f, 1.0f, 0.0f }, - TAU / 8.0f); rotate(&screenToWorldMovementTransform, (Vector) { 0.0f, 1.0f, 0.0f }, - TAU / 8.0f);
worldMovementUp = worldMovementDirection(0.0f, 1.0f); worldMovementUp = worldMovementDirection(0.0f, 1.0f);
worldMovementDown = worldMovementDirection(0.0f, -1.0f); worldMovementDown = worldMovementDirection(0.0f, -1.0f);
@ -38,7 +38,7 @@ void spawnPlayer(Transform transform) {
} }
void updatePlayer(float delta) { void updatePlayer(float delta) {
Vector3D direction = { 0.0f, 0.0f, 0.0f }; Vector direction = { 0.0f, 0.0f, 0.0f };
if (movementDirection & DIRECTION_UP) { if (movementDirection & DIRECTION_UP) {
direction = addVectors(direction, worldMovementUp); direction = addVectors(direction, worldMovementUp);
} }
@ -62,14 +62,14 @@ void stopMovement(Direction direction) {
movementDirection &= ~direction; movementDirection &= ~direction;
} }
static void movePlayer(Vector3D direction, float delta) { static void movePlayer(Vector direction, float delta) {
direction = clampMagnitude(direction, 1.0f); direction = clampMagnitude(direction, 1.0f);
Vector3D displacement = scaleVector(direction, delta * movementSpeed); Vector displacement = scaleVector(direction, delta * movementSpeed);
translate(&playerCharacter->transform, displacement); translate(&playerCharacter->transform, displacement);
} }
static Vector3D worldMovementDirection(float x, float y) { static Vector worldMovementDirection(float x, float y) {
Vector3D direction = (Vector3D) { x, 0.0f, -y }; Vector direction = (Vector) { x, 0.0f, -y };
direction = normalized( direction = normalized(
applyTransform(screenToWorldMovementTransform, direction)); applyTransform(screenToWorldMovementTransform, direction));
return direction; return direction;