aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-12-08 01:02:56 +0100
committerdec05eba <dec05eba@protonmail.com>2021-12-08 01:03:15 +0100
commitb7e3507a82d93b470d89f5cdf838f480bd7e4ab4 (patch)
treeac689868c0b0e6caa8a13038e2dd7f9b02e2ef50
parent1739930d6f4b16eb5f3118412cddd20277ac5639 (diff)
Vertex translation, fix vertex texture use
-rw-r--r--include/mgl/graphics/vertex.h7
-rw-r--r--src/graphics/texture.c1
-rw-r--r--src/graphics/vertex.c9
-rw-r--r--tests/main.c2
4 files changed, 11 insertions, 8 deletions
diff --git a/include/mgl/graphics/vertex.h b/include/mgl/graphics/vertex.h
index 8cc2f25..2980f83 100644
--- a/include/mgl/graphics/vertex.h
+++ b/include/mgl/graphics/vertex.h
@@ -7,17 +7,18 @@
#include <stddef.h>
typedef struct mgl_context mgl_context;
+typedef struct mgl_vertex mgl_vertex;
-typedef struct {
+struct mgl_vertex {
mgl_vec2f position;
mgl_vec2f texcoords;
mgl_color color;
-} mgl_vertex;
+};
/*
Note: this sends the vertices to the gpu everytime this is called. This should only be used for small lists of vertices
or if every vertex needs to change every frame. Use mgl_vertex_buffer instead if possible.
*/
-void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type);
+void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type, mgl_vec2f position);
#endif /* MGL_VERTEX_H */
diff --git a/src/graphics/texture.c b/src/graphics/texture.c
index 9ea9b12..7d8fdcf 100644
--- a/src/graphics/texture.c
+++ b/src/graphics/texture.c
@@ -149,6 +149,7 @@ int mgl_texture_resize(mgl_texture *self, int new_width, int new_height, mgl_tex
return 0;
}
+/* TODO: Optimize. Do not set matrix if the current coordinate type was the same as the previous one */
void mgl_texture_use(const mgl_texture *texture) {
mgl_context *context = mgl_get_context();
diff --git a/src/graphics/vertex.c b/src/graphics/vertex.c
index 110c86d..8e9b5b9 100644
--- a/src/graphics/vertex.c
+++ b/src/graphics/vertex.c
@@ -1,16 +1,17 @@
#include "../../include/mgl/graphics/vertex.h"
#include "../../include/mgl/mgl.h"
-void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type) {
+void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type, mgl_vec2f position) {
if(vertex_count == 0)
return;
- context->gl.glMatrixMode(GL_TEXTURE);
- context->gl.glLoadIdentity();
- context->gl.glMatrixMode(GL_MODELVIEW);
+ context->gl.glPushMatrix();
+ context->gl.glTranslatef(position.x, position.y, 0.0f);
context->gl.glVertexPointer(2, GL_FLOAT, sizeof(mgl_vertex), (void*)&vertices[0].position);
context->gl.glTexCoordPointer(2, GL_FLOAT, sizeof(mgl_vertex), (void*)&vertices[0].texcoords);
context->gl.glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(mgl_vertex), (void*)&vertices[0].color);
context->gl.glDrawArrays(mgl_primitive_type_to_gl_mode(primitive_type), 0, vertex_count);
+
+ context->gl.glPopMatrix();
}
diff --git a/tests/main.c b/tests/main.c
index 359d92b..df68dd5 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -115,7 +115,7 @@ static void draw(mgl_window *window, void *userdata) {
}
};
- mgl_vertices_draw(context, vertices, 4, MGL_PRIMITIVE_QUADS);
+ mgl_vertices_draw(context, vertices, 4, MGL_PRIMITIVE_QUADS, (mgl_vec2f){ 0.0f, 0.0f });
mgl_window_set_view(window, &prev_view);
mgl_rectangle rect = {