From d1c87b38ac982e6197193e3999da18d4c9891ad9 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 20 Feb 2023 22:28:07 +0000 Subject: [PATCH] Grid rendering --- source/engine/2dphysics.c | 34 +++++++++++++++------------------ source/engine/debug/debugdraw.c | 15 ++++++++++++++- source/engine/openglrender.c | 4 +++- source/engine/openglrender.h | 1 + source/engine/sprite.c | 2 +- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 0b26df0..fcf4054 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -204,8 +204,19 @@ void phys2d_circledel(struct phys2d_circle *c) cpVect world2go(struct gameobject *go, cpVect worldpos) { - worldpos = cpvsub(worldpos, cpBodyGetPosition(go->body)); - worldpos = cpvmult(worldpos, 1/go->scale); + cpTransform T = {0}; + cpVect pos = cpBodyGetPosition(go->body); + worldpos.x -= pos.x; + worldpos.y -= pos.y; +// worldpos.x /= go->scale; +// worldpos.y /= go->scale; + float angle = cpBodyGetAngle(go->body); + T.a = go->flipx * cos(-angle) / go->scale; + T.b = sin(-angle) / go->scale; + T.c = -sin(-angle) / go->scale; + T.d = go->flipy * cos(-angle) / go->scale; + worldpos = cpTransformPoint(T,worldpos); + return worldpos; } @@ -215,29 +226,14 @@ cpVect go2world(struct gameobject *go, cpVect gopos) float angle = cpBodyGetAngle(go->body); cpTransform T = {0}; T.a = go->scale * go->flipx * cos(angle); - T.b = -sin(angle) * go->scale; - T.c = sin(angle) * go->scale; + T.b = sin(angle) * go->scale * go->flipx; + T.c = -sin(angle) * go->scale * go->flipy; T.d = go->scale * go->flipy * cos(angle); T.tx = pos.x; T.ty = pos.y; return cpTransformPoint(T, gopos); } -cpTransform body2transform(cpBody *body) -{ - cpTransform T = {0}; - cpVect pos = cpBodyGetPosition(body); - float angle = cpBodyGetAngle(body); - T.a = cos(angle); - T.b = -sin(angle); - T.c = sin(angle); - T.d = cos(angle); - T.tx = pos.x; - T.ty = pos.y; - - return T; -} - cpVect gotransformpoint(struct gameobject *go, cpVect point) { point.x *= go->scale * go->flipx; diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 6d40d02..c06648e 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -241,8 +241,10 @@ void draw_grid(int width, int span) shader_setint(gridShader, "span", span); cpVect offset = cam_pos(); + offset = cpvmult(offset, 1/cam_zoom()); offset.x -= mainwin->width/2; offset.y -= mainwin->height/2; + shader_setvec2(gridShader, "offset", &offset); glBindVertexArray(gridVAO); @@ -251,7 +253,18 @@ void draw_grid(int width, int span) void draw_point(int x, int y, float r, float *color) { - draw_circle(x, y, r, r, color, 0); + shader_use(circleShader); + + float verts[] = { x, y }; + + glBindBuffer(GL_ARRAY_BUFFER, circleVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); + glPointSize(r); + + glBindVertexArray(circleVAO); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); + glDrawArrays(GL_POINTS, 0, 4); } void draw_cppoint(struct cpVect point, float r) diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 1a52f44..53f473e 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -118,8 +118,10 @@ void set_cam_body(cpBody *body) { cpVect cam_pos() { return camera ? cpBodyGetPosition(camera) : cpvzero; } - static float zoom = 1.f; +float cam_zoom() { return zoom; } + + void add_zoom(float val) { zoom = val; } void openglRender(struct window *window) diff --git a/source/engine/openglrender.h b/source/engine/openglrender.h index 151bafb..f52c70e 100644 --- a/source/engine/openglrender.h +++ b/source/engine/openglrender.h @@ -51,6 +51,7 @@ void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind void set_cam_body(cpBody *body); cpVect cam_pos(); +float cam_zoom(); void add_zoom(float val); #endif diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 4c9762e..f313329 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -137,7 +137,7 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo mat4_translate_vec2(model, t_offset); -mat4_scale_vec2(model, t_scale); + mat4_scale_vec2(model, t_scale); mat4_rotation_z(r_model, angle); mat4_multiply(model, r_model, model);