sokol sprite render

This commit is contained in:
John Alanbrook 2023-05-04 22:07:00 +00:00
parent 1408c05103
commit ae0f41539b
25 changed files with 338 additions and 352 deletions

View file

@ -83,7 +83,7 @@ ifeq ($(OS), WIN32)
EXT = .exe EXT = .exe
else else
LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic
ELIBS = engine pthread yughc glfw3 tcc quickjs c m dl ELIBS = engine pthread yughc glfw3 tcc quickjs c m dl GL
CLIBS = CLIBS =
endif endif

View file

@ -16,7 +16,7 @@ void DrawMesh(struct mesh *mesh, struct shader *shader)
uint32_t heightNr = 1; uint32_t heightNr = 1;
for (int i = 0; i < (mesh->te - mesh->textures); i++) { for (int i = 0; i < (mesh->te - mesh->textures); i++) {
glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding // glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
// retrieve texture number (the N in diffuse_textureN) // retrieve texture number (the N in diffuse_textureN)
char number = 0; char number = 0;
// TODO: malloc every single frame ... nope! Change to stack // TODO: malloc every single frame ... nope! Change to stack
@ -50,8 +50,6 @@ void DrawMesh(struct mesh *mesh, struct shader *shader)
void DrawMeshAgain(struct mesh *mesh) void DrawMeshAgain(struct mesh *mesh)
{ {
glDrawElements(GL_TRIANGLES, (mesh->ie - mesh->indices),
GL_UNSIGNED_INT, 0);
} }
struct mesh *MakeMesh(struct Vertex *vertices, struct Vertex *ve, struct mesh *MakeMesh(struct Vertex *vertices, struct Vertex *ve,
@ -72,6 +70,7 @@ struct mesh *MakeMesh(struct Vertex *vertices, struct Vertex *ve,
void setupmesh(struct mesh *mesh) void setupmesh(struct mesh *mesh)
{ {
/*
// create buffers/arrays // create buffers/arrays
glGenVertexArrays(1, &mesh->VAO); glGenVertexArrays(1, &mesh->VAO);
glGenBuffers(1, &mesh->VBO); glGenBuffers(1, &mesh->VBO);
@ -112,13 +111,15 @@ void setupmesh(struct mesh *mesh)
// Bone ids // Bone ids
glEnableVertexAttribArray(5); glEnableVertexAttribArray(5);
/* glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex,
m_BoneIDs m_BoneIDs
[MAX_BONE_INFLUENCE])); [MAX_BONE_INFLUENCE]));
*/
// Weights // Weights
glEnableVertexAttribArray(6); glEnableVertexAttribArray(6);
// glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, m_Weights)); // glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, m_Weights));
glBindVertexArray(0); glBindVertexArray(0);
*/
} }

View file

@ -53,8 +53,8 @@ static const float skyboxVertices[216] = {
struct mSkybox *MakeSkybox(const char *cubemap) struct mSkybox *MakeSkybox(const char *cubemap)
{ {
struct mSkybox *newskybox = /*
(struct mSkybox *) malloc(sizeof(struct mSkybox)); struct mSkybox *newskybox = malloc(sizeof(struct mSkybox));
newskybox->shader = MakeShader("skyvert.glsl", "skyfrag.glsl"); newskybox->shader = MakeShader("skyvert.glsl", "skyfrag.glsl");
shader_compile(newskybox->shader); shader_compile(newskybox->shader);
@ -63,24 +63,24 @@ struct mSkybox *MakeSkybox(const char *cubemap)
glGenBuffers(1, &newskybox->VBO); glGenBuffers(1, &newskybox->VBO);
glBindVertexArray(newskybox->VAO); glBindVertexArray(newskybox->VAO);
glBindBuffer(GL_ARRAY_BUFFER, newskybox->VBO); glBindBuffer(GL_ARRAY_BUFFER, newskybox->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
GL_STATIC_DRAW);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
(void *) 0); (void *) 0);
shader_use(newskybox->shader); shader_use(newskybox->shader);
shader_setint(newskybox->shader, "skybox", 0); shader_setint(newskybox->shader, "skybox", 0);
*/
/* /*
const char *faces[6] = const char *faces[6] =
{ "right.jpg", "left.jpg", "top.jpg", "bottom.jpg", "front.jpg", { "right.jpg", "left.jpg", "top.jpg", "bottom.jpg", "front.jpg",
"back.jpg" "back.jpg"
}; };
*/ */
/*
glGenTextures(1, &newskybox->id); glGenTextures(1, &newskybox->id);
glBindTexture(GL_TEXTURE_CUBE_MAP, newskybox->id); glBindTexture(GL_TEXTURE_CUBE_MAP, newskybox->id);
*/
/*char buf[100] = { '\0' };*/ /*char buf[100] = { '\0' };*/
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -94,7 +94,7 @@ struct mSkybox *MakeSkybox(const char *cubemap)
2048, 0, GL_RGB, GL_UNSIGNED_BYTE, data->pixels); 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, data->pixels);
*/ */
} }
/*
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
@ -106,11 +106,13 @@ struct mSkybox *MakeSkybox(const char *cubemap)
return newskybox; return newskybox;
*/
} }
void skybox_draw(const struct mSkybox *skybox, void skybox_draw(const struct mSkybox *skybox,
const struct mCamera *camera) const struct mCamera *camera)
{ {
/*
shader_use(skybox->shader); shader_use(skybox->shader);
mfloat_t view[16] = { 0.f }; mfloat_t view[16] = { 0.f };
@ -123,4 +125,5 @@ void skybox_draw(const struct mSkybox *skybox,
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->id); glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->id);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0); glBindVertexArray(0);
*/
} }

View file

@ -18,18 +18,22 @@ struct shader *vid_shader;
static void ds_update_texture(uint32_t unit, uint32_t texture, plm_plane_t * plane) static void ds_update_texture(uint32_t unit, uint32_t texture, plm_plane_t * plane)
{ {
/*
glActiveTexture(unit); glActiveTexture(unit);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, plane->width, plane->height, 0, GL_RED, GL_UNSIGNED_BYTE, plane->data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, plane->width, plane->height, 0, GL_RED, GL_UNSIGNED_BYTE, plane->data);
*/
} }
static void render_frame(plm_t * mpeg, plm_frame_t * frame, void *user) static void render_frame(plm_t * mpeg, plm_frame_t * frame, void *user)
{ {
struct datastream *ds = user; struct datastream *ds = user;
shader_use(ds->shader); shader_use(ds->shader);
/*
ds_update_texture(GL_TEXTURE0, ds->texture_y, &frame->y); ds_update_texture(GL_TEXTURE0, ds->texture_y, &frame->y);
ds_update_texture(GL_TEXTURE1, ds->texture_cb, &frame->cb); ds_update_texture(GL_TEXTURE1, ds->texture_cb, &frame->cb);
ds_update_texture(GL_TEXTURE2, ds->texture_cr, &frame->cr); ds_update_texture(GL_TEXTURE2, ds->texture_cr, &frame->cr);
*/
} }
static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user) static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user)
@ -45,11 +49,13 @@ static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user)
struct Texture *ds_maketexture(struct datastream *ds) struct Texture *ds_maketexture(struct datastream *ds)
{ {
/*
struct Texture *new = malloc(sizeof(*new)); struct Texture *new = malloc(sizeof(*new));
new->id = ds->texture_cb; new->id = ds->texture_cb;
new->width = 500; new->width = 500;
new->height = 500; new->height = 500;
return new; return new;
*/
} }
void ds_openvideo(struct datastream *ds, const char *video, const char *adriver) void ds_openvideo(struct datastream *ds, const char *video, const char *adriver)
@ -110,6 +116,7 @@ void ds_openvideo(struct datastream *ds, const char *video, const char *adriver)
struct datastream *MakeDatastream() struct datastream *MakeDatastream()
{ {
struct datastream *newds = malloc(sizeof(*newds)); struct datastream *newds = malloc(sizeof(*newds));
/*
if (!vid_shader) vid_shader = MakeShader("videovert.glsl", "videofrag.glsl"); if (!vid_shader) vid_shader = MakeShader("videovert.glsl", "videofrag.glsl");
newds->shader = vid_shader; newds->shader = vid_shader;
@ -137,7 +144,7 @@ struct datastream *MakeDatastream()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
shader_setint(newds->shader, "texture_cr", 2); shader_setint(newds->shader, "texture_cr", 2);
*/
return newds; return newds;
} }

