Convert data counts and indices to size_t

master
outfrost 2020-04-30 02:36:41 +02:00
parent 9040283b71
commit c78af1fbe8
5 changed files with 30 additions and 27 deletions

View File

@ -3,6 +3,7 @@
#include <assimp/postprocess.h>
#include "asset.h"
#include "assimp_types.h"
#include "logger.h"
#include "tga.h"
@ -20,6 +21,8 @@ const Asset3D* importAsset(const char* path) {
const unsigned int numMeshes = scene->mNumMeshes;
const unsigned int numMaterials = scene->mNumMaterials;
// TODO Consider assets with some arrays empty, and prevent zero mallocs
Asset3D* asset = malloc(sizeof(Asset3D));
asset->numMeshes = numMeshes;
@ -64,8 +67,7 @@ const Asset3D* importAsset(const char* path) {
const unsigned int numIndices = aiFace.mNumIndices;
Face face = { .numIndices = numIndices,
.indices = malloc(numIndices
* sizeof(unsigned int)) };
.indices = malloc(numIndices * sizeof(size_t)) };
for (unsigned int i = 0; i < numIndices; ++i) {
face.indices[i] = aiFace.mIndices[i];
@ -92,11 +94,11 @@ const Asset3D* importAsset(const char* path) {
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);
const char* textureFilename = replaceFileExtension(originalTexturePath, ".tga");
const size_t textureFilenameLength = strlen(textureFilename);
char* texturePath = malloc(strlen("assets/") + textureFilenameLength + 1);
strcpy(texturePath, "assets/");
strncat(texturePath, textureFile, textureFileLength);
strncat(texturePath, textureFilename, textureFilenameLength);
TgaImage* textureImage = readTga(texturePath);
if (textureImage == NULL) {
@ -130,7 +132,7 @@ static const AiScene* importScene(const char* path) {
if (scene == NULL) {
logError("Failed to import asset from %s", path);
}
else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
else if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) {
logError("Incomplete scene imported from %s", path);
aiReleaseImport(scene);
scene = NULL;
@ -144,7 +146,8 @@ static Vector3D convertAiVector3D(AiVector3D vect) {
.z = vect.z };
}
/** BUGS
/**
* BUGS
* The following function will not work properly with texture
* file names (excluding directory part) beginning with '.'
*/

View File

@ -1,9 +1,9 @@
#ifndef ASSET_H_
#define ASSET_H_
#include <GL/gl.h>
#include <stddef.h>
#include "assimp_types.h"
#include <GL/gl.h>
#include "geometry.h"
@ -13,25 +13,25 @@ typedef struct Face Face;
typedef struct Material Material;
struct Asset3D {
unsigned int numMeshes;
size_t numMeshes;
Mesh* meshes;
unsigned int numMaterials;
size_t numMaterials;
Material* materials;
};
struct Mesh {
unsigned int numVertices;
size_t numVertices;
Vector3D* vertices;
Vector3D* normals;
Vector3D* textureCoords;
unsigned int numFaces;
size_t numFaces;
Face* faces;
unsigned int materialIndex;
size_t materialIndex;
};
struct Face {
unsigned int numIndices;
unsigned int* indices;
size_t numIndices;
size_t* indices;
};
struct Material {

View File

@ -107,10 +107,10 @@ static void drawAxes() {
static void renderBlockGrid(const BlockGrid grid) {
glMatrixMode(GL_MODELVIEW);
for (int z = 0; z < grid.depth; ++z) {
for (size_t z = 0; z < grid.depth; ++z) {
glLoadIdentity();
glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
for (int x = 0; x < grid.width; ++x) {
for (size_t x = 0; x < grid.width; ++x) {
drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
}
@ -154,7 +154,7 @@ static void drawAsset3D(const Asset3D* asset3D) {
glBegin(faceMode);
for (size_t i = 0; i < face.numIndices; ++i) {
unsigned int vertIndex = face.indices[i];
size_t vertIndex = face.indices[i];
if (hasNormals) {
if (hasTextureCoords) {
Vector3D coords = mesh.textureCoords[vertIndex];

View File

@ -48,10 +48,10 @@ void buildLevelFromImage(TgaImage* image) {
* sizeof(Block*)) };
playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
for (int row = 0; row < newGrid.depth; ++row) {
for (int x = 0; x < newGrid.width; ++x) {
for (size_t row = 0; row < newGrid.depth; ++row) {
for (size_t x = 0; x < newGrid.width; ++x) {
// Flip the image vertically due to (0, 0) being bottom left
int z = newGrid.depth - row - 1;
size_t z = newGrid.depth - row - 1;
uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
Block* block;

View File

@ -19,8 +19,8 @@ typedef struct {
} Block;
typedef struct {
int width;
int depth;
size_t width;
size_t depth;
Block** blocks;
} BlockGrid;
@ -32,11 +32,11 @@ Vector3D playerSpawnPos;
void initLevel();
void buildLevelFromImage(TgaImage* image);
static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) {
static inline Block* getBlockFromGrid(BlockGrid grid, size_t x, size_t z) {
return grid.blocks[(z * grid.width) + x];
}
static inline void setBlockInGrid(BlockGrid grid, int x, int z, Block* block) {
static inline void setBlockInGrid(BlockGrid grid, size_t x, size_t z, Block* block) {
grid.blocks[(z * grid.width) + x] = block;
}