Import .obj model from Blender

Add third-party hashmap library to help when importing
This commit is contained in:
var
2026-04-04 20:19:46 -05:00
parent 06535ccf01
commit 565c4e0e6f
11 changed files with 1844 additions and 3 deletions

396
res/model/human.obj Normal file
View File

@@ -0,0 +1,396 @@
# Blender 5.1.0
# www.blender.org
o Cube
v 0.392226 5.791883 -0.323902
v 1.000000 2.797271 -0.500000
v 0.375191 5.791883 0.193573
v 1.000000 2.797271 0.410409
v 0.000000 2.797271 -0.500000
v 0.000000 5.791883 0.313965
v 0.000000 2.797271 0.410409
v 0.000000 5.791883 -0.431258
v 0.973876 5.462687 -0.577779
v 1.000000 4.791883 -0.500000
v 0.921580 3.413690 -0.372389
v 0.973876 5.462687 0.615027
v 1.000000 4.791883 0.533735
v 0.815817 3.413690 0.422394
v 0.000000 5.291883 -0.500000
v 0.000000 4.791883 -0.500000
v 0.000000 3.413690 -0.431220
v 0.000000 5.291883 0.533735
v 0.000000 4.791883 0.533735
v 0.000000 3.413690 0.410409
v 1.309145 5.351887 -0.469410
v 1.226283 4.706231 -0.312568
v 1.309145 5.351887 0.467801
v 1.226283 4.706231 0.309653
v 0.292031 0.000000 0.293679
v 0.879388 0.000000 0.293679
v 0.879388 0.000000 -0.293679
v 0.292031 0.000000 -0.293679
v 0.500000 6.791883 -0.500000
v 0.500000 6.791883 0.500000
v 0.000000 6.791883 -0.500000
v 0.000000 6.791883 0.500000
v 0.500000 5.899879 0.500000
v 0.000000 5.899879 0.500000
v 0.000000 5.899879 -0.500000
v 0.500000 5.899879 -0.500000
v 0.665692 5.541883 -0.391726
v 0.525278 5.541883 0.301663
v 0.000000 5.541883 -0.500000
v 0.000000 5.541883 0.465368
v 0.064081 2.647533 -0.468467
v 0.064081 2.647533 0.392291
v 0.995814 2.647533 0.392291
v 0.995814 2.647533 -0.468467
v 0.170786 1.383736 -0.403152
v 0.973834 1.383736 0.355442
v 0.170786 1.383736 0.355442
v 0.973834 1.383736 -0.403152
v 0.960790 4.541883 -0.436195
v 0.907909 4.541883 0.461197
v 0.000000 4.541883 -0.465610
v 0.000000 4.541883 0.455205
v 2.256556 4.848390 -0.244752
v 2.192387 4.348389 -0.123293
v 2.256556 4.848390 0.481030
v 2.192387 4.348389 0.358559
v 3.076406 4.246439 -0.125417
v 3.033353 3.910976 -0.043927
v 3.076406 4.246439 0.361528
v 3.033353 3.910976 0.279360
v -0.392226 5.791883 -0.323902
v -1.000000 2.797271 -0.500000
v -0.375191 5.791883 0.193573
v -1.000000 2.797271 0.410409
v -0.973876 5.462687 -0.577779
v -1.000000 4.791883 -0.500000
v -0.921580 3.413690 -0.372389
v -0.973876 5.462687 0.615027
v -1.000000 4.791883 0.533735
v -0.815817 3.413690 0.422394
v -1.309145 5.351887 -0.469410
v -1.226283 4.706231 -0.312568
v -1.309145 5.351887 0.467801
v -1.226283 4.706231 0.309653
v -0.292031 0.000000 0.293679
v -0.879388 0.000000 0.293679
v -0.879388 0.000000 -0.293679
v -0.292031 0.000000 -0.293679
v -0.500000 6.791883 -0.500000
v -0.500000 6.791883 0.500000
v -0.500000 5.899879 0.500000
v -0.500000 5.899879 -0.500000
v -0.665692 5.541883 -0.391726
v -0.525278 5.541883 0.301663
v -0.064081 2.647533 -0.468467
v -0.064081 2.647533 0.392291
v -0.995814 2.647533 0.392291
v -0.995814 2.647533 -0.468467
v -0.170786 1.383736 -0.403152
v -0.973834 1.383736 0.355442
v -0.170786 1.383736 0.355442
v -0.973834 1.383736 -0.403152
v -0.960790 4.541883 -0.436195
v -0.907909 4.541883 0.461197
v -2.256556 4.848390 -0.244752
v -2.192387 4.348389 -0.123293
v -2.256556 4.848390 0.481030
v -2.192387 4.348389 0.358559
v -3.076406 4.246439 -0.125417
v -3.033353 3.910976 -0.043927
v -3.076406 4.246439 0.361528
v -3.033353 3.910976 0.279360
vt 0.062500 0.812500
vt 0.000000 0.750000
vt 0.062500 0.750000
vt 0.375000 0.500000
vt 0.437500 0.562500
vt 0.375000 0.562500
vt 0.375000 0.437500
vt 0.437500 0.500000
vt 0.375000 0.625000
vt 0.500000 0.562500
vt 0.625000 0.625000
vt 0.500000 0.625000
vt 0.375000 0.875000
vt 0.312500 0.937500
vt 0.312500 0.875000
vt 0.437500 0.875000
vt 0.500000 0.937500
vt 0.437500 0.937500
vt 0.750000 0.937500
vt 0.812500 1.000000
vt 0.750000 1.000000
vt 0.312500 0.750000
vt 0.375000 0.812500
vt 0.312500 0.812500
vt 0.500000 0.750000
vt 0.437500 0.812500
vt 0.437500 0.750000
vt 0.812500 0.437500
vt 0.750000 0.500000
vt 0.750000 0.437500
vt 0.500000 0.812500
vt 0.562500 0.875000
vt 0.500000 0.875000
vt 0.625000 0.812500
vt 0.625000 0.875000
vt 0.812500 0.875000
vt 0.812500 0.937500
vt 0.312500 0.500000
vt 0.250000 0.562500
vt 0.250000 0.500000
vt 0.812500 0.562500
vt 0.750000 0.562500
vt 0.125000 0.812500
vt 0.062500 0.875000
vt 0.187500 0.875000
vt 0.187500 0.812500
vt 0.125000 0.937500
vt 0.125000 0.875000
vt 0.125000 0.750000
vt 0.187500 0.750000
vt 0.687500 0.500000
vt 0.625000 0.562500
vt 0.625000 0.500000
vt 0.500000 0.437500
vt 0.500000 0.500000
vt 0.437500 0.625000
vt 0.312500 0.625000
vt 0.312500 0.562500
vt 0.312500 0.437500
vt 0.250000 0.750000
vt 0.250000 0.937500
vt 0.250000 0.875000
vt 0.250000 0.812500
vt 0.062500 0.937500
vt 0.000000 0.875000
vt 0.187500 0.937500
vt 0.000000 0.812500
vt 0.375000 0.750000
vt 0.375000 0.937500
vt 0.187500 0.500000
vt 0.125000 0.562500
vt 0.125000 0.500000
vt 0.812500 0.625000
vt 0.750000 0.625000
vt 0.750000 0.875000
vt 0.812500 0.812500
vt 0.812500 0.687500
vt 0.750000 0.750000
vt 0.750000 0.687500
vt 0.750000 0.812500
vt 0.812500 0.750000
vt 0.687500 0.812500
vt 0.687500 0.875000
vt 0.437500 0.437500
vt 0.562500 0.812500
vt 0.812500 0.500000
vt 0.000000 0.937500
vt 0.187500 0.562500
vt 0.687500 0.562500
vt 0.625000 0.437500
s 0
f 47/1 28/2 25/3
f 37/4 3/5 38/6
f 39/7 1/8 37/4
f 3/5 40/9 38/6
f 33/10 32/11 34/12
f 14/13 7/14 4/15
f 50/16 19/17 52/18
f 13/19 18/20 19/21
f 5/22 11/23 2/24
f 16/25 49/26 51/27
f 15/28 10/29 16/30
f 11/23 4/15 2/24
f 10/31 50/16 49/26
f 10/31 24/32 13/33
f 24/32 54/34 56/35
f 13/19 23/36 12/37
f 9/38 23/39 21/40
f 10/29 21/41 22/42
f 27/43 25/44 28/1
f 46/45 27/43 48/46
f 46/45 25/47 26/48
f 48/46 28/49 45/50
f 31/51 30/52 29/53
f 35/54 29/53 36/55
f 36/55 30/52 33/10
f 3/5 36/55 33/10
f 1/8 35/54 36/55
f 3/5 34/12 6/56
f 38/6 18/57 12/58
f 15/59 37/4 9/38
f 37/4 12/58 9/38
f 2/24 41/60 5/22
f 4/15 42/61 43/62
f 4/15 44/63 2/24
f 7/64 41/65 42/44
f 44/63 45/50 41/60
f 43/62 47/66 46/45
f 43/62 48/46 44/63
f 42/44 45/67 47/1
f 11/23 50/16 14/13
f 51/27 11/23 17/68
f 14/13 52/18 20/69
f 53/70 59/71 57/72
f 22/42 53/73 54/74
f 23/39 53/70 21/40
f 24/75 55/76 23/36
f 57/77 60/78 58/79
f 56/80 59/81 55/76
f 56/35 58/82 60/83
f 54/74 57/77 58/79
f 91/1 78/2 89/67
f 83/4 63/5 61/8
f 39/7 61/8 8/84
f 40/9 63/5 84/6
f 32/11 81/10 34/12
f 7/14 70/13 64/15
f 94/16 19/17 69/33
f 69/19 18/20 68/37
f 5/22 67/23 17/68
f 93/26 16/25 51/27
f 66/29 15/28 16/30
f 64/15 67/23 62/24
f 94/16 66/31 93/26
f 74/32 66/31 69/33
f 74/32 96/34 72/85
f 73/36 69/19 68/37
f 65/38 73/39 68/58
f 66/29 71/41 65/86
f 75/44 77/43 78/1
f 77/43 90/45 92/46
f 90/45 75/47 91/66
f 78/49 92/46 89/50
f 80/52 31/51 79/53
f 79/53 35/54 82/55
f 80/52 82/55 81/10
f 63/5 82/55 61/8
f 61/8 35/54 8/84
f 34/12 63/5 6/56
f 18/57 84/6 68/58
f 15/59 83/4 39/7
f 68/58 83/4 65/38
f 85/60 62/24 5/22
f 64/15 86/61 7/14
f 88/63 64/15 62/24
f 7/64 85/65 5/87
f 89/50 88/63 85/60
f 87/62 91/66 86/61
f 92/46 87/62 88/63
f 86/44 89/67 85/65
f 67/23 94/16 93/26
f 67/23 51/27 17/68
f 70/13 52/18 94/16
f 95/70 101/71 97/88
f 72/42 95/73 71/41
f 95/70 73/39 71/40
f 97/76 74/75 73/36
f 102/78 99/77 100/79
f 101/81 98/80 97/76
f 98/35 100/82 96/34
f 96/74 99/77 95/73
f 47/1 45/67 28/2
f 37/4 1/8 3/5
f 39/7 8/84 1/8
f 3/5 6/56 40/9
f 33/10 30/52 32/11
f 14/13 20/69 7/14
f 50/16 13/33 19/17
f 13/19 12/37 18/20
f 5/22 17/68 11/23
f 16/25 10/31 49/26
f 15/28 9/86 10/29
f 11/23 14/13 4/15
f 10/31 13/33 50/16
f 10/31 22/85 24/32
f 24/32 22/85 54/34
f 13/19 24/75 23/36
f 9/38 12/58 23/39
f 10/29 9/86 21/41
f 27/43 26/48 25/44
f 46/45 26/48 27/43
f 46/45 47/66 25/47
f 48/46 27/43 28/49
f 31/51 32/89 30/52
f 35/54 31/90 29/53
f 36/55 29/53 30/52
f 3/5 1/8 36/55
f 1/8 8/84 35/54
f 3/5 33/10 34/12
f 38/6 40/9 18/57
f 15/59 39/7 37/4
f 37/4 38/6 12/58
f 2/24 44/63 41/60
f 4/15 7/14 42/61
f 4/15 43/62 44/63
f 7/64 5/87 41/65
f 44/63 48/46 45/50
f 43/62 42/61 47/66
f 43/62 46/45 48/46
f 42/44 41/65 45/67
f 11/23 49/26 50/16
f 51/27 49/26 11/23
f 14/13 50/16 52/18
f 53/70 55/88 59/71
f 22/42 21/41 53/73
f 23/39 55/88 53/70
f 24/75 56/80 55/76
f 57/77 59/81 60/78
f 56/80 60/78 59/81
f 56/35 54/34 58/82
f 54/74 53/73 57/77
f 91/1 75/3 78/2
f 83/4 84/6 63/5
f 39/7 83/4 61/8
f 40/9 6/56 63/5
f 32/11 80/52 81/10
f 7/14 20/69 70/13
f 94/16 52/18 19/17
f 69/19 19/21 18/20
f 5/22 62/24 67/23
f 93/26 66/31 16/25
f 66/29 65/86 15/28
f 64/15 70/13 67/23
f 94/16 69/33 66/31
f 74/32 72/85 66/31
f 74/32 98/35 96/34
f 73/36 74/75 69/19
f 65/38 71/40 73/39
f 66/29 72/42 71/41
f 75/44 76/48 77/43
f 77/43 76/48 90/45
f 90/45 76/48 75/47
f 78/49 77/43 92/46
f 80/52 32/89 31/51
f 79/53 31/90 35/54
f 80/52 79/53 82/55
f 63/5 81/10 82/55
f 61/8 82/55 35/54
f 34/12 81/10 63/5
f 18/57 40/9 84/6
f 15/59 65/38 83/4
f 68/58 84/6 83/4
f 85/60 88/63 62/24
f 64/15 87/62 86/61
f 88/63 87/62 64/15
f 7/64 86/44 85/65
f 89/50 92/46 88/63
f 87/62 90/45 91/66
f 92/46 90/45 87/62
f 86/44 91/1 89/67
f 67/23 70/13 94/16
f 67/23 93/26 51/27
f 70/13 20/69 52/18
f 95/70 99/72 101/71
f 72/42 96/74 95/73
f 95/70 97/88 73/39
f 97/76 98/80 74/75
f 102/78 101/81 99/77
f 101/81 102/78 98/80
f 98/35 102/83 100/82
f 96/74 100/79 99/77

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -28,9 +28,10 @@ void Game_Update(GameState *gs)
if (gs->input.q) move[2] = speed; if (gs->input.q) move[2] = speed;
else if (gs->input.e) move[2] = -speed; else if (gs->input.e) move[2] = -speed;
ShapeInstance *target = &(gs->shapes[0]->instances[2]); ShapeInstance *target = &(gs->shapes[2]->instances[0]);
vec3 displacement; vec3 displacement;
glm_vec3_scale(gs->camera.front, -10.0, displacement); glm_vec3_scale(gs->camera.front, -20.0f, displacement);
displacement[1] += 16.0f;
glm_vec3_add(target->position, move, target->position); glm_vec3_add(target->position, move, target->position);
glm_vec3_add(target->position, displacement, gs->camera.position); glm_vec3_add(target->position, displacement, gs->camera.position);

