begin 3d stuff
This commit is contained in:
parent
20ae97dbfd
commit
f32afaa7ec
5 changed files with 4130 additions and 66 deletions
7
Makefile
7
Makefile
|
@ -2,7 +2,7 @@ BINARY=gear
|
|||
|
||||
SHADERC=sokol-shdc
|
||||
|
||||
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard
|
||||
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard -std=c99
|
||||
SHADER_FLAGS=--slang glsl430:hlsl5:metal_macos
|
||||
|
||||
C_FILES=$(shell find -L * -type f -name '*.c')
|
||||
|
@ -28,9 +28,12 @@ else
|
|||
endif
|
||||
endif
|
||||
|
||||
$(BINARY): $(SHADER_OBJ) $(C_OBJ) $(OBJC_OBJ)
|
||||
$(BINARY): shaders $(C_OBJ) $(OBJC_OBJ)
|
||||
$(CC) $(C_OBJ) $(OBJC_OBJ) $(LDFLAGS) -o $(BINARY)
|
||||
|
||||
shaders: $(SHADER_OBJ)
|
||||
@echo Shaders compiled
|
||||
|
||||
%.glsl.h: %.glsl
|
||||
$(SHADERC) --input $< --output $@ $(SHADER_FLAGS)
|
||||
|
||||
|
|
3932
include/HandmadeMath/HandmadeMath.h
Normal file
3932
include/HandmadeMath/HandmadeMath.h
Normal file
File diff suppressed because it is too large
Load diff
116
include/HandmadeMath/LICENSE
Normal file
116
include/HandmadeMath/LICENSE
Normal file
|
@ -0,0 +1,116 @@
|
|||
CC0 1.0 Universal
|
||||
|
||||
Statement of Purpose
|
||||
|
||||
The laws of most jurisdictions throughout the world automatically confer
|
||||
exclusive Copyright and Related Rights (defined below) upon the creator and
|
||||
subsequent owner(s) (each and all, an "owner") of an original work of
|
||||
authorship and/or a database (each, a "Work").
|
||||
|
||||
Certain owners wish to permanently relinquish those rights to a Work for the
|
||||
purpose of contributing to a commons of creative, cultural and scientific
|
||||
works ("Commons") that the public can reliably and without fear of later
|
||||
claims of infringement build upon, modify, incorporate in other works, reuse
|
||||
and redistribute as freely as possible in any form whatsoever and for any
|
||||
purposes, including without limitation commercial purposes. These owners may
|
||||
contribute to the Commons to promote the ideal of a free culture and the
|
||||
further production of creative, cultural and scientific works, or to gain
|
||||
reputation or greater distribution for their Work in part through the use and
|
||||
efforts of others.
|
||||
|
||||
For these and/or other purposes and motivations, and without any expectation
|
||||
of additional consideration or compensation, the person associating CC0 with a
|
||||
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
||||
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
||||
and publicly distribute the Work under its terms, with knowledge of his or her
|
||||
Copyright and Related Rights in the Work and the meaning and intended legal
|
||||
effect of CC0 on those rights.
|
||||
|
||||
1. Copyright and Related Rights. A Work made available under CC0 may be
|
||||
protected by copyright and related or neighboring rights ("Copyright and
|
||||
Related Rights"). Copyright and Related Rights include, but are not limited
|
||||
to, the following:
|
||||
|
||||
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
||||
and translate a Work;
|
||||
|
||||
ii. moral rights retained by the original author(s) and/or performer(s);
|
||||
|
||||
iii. publicity and privacy rights pertaining to a person's image or likeness
|
||||
depicted in a Work;
|
||||
|
||||
iv. rights protecting against unfair competition in regards to a Work,
|
||||
subject to the limitations in paragraph 4(a), below;
|
||||
|
||||
v. rights protecting the extraction, dissemination, use and reuse of data in
|
||||
a Work;
|
||||
|
||||
vi. database rights (such as those arising under Directive 96/9/EC of the
|
||||
European Parliament and of the Council of 11 March 1996 on the legal
|
||||
protection of databases, and under any national implementation thereof,
|
||||
including any amended or successor version of such directive); and
|
||||
|
||||
vii. other similar, equivalent or corresponding rights throughout the world
|
||||
based on applicable law or treaty, and any national implementations thereof.
|
||||
|
||||
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
||||
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
||||
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
||||
and Related Rights and associated claims and causes of action, whether now
|
||||
known or unknown (including existing as well as future claims and causes of
|
||||
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
||||
duration provided by applicable law or treaty (including future time
|
||||
extensions), (iii) in any current or future medium and for any number of
|
||||
copies, and (iv) for any purpose whatsoever, including without limitation
|
||||
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
||||
the Waiver for the benefit of each member of the public at large and to the
|
||||
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
||||
shall not be subject to revocation, rescission, cancellation, termination, or
|
||||
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
||||
by the public as contemplated by Affirmer's express Statement of Purpose.
|
||||
|
||||
3. Public License Fallback. Should any part of the Waiver for any reason be
|
||||
judged legally invalid or ineffective under applicable law, then the Waiver
|
||||
shall be preserved to the maximum extent permitted taking into account
|
||||
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
||||
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
||||
non transferable, non sublicensable, non exclusive, irrevocable and
|
||||
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
||||
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
||||
provided by applicable law or treaty (including future time extensions), (iii)
|
||||
in any current or future medium and for any number of copies, and (iv) for any
|
||||
purpose whatsoever, including without limitation commercial, advertising or
|
||||
promotional purposes (the "License"). The License shall be deemed effective as
|
||||
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
||||
License for any reason be judged legally invalid or ineffective under
|
||||
applicable law, such partial invalidity or ineffectiveness shall not
|
||||
invalidate the remainder of the License, and in such case Affirmer hereby
|
||||
affirms that he or she will not (i) exercise any of his or her remaining
|
||||
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
||||
and causes of action with respect to the Work, in either case contrary to
|
||||
Affirmer's express Statement of Purpose.
|
||||
|
||||
4. Limitations and Disclaimers.
|
||||
|
||||
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
||||
surrendered, licensed or otherwise affected by this document.
|
||||
|
||||
b. Affirmer offers the Work as-is and makes no representations or warranties
|
||||
of any kind concerning the Work, express, implied, statutory or otherwise,
|
||||
including without limitation warranties of title, merchantability, fitness
|
||||
for a particular purpose, non infringement, or the absence of latent or
|
||||
other defects, accuracy, or the present or absence of errors, whether or not
|
||||
discoverable, all to the greatest extent permissible under applicable law.
|
||||
|
||||
c. Affirmer disclaims responsibility for clearing rights of other persons
|
||||
that may apply to the Work or any use thereof, including without limitation
|
||||
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
||||
disclaims responsibility for obtaining any necessary consents, permissions
|
||||
or other rights required for any use of the Work.
|
||||
|
||||
d. Affirmer understands and acknowledges that Creative Commons is not a
|
||||
party to this document and has no duty or obligation with respect to this
|
||||
CC0 or use of the Work.
|
||||
|
||||
For more information, please see
|
||||
<http://creativecommons.org/publicdomain/zero/1.0/>
|
36
shaders/transform.glsl
Normal file
36
shaders/transform.glsl
Normal file
|
@ -0,0 +1,36 @@
|
|||
@ctype mat4 HMM_Mat4
|
||||
@vs vs
|
||||
in vec4 pos;
|
||||
in vec4 color0;
|
||||
in vec2 texcoord0;
|
||||
|
||||
uniform vs_params {
|
||||
mat4 mvp;
|
||||
};
|
||||
|
||||
out vec4 color;
|
||||
out vec2 uv;
|
||||
|
||||
void main() {
|
||||
gl_Position = mvp * pos;
|
||||
color = color0;
|
||||
uv = texcoord0;
|
||||
}
|
||||
@end
|
||||
|
||||
@fs fs
|
||||
uniform texture2D tex;
|
||||
uniform sampler smp;
|
||||
|
||||
in vec4 color;
|
||||
in vec2 uv;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
frag_color = texture(sampler2D(tex, smp), uv) * color;
|
||||
}
|
||||
@end
|
||||
|
||||
@program transform vs fs
|
||||
|
105
src/main.c
105
src/main.c
|
@ -2,10 +2,10 @@
|
|||
#include <sokol/sokol_gfx.h>
|
||||
#include <sokol/sokol_log.h>
|
||||
#include <sokol/sokol_glue.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <shaders/shader.glsl.h>
|
||||
#include <slibs/slibs.h>
|
||||
#include <HandmadeMath/HandmadeMath.h>
|
||||
|
||||
#include <shaders/transform.glsl.h>
|
||||
|
||||
static struct {
|
||||
sg_pipeline pip;
|
||||
|
@ -15,6 +15,7 @@ static struct {
|
|||
|
||||
typedef struct {
|
||||
float x, y, z;
|
||||
uint32_t color;
|
||||
int16_t u, v;
|
||||
} vertex_t;
|
||||
|
||||
|
@ -24,15 +25,14 @@ static void init(void) {
|
|||
.logger.func = slog_func,
|
||||
});
|
||||
|
||||
{
|
||||
vertex_t vertices[] = {
|
||||
{ 1.0f, 1.0f, 0.0f, 1.0f * 32767, 1.0f * 32767 }, // top right
|
||||
{ 1.0f, -1.0f, 0.0f, 1.0f * 32767, 0.0f * 32767 }, // bottom right
|
||||
{ -1.0f, -1.0f, 0.0f, 0.0f * 32767, 0.0f * 32767 }, // bottom left
|
||||
{ -1.0f, 1.0f, 0.0f, 0.0f * 32767, 1.0f * 32767 } // top left
|
||||
{ 1.0f, 1.0f, 0.0f, 0xFFFF0000, 1.0f * 32767, 1.0f * 32767 }, // top right
|
||||
{ 1.0f, -1.0f, 0.0f, 0xFF00FF00, 1.0f * 32767, 0.0f * 32767 }, // bottom right
|
||||
{ -1.0f, -1.0f, 0.0f, 0xFF0000FF, 0.0f * 32767, 0.0f * 32767 }, // bottom left
|
||||
{ -1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 1.0f * 32767 } // top left
|
||||
};
|
||||
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
uint16_t indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3 // second triangle
|
||||
};
|
||||
|
@ -51,8 +51,8 @@ static void init(void) {
|
|||
});
|
||||
|
||||
uint32_t pixels[2*2] = {
|
||||
0xFF595959, 0xFF242424,
|
||||
0xFF242424, 0xFF595959
|
||||
0xFFFFFFFF, 0xFF222222,
|
||||
0xFF222222, 0xFFFFFFFF
|
||||
};
|
||||
|
||||
bind->fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){
|
||||
|
@ -66,66 +66,25 @@ static void init(void) {
|
|||
bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){
|
||||
.label = "triangle-sampler"
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
vertex_t vertices[] = {
|
||||
{ 0.5f, 0.5f, 0.0f, 1.0f * 32767, 1.0f * 32767 }, // top right
|
||||
{ 0.5f, -0.5f, 0.0f, 1.0f * 32767, 0.0f * 32767 }, // bottom right
|
||||
{ -0.5f, -0.5f, 0.0f, 0.0f * 32767, 0.0f * 32767 }, // bottom left
|
||||
{ -0.5f, 0.5f, 0.0f, 0.0f * 32767, 1.0f * 32767 } // top left
|
||||
};
|
||||
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3 // second triangle
|
||||
};
|
||||
|
||||
sg_bindings* bind = calloc(1, sizeof(sg_bindings));
|
||||
sl_vec_push(state.bindings, bind);
|
||||
bind->vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
|
||||
.data = SG_RANGE(vertices),
|
||||
.label = "triangle-vertices"
|
||||
});
|
||||
|
||||
bind->index_buffer = sg_make_buffer(&(sg_buffer_desc){
|
||||
.type = SG_BUFFERTYPE_INDEXBUFFER,
|
||||
.data = SG_RANGE(indices),
|
||||
.label = "triangle-indices"
|
||||
});
|
||||
|
||||
uint32_t pixels[2 * 2] = {
|
||||
0xFF242424, 0xFF595959,
|
||||
0xFF595959, 0xFF242424,
|
||||
};
|
||||
|
||||
bind->fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){
|
||||
.width = 2,
|
||||
.height = 2,
|
||||
.data.subimage[0][0] = SG_RANGE(pixels),
|
||||
.label = "triangle-texture",
|
||||
.pixel_format = SG_PIXELFORMAT_BGRA8,
|
||||
});
|
||||
|
||||
bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){
|
||||
.label = "triangle-sampler"
|
||||
});
|
||||
}
|
||||
|
||||
sg_shader shd = sg_make_shader(triangle_shader_desc(sg_query_backend()));
|
||||
sg_shader shd = sg_make_shader(transform_shader_desc(sg_query_backend()));
|
||||
|
||||
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
|
||||
.shader = shd,
|
||||
.layout = {
|
||||
.attrs = {
|
||||
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
|
||||
[ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT3,
|
||||
[ATTR_vs_color0].format = SG_VERTEXFORMAT_UBYTE4N,
|
||||
[ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N,
|
||||
}
|
||||
},
|
||||
.label = "triangle-pipeline",
|
||||
.index_type = SG_INDEXTYPE_UINT32,
|
||||
.index_type = SG_INDEXTYPE_UINT16,
|
||||
.cull_mode = SG_CULLMODE_BACK,
|
||||
.sample_count = 1,
|
||||
.depth = {
|
||||
.compare = SG_COMPAREFUNC_LESS_EQUAL,
|
||||
.write_enabled = true,
|
||||
},
|
||||
.label = "triangle-pipeline",
|
||||
});
|
||||
|
||||
state.pass_action = (sg_pass_action) {
|
||||
|
@ -133,11 +92,29 @@ static void init(void) {
|
|||
};
|
||||
}
|
||||
|
||||
float t = 0.0f;
|
||||
|
||||
void frame(void) {
|
||||
t += sapp_frame_duration();
|
||||
|
||||
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
|
||||
sg_apply_pipeline(state.pip);
|
||||
for(sl_vec_it(bind, state.bindings)) {
|
||||
sg_apply_bindings(*bind);
|
||||
|
||||
HMM_Mat4 view = HMM_Translate(HMM_V3(0.0f, 0.0f, -3.0f));
|
||||
HMM_Mat4 projection = HMM_Perspective_RH_NO(HMM_AngleDeg(45.0f), sapp_widthf() / sapp_heightf(), 0.1f, 100.0f);
|
||||
HMM_Mat4 view_proj = HMM_MulM4(projection, view);
|
||||
|
||||
for(size_t i = 0; i < state.bindings.size; i++) {
|
||||
HMM_Mat4 model = HMM_Rotate_RH(HMM_AngleDeg(-55.0f), HMM_V3(1.0f, 0.0f, 0.0f));
|
||||
model = HMM_MulM4(model, HMM_Rotate_RH(HMM_AngleDeg(sin(t * 2) * 10), HMM_V3(0.0f, 0.2f, 0.0f)));
|
||||
HMM_Mat4 mvp = HMM_MulM4(view_proj, model);
|
||||
|
||||
vs_params_t params = {
|
||||
.mvp = mvp
|
||||
};
|
||||
|
||||
sg_apply_bindings(state.bindings.data[i]);
|
||||
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, SG_RANGE_REF(params));
|
||||
sg_draw(0, 6, 1);
|
||||
}
|
||||
sg_end_pass();
|
||||
|
@ -160,4 +137,4 @@ sapp_desc sokol_main(int argc, char* argv[]) {
|
|||
.icon.sokol_default = true,
|
||||
.logger.func = slog_func,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue