renderer2d work

This commit is contained in:
DaniTheSkunk 2023-04-16 22:40:41 +00:00
parent 3ea92c4618
commit 5c8abf1a7d
9 changed files with 215 additions and 96 deletions

View File

@ -3,20 +3,45 @@
<Objective-C>
<option name="HEADER_GUARD_STYLE_PATTERN" value="GUARD_${UUID}" />
</Objective-C>
<clangFormatSettings>
<option name="ENABLED" value="true" />
</clangFormatSettings>
<files>
<extensions>
<pair source="c" header="h" fileNamingConvention="SNAKE_CASE" />
<pair source="cu" header="cuh" fileNamingConvention="NONE" />
<pair source="ixx" header="" fileNamingConvention="NONE" />
<pair source="mxx" header="" fileNamingConvention="NONE" />
<pair source="cppm" header="" fileNamingConvention="NONE" />
<pair source="ccm" header="" fileNamingConvention="NONE" />
<pair source="cxxm" header="" fileNamingConvention="NONE" />
<pair source="c++m" header="" fileNamingConvention="NONE" />
</extensions>
</files>
<Objective-C-extensions>
<rules>
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SCREAMING_SNAKE_CASE" suffix="" />
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
</rules>
</Objective-C-extensions>
<Objective-C-extensions>
<rules>
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SCREAMING_SNAKE_CASE" suffix="" />
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
</rules>
</Objective-C-extensions>
</code_scheme>
</component>

View File

@ -1,5 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@ -2,12 +2,19 @@
#define GUARD_2B56918289975E8AA8711D6E080ED4B9
#include "shader.h"
#include "types.h"
#include "vec2f.h"
#include "vec4f.h"
#include "vertex_buffer.h"
struct sw_renderer;
struct sw_renderer2d {
struct sw_shaderprogram shader;
struct sw_renderer *renderer;
struct sw_vertex_buffer *vertex_buffer;
struct sw_vec4f color;
u32 vao, vbo;
};
struct sw_renderer2d *sw_renderer2d_create();
@ -22,6 +29,17 @@ struct sw_renderer2d *sw_renderer2d_create();
- draw "sprite" (quad of same size as texture)
- draw line
- transform matrix stack
- coordinate style, match pixel coordinates, vs 1.f full vertical range
*/
void sw_renderer2d_draw_quad(
struct sw_renderer2d *renderer,
struct sw_vec2f a,
struct sw_vec2f b,
struct sw_vec2f c,
struct sw_vec2f d
);
void sw_renderer2d_set_color(struct sw_renderer2d *ren, struct sw_vec4f color);
#endif /* GUARD_2B56918289975E8AA8711D6E080ED4B9 */

View File

@ -11,10 +11,14 @@ struct sw_vertex_buffer {
};
struct sw_vertex_buffer *sw_vertex_buffer_create(u32 elem_size);
void sw_vertex_buffer_destroy(struct sw_vertex_buffer *vb);
/* copies elem data */
void sw_vertex_buffer_add(struct sw_vertex_buffer *vb, void *elem);
void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb);
void sw_vertex_buffer_send(struct sw_vertex_buffer *vb);
#endif /* GUARD_B3E8DB670A75ACCB1082192FB0485037 */

View File