20
src/hashmap/LICENSE Normal file
View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2020 Joshua J Baker
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1154
src/hashmap/hashmap.c Normal file

File diff suppressed because it is too large Load Diff

62
src/hashmap/hashmap.h Normal file
View File

@@ -0,0 +1,62 @@
// Copyright 2020 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
#ifndef HASHMAP_H
#define HASHMAP_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#if defined(__cplusplus)
extern "C" {
#endif // __cplusplus
struct hashmap;
struct hashmap *hashmap_new(size_t elsize, size_t cap, uint64_t seed0,
uint64_t seed1,
uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b, void *udata),
void (*elfree)(void *item),
void *udata);
struct hashmap *hashmap_new_with_allocator(void *(*malloc)(size_t),
void *(*realloc)(void *, size_t), void (*free)(void*), size_t elsize,
size_t cap, uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b, void *udata),
void (*elfree)(void *item),
void *udata);
void hashmap_free(struct hashmap *map);
void hashmap_clear(struct hashmap *map, bool update_cap);
size_t hashmap_count(const struct hashmap *map);
bool hashmap_oom(struct hashmap *map);
const void *hashmap_get(const struct hashmap *map, const void *item);
const void *hashmap_set(struct hashmap *map, const void *item);
const void *hashmap_delete(struct hashmap *map, const void *item);
const void *hashmap_probe(struct hashmap *map, uint64_t position);
bool hashmap_scan(struct hashmap *map, bool (*iter)(const void *item, void *udata), void *udata);
bool hashmap_iter(struct hashmap *map, size_t *i, void **item);
uint64_t hashmap_sip(const void *data, size_t len, uint64_t seed0, uint64_t seed1);
uint64_t hashmap_murmur(const void *data, size_t len, uint64_t seed0, uint64_t seed1);
uint64_t hashmap_xxhash3(const void *data, size_t len, uint64_t seed0, uint64_t seed1);
const void *hashmap_get_with_hash(const struct hashmap *map, const void *key, uint64_t hash);
const void *hashmap_delete_with_hash(struct hashmap *map, const void *key, uint64_t hash);
const void *hashmap_set_with_hash(struct hashmap *map, const void *item, uint64_t hash);
void hashmap_set_grow_by_power(struct hashmap *map, size_t power);
void hashmap_set_load_factor(struct hashmap *map, double load_factor);
// DEPRECATED: use `hashmap_new_with_allocator`
void hashmap_set_allocator(void *(*malloc)(size_t), void (*free)(void*));
#if defined(__cplusplus)
}
#endif // __cplusplus
#endif // HASHMAP_H

