+++ /dev/null
-#include <stdlib.h>
-#include <assimp/cimport.h>
-#include <assimp/postprocess.h>
-
-#include "asset.h"
-#include "logger.h"
-#include "tga.h"
-
-static const AiScene* importScene(const char* path);
-static Vector3D convertAiVector3D(AiVector3D vect);
-static const char* replaceFileExtension(const AiString path, const char* ext);
-
-
-
-const Asset3D* importAsset(const char* path) {
- const AiScene* scene = importScene(path);
- if (scene == NULL) {
- return NULL;
- }
-
- const unsigned int numMeshes = scene->mNumMeshes;
- const unsigned int numMaterials = scene->mNumMaterials;
-
- Asset3D* asset = malloc(sizeof(Asset3D));
- asset->numMeshes = numMeshes;
- asset->meshes = malloc(numMeshes * sizeof(Mesh));
- asset->numMaterials = numMaterials;
- asset->materials = malloc(numMaterials * sizeof(Material));
-
- for (unsigned int meshIndex = 0; meshIndex < numMeshes; ++meshIndex) {
- const AiMesh* aiMesh = scene->mMeshes[meshIndex];
- const unsigned int numVertices = aiMesh->mNumVertices;
- const unsigned int numFaces = aiMesh->mNumFaces;
-
- Mesh mesh = { .numVertices = numVertices,
- .vertices = malloc(numVertices * sizeof(Vector3D)),
- .normals = NULL,
- .textureCoords = NULL,
- .numFaces = numFaces,
- .faces = malloc(numFaces * sizeof(Face)),
- .materialIndex = aiMesh->mMaterialIndex };
-
- for (unsigned int vertIndex = 0; vertIndex < numVertices; ++vertIndex) {
- mesh.vertices[vertIndex] = convertAiVector3D(
- aiMesh->mVertices[vertIndex]);
- }
-
- if (aiMesh->mNormals != NULL) {
- mesh.normals = malloc(numVertices * sizeof(Vector3D));
- for (unsigned int normIndex = 0; normIndex < numVertices; ++normIndex) {
- mesh.normals[normIndex] = convertAiVector3D(
- aiMesh->mNormals[normIndex]);
- }
- }
-
- mesh.textureCoords = malloc(numVertices * sizeof(Vector3D));
- for (unsigned int texcIndex = 0; texcIndex < numVertices; ++texcIndex) {
- mesh.textureCoords[texcIndex] = convertAiVector3D(
- aiMesh->mTextureCoords[0][texcIndex]);
- }
-
- for (unsigned int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
- const AiFace aiFace = aiMesh->mFaces[faceIndex];
- const unsigned int numIndices = aiFace.mNumIndices;
-
- Face face = { .numIndices = numIndices,
- .indices = malloc(numIndices
- * sizeof(unsigned int)) };
-
- for (unsigned int i = 0; i < numIndices; ++i) {
- face.indices[i] = aiFace.mIndices[i];
- }
-
- mesh.faces[faceIndex] = face;
- }
-
- asset->meshes[meshIndex] = mesh;
- }
-
- GLuint* textureIds = malloc(numMaterials * sizeof(GLuint));
- glGenTextures(numMaterials, textureIds);
-
- for (unsigned int matIndex = 0; matIndex < numMaterials; ++matIndex) {
- Material material = { .textureId = textureIds[matIndex] };
-
- glBindTexture(GL_TEXTURE_2D, material.textureId);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-
- AiString originalTexturePath;
- if (aiGetMaterialTexture(scene->mMaterials[matIndex],
- aiTextureType_DIFFUSE,
- 0,
- &originalTexturePath,
- NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
- const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
- const size_t textureFileLength = strlen(textureFile);
- char* texturePath = malloc(strlen("assets/") + textureFileLength + 1);
- strcpy(texturePath, "assets/");
- strncat(texturePath, textureFile, textureFileLength);
-
- TgaImage* textureImage = readTga(texturePath);
- if (textureImage == NULL) {
- logError("Asset texture file not found: %s", texturePath);
- }
- else {
- glTexImage2D(GL_TEXTURE_2D,
- 0,
- textureImage->imageComponents,
- textureImage->header.imageWidth,
- textureImage->header.imageHeight,
- 0,
- textureImage->imageFormat,
- GL_UNSIGNED_BYTE,
- textureImage->bytes);
- free(textureImage->bytes);
- free(textureImage);
- }
- }
-
- asset->materials[matIndex] = material;
- }
- glBindTexture(GL_TEXTURE_2D, 0);
-
- aiReleaseImport(scene);
- return asset;
-}
-
-static const AiScene* importScene(const char* path) {
- const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
- if (scene == NULL) {
- logError("Failed to import asset from %s", path);
- }
- else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
- logError("Incomplete scene imported from %s", path);
- aiReleaseImport(scene);
- scene = NULL;
- }
- return scene;
-}
-
-static Vector3D convertAiVector3D(AiVector3D vect) {
- return (Vector3D) { .x = vect.x,
- .y = vect.y,
- .z = vect.z };
-}
-
-/** BUGS
- * The following function will not work properly with texture
- * file names (excluding directory part) beginning with '.'
- */
-static const char* replaceFileExtension(const AiString path, const char* ext) {
- size_t lengthToCopy = path.length;
-
- char* lastDotSubstr = strrchr(path.data, '.');
- if (lastDotSubstr != NULL) {
- if (strpbrk(lastDotSubstr, "\\/") == NULL) {
- lengthToCopy = lastDotSubstr - path.data;
- }
- }
-
- size_t extLength = strlen(ext) + 1;
- char* newPath = malloc(lengthToCopy + extLength);
- strncpy(newPath, path.data, lengthToCopy);
- strncpy(newPath + lengthToCopy, ext, extLength);
-
- return newPath;
-}
+++ /dev/null
-#ifndef ASSET_H_
-#define ASSET_H_
-
-#include <GL/gl.h>
-
-#include "assimp_types.h"
-
-#include "typedefs.h"
-
-typedef struct Asset3D Asset3D;
-typedef struct Mesh Mesh;
-typedef struct Face Face;
-typedef struct Material Material;
-
-struct Asset3D {
- unsigned int numMeshes;
- Mesh* meshes;
- unsigned int numMaterials;
- Material* materials;
-};
-
-struct Mesh {
- unsigned int numVertices;
- Vector3D* vertices;
- Vector3D* normals;
- Vector3D* textureCoords;
- unsigned int numFaces;
- Face* faces;
- unsigned int materialIndex;
-};
-
-struct Face {
- unsigned int numIndices;
- unsigned int* indices;
-};
-
-struct Material {
- GLuint textureId;
-};
-
-const Asset3D* importAsset(const char* path);
-
-#endif
+++ /dev/null
-#ifndef ASSIMP_TYPES_H_
-#define ASSIMP_TYPES_H_
-
-#include <assimp/scene.h>
-
-#ifdef ASSIMP_DOUBLE_PRECISION
- #error "ASSIMP_DOUBLE_PRECISION is defined"
- #error "shadowclad relies on ai_real defined as a single precision float"
-#endif
-
-typedef struct aiScene AiScene;
-typedef struct aiNode AiNode;
-typedef struct aiMesh AiMesh;
-typedef struct aiFace AiFace;
-typedef struct aiVector3D AiVector3D;
-typedef struct aiString AiString;
-
-#endif
+++ /dev/null
-#include <GL/gl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-char* getGlInfoString() {
- const char* glVersion = (const char*) glGetString(GL_VERSION);
- const char* glslVersion = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);
- const char* glRenderer = (const char*) glGetString(GL_RENDERER);
-
- size_t glInfoLength = strlen("OpenGL - GLSL - ")
- + strlen(glVersion)
- + strlen(glslVersion)
- + strlen(glRenderer);
-
- char* glInfoString = malloc(glInfoLength + sizeof(char));
- sprintf(glInfoString,
- "OpenGL %s - GLSL %s - %s",
- glVersion,
- glslVersion,
- glRenderer);
-
- return glInfoString;
-}
+++ /dev/null
-#ifndef DEBUGUTIL_H_
-#define DEBUGUTIL_H_
-
-#include <stdio.h>
-
-char* getGlInfoString();
-
-#endif
--- /dev/null
+#include <stdlib.h>
+#include <assimp/cimport.h>
+#include <assimp/postprocess.h>
+
+#include "asset.h"
+#include "logger.h"
+#include "tga.h"
+
+static const AiScene* importScene(const char* path);
+static Vector3D convertAiVector3D(AiVector3D vect);
+static const char* replaceFileExtension(const AiString path, const char* ext);
+
+
+
+const Asset3D* importAsset(const char* path) {
+ const AiScene* scene = importScene(path);
+ if (scene == NULL) {
+ return NULL;
+ }
+
+ const unsigned int numMeshes = scene->mNumMeshes;
+ const unsigned int numMaterials = scene->mNumMaterials;
+
+ Asset3D* asset = malloc(sizeof(Asset3D));
+ asset->numMeshes = numMeshes;
+ asset->meshes = malloc(numMeshes * sizeof(Mesh));
+ asset->numMaterials = numMaterials;
+ asset->materials = malloc(numMaterials * sizeof(Material));
+
+ for (unsigned int meshIndex = 0; meshIndex < numMeshes; ++meshIndex) {
+ const AiMesh* aiMesh = scene->mMeshes[meshIndex];
+ const unsigned int numVertices = aiMesh->mNumVertices;
+ const unsigned int numFaces = aiMesh->mNumFaces;
+
+ Mesh mesh = { .numVertices = numVertices,
+ .vertices = malloc(numVertices * sizeof(Vector3D)),
+ .normals = NULL,
+ .textureCoords = NULL,
+ .numFaces = numFaces,
+ .faces = malloc(numFaces * sizeof(Face)),
+ .materialIndex = aiMesh->mMaterialIndex };
+
+ for (unsigned int vertIndex = 0; vertIndex < numVertices; ++vertIndex) {
+ mesh.vertices[vertIndex] = convertAiVector3D(
+ aiMesh->mVertices[vertIndex]);
+ }
+
+ if (aiMesh->mNormals != NULL) {
+ mesh.normals = malloc(numVertices * sizeof(Vector3D));
+ for (unsigned int normIndex = 0; normIndex < numVertices; ++normIndex) {
+ mesh.normals[normIndex] = convertAiVector3D(
+ aiMesh->mNormals[normIndex]);
+ }
+ }
+
+ mesh.textureCoords = malloc(numVertices * sizeof(Vector3D));
+ for (unsigned int texcIndex = 0; texcIndex < numVertices; ++texcIndex) {
+ mesh.textureCoords[texcIndex] = convertAiVector3D(
+ aiMesh->mTextureCoords[0][texcIndex]);
+ }
+
+ for (unsigned int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
+ const AiFace aiFace = aiMesh->mFaces[faceIndex];
+ const unsigned int numIndices = aiFace.mNumIndices;
+
+ Face face = { .numIndices = numIndices,
+ .indices = malloc(numIndices
+ * sizeof(unsigned int)) };
+
+ for (unsigned int i = 0; i < numIndices; ++i) {
+ face.indices[i] = aiFace.mIndices[i];
+ }
+
+ mesh.faces[faceIndex] = face;
+ }
+
+ asset->meshes[meshIndex] = mesh;
+ }
+
+ GLuint* textureIds = malloc(numMaterials * sizeof(GLuint));
+ glGenTextures(numMaterials, textureIds);
+
+ for (unsigned int matIndex = 0; matIndex < numMaterials; ++matIndex) {
+ Material material = { .textureId = textureIds[matIndex] };
+
+ glBindTexture(GL_TEXTURE_2D, material.textureId);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+ AiString originalTexturePath;
+ if (aiGetMaterialTexture(scene->mMaterials[matIndex],
+ aiTextureType_DIFFUSE,
+ 0,
+ &originalTexturePath,
+ NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
+ const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
+ const size_t textureFileLength = strlen(textureFile);
+ char* texturePath = malloc(strlen("assets/") + textureFileLength + 1);
+ strcpy(texturePath, "assets/");
+ strncat(texturePath, textureFile, textureFileLength);
+
+ TgaImage* textureImage = readTga(texturePath);
+ if (textureImage == NULL) {
+ logError("Asset texture file not found: %s", texturePath);
+ }
+ else {
+ glTexImage2D(GL_TEXTURE_2D,
+ 0,
+ textureImage->imageComponents,
+ textureImage->header.imageWidth,
+ textureImage->header.imageHeight,
+ 0,
+ textureImage->imageFormat,
+ GL_UNSIGNED_BYTE,
+ textureImage->bytes);
+ free(textureImage->bytes);
+ free(textureImage);
+ }
+ }
+
+ asset->materials[matIndex] = material;
+ }
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ aiReleaseImport(scene);
+ return asset;
+}
+
+static const AiScene* importScene(const char* path) {
+ const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
+ if (scene == NULL) {
+ logError("Failed to import asset from %s", path);
+ }
+ else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
+ logError("Incomplete scene imported from %s", path);
+ aiReleaseImport(scene);
+ scene = NULL;
+ }
+ return scene;
+}
+
+static Vector3D convertAiVector3D(AiVector3D vect) {
+ return (Vector3D) { .x = vect.x,
+ .y = vect.y,
+ .z = vect.z };
+}
+
+/** BUGS
+ * The following function will not work properly with texture
+ * file names (excluding directory part) beginning with '.'
+ */
+static const char* replaceFileExtension(const AiString path, const char* ext) {
+ size_t lengthToCopy = path.length;
+
+ char* lastDotSubstr = strrchr(path.data, '.');
+ if (lastDotSubstr != NULL) {
+ if (strpbrk(lastDotSubstr, "\\/") == NULL) {
+ lengthToCopy = lastDotSubstr - path.data;
+ }
+ }
+
+ size_t extLength = strlen(ext) + 1;
+ char* newPath = malloc(lengthToCopy + extLength);
+ strncpy(newPath, path.data, lengthToCopy);
+ strncpy(newPath + lengthToCopy, ext, extLength);
+
+ return newPath;
+}
--- /dev/null
+#ifndef ASSET_H_
+#define ASSET_H_
+
+#include <GL/gl.h>
+
+#include "assimp_types.h"
+
+#include "typedefs.h"
+
+typedef struct Asset3D Asset3D;
+typedef struct Mesh Mesh;
+typedef struct Face Face;
+typedef struct Material Material;
+
+struct Asset3D {
+ unsigned int numMeshes;
+ Mesh* meshes;
+ unsigned int numMaterials;
+ Material* materials;
+};
+
+struct Mesh {
+ unsigned int numVertices;
+ Vector3D* vertices;
+ Vector3D* normals;
+ Vector3D* textureCoords;
+ unsigned int numFaces;
+ Face* faces;
+ unsigned int materialIndex;
+};
+
+struct Face {
+ unsigned int numIndices;
+ unsigned int* indices;
+};
+
+struct Material {
+ GLuint textureId;
+};
+
+const Asset3D* importAsset(const char* path);
+
+#endif
--- /dev/null
+#ifndef ASSIMP_TYPES_H_
+#define ASSIMP_TYPES_H_
+
+#include <assimp/scene.h>
+
+#ifdef ASSIMP_DOUBLE_PRECISION
+ #error "ASSIMP_DOUBLE_PRECISION is defined"
+ #error "shadowclad relies on ai_real defined as a single precision float"
+#endif
+
+typedef struct aiScene AiScene;
+typedef struct aiNode AiNode;
+typedef struct aiMesh AiMesh;
+typedef struct aiFace AiFace;
+typedef struct aiVector3D AiVector3D;
+typedef struct aiString AiString;
+
+#endif
--- /dev/null
+#include <GL/gl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char* getGlInfoString() {
+ const char* glVersion = (const char*) glGetString(GL_VERSION);
+ const char* glslVersion = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);
+ const char* glRenderer = (const char*) glGetString(GL_RENDERER);
+
+ size_t glInfoLength = strlen("OpenGL - GLSL - ")
+ + strlen(glVersion)
+ + strlen(glslVersion)
+ + strlen(glRenderer);
+
+ char* glInfoString = malloc(glInfoLength + sizeof(char));
+ sprintf(glInfoString,
+ "OpenGL %s - GLSL %s - %s",
+ glVersion,
+ glslVersion,
+ glRenderer);
+
+ return glInfoString;
+}
--- /dev/null
+#ifndef DEBUGUTIL_H_
+#define DEBUGUTIL_H_
+
+#include <stdio.h>
+
+char* getGlInfoString();
+
+#endif
--- /dev/null
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "logger.h"
+
+LogLevel logLevel = LOGLEVEL_DEBUG;
+
+
+
+void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...) {
+ if (msgLevel > logLevel) {
+ return;
+ }
+
+ const char* msgLevelString;
+ switch (msgLevel) {
+ case LOGLEVEL_ERROR:
+ msgLevelString = "error: ";
+ break;
+ case LOGLEVEL_WARNING:
+ msgLevelString = "warning: ";
+ break;
+ case LOGLEVEL_INFO:
+ msgLevelString = "";
+ break;
+ case LOGLEVEL_DEBUG:
+ msgLevelString = "debug: ";
+ break;
+ default:
+ msgLevelString = "(invalid message level!) ";
+ break;
+ }
+
+ va_list args;
+ va_start(args, message);
+
+ fprintf(stderr, "%s:%i: %s", file, line, msgLevelString);
+ vfprintf(stderr, message, args);
+ fputc('\n', stderr);
+
+ va_end(args);
+}
--- /dev/null
+#ifndef LOGGER_H_
+#define LOGGER_H_
+
+typedef enum {
+ LOGLEVEL_ERROR,
+ LOGLEVEL_WARNING,
+ LOGLEVEL_INFO,
+ LOGLEVEL_DEBUG
+} LogLevel;
+
+LogLevel logLevel;
+
+#define logError(...) logMessage(LOGLEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
+#define logWarning(...) logMessage(LOGLEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)
+#define logInfo(...) logMessage(LOGLEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
+#define logDebug(...) logMessage(LOGLEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
+
+void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...);
+
+#endif
--- /dev/null
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 199309L
+#endif
+
+#include <stdbool.h>
+#include <stdio.h> // TODO remove
+#include <time.h>
+
+#include "logger.h"
+
+typedef struct timespec Timepoint;
+
+static Timepoint lastDisplayTime;
+static int frames = 0;
+static bool meteringEnabled = false;
+
+
+
+void initPerformanceMetering() {
+ if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
+ logWarning("Clock read failed, performance metering unavailable");
+ }
+ else {
+ meteringEnabled = true;
+ }
+}
+
+void frameRendered() {
+ if (meteringEnabled) {
+ ++frames;
+ Timepoint now;
+
+ if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
+ logWarning("Clock read failed, stopping performance metering");
+ meteringEnabled = false;
+ return;
+ }
+
+ time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
+ if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
+
+ if (fullSeconds > 0) {
+ float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
+ seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
+ // This goes to STDOUT because it's, uh, temporary
+ printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
+ lastDisplayTime = now;
+ frames = 0;
+ }
+ }
+}
--- /dev/null
+#ifndef PERFORMANCE_H_
+#define PERFORMANCE_H_
+
+void initPerformanceMetering();
+void frameRendered();
+
+#endif
--- /dev/null
+#include <GL/glut.h>
+#include <stdbool.h>
+
+#include "level.h"
+#include "performance.h"
+#include "player.h"
+#include "typedefs.h"
+
+const float AXIS_RADIUS = 5.0f;
+
+static void setupCamera();
+static void moveCameraTo(const Vector3D pos);
+static void drawAxes();
+static void renderBlockGrid(const BlockGrid grid);
+static void renderCharacter(const Character* character, const Vector3D pos);
+static void drawAsset3D(const Asset3D* asset3D);
+
+float viewportAspectRatio = 1.0f;
+
+
+
+void initRender() {
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+ GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
+ GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
+ GLfloat light0_specular[] = {0.96f, 0.98f, 1.0f, 1.0f};
+ GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 0.0f}; // (w == 0.0f) == directional
+
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
+ glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
+
+ glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
+ glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
+ glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
+}
+
+void renderScene() {
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glEnable(GL_NORMALIZE);
+ glEnable(GL_CULL_FACE);
+ glEnable(GL_DEPTH_TEST);
+
+ setupCamera();
+ moveCameraTo(playerPos);
+
+ glDisable(GL_LIGHTING);
+ drawAxes();
+ glEnable(GL_LIGHTING);
+
+ glEnable(GL_LIGHT0);
+ glEnable(GL_TEXTURE_2D);
+ renderBlockGrid(levelGrid);
+ renderCharacter(&playerCharacter, playerPos);
+ glDisable(GL_TEXTURE_2D);
+ glDisable(GL_LIGHT0);
+
+ glFlush();
+ glutSwapBuffers();
+ frameRendered();
+ glutPostRedisplay();
+}
+
+static void setupCamera() {
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-16.0,
+ 16.0,
+ -16.0/viewportAspectRatio,
+ 16.0/viewportAspectRatio,
+ -128.0,
+ 128.0);
+ glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
+ glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
+}
+
+static void moveCameraTo(const Vector3D pos) {
+ glMatrixMode(GL_PROJECTION);
+ glTranslatef(-pos.x, -pos.y, -pos.z);
+}
+
+static void drawAxes() {
+ glMatrixMode(GL_MODELVIEW);
+ // X axis
+ glColor3f(1.0f, 0.0f, 0.0f);
+ glBegin(GL_LINES);
+ glVertex3f(0.0f, 0.0f, 0.0f);
+ glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
+ glEnd();
+ // Y axis
+ glColor3f(0.0f, 1.0f, 0.0f);
+ glBegin(GL_LINES);
+ glVertex3f(0.0f, 0.0f, 0.0f);
+ glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
+ glEnd();
+ // Z axis
+ glColor3f(0.0f, 0.0f, 1.0f);
+ glBegin(GL_LINES);
+ glVertex3f(0.0f, 0.0f, 0.0f);
+ glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
+ glEnd();
+}
+
+static void renderBlockGrid(const BlockGrid grid) {
+ glMatrixMode(GL_MODELVIEW);
+ for (int z = 0; z < grid.depth; ++z) {
+ glLoadIdentity();
+ glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
+ for (int x = 0; x < grid.width; ++x) {
+ drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
+ glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
+ }
+ }
+ glLoadIdentity();
+}
+
+static void renderCharacter(const Character* character, const Vector3D pos) {
+ glMatrixMode(GL_MODELVIEW);
+ glTranslatef(pos.x, pos.y, pos.z);
+ drawAsset3D(character->asset3D);
+ glLoadIdentity();
+}
+
+static void drawAsset3D(const Asset3D* asset3D) {
+ if (asset3D == NULL) {
+ return;
+ }
+
+ glMatrixMode(GL_MODELVIEW);
+ glColor3f(0.5f, 1.0f, 0.0f);
+
+ for (size_t meshIndex = 0; meshIndex < asset3D->numMeshes; ++meshIndex) {
+ const Mesh mesh = asset3D->meshes[meshIndex];
+ glBindTexture(GL_TEXTURE_2D,
+ asset3D->materials[mesh.materialIndex].textureId);
+ bool hasNormals = mesh.normals != NULL;
+ bool hasTextureCoords = mesh.textureCoords != NULL;
+
+ for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
+ const Face face = mesh.faces[faceIndex];
+
+ GLenum faceMode;
+ switch (face.numIndices) {
+ case 1: faceMode = GL_POINTS; break;
+ case 2: faceMode = GL_LINES; break;
+ case 3: faceMode = GL_TRIANGLES; break;
+ default: faceMode = GL_POLYGON; break;
+ }
+
+ glBegin(faceMode);
+
+ for (size_t i = 0; i < face.numIndices; ++i) {
+ unsigned int vertIndex = face.indices[i];
+ if (hasNormals) {
+ if (hasTextureCoords) {
+ Vector3D coords = mesh.textureCoords[vertIndex];
+ glTexCoord2f(coords.x, coords.y);
+ }
+ Vector3D normal = mesh.normals[vertIndex];
+ glNormal3f(normal.x, normal.y, normal.z);
+ }
+ Vector3D vertex = mesh.vertices[vertIndex];
+ glVertex3f(vertex.x, vertex.y, vertex.z);
+ }
+
+ glEnd();
+ }
+ }
+ glBindTexture(GL_TEXTURE_2D, 0);
+}
--- /dev/null
+#ifndef RENDER_H_
+#define RENDER_H_
+
+float viewportAspectRatio;
+
+void initRender();
+void renderScene();
+
+#endif
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "tga.h"
+
+TgaImage* readTga(const char* path) {
+ FILE* tgaFile = fopen(path, "rb");
+ if (tgaFile == NULL) {
+ return NULL;
+ }
+
+ TgaHeader header;
+
+ if (fread(&header, sizeof(TgaHeader), 1, tgaFile) != 1) {
+ fclose(tgaFile);
+ return NULL;
+ }
+
+ GLenum imageFormat;
+ GLint imageComponents;
+
+ switch (header.imageBpp) {
+ case 32:
+ imageFormat = GL_BGRA;
+ imageComponents = GL_RGBA8;
+ break;
+ case 24:
+ imageFormat = GL_BGR;
+ imageComponents = GL_RGB8;
+ break;
+ case 8:
+ imageFormat = GL_LUMINANCE;
+ imageComponents = GL_LUMINANCE8;
+ break;
+ default:
+ fclose(tgaFile);
+ return NULL;
+ }
+
+ unsigned long imageSize = header.imageWidth * header.imageHeight * (header.imageBpp >> 3);
+
+ GLbyte* bytes = malloc(imageSize * sizeof(GLbyte));
+ if (bytes == NULL) {
+ fclose(tgaFile);
+ return NULL;
+ }
+
+ if (fread(bytes, imageSize, 1, tgaFile) != 1) {
+ free(bytes);
+ fclose(tgaFile);
+ return NULL;
+ }
+
+ fclose(tgaFile);
+
+ TgaImage* image = malloc(sizeof(TgaImage));
+ if (image == NULL) {
+ return NULL;
+ }
+
+ (*image).header = header;
+ (*image).imageFormat = imageFormat;
+ (*image).imageComponents = imageComponents;
+ (*image).bytes = bytes;
+
+ return image;
+}
--- /dev/null
+#ifndef TGA_H_
+#define TGA_H_
+
+#include <GL/gl.h>
+
+#pragma pack(push, 1)
+typedef struct {
+ GLubyte idLength;
+ GLbyte colorMapType;
+ GLbyte imageType;
+ GLushort colorMapStart;
+ GLushort colorMapLength;
+ GLubyte colorMapBpp;
+ GLushort originX;
+ GLushort originY;
+ GLushort imageWidth;
+ GLushort imageHeight;
+ GLubyte imageBpp;
+ GLbyte imageDescriptor;
+} TgaHeader;
+#pragma pack(pop)
+
+typedef struct {
+ TgaHeader header;
+ GLenum imageFormat;
+ GLint imageComponents;
+ GLbyte* bytes;
+} TgaImage;
+
+TgaImage* readTga(const char* path);
+
+#endif
--- /dev/null
+#ifndef TYPEDEFS_H_
+#define TYPEDEFS_H_
+
+typedef struct {
+ float x;
+ float y;
+ float z;
+} Vector3D;
+
+#endif
--- /dev/null
+#include <GL/gl.h>
+
+#include "render.h"
+
+void resizeStage(GLsizei width, GLsizei height) {
+ if (height == 0)
+ height = 1;
+
+ glViewport(0, 0, width, height);
+
+ viewportAspectRatio = (float) width / (float) height;
+}
--- /dev/null
+#ifndef GLUT_JANITOR_H_
+#define GLUT_JANITOR_H_
+
+#include <GL/gl.h>
+
+void resizeStage(GLsizei width, GLsizei height);
+
+#endif
--- /dev/null
+#include <GL/gl.h>
+#include <stdlib.h>
+
+#include "level.h"
+#include "logger.h"
+#include "player.h"
+
+static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
+ .asset3D = NULL };
+static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
+ .asset3D = NULL };
+
+static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
+ &blockEmpty, &blockEmpty, &blockEmpty,
+ &blockWall01, &blockEmpty, &blockWall01 };
+
+BlockGrid levelGrid = { .width = 3,
+ .depth = 3,
+ .blocks = testBlocks };
+
+#define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
+Vector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
+
+
+
+void initLevel() {
+ blockWall01.asset3D = importAsset("assets/wall01.3ds");
+
+ buildLevelFromImage(readTga("assets/level01.tga"));
+}
+
+void buildLevelFromImage(TgaImage* image) {
+ if (image == NULL) {
+ logError("Null image received, cannot build level");
+ return;
+ }
+
+ if (image->header.imageBpp != 32) {
+ logError("Invalid level image format (%d bpp)", image->header.imageBpp);
+ return;
+ }
+
+ BlockGrid newGrid = { .width = image->header.imageWidth,
+ .depth = image->header.imageHeight,
+ .blocks = malloc(image->header.imageWidth
+ * image->header.imageHeight
+ * sizeof(Block*)) };
+ playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
+
+ for (int row = 0; row < newGrid.depth; ++row) {
+ for (int x = 0; x < newGrid.width; ++x) {
+ // Flip the image vertically due to (0, 0) being bottom left
+ int z = newGrid.depth - row - 1;
+
+ uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
+ Block* block;
+ switch (pixelColorARGB) {
+ case 0xFFFF0000:
+ block = &blockWall01;
+ break;
+ case 0xFF00FFFF:
+ block = &blockEmpty;
+ playerSpawnPos = (Vector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
+ break;
+ default:
+ block = &blockEmpty;
+ break;
+ }
+ setBlockInGrid(newGrid, x, z, block);
+ }
+ }
+
+ levelGrid = newGrid;
+ spawnPlayer();
+}
--- /dev/null
+#ifndef LEVEL_H_
+#define LEVEL_H_
+
+#include <stdint.h>
+
+#include "asset.h"
+#include "tga.h"
+
+typedef enum {
+ BLOCKTYPE_SPACE,
+ BLOCKTYPE_OBSTACLE_X,
+ BLOCKTYPE_OBSTACLE_Z,
+ BLOCKTYPE_OBSTACLE
+} BlockType;
+
+typedef struct {
+ const BlockType type;
+ const Asset3D* asset3D;
+} Block;
+
+typedef struct {
+ int width;
+ int depth;
+ Block** blocks;
+} BlockGrid;
+
+#define BLOCKGRID_CELL_SIZE 2.5f
+
+BlockGrid levelGrid;
+Vector3D playerSpawnPos;
+
+void initLevel();
+void buildLevelFromImage(TgaImage* image);
+
+static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) {
+ return grid.blocks[(z * grid.width) + x];
+}
+
+static inline void setBlockInGrid(BlockGrid grid, int x, int z, Block* block) {
+ grid.blocks[(z * grid.width) + x] = block;
+}
+
+#endif
--- /dev/null
+#include "level.h"
+#include "logger.h"
+#include "player.h"
+
+Character playerCharacter = { .asset3D = NULL };
+
+
+
+void initPlayer() {
+ playerCharacter.asset3D = importAsset("assets/playercharacter.3ds");
+}
+
+void spawnPlayer() {
+ playerPos = playerSpawnPos;
+}
--- /dev/null
+#ifndef PLAYER_H_
+#define PLAYER_H_
+
+#include <GL/gl.h>
+
+#include "asset.h"
+
+typedef struct {
+ const Asset3D* asset3D;
+} Character;
+
+Character playerCharacter;
+Vector3D playerPos;
+
+void initPlayer();
+void spawnPlayer();
+
+#endif
+++ /dev/null
-#include <GL/gl.h>
-#include <stdlib.h>
-
-#include "level.h"
-#include "logger.h"
-#include "player.h"
-
-static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
- .asset3D = NULL };
-static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
- .asset3D = NULL };
-
-static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
- &blockEmpty, &blockEmpty, &blockEmpty,
- &blockWall01, &blockEmpty, &blockWall01 };
-
-BlockGrid levelGrid = { .width = 3,
- .depth = 3,
- .blocks = testBlocks };
-
-#define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
-Vector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
-
-
-
-void initLevel() {
- blockWall01.asset3D = importAsset("assets/wall01.3ds");
-
- buildLevelFromImage(readTga("assets/level01.tga"));
-}
-
-void buildLevelFromImage(TgaImage* image) {
- if (image == NULL) {
- logError("Null image received, cannot build level");
- return;
- }
-
- if (image->header.imageBpp != 32) {
- logError("Invalid level image format (%d bpp)", image->header.imageBpp);
- return;
- }
-
- BlockGrid newGrid = { .width = image->header.imageWidth,
- .depth = image->header.imageHeight,
- .blocks = malloc(image->header.imageWidth
- * image->header.imageHeight
- * sizeof(Block*)) };
- playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
-
- for (int row = 0; row < newGrid.depth; ++row) {
- for (int x = 0; x < newGrid.width; ++x) {
- // Flip the image vertically due to (0, 0) being bottom left
- int z = newGrid.depth - row - 1;
-
- uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
- Block* block;
- switch (pixelColorARGB) {
- case 0xFFFF0000:
- block = &blockWall01;
- break;
- case 0xFF00FFFF:
- block = &blockEmpty;
- playerSpawnPos = (Vector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
- break;
- default:
- block = &blockEmpty;
- break;
- }
- setBlockInGrid(newGrid, x, z, block);
- }
- }
-
- levelGrid = newGrid;
- spawnPlayer();
-}
+++ /dev/null
-#ifndef LEVEL_H_
-#define LEVEL_H_
-
-#include <stdint.h>
-
-#include "asset.h"
-#include "tga.h"
-
-typedef enum {
- BLOCKTYPE_SPACE,
- BLOCKTYPE_OBSTACLE_X,
- BLOCKTYPE_OBSTACLE_Z,
- BLOCKTYPE_OBSTACLE
-} BlockType;
-
-typedef struct {
- const BlockType type;
- const Asset3D* asset3D;
-} Block;
-
-typedef struct {
- int width;
- int depth;
- Block** blocks;
-} BlockGrid;
-
-#define BLOCKGRID_CELL_SIZE 2.5f
-
-BlockGrid levelGrid;
-Vector3D playerSpawnPos;
-
-void initLevel();
-void buildLevelFromImage(TgaImage* image);
-
-static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) {
- return grid.blocks[(z * grid.width) + x];
-}
-
-static inline void setBlockInGrid(BlockGrid grid, int x, int z, Block* block) {
- grid.blocks[(z * grid.width) + x] = block;
-}
-
-#endif
+++ /dev/null
-#include <stdarg.h>
-#include <stdio.h>
-
-#include "logger.h"
-
-LogLevel logLevel = LOGLEVEL_DEBUG;
-
-
-
-void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...) {
- if (msgLevel > logLevel) {
- return;
- }
-
- const char* msgLevelString;
- switch (msgLevel) {
- case LOGLEVEL_ERROR:
- msgLevelString = "error: ";
- break;
- case LOGLEVEL_WARNING:
- msgLevelString = "warning: ";
- break;
- case LOGLEVEL_INFO:
- msgLevelString = "";
- break;
- case LOGLEVEL_DEBUG:
- msgLevelString = "debug: ";
- break;
- default:
- msgLevelString = "(invalid message level!) ";
- break;
- }
-
- va_list args;
- va_start(args, message);
-
- fprintf(stderr, "%s:%i: %s", file, line, msgLevelString);
- vfprintf(stderr, message, args);
- fputc('\n', stderr);
-
- va_end(args);
-}
+++ /dev/null
-#ifndef LOGGER_H_
-#define LOGGER_H_
-
-typedef enum {
- LOGLEVEL_ERROR,
- LOGLEVEL_WARNING,
- LOGLEVEL_INFO,
- LOGLEVEL_DEBUG
-} LogLevel;
-
-LogLevel logLevel;
-
-#define logError(...) logMessage(LOGLEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
-#define logWarning(...) logMessage(LOGLEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)
-#define logInfo(...) logMessage(LOGLEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
-#define logDebug(...) logMessage(LOGLEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
-
-void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...);
-
-#endif
+++ /dev/null
-#ifndef _POSIX_C_SOURCE
-#define _POSIX_C_SOURCE 199309L
-#endif
-
-#include <stdbool.h>
-#include <stdio.h> // TODO remove
-#include <time.h>
-
-#include "logger.h"
-
-typedef struct timespec Timepoint;
-
-static Timepoint lastDisplayTime;
-static int frames = 0;
-static bool meteringEnabled = false;
-
-
-
-void initPerformanceMetering() {
- if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
- logWarning("Clock read failed, performance metering unavailable");
- }
- else {
- meteringEnabled = true;
- }
-}
-
-void frameRendered() {
- if (meteringEnabled) {
- ++frames;
- Timepoint now;
-
- if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
- logWarning("Clock read failed, stopping performance metering");
- meteringEnabled = false;
- return;
- }
-
- time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
- if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
-
- if (fullSeconds > 0) {
- float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
- seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
- // This goes to STDOUT because it's, uh, temporary
- printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
- lastDisplayTime = now;
- frames = 0;
- }
- }
-}
+++ /dev/null
-#ifndef PERFORMANCE_H_
-#define PERFORMANCE_H_
-
-void initPerformanceMetering();
-void frameRendered();
-
-#endif
+++ /dev/null
-#include "level.h"
-#include "logger.h"
-#include "player.h"
-
-Character playerCharacter = { .asset3D = NULL };
-
-
-
-void initPlayer() {
- playerCharacter.asset3D = importAsset("assets/playercharacter.3ds");
-}
-
-void spawnPlayer() {
- playerPos = playerSpawnPos;
-}
+++ /dev/null
-#ifndef PLAYER_H_
-#define PLAYER_H_
-
-#include <GL/gl.h>
-
-#include "asset.h"
-
-typedef struct {
- const Asset3D* asset3D;
-} Character;
-
-Character playerCharacter;
-Vector3D playerPos;
-
-void initPlayer();
-void spawnPlayer();
-
-#endif
+++ /dev/null
-#include <GL/glut.h>
-#include <stdbool.h>
-
-#include "level.h"
-#include "performance.h"
-#include "player.h"
-#include "typedefs.h"
-
-const float AXIS_RADIUS = 5.0f;
-
-static void setupCamera();
-static void moveCameraTo(const Vector3D pos);
-static void drawAxes();
-static void renderBlockGrid(const BlockGrid grid);
-static void renderCharacter(const Character* character, const Vector3D pos);
-static void drawAsset3D(const Asset3D* asset3D);
-
-float viewportAspectRatio = 1.0f;
-
-
-
-void initRender() {
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-
- GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
- GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
- GLfloat light0_specular[] = {0.96f, 0.98f, 1.0f, 1.0f};
- GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 0.0f}; // (w == 0.0f) == directional
-
- glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
-
- glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
- glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
-}
-
-void renderScene() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- glEnable(GL_NORMALIZE);
- glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
-
- setupCamera();
- moveCameraTo(playerPos);
-
- glDisable(GL_LIGHTING);
- drawAxes();
- glEnable(GL_LIGHTING);
-
- glEnable(GL_LIGHT0);
- glEnable(GL_TEXTURE_2D);
- renderBlockGrid(levelGrid);
- renderCharacter(&playerCharacter, playerPos);
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_LIGHT0);
-
- glFlush();
- glutSwapBuffers();
- frameRendered();
- glutPostRedisplay();
-}
-
-static void setupCamera() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-16.0,
- 16.0,
- -16.0/viewportAspectRatio,
- 16.0/viewportAspectRatio,
- -128.0,
- 128.0);
- glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
- glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
-}
-
-static void moveCameraTo(const Vector3D pos) {
- glMatrixMode(GL_PROJECTION);
- glTranslatef(-pos.x, -pos.y, -pos.z);
-}
-
-static void drawAxes() {
- glMatrixMode(GL_MODELVIEW);
- // X axis
- glColor3f(1.0f, 0.0f, 0.0f);
- glBegin(GL_LINES);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
- glEnd();
- // Y axis
- glColor3f(0.0f, 1.0f, 0.0f);
- glBegin(GL_LINES);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
- glEnd();
- // Z axis
- glColor3f(0.0f, 0.0f, 1.0f);
- glBegin(GL_LINES);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
- glEnd();
-}
-
-static void renderBlockGrid(const BlockGrid grid) {
- glMatrixMode(GL_MODELVIEW);
- for (int z = 0; z < grid.depth; ++z) {
- glLoadIdentity();
- glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
- for (int x = 0; x < grid.width; ++x) {
- drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
- glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
- }
- }
- glLoadIdentity();
-}
-
-static void renderCharacter(const Character* character, const Vector3D pos) {
- glMatrixMode(GL_MODELVIEW);
- glTranslatef(pos.x, pos.y, pos.z);
- drawAsset3D(character->asset3D);
- glLoadIdentity();
-}
-
-static void drawAsset3D(const Asset3D* asset3D) {
- if (asset3D == NULL) {
- return;
- }
-
- glMatrixMode(GL_MODELVIEW);
- glColor3f(0.5f, 1.0f, 0.0f);
-
- for (size_t meshIndex = 0; meshIndex < asset3D->numMeshes; ++meshIndex) {
- const Mesh mesh = asset3D->meshes[meshIndex];
- glBindTexture(GL_TEXTURE_2D,
- asset3D->materials[mesh.materialIndex].textureId);
- bool hasNormals = mesh.normals != NULL;
- bool hasTextureCoords = mesh.textureCoords != NULL;
-
- for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
- const Face face = mesh.faces[faceIndex];
-
- GLenum faceMode;
- switch (face.numIndices) {
- case 1: faceMode = GL_POINTS; break;
- case 2: faceMode = GL_LINES; break;
- case 3: faceMode = GL_TRIANGLES; break;
- default: faceMode = GL_POLYGON; break;
- }
-
- glBegin(faceMode);
-
- for (size_t i = 0; i < face.numIndices; ++i) {
- unsigned int vertIndex = face.indices[i];
- if (hasNormals) {
- if (hasTextureCoords) {
- Vector3D coords = mesh.textureCoords[vertIndex];
- glTexCoord2f(coords.x, coords.y);
- }
- Vector3D normal = mesh.normals[vertIndex];
- glNormal3f(normal.x, normal.y, normal.z);
- }
- Vector3D vertex = mesh.vertices[vertIndex];
- glVertex3f(vertex.x, vertex.y, vertex.z);
- }
-
- glEnd();
- }
- }
- glBindTexture(GL_TEXTURE_2D, 0);
-}
+++ /dev/null
-#ifndef RENDER_H_
-#define RENDER_H_
-
-float viewportAspectRatio;
-
-void initRender();
-void renderScene();
-
-#endif
+++ /dev/null
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "tga.h"
-
-TgaImage* readTga(const char* path) {
- FILE* tgaFile = fopen(path, "rb");
- if (tgaFile == NULL) {
- return NULL;
- }
-
- TgaHeader header;
-
- if (fread(&header, sizeof(TgaHeader), 1, tgaFile) != 1) {
- fclose(tgaFile);
- return NULL;
- }
-
- GLenum imageFormat;
- GLint imageComponents;
-
- switch (header.imageBpp) {
- case 32:
- imageFormat = GL_BGRA;
- imageComponents = GL_RGBA8;
- break;
- case 24:
- imageFormat = GL_BGR;
- imageComponents = GL_RGB8;
- break;
- case 8:
- imageFormat = GL_LUMINANCE;
- imageComponents = GL_LUMINANCE8;
- break;
- default:
- fclose(tgaFile);
- return NULL;
- }
-
- unsigned long imageSize = header.imageWidth * header.imageHeight * (header.imageBpp >> 3);
-
- GLbyte* bytes = malloc(imageSize * sizeof(GLbyte));
- if (bytes == NULL) {
- fclose(tgaFile);
- return NULL;
- }
-
- if (fread(bytes, imageSize, 1, tgaFile) != 1) {
- free(bytes);
- fclose(tgaFile);
- return NULL;
- }
-
- fclose(tgaFile);
-
- TgaImage* image = malloc(sizeof(TgaImage));
- if (image == NULL) {
- return NULL;
- }
-
- (*image).header = header;
- (*image).imageFormat = imageFormat;
- (*image).imageComponents = imageComponents;
- (*image).bytes = bytes;
-
- return image;
-}
+++ /dev/null
-#ifndef TGA_H_
-#define TGA_H_
-
-#include <GL/gl.h>
-
-#pragma pack(push, 1)
-typedef struct {
- GLubyte idLength;
- GLbyte colorMapType;
- GLbyte imageType;
- GLushort colorMapStart;
- GLushort colorMapLength;
- GLubyte colorMapBpp;
- GLushort originX;
- GLushort originY;
- GLushort imageWidth;
- GLushort imageHeight;
- GLubyte imageBpp;
- GLbyte imageDescriptor;
-} TgaHeader;
-#pragma pack(pop)
-
-typedef struct {
- TgaHeader header;
- GLenum imageFormat;
- GLint imageComponents;
- GLbyte* bytes;
-} TgaImage;
-
-TgaImage* readTga(const char* path);
-
-#endif
+++ /dev/null
-#ifndef TYPEDEFS_H_
-#define TYPEDEFS_H_
-
-typedef struct {
- float x;
- float y;
- float z;
-} Vector3D;
-
-#endif
+++ /dev/null
-#include <GL/gl.h>
-
-#include "render.h"
-
-void resizeStage(GLsizei width, GLsizei height) {
- if (height == 0)
- height = 1;
-
- glViewport(0, 0, width, height);
-
- viewportAspectRatio = (float) width / (float) height;
-}
+++ /dev/null
-#ifndef GLUT_JANITOR_H_
-#define GLUT_JANITOR_H_
-
-#include <GL/gl.h>
-
-void resizeStage(GLsizei width, GLsizei height);
-
-#endif