View file

@ -9,6 +9,7 @@
#include "window.h" #include "window.h"
#include "2dphysics.h" #include "2dphysics.h"
#include "stb_ds.h" #include "stb_ds.h"
#include "sokol/sokol_gfx.h"
static uint32_t circleVBO; static uint32_t circleVBO;
static uint32_t circleVAO; static uint32_t circleVAO;
@ -22,15 +23,26 @@ static uint32_t rectVBO;
static uint32_t rectVAO; static uint32_t rectVAO;
static struct shader *rectShader; static struct shader *rectShader;
typedef struct {
float proj[16];
float res[2];
} circle_ubo;
void debugdraw_init() void debugdraw_init()
{ {
circleShader = MakeShader("circlevert.glsl", "circlefrag.glsl"); // circleShader = MakeShader("shaders/circlevert.glsl", "shaders/circlefrag.glsl");
shader_setUBO(circleShader, "Projection", 0); sg_shader csg = sg_make_shader(&(sg_shader_desc){
shader_setUBO(circleShader, "Resolution", 0); .vs.source = slurp_text("shaders/circlevert.glsl"),
glGenBuffers(1, &circleVBO); .fs.source = slurp_text("shaders/circlefrag.glsl"),
glGenVertexArrays(1, &circleVAO); .vs.uniform_blocks[0] = {
.size = sizeof(circle_ubo),
.uniforms = {
[0] = { .name = "proj", .type = SG_UNIFORMTYPE_MAT4 },
[1] = { .name = "res", .type = SG_UNIFORMTYPE_FLOAT2 },
}
}
});
/*
float gridverts[] = { float gridverts[] = {
-1.f, -1.f, -1.f, -1.f,
1.f, -1.f, 1.f, -1.f,
@ -52,6 +64,7 @@ void debugdraw_init()
shader_setUBO(rectShader, "Projection", 0); shader_setUBO(rectShader, "Projection", 0);
glGenBuffers(1, &rectVBO); glGenBuffers(1, &rectVBO);
glGenVertexArrays(1, &rectVAO); glGenVertexArrays(1, &rectVAO);
*/
} }
void draw_line(int x1, int y1, int x2, int y2, float *color) void draw_line(int x1, int y1, int x2, int y2, float *color)
@ -138,7 +151,7 @@ void draw_edge(cpVect *points, int n, struct color color, int thickness)
shader_use(rectShader); shader_use(rectShader);
shader_setvec3(rectShader, "linecolor", col); shader_setvec3(rectShader, "linecolor", col);
/*
if (thickness <= 1) { if (thickness <= 1) {
// glLineStipple(1, 0x00FF); // glLineStipple(1, 0x00FF);
// glEnable(GL_LINE_STIPPLE); // glEnable(GL_LINE_STIPPLE);
@ -182,11 +195,12 @@ void draw_edge(cpVect *points, int n, struct color color, int thickness)
glDrawArrays(GL_LINE_LOOP,0,n*2); glDrawArrays(GL_LINE_LOOP,0,n*2);
} }
*/
} }
void draw_circle(int x, int y, float radius, int pixels, float *color, int fill) void draw_circle(int x, int y, float radius, int pixels, float *color, int fill)
{ {
shader_use(circleShader); /* shader_use(circleShader);
float verts[] = { float verts[] = {
x - radius, y - radius, -1, -1, x - radius, y - radius, -1, -1,
@ -208,6 +222,7 @@ void draw_circle(int x, int y, float radius, int pixels, float *color, int fill)
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
*/
} }
void draw_rect(int x, int y, int w, int h, float *color) void draw_rect(int x, int y, int w, int h, float *color)
@ -241,7 +256,7 @@ void draw_arrow(struct cpVect start, struct cpVect end, struct color color, int
void draw_grid(int width, int span) void draw_grid(int width, int span)
{ {
shader_use(gridShader); /* shader_use(gridShader);
shader_setint(gridShader, "thickness", width); shader_setint(gridShader, "thickness", width);
shader_setint(gridShader, "span", span); shader_setint(gridShader, "span", span);
@ -254,6 +269,7 @@ void draw_grid(int width, int span)
glBindVertexArray(gridVAO); glBindVertexArray(gridVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
*/
} }
void draw_point(int x, int y, float r, float *color) void draw_point(int x, int y, float r, float *color)
@ -275,7 +291,7 @@ void draw_points(struct cpVect *points, int n, float size, float *color)
void draw_poly(float *points, int n, float *color) void draw_poly(float *points, int n, float *color)
{ {
shader_use(rectShader); /* shader_use(rectShader);
shader_setvec3(rectShader, "linecolor", color); shader_setvec3(rectShader, "linecolor", color);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO); glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * n * 2, points, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * n * 2, points, GL_DYNAMIC_DRAW);
@ -289,6 +305,7 @@ void draw_poly(float *points, int n, float *color)
shader_setfloat(rectShader, "alpha", 1.f); shader_setfloat(rectShader, "alpha", 1.f);
glDrawArrays(GL_LINE_LOOP, 0, n); glDrawArrays(GL_LINE_LOOP, 0, n);
*/
} }
void draw_polyvec(cpVect *points, int n, float *color) void draw_polyvec(cpVect *points, int n, float *color)

View file

@ -86,14 +86,3 @@ void log_cat(FILE *f) {
YughInfo(out); YughInfo(out);
} }
} }
void FlushGLErrors()
{
GLenum glErr = GL_NO_ERROR;
glErr = glGetError();
while (glErr != GL_NO_ERROR) {
YughLog(0, 3,
"GL Error: %d", glErr);
glErr = glGetError();
}
}

