object loading finished also you can see things now

This commit is contained in:
MallocNull 2020-12-12 15:23:26 -06:00
parent db6b4ae12b
commit 020dacba24
15 changed files with 430 additions and 132 deletions

View file

@ -28,8 +28,12 @@ target_link_libraries(ogl_test
${GLEW_LIBRARIES}
)
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(ogl_test m)
endif()
#TODO replace this with something that runs on build
file(COPY src/data DESTINATION ./)
file(COPY src/shaders DESTINATION ./)
#file(COPY src/data DESTINATION ./)
#file(COPY src/shaders DESTINATION ./)
add_executable(rbm_gen rbm/main.c)

View file

@ -206,6 +206,11 @@ int main(int argc, char* argv[]) {
fclose(fpi);
if(face_cnt == 0) {
printf("OBJ file must have faces!");
return -1;
}
uint8_t magic[] = {0xDE, 0xAF, 0xB0, 0x0B};
fwrite(magic, 1, 4, fpo);

View file

@ -1,95 +0,0 @@
ply
format ascii 1.0
comment Created by Blender 2.79 (sub 0) - www.blender.org, source file: ''
element vertex 60
property float x
property float y
property float z
property float nx
property float ny
property float nz
property float s
property float t
element face 20
property list uchar uint vertex_indices
end_header
0.000000 0.000000 -1.000000 0.187597 -0.577354 -0.794651 0.818181 0.000000
0.723600 -0.525720 -0.447215 0.187597 -0.577354 -0.794651 0.727272 0.157461
-0.276385 -0.850640 -0.447215 0.187597 -0.577354 -0.794651 0.909090 0.157461
0.723600 -0.525720 -0.447215 0.607065 0.000000 -0.794652 0.727272 0.157461
0.000000 0.000000 -1.000000 0.607065 0.000000 -0.794652 0.636363 0.000000
0.723600 0.525720 -0.447215 0.607065 0.000000 -0.794652 0.545454 0.157461
0.000000 0.000000 -1.000000 -0.491122 -0.356829 -0.794652 0.090909 0.000000
-0.276385 -0.850640 -0.447215 -0.491122 -0.356829 -0.794652 0.000000 0.157461
-0.894425 0.000000 -0.447215 -0.491122 -0.356829 -0.794652 0.181818 0.157461
0.000000 0.000000 -1.000000 -0.491122 0.356829 -0.794652 0.272727 0.000000
-0.894425 0.000000 -0.447215 -0.491122 0.356829 -0.794652 0.181818 0.157461
-0.276385 0.850640 -0.447215 -0.491122 0.356829 -0.794652 0.363636 0.157461
0.000000 0.000000 -1.000000 0.187597 0.577354 -0.794651 0.454545 0.000000
-0.276385 0.850640 -0.447215 0.187597 0.577354 -0.794651 0.363636 0.157461
0.723600 0.525720 -0.447215 0.187597 0.577354 -0.794651 0.545454 0.157461
0.723600 -0.525720 -0.447215 0.982246 0.000000 -0.187597 0.727272 0.157461
0.723600 0.525720 -0.447215 0.982246 0.000000 -0.187597 0.545454 0.157461
0.894425 0.000000 0.447215 0.982246 0.000000 -0.187597 0.636363 0.314921
-0.276385 -0.850640 -0.447215 0.303536 -0.934171 -0.187589 0.909090 0.157461
0.723600 -0.525720 -0.447215 0.303536 -0.934171 -0.187589 0.727272 0.157461
0.276385 -0.850640 0.447215 0.303536 -0.934171 -0.187589 0.818181 0.314921
-0.894425 0.000000 -0.447215 -0.794649 -0.577359 -0.187587 0.181818 0.157461
-0.276385 -0.850640 -0.447215 -0.794649 -0.577359 -0.187587 0.000000 0.157461
-0.723600 -0.525720 0.447215 -0.794649 -0.577359 -0.187587 0.090909 0.314921
-0.276385 0.850640 -0.447215 -0.794649 0.577359 -0.187587 0.363636 0.157461
-0.894425 0.000000 -0.447215 -0.794649 0.577359 -0.187587 0.181818 0.157461
-0.723600 0.525720 0.447215 -0.794649 0.577359 -0.187587 0.272727 0.314921
0.723600 0.525720 -0.447215 0.303536 0.934171 -0.187589 0.545454 0.157461
-0.276385 0.850640 -0.447215 0.303536 0.934171 -0.187589 0.363636 0.157461
0.276385 0.850640 0.447215 0.303536 0.934171 -0.187589 0.454545 0.314921
0.723600 -0.525720 -0.447215 0.794649 -0.577359 0.187587 0.727272 0.157461
0.894425 0.000000 0.447215 0.794649 -0.577359 0.187587 0.636363 0.314921
0.276385 -0.850640 0.447215 0.794649 -0.577359 0.187587 0.818181 0.314921
-0.276385 -0.850640 -0.447215 -0.303536 -0.934171 0.187589 0.909090 0.157461
0.276385 -0.850640 0.447215 -0.303536 -0.934171 0.187589 0.818181 0.314921
-0.723600 -0.525720 0.447215 -0.303536 -0.934171 0.187589 1.000000 0.314921
-0.894425 0.000000 -0.447215 -0.982246 0.000000 0.187597 0.181818 0.157461
-0.723600 -0.525720 0.447215 -0.982246 0.000000 0.187597 0.090909 0.314921
-0.723600 0.525720 0.447215 -0.982246 0.000000 0.187597 0.272727 0.314921
-0.276385 0.850640 -0.447215 -0.303536 0.934171 0.187589 0.363636 0.157461
-0.723600 0.525720 0.447215 -0.303536 0.934171 0.187589 0.272727 0.314921
0.276385 0.850640 0.447215 -0.303536 0.934171 0.187589 0.454545 0.314921
0.723600 0.525720 -0.447215 0.794649 0.577359 0.187587 0.545454 0.157461
0.276385 0.850640 0.447215 0.794649 0.577359 0.187587 0.454545 0.314921
0.894425 0.000000 0.447215 0.794649 0.577359 0.187587 0.636363 0.314921
0.276385 -0.850640 0.447215 0.491122 -0.356829 0.794652 0.818181 0.314921
0.894425 0.000000 0.447215 0.491122 -0.356829 0.794652 0.636363 0.314921
0.000000 0.000000 1.000000 0.491122 -0.356829 0.794652 0.727272 0.472382
-0.723600 -0.525720 0.447215 -0.187597 -0.577354 0.794651 1.000000 0.314921
0.276385 -0.850640 0.447215 -0.187597 -0.577354 0.794651 0.818181 0.314921
0.000000 0.000000 1.000000 -0.187597 -0.577354 0.794651 0.909090 0.472382
-0.723600 0.525720 0.447215 -0.607065 0.000000 0.794652 0.272727 0.314921
-0.723600 -0.525720 0.447215 -0.607065 0.000000 0.794652 0.090909 0.314921
0.000000 0.000000 1.000000 -0.607065 0.000000 0.794652 0.181818 0.472382
0.276385 0.850640 0.447215 -0.187597 0.577354 0.794651 0.454545 0.314921
-0.723600 0.525720 0.447215 -0.187597 0.577354 0.794651 0.272727 0.314921
0.000000 0.000000 1.000000 -0.187597 0.577354 0.794651 0.363636 0.472382
0.894425 0.000000 0.447215 0.491122 0.356829 0.794652 0.636363 0.314921
0.276385 0.850640 0.447215 0.491122 0.356829 0.794652 0.454545 0.314921
0.000000 0.000000 1.000000 0.491122 0.356829 0.794652 0.545454 0.472382
3 0 1 2
3 3 4 5
3 6 7 8
3 9 10 11
3 12 13 14
3 15 16 17
3 18 19 20
3 21 22 23
3 24 25 26
3 27 28 29
3 30 31 32
3 33 34 35
3 36 37 38
3 39 40 41
3 42 43 44
3 45 46 47
3 48 49 50
3 51 52 53
3 54 55 56
3 57 58 59

View file

@ -1 +1 @@
majorpos borpo the best game that has ever been made ever !!
boo

22
src/koa/etc.c Normal file
View file

@ -0,0 +1,22 @@
#include "etc.h"
void mfree(int n, ...) {
va_list args;
va_start(args, n);
if(n > 0)
for(int i = 0; i < n; ++i)
free(va_arg(args, void*));
else {
void* ptr = NULL;
for(;;) {
ptr = va_arg(args, void*);
if(ptr == NULL)
break;
free(ptr);
}
}
va_end(args);
}

9
src/koa/etc.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef KOA_ETC_H
#define KOA_ETC_H
#include <stdlib.h>
#include <stdarg.h>
void mfree(int n, ...);
#endif

View file

@ -50,10 +50,11 @@ char* file_read(const char* file) {
size = ftell(fp);
rewind(fp);
content = malloc(sizeof(char) * size);
content = malloc(sizeof(char) * size + 1);
if(content == NULL)
return NULL;
content[size] = '\0';
fread(content, 1, size, fp);
fclose(fp);

View file

@ -8,7 +8,8 @@
#include "koa/thread.h"
#include "koa/time.h"
#define __STDERR err_ptr()
//#define __STDERR err_ptr()
#define __STDERR stdout
FILE* err_ptr(void);
void err_out(const char*);

View file

@ -5,9 +5,11 @@
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <cglm/cglm.h>
#include "koa/file.h"
#include "okuu/mesh.h"
#include "okuu/shader.h"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
@ -16,12 +18,99 @@ struct {
SDL_Window* window;
SDL_GLContext ctx;
int mode, running;
mesh_t* monkey;
} _g;
struct {
shader_t* shader;
enum {
DEF_MODEL,
DEF_VIEW,
DEF_PROJ
};
} _s_def;
int init();
void deinit();
void run();
int main(int argc, char* argv[]) {
if(init() < 0)
return -1;
_g.monkey = mesh_load("data/monkey.rbm");
_s_def.shader = shader_create("default");
shader_source(_s_def.shader, 2,
"shaders/test.vert", GL_VERTEX_SHADER,
"shaders/test.frag", GL_FRAGMENT_SHADER
);
shader_attribs(_s_def.shader, 3,
"model", "view", "projection"
);
_g.running = 1;
while(_g.running)
run();
deinit();
return 0;
}
void run() {
static mat4 model, view, projection;
static int init = 1;
if(init) {
//glm_translate_make(model, (vec3){ 0.0f, 0.f, -3.f });
//glm_rotate(model, glm_rad(180), (vec3){0.f, 1.f, 0.f});
glm_rotate_make(model, glm_rad(180), (vec3){ 0.f, 1.f, 0.f });
//glm_mat4_identity(model);
//glm_mat4_identity(view);
glm_lookat(
(vec3){3.f, 3.f, 3.f},
(vec3){0.f, 0.f, 0.f},
(vec3){0.f, 1.f, 0.f},
view
);
glm_perspective(
glm_rad(45),
(float)WINDOW_WIDTH/(float)WINDOW_HEIGHT,
0.1f, 10.f,
projection
);
/*glm_perspective_default(
(float)WINDOW_WIDTH/(float)WINDOW_HEIGHT,
projection
);*/
shader_start(_s_def.shader); {
glUniformMatrix4fv(_ATTR(DEF_MODEL), 1, GL_FALSE, (float*)model);
glUniformMatrix4fv(_ATTR(DEF_VIEW), 1, GL_FALSE, (float*)view);
glUniformMatrix4fv(_ATTR(DEF_PROJ), 1, GL_FALSE, (float*)projection);
} shader_stop();
init = 0;
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glClearColor(0.f, 0.f, 0.5f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader_start(_s_def.shader); {
mesh_bind(_g.monkey);
mesh_render(_g.monkey);
mesh_unbind();
} shader_stop();
SDL_GL_SwapWindow(_g.window);
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
if(ev.type == SDL_KEYDOWN) {
@ -34,8 +123,8 @@ void run() {
SDL_SetWindowFullscreen(
_g.window,
_g.mode == 0
? 0
: SDL_WINDOW_FULLSCREEN
? 0
: SDL_WINDOW_FULLSCREEN
);
break;
}
@ -43,24 +132,6 @@ void run() {
_g.running = 0;
}
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(_g.window);
}
int main(int argc, char* argv[]) {
if(init() < 0)
return -1;
_g.running = 1;
while(_g.running)
run();
deinit();
return 0;
}
int init() {
@ -89,6 +160,11 @@ int init() {
return -4;
SDL_GL_SetSwapInterval(1);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
_g.mode = 0;
return 0;
}

View file

@ -1,15 +1,252 @@
#include "mesh.h"
typedef struct {
float data[3];
} _pt_t;
typedef struct {
uint32_t data[3][3];
} _tri_t;
uint8_t* _ord(uint8_t* buf, int length) {
static int end_chk = -1;
if(end_chk == -1) {
uint16_t chk = 0xB00B;
end_chk = ((uint8_t*)&chk)[0] == 0x0B;
}
if(end_chk)
return buf;
uint8_t tmp[8] = { 0 };
for(int i = 0; i < length; ++i)
tmp[i] = buf[length - i - 1];
memcpy(buf, tmp, length);
return buf;
}
int _populate_pts
(_pt_t* data, FILE* fp, uint32_t length, int width)
{
uint8_t buffer[4];
int chk;
for(uint32_t i = 0; i < length; ++i) {
for(int j = 0; j < width; ++j) {
chk = fread(buffer, 1, 4, fp);
if(chk != 4)
return 0;
data[i].data[j] = *(float*)_ord(buffer, 4);
}
}
return 1;
}
int _populate_faces
(_tri_t* data, FILE* fp, uint32_t at, int length, int flags)
{
uint8_t buffer[4] = { 0 };
int chk;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
if((flags & (1 << j)) == 0)
continue;
chk = fread(buffer, 1, length, fp);
if(chk != length)
return 0;
data[at].data[i][j] = *(uint32_t *) _ord(buffer, 4);
}
}
return 1;
}
mesh_t* mesh_load(const char* file) {
FILE* fp = fopen(file, "rb");
if(fp == NULL)
return NULL;
enum { VERTEX_CNT = 0, NORMAL_CNT, TEX_CNT, FACE_CNT };
int counts[4] = {0, 0, 0, 0};
char line[1024];
enum { VERT_CNT = 0, TEX_CNT, NORM_CNT, FACE_CNT };
uint32_t counts[4] = { 0 };
for(;;) {
fgets(line, 1024, fp);
const uint8_t magic[] = {0xDE, 0xAF, 0xB0, 0x0B};
uint8_t buffer[4] = { 0 };
fread(buffer, 1, 4, fp);
if(memcmp(buffer, magic, 4) != 0) {
fclose(fp);
return NULL;
}
for(int i = 0; i < 4; ++i) {
fread(buffer, 1, 4, fp);
counts[i] = *(uint32_t*)_ord(buffer, 4);
}
if(feof(fp) || counts[VERT_CNT] == 0 || counts[FACE_CNT] == 0) {
fclose(fp);
return NULL;
}
_pt_t *verts = malloc(sizeof(_pt_t) * counts[VERT_CNT]),
*texs = malloc(sizeof(_pt_t) * counts[TEX_CNT]),
*norms = malloc(sizeof(_pt_t) * counts[NORM_CNT]);
_tri_t *faces = malloc(sizeof(_tri_t) * counts[FACE_CNT]);
int chk = 1;
chk &= _populate_pts(verts, fp, counts[VERT_CNT], 3);
if(counts[TEX_CNT] > 0)
chk &= _populate_pts(texs, fp, counts[TEX_CNT], 2);
if(counts[NORM_CNT] > 0)
chk &= _populate_pts(norms, fp, counts[NORM_CNT], 3);
int index_length = counts[FACE_CNT] <= 0xFFFF ? 2 : 4;
for(uint32_t i = 0; i < counts[FACE_CNT]; ++i) {
if(counts[TEX_CNT] > 0 && counts[NORM_CNT] > 0)
chk &= _populate_faces(faces, fp, i, index_length, 0b111);
else if(counts[TEX_CNT] > 0 && counts[NORM_CNT] == 0)
chk &= _populate_faces(faces, fp, i, index_length, 0b110);
else if(counts[TEX_CNT] == 0 && counts[NORM_CNT] > 0)
chk &= _populate_faces(faces, fp, i, index_length, 0b101);
else
chk &= _populate_faces(faces, fp, i, index_length, 0b100);
}
fclose(fp);
if(!chk) {
mfree(0, verts, texs, norms, faces, NULL);
return NULL;
}
float *verts_asm = malloc(sizeof(float) * counts[FACE_CNT] * 3 * 3),
*texs_asm = malloc(sizeof(float) * counts[FACE_CNT] * 3 * 2),
*norms_asm = malloc(sizeof(float) * counts[FACE_CNT] * 3 * 3);
mesh_t* mesh = malloc(sizeof(mesh_t));
for(uint32_t i = 0; i < counts[FACE_CNT]; ++i) {
for(int j = 0; j < 3; ++j) {
if(faces[i].data[j][0] > counts[VERT_CNT] ||
faces[i].data[j][1] > counts[TEX_CNT] ||
faces[i].data[j][2] > counts[NORM_CNT])
{
mfree(0,
verts_asm, texs_asm, norms_asm,
verts, texs, norms, faces, NULL
);
return NULL;
}
memcpy(
&verts_asm[i*3*3 + 3*j],
verts[faces[i].data[j][0] - 1].data,
3 * sizeof(float)
);
if(counts[TEX_CNT] > 0 && faces[i].data[j][1] != 0) {
memcpy(
&texs_asm[i * 3 * 2 + 2 * j],
texs[faces[i].data[j][1] - 1].data,
2 * sizeof(float)
);
}
if(counts[NORM_CNT] > 0 && faces[i].data[j][2] != 0) {
memcpy(
&norms_asm[i * 3 * 3 + 3 * j],
norms[faces[i].data[j][2] - 1].data,
3 * sizeof(float)
);
}
}
}
glGenBuffers(3, mesh->buffers);
glGenVertexArrays(1, &mesh->vao);
glBindBuffer(GL_ARRAY_BUFFER, mesh->buffers[0]);
glBufferData(
GL_ARRAY_BUFFER,
counts[FACE_CNT] * 3 * 3 * sizeof(float),
verts_asm,
GL_STATIC_DRAW
);
glBindBuffer(GL_ARRAY_BUFFER, mesh->buffers[1]);
glBufferData(
GL_ARRAY_BUFFER,
counts[FACE_CNT] * 3 * 2 * sizeof(float),
texs_asm,
GL_STATIC_DRAW
);
glBindBuffer(GL_ARRAY_BUFFER, mesh->buffers[2]);
glBufferData(
GL_ARRAY_BUFFER,
counts[FACE_CNT] * 3 * 3 * sizeof(float),
norms_asm,
GL_STATIC_DRAW
);
mfree(0,
verts_asm, texs_asm, norms_asm,
verts, texs, norms, faces, NULL
);
glBindVertexArray(mesh->vao); {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mesh->buffers[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, mesh->buffers[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, mesh->buffers[2]);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
} glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
mesh->tri_cnt = counts[FACE_CNT];
return mesh;
}
void mesh_bind(mesh_t* mesh) {
glBindVertexArray(mesh->vao);
}
void mesh_render(mesh_t* mesh) {
glDrawArrays(GL_TRIANGLES, 0, mesh->tri_cnt * 3);
}
void mesh_unbind() {
glBindVertexArray(0);
}
void mesh_unload(mesh_t* mesh) {
glDeleteVertexArrays(1, &mesh->vao);
glDeleteBuffers(3, mesh->buffers);
free(mesh);
}

View file

@ -1,14 +1,29 @@
#ifndef OKUU_MESH_H
#define OKUU_MESH_H
#include <SDL.h>
#include <GL/glew.h>
#include <SDL_opengl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "koa/etc.h"
typedef struct {
GLuint buffers[3], vao;
uint32_t tri_cnt;
} mesh_t;
mesh_t* mesh_load(const char*);
void mesh_bind(mesh_t*);
void mesh_render(mesh_t*);
void mesh_unbind();
void mesh_unload(mesh_t*);
#endif

View file

@ -128,7 +128,7 @@ void shader_start(shader_t* shader) {
_active = shader;
}
void shader_stop(shader_t* shader) {
void shader_stop() {
glUseProgram(0);
_active = NULL;
}

View file

@ -27,7 +27,7 @@ GLint shader_attr(const shader_t*, int);
shader_t* shader_active();
void shader_start(shader_t*);
void shader_stop(shader_t*);
void shader_stop();
void shader_destroy(shader_t*);

View file

@ -0,0 +1,12 @@
#version 100
precision mediump float;
//varying vec3 coord;
void main() {
//gl_FragColor = vec4(coord.z, coord.z, coord.z, 1.0);
float xval = gl_FragCoord.x / 640.0;
float yval = gl_FragCoord.y / 480.0;
gl_FragColor = vec4(xval, yval, 1.0, 1.0);
}

View file

@ -1,5 +1,16 @@
#version 120
#version 100
precision highp float;
attribute vec3 vertex;
attribute vec2 texuv;
attribute vec3 normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = vec4(vec3(0.0), 1.0);
gl_Position =
projection * view * model *
vec4(vertex, 1.0);
}