This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetkit_test.c
165 lines (130 loc) · 4.22 KB
/
assetkit_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// main.c
// assetkit_render
//
// Created by Recep Aslantas on 9/30/16.
// Copyright © 2016 Recep Aslantas. All rights reserved.
//
#include "assetkit_test.h"
#include <assetkit.h> /* importer */
#include <ak-opengl.h> /* load assetkit to libgk */
#include <gk.h> /* renderer */
#include <cglm.h> /* math */
const char *fname = "./cube_phong.dae";
GkContext *ctx;
AkDoc *doc;
GkModel *model;
GkScene *scene;
mat4 projMatrix;
mat4 viewMatrix;
bool do_render = true; /* render only if needed */
void
load_collada() {
AkResult ret;
/* load COLLADA, that's it! */
ret = ak_load(&doc, fname, NULL);
if (ret != AK_OK)
exit(-1);
/* load collada to OpenGL through libgk */
/* render context */
ctx = gkContextNew();
/* load whole visual scene, this returns GkScene for rendering */
agk_loadScene(ctx,
doc,
&doc->scene,
GL_STATIC_DRAW,
&scene);
}
void
init() {
load_collada();
/* get first found camera and it's attribs (VIEW MATRIX, PROJ MATRIX) */
ak_firstCamera(doc,
NULL, /* for now we don't need to store cam ref */
viewMatrix[0],
projMatrix[0]);
/* cache VIEW matrix */
glm_mat4_inv(viewMatrix, scene->v);
/* default shaders: common materials */
/* NOTE: every node, model can have different shaders e.g. GLSL profile */
/* if nodes, models don't have program info then they use parent's */
/* so we provided only ONE prog info and all nodes will use this */
/* also check bottom to see how to use custom shaders */
scene->pinfo = gkDefaultProgram();
/* invalidate VIEW matrix
= force re-render + re-calculate and cache model and final matrices */
scene->vIsValid = 0;
/* NOTE for node matrices: all matrices will be cached by first draw call */
/* so you don't need manage cache manually BUT YOU CAN INVALIDATE */
/* if you invalidate matrix, then all sub-nodes' matrices will be */
/* recalculated (only children and instance nodes) and cached again */
/* also like prog info, nodes, models use parent matrix if they */
/* don't have any transform, this saves extra space, for instance; */
/* if only root node has transform then all sub-nodes, node-instances*/
/* will use same matrix, space=1 * sizeof(mat4) + ptrsize * refcount */
glEnable(GL_DEPTH_TEST);
glClearColor(0.64, 0.64, 0.64, 1);
resize();
}
/* optional, resize projection with window */
void
resize() {
do_render = true;
/* fix PROJECTION matrix */
glm_perspective_resize(projMatrix);
/* cache VIEW and PROJECTION x VIEW matrices */
glm_mat4_mul(projMatrix,
scene->v,
scene->pv);
/* invalidate PROJECTION x VIEW
= force re-render + re-calculate and cache model and final matrices */
scene->pvIsValid = 0;
/* render manually */
render();
}
void
render() {
if (!do_render)
return;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* render scene, that's it! */
/* you can render specific Node, Model using RenderNode, RenderModel... */
/* so you don't have to have nodes */
gkRenderScene(scene);
do_render = false;
}
void
dealloc() {
/* free collada file */
ak_free(doc);
/* TODO: free libgk */
}
/* OPTIONAL: */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* this shows how to load custom shaders from folder */
/*
void
createProg() {
GkProgInfo *pinfo;
program = glCreateProgram();
gkShaderLoadFromFolder(realpath("shader", tmpPathBuf),
&shaders);
gkAttachShaders(program, shaders);
glLinkProgram(program);
#ifdef DEBUG
if (!gkProgramIsValid(program)) {
gkProgramLogInfo(program, stderr);
exit(-1);
}
#endif
glUseProgram(program);
pinfo = (GkProgInfo *)malloc(sizeof(*pinfo));
pinfo->mvpi = glGetUniformLocation(program, "MVP");;
pinfo->mvi = glGetUniformLocation(program, "MV");;
pinfo->nmi = glGetUniformLocation(program, "NM");;
pinfo->nmui = glGetUniformLocation(program, "NMU");;
pinfo->prog = program;
pinfo->refc = 1;
scene->pinfo = pinfo;
}
*/