View file

@ -32,8 +32,6 @@ extern int logLevel;
void mYughLog(int category, int priority, int line, const char *file, const char *message, ...); void mYughLog(int category, int priority, int line, const char *file, const char *message, ...);
void FlushGLErrors();
void log_setfile(char *file); void log_setfile(char *file);
void log_cat(FILE *f); void log_cat(FILE *f);
void log_print(const char *str); void log_print(const char *str);

View file

@ -16,11 +16,9 @@
#include "stb_rect_pack.h" #include "stb_rect_pack.h"
#include "stb_image_write.h" #include "stb_image_write.h"
static uint32_t VBO = 0;
static uint32_t VAO = 0;
struct sFont *font; struct sFont *font;
static struct shader *shader; static struct shader *shader;
static sg_shader fontshader;
unsigned char *slurp_file(const char *filename) { unsigned char *slurp_file(const char *filename) {
FILE *f = fopen(filename, "rb"); FILE *f = fopen(filename, "rb");
@ -30,7 +28,8 @@ unsigned char *slurp_file(const char *filename) {
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
long fsize = ftell(f); long fsize = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
unsigned char *slurp = malloc(fsize); unsigned char *slurp = malloc(fsize+1);
fread(slurp,fsize,1,f);
fclose(f); fclose(f);
return slurp; return slurp;
@ -64,25 +63,46 @@ int slurp_write(const char *txt, const char *filename)
return 0; return 0;
} }
static sg_bindings bind_text;
static sg_pipeline pipe_text;
void font_init(struct shader *textshader) { void font_init(struct shader *textshader) {
shader = textshader; shader = textshader;
shader_use(shader); fontshader = sg_make_shader(&(sg_shader_desc){
.vs.source = slurp_text("shaders/textvert.glsl"),
.fs.source = slurp_text("shaders/textfrag.glsl"),
.vs.uniform_blocks[0] = {
.size = sizeof(float)*16,
.uniforms = {
[0] = { .name = "projection", .type = SG_UNIFORMTYPE_MAT4 }
}
},
glGenBuffers(1, &VBO); .fs.uniform_blocks[0] = {
glGenVertexArrays(1, &VAO); .size = sizeof(float)*3+sizeof(int),
.uniforms = {
[0] = { .name = "textColor", .type = SG_UNIFORMTYPE_FLOAT3 },
[1] = { .name = "invert", .type = SG_UNIFORMTYPE_INT }
}
}
});
glBindVertexArray(VAO); pipe_text = sg_make_pipeline(&(sg_pipeline_desc){
.shader = fontshader,
.layout = {
.attrs = { [0].format = SG_VERTEXFORMAT_FLOAT4 }
},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.label = "text pipeline"
});
glBindBuffer(GL_ARRAY_BUFFER, VBO); bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(float)*16,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM
});
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
// Default font
//font = MakeFont("teenytinypixels.ttf", 30);
font = MakeFont("LessPerfectDOSVGA.ttf", 16); font = MakeFont("LessPerfectDOSVGA.ttf", 16);
} }
@ -102,14 +122,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
char fontpath[256]; char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile); snprintf(fontpath, 256, "fonts/%s", fontfile);
FILE *f = fopen(fontpath, "rb"); unsigned char *ttf_buffer = slurp_file(fontpath);
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *ttf_buffer = malloc(fsize+1);
fread(ttf_buffer, fsize, 1, f);
fclose(f);
unsigned char *bitmap = malloc(packsize*packsize); unsigned char *bitmap = malloc(packsize*packsize);
stbtt_packedchar glyphs[95]; stbtt_packedchar glyphs[95];
@ -127,16 +140,19 @@ struct sFont *MakeFont(const char *fontfile, int height)
YughError("Failed to make font %s", fontfile); YughError("Failed to make font %s", fontfile);
} }
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); newfont->texID = sg_make_image(&(sg_image_desc){
glGenTextures(1, &newfont->texID); .type = SG_IMAGETYPE_2D,
glBindTexture(GL_TEXTURE_2D, newfont->texID); .width = packsize,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, packsize, packsize, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap); .height = packsize,
.pixel_format = SG_PIXELFORMAT_R8,
//glGenerateMipmap(GL_TEXTURE_2D); .usage = SG_USAGE_IMMUTABLE,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); .min_filter = SG_FILTER_NEAREST,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); .mag_filter = SG_FILTER_NEAREST,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); .data.subimage[0][0] = {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); .ptr = bitmap,
.size = packsize*packsize
}
});
free(ttf_buffer); free(ttf_buffer);
free(bitmap); free(bitmap);
@ -211,7 +227,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
/* Check if the vertex is off screen */ /* Check if the vertex is off screen */
if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height) if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height)
return; return;
/*
if (drawcaret == curchar) { if (drawcaret == curchar) {
draw_char_box(c, cursor, scale, color); draw_char_box(c, cursor, scale, color);
shader_use(shader); shader_use(shader);
@ -223,34 +239,34 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
} }
*/
shader_setvec3(shader, "textColor", shadowcolor); shader_setvec3(shader, "textColor", shadowcolor);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); sg_update_buffer(bind_text.vertex_buffers[0], verts);
sg_draw(0,4,1);
offset[0] = 1; offset[0] = 1;
offset[1] = -1; offset[1] = -1;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); sg_update_buffer(bind_text.vertex_buffers[0], verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); sg_draw(0,4,1);
offset[1] = 1; offset[1] = 1;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); sg_update_buffer(bind_text.vertex_buffers[0], verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); sg_draw(0,4,1);
offset[0] = -1; offset[0] = -1;
offset[1] = -1; offset[1] = -1;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); sg_update_buffer(bind_text.vertex_buffers[0], verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); sg_draw(0,4,1);
offset[0] = offset[1] = 0; offset[0] = offset[1] = 0;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
shader_setvec3(shader, "textColor", color); shader_setvec3(shader, "textColor", color);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); sg_update_buffer(bind_text.vertex_buffers[0], verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); sg_draw(0,4,1);
} }
void text_settype(struct sFont *mfont) void text_settype(struct sFont *mfont)
@ -266,13 +282,9 @@ int renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3]
mfloat_t cursor[2] = { 0.f }; mfloat_t cursor[2] = { 0.f };
cursor[0] = pos[0]; cursor[0] = pos[0];
cursor[1] = pos[1]; cursor[1] = pos[1];
shader_use(shader); sg_apply_pipeline(pipe_text);
shader_setvec3(shader, "textColor", color); sg_apply_bindings(&bind_text);
glActiveTexture(GL_TEXTURE0); sg_apply_uniforms(SG_SHADERSTAGE_FS,0,color);
glBindTexture(GL_TEXTURE_2D, font->texID);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float)*2, NULL, GL_STREAM_DRAW); /* x2 on the size for the outline pass */
const unsigned char *line, *wordstart, *drawstart; const unsigned char *line, *wordstart, *drawstart;
line = drawstart = (unsigned char*)text; line = drawstart = (unsigned char*)text;
@ -330,7 +342,5 @@ int renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3]
draw_char_box(font->Characters[69], cursor, scale, color); draw_char_box(font->Characters[69], cursor, scale, color);
} }
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4*2);
return cursor[1] - pos[1]; return cursor[1] - pos[1];
} }

