Grid rendering

This commit is contained in:
John Alanbrook 2023-02-20 22:28:07 +00:00
parent 49a17e0d59
commit d1c87b38ac
5 changed files with 34 additions and 22 deletions

View file

@ -204,8 +204,19 @@ void phys2d_circledel(struct phys2d_circle *c)
cpVect world2go(struct gameobject *go, cpVect worldpos) cpVect world2go(struct gameobject *go, cpVect worldpos)
{ {
worldpos = cpvsub(worldpos, cpBodyGetPosition(go->body)); cpTransform T = {0};
worldpos = cpvmult(worldpos, 1/go->scale); 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; return worldpos;
} }
@ -215,29 +226,14 @@ cpVect go2world(struct gameobject *go, cpVect gopos)
float angle = cpBodyGetAngle(go->body); float angle = cpBodyGetAngle(go->body);
cpTransform T = {0}; cpTransform T = {0};
T.a = go->scale * go->flipx * cos(angle); T.a = go->scale * go->flipx * cos(angle);
T.b = -sin(angle) * go->scale; T.b = sin(angle) * go->scale * go->flipx;
T.c = sin(angle) * go->scale; T.c = -sin(angle) * go->scale * go->flipy;
T.d = go->scale * go->flipy * cos(angle); T.d = go->scale * go->flipy * cos(angle);
T.tx = pos.x; T.tx = pos.x;
T.ty = pos.y; T.ty = pos.y;
return cpTransformPoint(T, gopos); 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) cpVect gotransformpoint(struct gameobject *go, cpVect point)
{ {
point.x *= go->scale * go->flipx; point.x *= go->scale * go->flipx;

View file

@ -241,8 +241,10 @@ void draw_grid(int width, int span)
shader_setint(gridShader, "span", span); shader_setint(gridShader, "span", span);
cpVect offset = cam_pos(); cpVect offset = cam_pos();
offset = cpvmult(offset, 1/cam_zoom());
offset.x -= mainwin->width/2; offset.x -= mainwin->width/2;
offset.y -= mainwin->height/2; offset.y -= mainwin->height/2;
shader_setvec2(gridShader, "offset", &offset); shader_setvec2(gridShader, "offset", &offset);
glBindVertexArray(gridVAO); glBindVertexArray(gridVAO);
@ -251,7 +253,18 @@ void draw_grid(int width, int span)
void draw_point(int x, int y, float r, float *color) 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) void draw_cppoint(struct cpVect point, float r)

View file

@ -118,8 +118,10 @@ void set_cam_body(cpBody *body) {
cpVect cam_pos() { cpVect cam_pos() {
return camera ? cpBodyGetPosition(camera) : cpvzero; return camera ? cpBodyGetPosition(camera) : cpvzero;
} }
static float zoom = 1.f; static float zoom = 1.f;
float cam_zoom() { return zoom; }
void add_zoom(float val) { zoom = val; } void add_zoom(float val) { zoom = val; }
void openglRender(struct window *window) void openglRender(struct window *window)

View file

@ -51,6 +51,7 @@ 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();
void add_zoom(float val); void add_zoom(float val);
#endif #endif

View file

@ -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_translate_vec2(model, t_offset);
mat4_scale_vec2(model, t_scale); mat4_scale_vec2(model, t_scale);
mat4_rotation_z(r_model, angle); mat4_rotation_z(r_model, angle);
mat4_multiply(model, r_model, model); mat4_multiply(model, r_model, model);