@ -10,7 +10,9 @@
#include "window.h"
#include <gl/gl.h>
#include <string.h>
#define GSA_NOMAIN
#include "gsa.h"
#include "gsa_input.h"
@ -34,6 +36,7 @@ static u32 tex;
#define GSA_LOOP_STACK_SIZE 16
static bool (*loop_stack[GSA_LOOP_STACK_SIZE])();
static i8 loop_stack_i;
static struct sw_vertex_buffer *vertex_buffer;
@ -70,7 +73,12 @@ int gsa_main(int argc, char *argv[]) {
frag_data = sw_skip_get_string(sw_skip_self, "gsa_frag.glsl");
vert_data = sw_skip_get_string(sw_skip_self, "gsa_vert.glsl");
program =
sw_shaderprogram_create((u8 *)vert_data, 0, (u8 *)frag_data, 0, false);
sw_shaderprogram_create((u8 *) vert_data,
0,
(u8 *) frag_data,
0,
false
);
memcpy(palette, gfx->palette, sizeof(gfx->palette));
glGenTextures(1, &tex);
@ -113,7 +121,8 @@ int gsa_main(int argc, char *argv[]) {
(void *) (2 * sizeof(float))
);
glEnableVertexAttribArray(glGetAttribLocation(program._program, "pos"));
glEnableVertexAttribArray(glGetAttribLocation(program._program, "tex_coord")
glEnableVertexAttribArray(
glGetAttribLocation(program._program, "tex_coord")
);
glBindVertexArray(0);
@ -246,12 +255,7 @@ static void gsa_tick() {
glUniform4fv(
glGetUniformLocation(program._program, "palette"), 256, palette_gl
);
glBufferData(
GL_ARRAY_BUFFER,
(i64)(sizeof(struct render_vert) * vertex_buffer->elem_count),
vertex_buffer->data,
GL_DYNAMIC_DRAW
);
sw_vertex_buffer_send(vertex_buffer);
glDrawArrays(GL_TRIANGLES, 0, (i32) vertex_buffer->elem_count);
glBindVertexArray(0);
/* glEnd(); */

View File

@ -8,10 +8,12 @@
#include "gl.h"
struct vert {
f32 x, y, z;
struct sw_vec2f pos;
struct sw_vec4f color;
};
static void r_enter(struct sw_window *win, void *data);
static void r_exit(struct sw_window *win, void *data);
struct sw_renderer2d *sw_renderer2d_create() {
@ -25,28 +27,84 @@ struct sw_renderer2d *sw_renderer2d_create() {
vert = sw_skip_get_string(sw_skip_self, "renderer2d_vert.glsl");
ren->shader = sw_shaderprogram_create((u8 *) vert, 0, (u8 *) frag, 0, 0);
ren->vertex_buffer = sw_vertex_buffer_create(sizeof(struct vert));
ren->color = sw_vec4f(1.f, 1.f, 1.f, 1.f);
glGenVertexArrays(1, &ren->vao);
glBindVertexArray(ren->vao);
glGenBuffers(1, &ren->vbo);
glBindBuffer(GL_ARRAY_BUFFER, ren->vbo);
/* todo: vertex attrib thingies */
glVertexAttribPointer(
0, 2, GL_FLOAT, GL_FALSE, sizeof(struct vert), 0
);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
return ren;
}
void sw_renderer2d_draw_quad(
struct sw_renderer2d *ren,
struct sw_vec2f a,
struct sw_vec2f b,
struct sw_vec2f c,
struct sw_vec2f d
) {
struct vert vert;
vert.color = ren->color;
vert.pos = a;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = b;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = c;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = b;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = d;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = c;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
}
void sw_renderer2d_set_color(struct sw_renderer2d *ren, struct sw_vec4f color) {
ren->color = color;
}
static void r_enter(struct sw_window *win, void *data) {
struct sw_renderer2d *ren;
(void) win;
ren = data;
sw_vertex_buffer_clear(ren->vertex_buffer);
sw_renderer2d_draw_quad(
ren,
sw_vec2f(-0.5f, -0.5f),
sw_vec2f(0.5f, -0.5f),
sw_vec2f(-0.5f, 0.5f),
sw_vec2f(0.5f, 0.5f));
glBindVertexArray(ren->vao);
sw_shaderprogram_use(ren->shader);
glBegin(GL_TRIANGLES);
glVertex2i(0, 0);
glVertex2i(1, 0);
glVertex2i(0, 1);
glEnd();
sw_vertex_buffer_send(ren->vertex_buffer);
glDrawArrays(GL_TRIANGLES, 0, (i32) ren->vertex_buffer->elem_count);
glBindVertexArray(0);
}
static void r_exit(struct sw_window *win, void *data) {
(void) win;
(void) data;
}

View File

@ -1,7 +1,7 @@
#version 460
layout(location = 0) in vec3 pos;
layout(location = 0) in vec2 pos;
void main() {
gl_Position = vec4(pos, 1.0);
gl_Position = vec4(pos, 0.0, 1.0);
}

View File

@ -1,6 +1,7 @@
#include "vertex_buffer.h"
#include "skunkworks.h"
#include "gl.h"
#include <stdlib.h>
#include <string.h>
@ -41,3 +42,12 @@ void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb) {
/* keep capacity */
vb->elem_count = 0;
}
void sw_vertex_buffer_send(struct sw_vertex_buffer *vb) {
glBufferData(
GL_ARRAY_BUFFER,
vb->elem_size * vb->elem_count,
vb->data,
GL_DYNAMIC_DRAW
);
}

View File

@ -1,6 +1,6 @@
prefix=C:/Program Files (x86)/glew
prefix=C:/Program Files (x86)/skunkworks_c
exec_prefix=${prefix}
libdir=C:/Program Files (x86)/glew/lib
libdir=C:/Program Files (x86)/skunkworks_c/lib
includedir=${prefix}/include
Name: glew