View file

@ -3,6 +3,7 @@
#include "mathc.h" #include "mathc.h"
#include "texture.h" #include "texture.h"
#include "sokol/sokol_gfx.h"
struct shader; struct shader;
struct window; struct window;
@ -20,7 +21,7 @@ struct sFont {
uint32_t fontTexture; uint32_t fontTexture;
uint32_t height; uint32_t height;
struct Character Characters[127]; struct Character Characters[127];
uint32_t texID; sg_image texID;
}; };
void font_init(struct shader *s); void font_init(struct shader *s);

View file

@ -96,9 +96,11 @@ int nuke_btn(const char *lbl) {
} }
void nuke_img(char *path) { void nuke_img(char *path) {
/*
struct Texture *t = texture_pullfromfile(path); struct Texture *t = texture_pullfromfile(path);
nk_layout_row_static(ctx, t->height, t->width, 1); nk_layout_row_static(ctx, t->height, t->width, 1);
nk_image(ctx, nk_image_id(t->id)); nk_image(ctx, nk_image_id(t->id));
*/
} }
void nuke_property_int(const char *lbl, int min, int *val, int max, int step) { void nuke_property_int(const char *lbl, int min, int *val, int max, int step) {

View file

@ -52,15 +52,8 @@ bool renderReflection = true;
struct gameobject *selectedobject = NULL; struct gameobject *selectedobject = NULL;
char objectName[200] = { '\0' }; // object name buffer char objectName[200] = { '\0' }; // object name buffer
GLuint debugColorPickBO = 0;
GLuint debugColorPickTEX = 0;
struct sprite *tsprite = NULL; struct sprite *tsprite = NULL;
static unsigned int projUBO;
void debug_draw_phys(int draw) { void debug_draw_phys(int draw) {
debugDrawPhysics = draw; debugDrawPhysics = draw;
} }
@ -70,6 +63,9 @@ void opengl_rendermode(enum RenderMode r)
renderMode = r; renderMode = r;
} }
sg_pipeline mainpip;
sg_pass_action pass_action = {0};
void openglInit() void openglInit()
{ {
if (!mainwin) { if (!mainwin) {
@ -83,16 +79,13 @@ void openglInit()
animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl"); animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl");
textShader = MakeShader("textvert.glsl", "textfrag.glsl"); textShader = MakeShader("textvert.glsl", "textfrag.glsl");
shader_use(textShader);
shader_setint(textShader, "text", 0);
font_init(textShader); font_init(textShader);
sprite_initialize(); sprite_initialize();
debugdraw_init(); debugdraw_init();
glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]); glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]);
//glEnable(GL_CULL_FACE); /*
glCullFace(GL_BACK); glCullFace(GL_BACK);
glEnable(GL_BLEND); glEnable(GL_BLEND);
@ -107,13 +100,6 @@ void openglInit()
shader_setUBO(spriteShader, "Projection", 0); shader_setUBO(spriteShader, "Projection", 0);
shader_setUBO(textShader, "Projection", 0); shader_setUBO(textShader, "Projection", 0);
shader_setUBO(animSpriteShader, "Projection", 0); shader_setUBO(animSpriteShader, "Projection", 0);
/*
glGenBuffers(1,&resUBO);
glBindBuffer(GL_UNIFORM_BUFFER, resUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(float)*2, NULL, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, resUBO, 0, sizeof(float)*2);
glBindBuffer(GL_UNIFORM_BUFFER,0);
*/ */
} }
@ -132,13 +118,13 @@ float cam_zoom() { return zoom; }
void add_zoom(float val) { zoom = val; } void add_zoom(float val) { zoom = val; }
mfloat_t projection[16] = {0.f};
void openglRender(struct window *window) void openglRender(struct window *window)
{ {
glCullFace(GL_BACK); sg_begin_default_pass(&pass_action, window->width, window->height);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//////////// 2D projection //////////// 2D projection
mfloat_t projection[16] = { 0.f };
cpVect pos = cam_pos(); cpVect pos = cam_pos();
mat4_ortho(projection, pos.x - zoom*window->width/2, mat4_ortho(projection, pos.x - zoom*window->width/2,
@ -152,55 +138,30 @@ void openglRender(struct window *window)
0, 0,
window->height, -1.f, 1.f); window->height, -1.f, 1.f);
// Clear color and depth // sprite_draw_all();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); gui_draw_img("pill1.png", 200, 200);
// renderText("TEST RENDER", {100,100}, 1.f, {1.f,1.f,1.f}, 0,-1);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
/*
float res[2] = {window->width, window->height};
glBindBuffer(GL_UNIFORM_BUFFER, resUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(float)*2, res);
*/
/* Game sprites */
switch (renderMode) {
case LIT:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
shader_use(spriteShader);
break;
case WIREFRAME:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shader_use(wireframeShader);
break;
};
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
sprite_draw_all();
/* UI Elements & Debug elements */ /* UI Elements & Debug elements */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_DEPTH_TEST); // glDisable(GL_DEPTH_TEST);
//// DEBUG //// DEBUG
if (debugDrawPhysics) // if (debugDrawPhysics)
gameobject_draw_debugs(); // gameobject_draw_debugs();
call_debugs(); // call_debugs();
////// TEXT && GUI ////// TEXT && GUI
glBindBuffer(GL_UNIFORM_BUFFER, projUBO); // glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection); // glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection);
call_gui(); // call_gui();
nuke_start(); // nuke_start();
call_nk_gui(); // call_nk_gui();
nuke_end(); // nuke_end();
}
void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind) sg_end_pass();
{ sg_commit();
glUniformBlockBinding(shaderID, glGetUniformBlockIndex(shaderID, bufferName), bufferBind);
} }

