render OBJ files
This commit is contained in:
parent
e267f4476a
commit
f8d67c57e3
9965
assets/teapot.obj
Normal file
9965
assets/teapot.obj
Normal file
File diff suppressed because it is too large
Load Diff
13
src/main.c
13
src/main.c
@ -6,6 +6,9 @@
|
|||||||
#include "mesh.h"
|
#include "mesh.h"
|
||||||
#include "array.h"
|
#include "array.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
triangle_t* triangles_to_render = NULL;
|
triangle_t* triangles_to_render = NULL;
|
||||||
|
|
||||||
vec3_t camera_position = {0, 0, -5};
|
vec3_t camera_position = {0, 0, -5};
|
||||||
@ -30,7 +33,9 @@ void setup(void) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// load cube vertices into mesh data
|
// load cube vertices into mesh data
|
||||||
load_cube_mesh_data();
|
// load_obj_file_data("../assets/cube.obj");
|
||||||
|
load_obj_file_data("../assets/teapot.obj");
|
||||||
|
// load_cube_mesh_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_input(void) {
|
void process_input(void) {
|
||||||
@ -71,9 +76,9 @@ void update(void) {
|
|||||||
//initialize array of triangles
|
//initialize array of triangles
|
||||||
triangles_to_render = NULL;
|
triangles_to_render = NULL;
|
||||||
|
|
||||||
mesh.rotation.y += 0.01f;
|
mesh.rotation.x += .01f;
|
||||||
mesh.rotation.z += 0.01f;
|
mesh.rotation.y += .01f;
|
||||||
mesh.rotation.x += 0.01f;
|
mesh.rotation.z += .01f;
|
||||||
|
|
||||||
int num_faces = array_length(mesh.faces);
|
int num_faces = array_length(mesh.faces);
|
||||||
for (int i = 0; i < num_faces; i++) {
|
for (int i = 0; i < num_faces; i++) {
|
||||||
|
58
src/mesh.c
58
src/mesh.c
@ -1,6 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "mesh.h"
|
#include "mesh.h"
|
||||||
#include "array.h"
|
#include "array.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
mesh_t mesh = {
|
mesh_t mesh = {
|
||||||
.vertices = NULL,
|
.vertices = NULL,
|
||||||
@ -40,6 +43,7 @@ face_t cube_faces[N_CUBE_FACES] = {
|
|||||||
{.a = 6, .b = 1, .c = 4},
|
{.a = 6, .b = 1, .c = 4},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void load_cube_mesh_data(void) {
|
void load_cube_mesh_data(void) {
|
||||||
for (int i = 0; i < N_CUBE_VERTICES; i++){
|
for (int i = 0; i < N_CUBE_VERTICES; i++){
|
||||||
vec3_t cube_vertex = cube_vertices[i];
|
vec3_t cube_vertex = cube_vertices[i];
|
||||||
@ -49,4 +53,56 @@ void load_cube_mesh_data(void) {
|
|||||||
face_t cube_face = cube_faces[i];
|
face_t cube_face = cube_faces[i];
|
||||||
array_push(mesh.faces, cube_face);
|
array_push(mesh.faces, cube_face);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void load_obj_file_data(char* filename) {
|
||||||
|
// load file to variable
|
||||||
|
char* obj = load_file(filename);
|
||||||
|
char* ch = NULL;
|
||||||
|
ch = strtok(obj, "\n");
|
||||||
|
while (ch != NULL)
|
||||||
|
{
|
||||||
|
if (strncmp("v ", ch, 2) == 0) {
|
||||||
|
printf("[%s]\n", ch);
|
||||||
|
vec3_t vertex;
|
||||||
|
sscanf(ch, "v %f %f %f", &vertex.x, &vertex.y, &vertex.z);
|
||||||
|
array_push(mesh.vertices, vertex);
|
||||||
|
} else if (strncmp("f ", ch, 2) == 0) {
|
||||||
|
int vertex_indices[3];
|
||||||
|
int textures_indices[3];
|
||||||
|
int normals_indices[3];
|
||||||
|
sscanf(ch, "f %d/%d/%d %d/%d/%d %d/%d/%d",
|
||||||
|
&vertex_indices[0], &textures_indices[0], &normals_indices[0],
|
||||||
|
&vertex_indices[1], &textures_indices[1], &normals_indices[1],
|
||||||
|
&vertex_indices[2], &textures_indices[2], &normals_indices[2]);
|
||||||
|
face_t face = {
|
||||||
|
.a = vertex_indices[0],
|
||||||
|
.b = vertex_indices[1],
|
||||||
|
.c = vertex_indices[2]
|
||||||
|
};
|
||||||
|
array_push(mesh.faces, face);
|
||||||
|
}
|
||||||
|
ch = strtok(NULL, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char* load_file(char const* filename) {
|
||||||
|
char* buffer = 0;
|
||||||
|
long length;
|
||||||
|
FILE * f = fopen (filename, "rb"); //was "rb"
|
||||||
|
|
||||||
|
if (f) {
|
||||||
|
fseek (f, 0, SEEK_END);
|
||||||
|
length = ftell (f);
|
||||||
|
fseek (f, 0, SEEK_SET);
|
||||||
|
buffer = (char*)malloc ((length+1)*sizeof(char));
|
||||||
|
if (buffer)
|
||||||
|
{
|
||||||
|
fread (buffer, sizeof(char), length, f);
|
||||||
|
}
|
||||||
|
fclose (f);
|
||||||
|
}
|
||||||
|
buffer[length] = '\0';
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -16,5 +16,6 @@ typedef struct {
|
|||||||
|
|
||||||
extern mesh_t mesh;
|
extern mesh_t mesh;
|
||||||
void load_cube_mesh_data(void);
|
void load_cube_mesh_data(void);
|
||||||
|
void load_obj_file_data(char* filename);
|
||||||
|
char* load_file(const char* filename);
|
||||||
#endif //RENDERER_MESH_H
|
#endif //RENDERER_MESH_H
|
Loading…
Reference in New Issue
Block a user