157
src/model.c Normal file
View File

@@ -0,0 +1,157 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GL/glew.h"
#include "cglm/cglm.h"
#include "hashmap/hashmap.h"
#include "shape.h"
static enum
{
OBJ_MAX_LINE_LENGTH = 256,
OBJ_MAX_ELEMENTS = 1024,
SHAPE_MAX_COORDS = 4096,
SHAPE_MAX_INDICES = 1024,
};
static struct VertRef
{
uint16_t xyzIndex; // refers to a vertex in the obj file
uint16_t uvIndex; // refers to a UV texture coord in the obj file
uint16_t outIndex; // index of the combined vertex (xyzuv) in the output array
};
static int VertRef_Compare(const void *itemA, const void *itemB, void *udata)
{
const struct VertRef *a = itemA;
const struct VertRef *b = itemB;
if (a->xyzIndex < b->xyzIndex) return -1;
else if (a->xyzIndex > b->xyzIndex) return 1;
else return a->uvIndex < b->uvIndex ? -1
: a->uvIndex > b->uvIndex ? 1
: 0;
}
static uint64_t VertRef_Hash(const void *item, uint64_t seed0, uint64_t seed1)
{
return hashmap_sip(item, 2 * sizeof(uint16_t), seed0, seed1);
}
static void ReadCoords(const char *str, float *coords, int n)
{
char *end = NULL;
for (int i = 0; i < n; i++)
{
coords[i] = strtof(str, &end);
if (end == str) break;
str = end;
}
}
static int ReadIndex(const char **str)
{
const int maxLen = 15;
char buffer[maxLen + 1];
int i;
buffer[maxLen] = '\0';
for (i = 0; i < maxLen; i++)
{
char c = str[0][i];
if (c == '/' || c == '\0' || c == EOF || isspace(c))
{
buffer[i++] = '\0';
break;
}
buffer[i] = c;
}
*str += i;
return atoi(buffer);
}
Shape *Model_ReadObjFile(const char *path)
{
struct hashmap *map = hashmap_new(sizeof(struct VertRef), 256, 0, 0, VertRef_Hash, VertRef_Compare, NULL, NULL);
FILE *file = fopen(path, "r");
char line[OBJ_MAX_LINE_LENGTH];
vec3 xyzVertices[OBJ_MAX_ELEMENTS];
vec2 uvVertices[OBJ_MAX_ELEMENTS];
int xyzNum = 0;
int uvNum = 0;
GLfloat outVertices[SHAPE_MAX_COORDS];
GLushort outIndices[SHAPE_MAX_INDICES];
int numVertices = 0;
int numIndices = 0;
int numTriangles = 0;
while (fgets(&(line[0]), OBJ_MAX_LINE_LENGTH, file) != NULL)
{
switch (line[0])
{
case '#': // comment
break;
case 'o': // named object
xyzNum = 0;
uvNum = 0;
break;
case 'v': // vertex or texture coord
if (line[1] == 't')
ReadCoords(&(line[3]), (void*)&(uvVertices[uvNum++]), 2);
else
ReadCoords(&(line[2]), (void*)&(xyzVertices[xyzNum++]), 3);
break;
case 's': // smooth shading option
break;
case 'f': // face (assume it's a triangle)
{
const char *str = &(line[2]);
for (int i = 0; i < 3; i++)
{
struct VertRef vertRef;
vertRef.xyzIndex = ReadIndex(&str) - 1;
vertRef.uvIndex = ReadIndex(&str) - 1;
struct VertRef *existing = hashmap_get(map, &vertRef);
if (existing == NULL)
{
vertRef.outIndex = numVertices++;
outIndices[numIndices++] = vertRef.outIndex;
GLfloat *vOut = &(outVertices[vertRef.outIndex * 5]);
vOut[0] = xyzVertices[vertRef.xyzIndex][0];
vOut[1] = xyzVertices[vertRef.xyzIndex][1];
vOut[2] = xyzVertices[vertRef.xyzIndex][2];
vOut[3] = 1.0f - uvVertices[vertRef.uvIndex][0];
vOut[4] = uvVertices[vertRef.uvIndex][1];
hashmap_set(map, &vertRef);
}
else
{
outIndices[numIndices++] = existing->outIndex;
}
}
numTriangles++;
}
break;
default:
break;
}
}
fclose(file);
hashmap_free(map);
printf("Imported model with %d triangles.\n", numTriangles);
return Shape_New(outVertices, numVertices * 5, outIndices, numIndices, 1);
}