View file

@ -1,8 +1,6 @@
#ifndef OPENGL_RENDER_H #ifndef OPENGL_RENDER_H
#define OPENGL_RENDER_H #define OPENGL_RENDER_H
#include "render.h"
struct mCamera; struct mCamera;
struct window; struct window;
@ -13,6 +11,8 @@ extern struct sprite *tsprite;
extern int renderMode; extern int renderMode;
extern float projection[16];
extern float gridScale; extern float gridScale;
extern float smallGridUnit; extern float smallGridUnit;
extern float bigGridUnit; extern float bigGridUnit;
@ -24,7 +24,7 @@ extern float gridOpacity;
extern float editorFOV; extern float editorFOV;
extern float shadowLookahead; extern float shadowLookahead;
extern char objectName[]; extern char objectName[];
extern GLuint debugColorPickBO; extern int debugColorPickBO;
extern struct gameobject *selectedobject; extern struct gameobject *selectedobject;
@ -47,8 +47,6 @@ void openglRender3d(struct window *window, struct mCamera *camera);
void debug_draw_phys(int draw); void debug_draw_phys(int draw);
void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind);
void set_cam_body(cpBody *body); void set_cam_body(cpBody *body);
cpVect cam_pos(); cpVect cam_pos();
float cam_zoom(); float cam_zoom();

View file

@ -2,7 +2,6 @@
#define RENDER_H #define RENDER_H
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#endif #endif

View file

@ -9,6 +9,7 @@
#include "resources.h" #include "resources.h"
#include "stb_ds.h" #include "stb_ds.h"
#include "timer.h" #include "timer.h"
#include "font.h"
#include "time.h" #include "time.h"
@ -29,8 +30,9 @@ struct shader *MakeShader(const char *vertpath, const char *fragpath)
return &arrlast(shaders); return &arrlast(shaders);
} }
int shader_compile_error(GLuint shader) int shader_compile_error(int shader)
{ {
/*
GLint success = 0; GLint success = 0;
GLchar infoLog[ERROR_BUFFER] = { '\0' }; GLchar infoLog[ERROR_BUFFER] = { '\0' };
@ -41,10 +43,12 @@ int shader_compile_error(GLuint shader)
YughLog(0, LOG_ERROR, "Shader compilation error.\nLog: %s", infoLog); YughLog(0, LOG_ERROR, "Shader compilation error.\nLog: %s", infoLog);
return 1; return 1;
*/
} }
int shader_link_error(GLuint shader) int shader_link_error(int shader)
{ {
/*
GLint success = 0; GLint success = 0;
GLchar infoLog[ERROR_BUFFER] = { '\0' }; GLchar infoLog[ERROR_BUFFER] = { '\0' };
@ -55,9 +59,10 @@ int shader_link_error(GLuint shader)
YughLog(0, LOG_ERROR, "Shader link error.\nLog: %s", infoLog); YughLog(0, LOG_ERROR, "Shader link error.\nLog: %s", infoLog);
return 1; return 1;
*/
} }
GLuint load_shader_from_file(const char *path, int type) int load_shader_from_file(const char *path, int type)
{ {
char spath[MAXPATH] = {'\0'}; char spath[MAXPATH] = {'\0'};
@ -77,7 +82,7 @@ GLuint load_shader_from_file(const char *path, int type)
fclose(f); fclose(f);
/*
GLuint id = glCreateShader(type); GLuint id = glCreateShader(type);
const char *code = buf; const char *code = buf;
glShaderSource(id, 1, &code, NULL); glShaderSource(id, 1, &code, NULL);
@ -89,31 +94,35 @@ GLuint load_shader_from_file(const char *path, int type)
free(buf); free(buf);
return id; return id;
*/
} }
void shader_compile(struct shader *shader) void shader_compile(struct shader *shader)
{ {
YughInfo("Making shader with %s and %s.", shader->vertpath, shader->fragpath); YughInfo("Making shader with %s and %s.", shader->vertpath, shader->fragpath);
char spath[MAXPATH];
sprintf(spath,"%s%s", "shaders/", shader->vertpath);
const char *vsrc = slurp_text(spath);
sprintf(spath, "%s%s", "shaders/", shader->fragpath);
const char *fsrc = slurp_text(spath);
GLuint vert = load_shader_from_file(shader->vertpath, GL_VERTEX_SHADER); shader->shd = sg_make_shader(&(sg_shader_desc){
GLuint frag = load_shader_from_file(shader->fragpath, GL_FRAGMENT_SHADER); .vs.source = vsrc,
.fs.source = fsrc,
.label = shader->vertpath,
});
shader->id = glCreateProgram(); free(vsrc);
glAttachShader(shader->id, vert); free(fsrc);
glAttachShader(shader->id, frag);
glLinkProgram(shader->id);
shader_link_error(shader->id);
glDeleteShader(vert);
glDeleteShader(frag);
} }
void shader_use(struct shader *shader) void shader_use(struct shader *shader)
{ {
glUseProgram(shader->id); // glUseProgram(shader->id);
} }
/*
void shader_setbool(struct shader *shader, const char *name, int val) void shader_setbool(struct shader *shader, const char *name, int val)
{ {
glUniform1i(glGetUniformLocation(shader->id, name), val); glUniform1i(glGetUniformLocation(shader->id, name), val);
@ -134,10 +143,6 @@ void shader_setvec2(struct shader *shader, const char *name, mfloat_t val[2])
glUniform2fv(glGetUniformLocation(shader->id, name), 1, val); glUniform2fv(glGetUniformLocation(shader->id, name), 1, val);
} }
void shader_setvec3(struct shader *shader, const char *name, mfloat_t val[3])
{
glUniform3fv(glGetUniformLocation(shader->id, name), 1, val);
}
void shader_setvec4(struct shader *shader, const char *name, mfloat_t val[4]) void shader_setvec4(struct shader *shader, const char *name, mfloat_t val[4])
{ {
@ -164,10 +169,18 @@ void shader_setUBO(struct shader *shader, const char *name, unsigned int index)
glUniformBlockBinding(shader->id, glGetUniformBlockIndex(shader->id, name), index); glUniformBlockBinding(shader->id, glGetUniformBlockIndex(shader->id, name), index);
} }
*/
void shader_compile_all() void shader_compile_all()
{ {
for (int i = 0; i < arrlen(shaders); i++) for (int i = 0; i < arrlen(shaders); i++)
shader_compile(&shaders[i]); shader_compile(&shaders[i]);
} }
void shader_setvec3(struct shader *shader, const char *name, mfloat_t val[3])
{
// glUniform3fv(glGetUniformLocation(shader->id, name), 1, val);
}
void shader_setmat4(struct shader *shader, const char *name, mfloat_t val[16])
{
// glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
}

View file

@ -2,9 +2,10 @@
#define SHADER_H #define SHADER_H
#include "mathc.h" #include "mathc.h"
#include "sokol/sokol_gfx.h"
struct shader { struct shader {
unsigned int id; sg_shader shd;
const char *vertpath; const char *vertpath;
const char *fragpath; const char *fragpath;
}; };
@ -13,7 +14,7 @@ void shader_compile_all();
struct shader *MakeShader(const char *vertpath, const char *fragpath); struct shader *MakeShader(const char *vertpath, const char *fragpath);
void shader_compile(struct shader *shader); void shader_compile(struct shader *shader);
void shader_use(struct shader *shader); void shader_use(struct shader *shader);
/*
void shader_setbool(struct shader *shader, const char *name, int val); void shader_setbool(struct shader *shader, const char *name, int val);
void shader_setint(struct shader *shader, const char *name, int val); void shader_setint(struct shader *shader, const char *name, int val);
void shader_setfloat(struct shader *shader, const char *name, float val); void shader_setfloat(struct shader *shader, const char *name, float val);
@ -26,5 +27,5 @@ void shader_setmat3(struct shader *shader, const char *name, mfloat_t val[9]);
void shader_setmat4(struct shader *shader, const char *name, mfloat_t val[16]); void shader_setmat4(struct shader *shader, const char *name, mfloat_t val[16]);
void shader_setUBO(struct shader *shader, const char *name, unsigned int index); void shader_setUBO(struct shader *shader, const char *name, unsigned int index);
*/
#endif #endif

View file

@ -19,6 +19,9 @@ static int first = -1;
static uint32_t VBO; static uint32_t VBO;
sg_pipeline pip_sprite;
sg_bindings bind_sprite;
int make_sprite(int go) int make_sprite(int go)
{ {
struct sprite sprite = { struct sprite sprite = {
@ -82,6 +85,9 @@ void sprite_io(struct sprite *sprite, FILE *f, int read)
void sprite_draw_all() void sprite_draw_all()
{ {
YughWarn("Applying sprite pipeline");
sg_apply_pipeline(pip_sprite);
static struct sprite **layers[5]; static struct sprite **layers[5];
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
@ -108,29 +114,57 @@ void sprite_settex(struct sprite *sprite, struct Texture *tex)
sprite_setframe(sprite, &ST_UNIT); sprite_setframe(sprite, &ST_UNIT);
} }
static uint32_t VAO = 0; sg_shader shader_sprite;
void sprite_initialize() void sprite_initialize()
{ {
glGenBuffers(1, &VBO); shader_sprite = sg_make_shader(&(sg_shader_desc){
glGenVertexArrays(1, &VAO); .vs.source = slurp_text("shaders/spritevert.glsl"),
.fs.source = slurp_text("shaders/spritefrag.glsl"),
.vs.uniform_blocks[0] = {
.size = 64,
.layout = SG_UNIFORMLAYOUT_STD140,
.uniforms = { [0] = { .name = "mpv", .type = SG_UNIFORMTYPE_MAT4 }}
},
glBindVertexArray(VAO); .fs.images[0] = {
.name = "image",
.image_type = SG_IMAGETYPE_2D,
.sampler_type = SG_SAMPLERTYPE_FLOAT,
},
glBindBuffer(GL_ARRAY_BUFFER, VBO); .fs.uniform_blocks[0] = {
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); .size = 12,
glEnableVertexAttribArray(0); .uniforms = { [0] = { .name = "spriteColor", .type = SG_UNIFORMTYPE_FLOAT3 }}
}
});
pip_sprite = sg_make_pipeline(&(sg_pipeline_desc){
.shader = shader_sprite,
.layout = {
.attrs = {
[0].format=SG_VERTEXFORMAT_FLOAT4
}
},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.label = "sprite pipeline"
});
bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(float)*16,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM,
.label = "sprite vertex buffer",
});
} }
void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2], struct glrect r, mfloat_t color[3]) { void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2], struct glrect r, float color[3]) {
mfloat_t model[16] = { 0.f }; float model[16] = { 0.f };
mfloat_t r_model[16] = { 0.f }; mfloat_t r_model[16] = { 0.f };
mfloat_t s_model[16] = { 0.f };
memcpy(model, UNITMAT4, sizeof(UNITMAT4)); memcpy(model, UNITMAT4, sizeof(UNITMAT4));
memcpy(r_model, UNITMAT4, sizeof(UNITMAT4)); memcpy(r_model, UNITMAT4, sizeof(UNITMAT4));
memcpy(s_model, UNITMAT4, sizeof(UNITMAT4));
mfloat_t t_scale[2] = { tex->width * st_s_w(r), tex->height * st_s_h(r) }; mfloat_t t_scale[2] = { tex->width * st_s_w(r) * size[0], tex->height * st_s_h(r) * size[1] };
mfloat_t t_offset[2] = { offset[0] * t_scale[0] * size[0], offset[1] * t_scale[1] * size[1]}; mfloat_t t_offset[2] = { offset[0] * t_scale[0] * size[0], offset[1] * t_scale[1] * size[1]};
mat4_translate_vec2(model, t_offset); mat4_translate_vec2(model, t_offset);
@ -141,27 +175,26 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo
mat4_multiply(model, r_model, model); mat4_multiply(model, r_model, model);
mat4_translate_vec2(model, pos); mat4_translate_vec2(model, pos);
mat4_multiply(model, projection,model);
shader_setmat4(spriteShader, "model", model);
shader_setvec3(spriteShader, "spriteColor", color);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
float vertices[] = { float vertices[] = {
0.f, 0.f, r.s0, r.t1, 0.f, 0.f, r.s0, r.t1,
size[0], 0.f, r.s1, r.t1, 1, 0.f, r.s1, r.t1,
0.f, size[1], r.s0, r.t0, 0.f, 1, r.s0, r.t0,
size[0], size[1], r.s1, r.t0 1.f, 1.f, r.s1, r.t0
}; };
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW); bind_sprite.fs_images[0] = tex->id;
sg_update_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(vertices));
sg_apply_bindings(&bind_sprite);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(model));
float c[3];
for (int i = 0; i < 3; i++) c[i] = color[i];
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(c));
sg_draw(0,4,1);
} }
void sprite_draw(struct sprite *sprite) void sprite_draw(struct sprite *sprite)
@ -184,12 +217,12 @@ void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame)
} }
void gui_draw_img(const char *img, float x, float y) { void gui_draw_img(const char *img, float x, float y) {
shader_use(spriteShader); sg_apply_pipeline(pip_sprite);
struct Texture *tex = texture_loadfromfile(img); struct Texture *tex = texture_loadfromfile(img);
float pos[2] = {x, y}; float pos[2] = {x, y};
float size[2] = {1.f, 1.f}; float size[2] = {1.f, 1.f};
float offset[2] = { 0.f, 0.f }; float offset[2] = { 0.f, 0.f };
float white[3] = {1.f,1.f,1.f}; float white[3] = {0.3f,1.f,1.f};
tex_draw(tex, pos, 0.f, size, offset, tex_get_rect(tex), white); tex_draw(tex, pos, 0.f, size, offset, tex_get_rect(tex), white);
} }
@ -209,7 +242,7 @@ void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2
shader_setmat4(vid_shader, "model", model); shader_setmat4(vid_shader, "model", model);
shader_setvec3(vid_shader, "spriteColor", color); shader_setvec3(vid_shader, "spriteColor", color);
/*
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, stream->texture_y); glBindTexture(GL_TEXTURE_2D, stream->texture_y);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
@ -219,4 +252,6 @@ void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2
// TODO: video bind VAO // TODO: video bind VAO
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
*/
} }