3
src/model.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
Shape *Model_ReadObjFile(const char *path);

View File

@@ -11,6 +11,7 @@
#include "camera.h" #include "camera.h"
#include "game.h" #include "game.h"
#include "shape.h" #include "shape.h"
#include "model.h"
static void LoadTextureArray(GLuint texture, const char* filePath, int nCols, int nRows) static void LoadTextureArray(GLuint texture, const char* filePath, int nCols, int nRows)
{ {
@@ -237,7 +238,7 @@ bool Render_Init(GameState *gs)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
printf("Initialized OpenGL.\n"); printf("Initialized OpenGL.\n");
gs->numShapes = 2; gs->numShapes = 3;
Shape *shape = Shape_MakePyramid(5); Shape *shape = Shape_MakePyramid(5);
gs->shapes[0] = shape; gs->shapes[0] = shape;
@@ -255,12 +256,21 @@ bool Render_Init(GameState *gs)
glGenBuffers(1, &shape->EBO); glGenBuffers(1, &shape->EBO);
printf("Created shape 1.\n"); printf("Created shape 1.\n");
shape = Model_ReadObjFile("res/model/human.obj");
gs->shapes[2] = shape;
glGenVertexArrays(1, &shape->VAO);
glGenBuffers(1, &shape->VBO);
glGenBuffers(1, &shape->IBO);
glGenBuffers(1, &shape->EBO);
printf("Created shape 2.\n");
LoadTexture(gs); LoadTexture(gs);
printf("Loaded textures.\n"); printf("Loaded textures.\n");
Camera_Init(&gs->camera); Camera_Init(&gs->camera);
InitShapeBuffers(gs->shapes[0]); InitShapeBuffers(gs->shapes[0]);
InitShapeBuffers(gs->shapes[1]); InitShapeBuffers(gs->shapes[1]);
InitShapeBuffers(gs->shapes[2]);
printf("Initialized buffers.\n"); printf("Initialized buffers.\n");
return true; return true;
} }