View file

@ -7,7 +7,7 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include "util.h" #include "util.h"
#include "parson.h" #include "sokol/sokol_gfx.h"
struct glrect ST_UNIT = { 0.f, 1.f, 0.f, 1.f }; struct glrect ST_UNIT = { 0.f, 1.f, 0.f, 1.f };
@ -47,54 +47,32 @@ struct Texture *texture_pullfromfile(const char *path)
} }
tex->data = data; tex->data = data;
glGenTextures(1, &tex->id); int filter;
glBindTexture(GL_TEXTURE_2D, tex->id);
GLenum fmt;
switch (n) {
case 1:
fmt = GL_RED;
break;
case 2:
fmt = GL_RG;
break;
case 3:
fmt = GL_RGB;
break;
case 4:
fmt = GL_RGBA;
break;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, fmt, GL_UNSIGNED_BYTE, data);
if (tex->opts.mips)
glGenerateMipmap(GL_TEXTURE_2D);
if (tex->opts.sprite) { if (tex->opts.sprite) {
if (tex->opts.mips) { if (tex->opts.mips)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); filter = SG_FILTER_NEAREST_MIPMAP_NEAREST;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST); else
filter = SG_FILTER_NEAREST;
} else { } else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); if (tex->opts.mips)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); filter = SG_FILTER_LINEAR_MIPMAP_LINEAR;
} else
} else { filter = SG_FILTER_LINEAR;
if (tex->opts.mips) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
} }
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); tex->id = sg_make_image(&(sg_image_desc){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); .type = SG_IMAGETYPE_2D,
.width = tex->width,
.height = tex->height,
.usage = SG_USAGE_IMMUTABLE,
.min_filter = filter,
.mag_filter = filter,
.data.subimage[0][0] = {
.ptr = data,
.size = tex->width*tex->height*4
}
});
if (shlen(texhash) == 0) if (shlen(texhash) == 0)
sh_new_arena(texhash); sh_new_arena(texhash);
@ -106,32 +84,7 @@ struct Texture *texture_pullfromfile(const char *path)
void texture_sync(const char *path) void texture_sync(const char *path)
{ {
struct Texture *tex = texture_pullfromfile(path); YughWarn("Need to implement texture sync.");
glBindTexture(GL_TEXTURE_2D, tex->id);
if (tex->opts.mips)
glGenerateMipmap(GL_TEXTURE_2D);
if (tex->opts.sprite) {
if (tex->opts.mips) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
} else {
if (tex->opts.mips) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
} }
char *tex_get_path(struct Texture *tex) { char *tex_get_path(struct Texture *tex) {
@ -148,7 +101,7 @@ char *tex_get_path(struct Texture *tex) {
struct Texture *texture_loadfromfile(const char *path) struct Texture *texture_loadfromfile(const char *path)
{ {
struct Texture *new = texture_pullfromfile(path); struct Texture *new = texture_pullfromfile(path);
/*
if (new->id == 0) { if (new->id == 0) {
glGenTextures(1, &new->id); glGenTextures(1, &new->id);
@ -156,7 +109,7 @@ struct Texture *texture_loadfromfile(const char *path)
YughInfo("Loaded texture path %s", path); YughInfo("Loaded texture path %s", path);
} }
*/
return new; return new;
} }
@ -230,10 +183,12 @@ void texanim_fromframes(struct TexAnim *anim, int frames)
void tex_gpu_free(struct Texture *tex) void tex_gpu_free(struct Texture *tex)
{ {
/*
if (tex->id != 0) { if (tex->id != 0) {
glDeleteTextures(1, &tex->id); glDeleteTextures(1, &tex->id);
tex->id = 0; tex->id = 0;
} }
*/
} }
int anim_frames(struct TexAnim *a) int anim_frames(struct TexAnim *a)
@ -257,9 +212,10 @@ cpVect tex_get_dimensions(struct Texture *tex)
void tex_bind(struct Texture *tex) void tex_bind(struct Texture *tex)
{ {
glActiveTexture(GL_TEXTURE0); /* glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id); glBindTexture(GL_TEXTURE_2D, tex->id);
glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id); glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id);
*/
} }
/********************** ANIM2D ****************/ /********************** ANIM2D ****************/

View file

@ -3,6 +3,8 @@
#include "timer.h" #include "timer.h"
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
#include "sokol/sokol_gfx.h"
#define TEX_SPEC 0 #define TEX_SPEC 0
#define TEX_NORM 1 #define TEX_NORM 1
@ -58,7 +60,7 @@ struct TextureOptions {
/* Represents an actual texture on the GPU */ /* Represents an actual texture on the GPU */
struct Texture { struct Texture {
unsigned int id; /* ID reference for the GPU memory location of the texture */ sg_image id; /* ID reference for the GPU memory location of the texture */
int width; int width;
int height; int height;
unsigned char *data; unsigned char *data;

View file

@ -79,6 +79,13 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
GLFWwindow *sharewin = mainwin == NULL ? NULL : mainwin->window; GLFWwindow *sharewin = mainwin == NULL ? NULL : mainwin->window;
if (sharewin) return sharewin;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
struct window w = { struct window w = {
.width = width, .width = width,
.height = height, .height = height,
@ -92,12 +99,12 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
} }
glfwMakeContextCurrent(w.window); glfwMakeContextCurrent(w.window);
int version = gladLoadGL(glfwGetProcAddress); // int version = gladLoadGL(glfwGetProcAddress);
if (!version) { // if (!version) {
YughError("Failed to initialize OpenGL context."); // YughError("Failed to initialize OpenGL context.");
exit(1); // exit(1);
} // }
YughInfo("Loaded OpenGL %d.%d", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); YughInfo("Loaded OpenGL %d.%d", 3,3);
glfwSwapInterval(1); glfwSwapInterval(1);
// Set callbacks // Set callbacks
@ -107,7 +114,7 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
glfwSetWindowFocusCallback(w.window, window_focus_callback); glfwSetWindowFocusCallback(w.window, window_focus_callback);
glfwSetKeyCallback(w.window, win_key_callback); glfwSetKeyCallback(w.window, win_key_callback);
nuke_init(&w); // nuke_init(&w);
arrput(windows, w); arrput(windows, w);

View file

@ -1,11 +1,5 @@
#include "yugine.h" #include "yugine.h"
#define SOKOL_IMPL
#define SOKOL_GLCORE33
#include "sokol/sokol_gfx.h"
#include "render.h"
#include "camera.h" #include "camera.h"
#include "window.h" #include "window.h"
@ -37,6 +31,13 @@
#include "string.h" #include "string.h"
#define SOKOL_TRACE_HOOKS
#define SOKOL_GFX_IMPL
#define SOKOL_GLCORE33
#include "sokol/sokol_gfx.h"
int physOn = 0; int physOn = 0;
double renderlag = 0; double renderlag = 0;
@ -63,18 +64,7 @@ int fps;
#define SIM_PLAY 1 #define SIM_PLAY 1
#define SIM_PAUSE 2 #define SIM_PAUSE 2
#define SIM_STEP 3 #define SIM_STEP 3
/*
int __builtin_clz(unsigned int a)
{
int to;
__asm__ __volatile__(
"bsr %edi, %eax\n\t"
"xor $31, %eax\n\t"
: "=&a"(to));
return to;
}
*/
void print_stacktrace() void print_stacktrace()
{ {
void *ents[512]; void *ents[512];
@ -117,6 +107,11 @@ void compile_script(const char *file)
fclose(f); fclose(f);
} }
void sg_logging(const char *tag, uint32_t lvl, uint32_t id, const char *msg, uint32_t line, const char *file, void *data)
{
mYughLog(0, 1, line, file, "tag: %s, msg: %s", tag, msg);
}
int main(int argc, char **args) { int main(int argc, char **args) {
int logout = 1; int logout = 1;
ed = 1; ed = 1;
@ -203,6 +198,13 @@ int main(int argc, char **args) {
renderMS = 1.0/vidmode->refreshRate; renderMS = 1.0/vidmode->refreshRate;
sg_setup(&(sg_desc){
.logger = {
.func = sg_logging,
.user_data = NULL,
},
});
input_init(); input_init();
openglInit(); openglInit();

View file

@ -1731,7 +1731,7 @@ var camera2d = gameobject.clone("camera2d", {
}); });
win_make(Game.title, Game.resolution[0], Game.resolution[1]); win_make(Game.title, Game.resolution[0], Game.resolution[1]);
win_icon("icon.png"); //win_icon("icon.png");
/* Default objects */ /* Default objects */
gameobject.clone("polygon2d", { gameobject.clone("polygon2d", {

View file

@ -2,18 +2,11 @@
layout (location = 0) in vec4 vertex; layout (location = 0) in vec4 vertex;
out vec2 coords; out vec2 coords;
layout (std140) uniform Projection uniform mat4 proj;
{ uniform vec2 res;
mat4 projection;
};
layout (std140) uniform Resolution
{
vec2 resolution;
};
void main() void main()
{ {
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); gl_Position = proj * vec4(vertex.xy, 0.0, 1.0);
coords = vertex.zw;; coords = vertex.zw;
} }

View file

@ -1,7 +1,6 @@
#version 330 core #version 330 core
in vec2 texcoords; in vec2 texcoords;
out vec4 color; out vec4 color;
uniform sampler2D image; uniform sampler2D image;
uniform vec3 spriteColor; uniform vec3 spriteColor;

View file

@ -2,15 +2,10 @@
layout (location = 0) in vec4 vertex; layout (location = 0) in vec4 vertex;
out vec2 texcoords; out vec2 texcoords;
layout (std140) uniform Projection uniform mat4 mpv;
{
mat4 projection;
};
uniform mat4 model;
void main() void main()
{ {
texcoords = vertex.zw; texcoords = vertex.zw;
gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); gl_Position = mpv * vec4(vertex.xy, 0.0, 1.0);
} }

View file

@ -2,10 +2,7 @@
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex> layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords; out vec2 TexCoords;
layout (std140) uniform Projection uniform mat4 projection;
{
mat4 projection;
};
void main() void main()
{ {