View File

@@ -4,6 +4,43 @@
#include "cglm/cglm.h" #include "cglm/cglm.h"
#include "shape.h" #include "shape.h"
Shape *Shape_New(GLfloat *vertices, int numVertices, GLushort *indices, int numIndices, int numInstances)
{
size_t numBytesVertices = numVertices * sizeof(GLfloat);
size_t numBytesIndices = numIndices * sizeof(GLushort);
Shape *shape = malloc(sizeof(Shape) + numBytesVertices + numBytesIndices);
void *tempPtr = (void*)(shape + 1);
shape->vertices = tempPtr;
shape->numVertices = numBytesVertices / sizeof(vertices[0]);
shape->indices = tempPtr + numBytesVertices;
shape->numIndices = numBytesIndices / sizeof(indices[0]);
memcpy(shape->vertices, vertices, numBytesVertices);
memcpy(shape->indices, indices, numBytesIndices);
shape->instances = calloc(numInstances, sizeof(ShapeInstance) + SHAPE_INSTANCE_SIZE);
shape->instanceData = (void*)(shape->instances + numInstances);
shape->numInstances = numInstances;
for (int i = 0; i < numInstances; i++)
{
ShapeInstance *instance = shape->instances + i;
instance->position[0] = 0.0f;
instance->position[1] = 20.0f;
instance->position[2] = (5.0f * i) - 10.0f;
instance->scale = 2.0f;
instance->collisionRadius = 1.0f;
glm_vec3_zero(instance->velocity);
glm_vec3_zero(instance->rotation);
float *textureId = shape->instanceData + (i * 17);
*textureId = 6;
mat4 *matrix = (void*)(textureId + 1);
}
return shape;
}
Shape *Shape_MakePyramid(int numInstances) Shape *Shape_MakePyramid(int numInstances)
{ {
GLfloat vertices[] = GLfloat vertices[] =

View File

@@ -33,5 +33,6 @@ typedef struct
GLuint EBO; GLuint EBO;
} Shape; } Shape;
Shape *Shape_New(GLfloat *vertices, int numVertices, GLushort *indices, int numIndices, int numInstances);
Shape *Shape_MakePyramid(int numInstances); Shape *Shape_MakePyramid(int numInstances);
Shape *Shape_MakePlane(); Shape *Shape_MakePlane();