From 01bfee71fcb7134cf78aacc83e471717634b6b65 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Tue, 15 Apr 2025 09:21:13 +0900 Subject: [PATCH] load test texture --- engine/gf_draw_common.c | 22 ++++++++++++-- engine/gf_graphic_common.c | 2 ++ engine/gf_texture.c | 4 +-- engine/graphic/opengl/gf_driver.c | 17 ++++++----- engine/graphic/opengl/gf_graphic.c | 47 +++++++++++++++++------------- engine/include/gf_graphic.h | 3 +- engine/include/gf_texture.h | 4 +-- engine/include/gf_type/draw.h | 1 + 8 files changed, 65 insertions(+), 35 deletions(-) diff --git a/engine/gf_draw_common.c b/engine/gf_draw_common.c index 3513c35..35a0d94 100644 --- a/engine/gf_draw_common.c +++ b/engine/gf_draw_common.c @@ -3,6 +3,7 @@ #include /* External library */ +#include /* Interface */ #include @@ -12,6 +13,7 @@ #include #include #include +#include #include #include @@ -24,6 +26,8 @@ void gf_draw_begin(void) { gf_draw_platform_begin(); } void gf_draw_end(void) { gf_draw_platform_end(); } +gf_texture_t* test_texture; + gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) { gf_draw_t* draw = malloc(sizeof(*draw)); memset(draw, 0, sizeof(*draw)); @@ -32,6 +36,7 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) { draw->width = 640; draw->height = 480; draw->running = 0; + draw->draw_3d = 0; strcpy(draw->title, title); gf_draw_platform_create(draw); if(draw->platform != NULL) { @@ -40,20 +45,26 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) { gf_draw_reshape(draw); draw->running = 1; - draw->light[0] = 10.0; + draw->light[0] = 0.0; draw->light[1] = 10.0; draw->light[2] = 0.0; draw->light[3] = 1.0; draw->camera[0] = 0; - draw->camera[1] = 10; - draw->camera[2] = 0; + draw->camera[1] = 2; + draw->camera[2] = 2; draw->lookat[0] = 0; draw->lookat[1] = 0; draw->lookat[2] = 0; draw->gui = gf_gui_create(draw); + if(1) { + int w, h, c; + unsigned char* d = stbi_load("texture/test.bmp", &w, &h, &c, 4); + test_texture = gf_texture_register(draw, w, h, d); + free(d); + } } else { free(draw); draw = NULL; @@ -65,6 +76,11 @@ void gf_draw_reshape(gf_draw_t* draw) { gf_draw_driver_reshape(draw); } /* Runs every frame */ void gf_draw_frame(gf_draw_t* draw) { + gf_color_t color; + color.r = color.g = color.b = color.a = 255; + if(draw->draw_3d) { + } + gf_graphic_draw_texture_polygon(draw, test_texture, color, 3, 4, 0.0, 0.0, -1.0, 0.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, -1.0); if(draw->draw != NULL) draw->draw(draw); } diff --git a/engine/gf_graphic_common.c b/engine/gf_graphic_common.c index 40a1f26..c9b7613 100644 --- a/engine/gf_graphic_common.c +++ b/engine/gf_graphic_common.c @@ -24,3 +24,5 @@ void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* } float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text) { return (float)strlen(text) * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y); } + +void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_color_t color) { gf_graphic_draw_texture_polygon(draw, texture, color, 2, 4, 0.0, 0.0, x, y, 0.0, 1.0, x, y + h, 1.0, 1.0, x + w, y + h, 1.0, 0.0, x + w, y); } diff --git a/engine/gf_texture.c b/engine/gf_texture.c index bb8f80e..8fe0798 100644 --- a/engine/gf_texture.c +++ b/engine/gf_texture.c @@ -14,7 +14,7 @@ /* Standard */ #include -gf_texture_t* gf_register_texture(gf_draw_t* draw, int width, int height, unsigned char* data) { +gf_texture_t* gf_texture_register(gf_draw_t* draw, int width, int height, unsigned char* data) { gf_texture_t* texture = malloc(sizeof(*texture)); gf_draw_driver_texture_t* ddtexture; texture->internal_width = width; @@ -30,7 +30,7 @@ gf_texture_t* gf_register_texture(gf_draw_t* draw, int width, int height, unsign return texture; } -void gf_destroy_texture(gf_texture_t* texture) { +void gf_texture_destroy(gf_texture_t* texture) { gf_draw_driver_destroy_texture(texture->draw_driver_texture); free(texture); } diff --git a/engine/graphic/opengl/gf_driver.c b/engine/graphic/opengl/gf_driver.c index 92ffebd..a7758ef 100644 --- a/engine/graphic/opengl/gf_driver.c +++ b/engine/graphic/opengl/gf_driver.c @@ -25,6 +25,7 @@ #include GLfloat lightwht[] = {1.0, 1.0, 1.0, 1.0}; +GLfloat lightgry[] = {0.6, 0.6, 0.6, 1.0}; GLfloat lightdim[] = {0.2, 0.2, 0.2, 1.0}; GLfloat lightblk[] = {0.0, 0.0, 0.0, 1.0}; @@ -79,17 +80,18 @@ void gf_draw_driver_init(gf_draw_t* draw) { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCullFace(GL_BACK); - glShadeModel(GL_FLAT); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); - glLightfv(GL_LIGHT0, GL_AMBIENT, lightdim); + glShadeModel(GL_SMOOTH); + glLightfv(GL_LIGHT0, GL_AMBIENT, lightgry); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightwht); - glLightfv(GL_LIGHT0, GL_SPECULAR, lightblk); + glLightfv(GL_LIGHT0, GL_SPECULAR, lightwht); for(i = 0; i < sizeof(gf_font) / sizeof(gf_font[0]); i++) { unsigned char* font = malloc(8 * 8 * 4); @@ -101,7 +103,7 @@ void gf_draw_driver_init(gf_draw_t* draw) { font[j * 4 + 2] = val; font[j * 4 + 3] = val; } - draw->font[i] = gf_register_texture(draw, 8, 8, font); + draw->font[i] = gf_texture_register(draw, 8, 8, font); free(font); } gf_log_function(NULL, "Registered %d glyphs", sizeof(gf_font) / sizeof(gf_font[0])); @@ -148,13 +150,14 @@ void gf_draw_driver_set_color(gf_draw_t* draw, gf_color_t color) { glColor4f(col void gf_draw_driver_destroy(gf_draw_t* draw) { int i; for(i = 0; i < sizeof(gf_font) / sizeof(gf_font[0]); i++) { - gf_destroy_texture(draw->font[i]); + gf_texture_destroy(draw->font[i]); } } void gf_draw_driver_before(gf_draw_t* draw) { - GLfloat lightpos[3]; + GLfloat lightpos[4]; GF_VECTOR_COPY(draw->light, lightpos); + lightpos[3] = draw->light[3]; gf_draw_driver_reshape(draw); diff --git a/engine/graphic/opengl/gf_graphic.c b/engine/graphic/opengl/gf_graphic.c index af60d9a..20533dc 100644 --- a/engine/graphic/opengl/gf_graphic.c +++ b/engine/graphic/opengl/gf_graphic.c @@ -39,34 +39,41 @@ void gf_graphic_end_2d(gf_draw_t* draw) { glEnable(GL_LIGHTING); } -void gf_graphic_clear(gf_draw_t* draw) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } +void gf_graphic_clear(gf_draw_t* draw) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } -void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_color_t color) { - double tw = (double)texture->width / texture->internal_width; - double th = (double)texture->height / texture->internal_height; +void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_color_t color, int dim, int npair, ...) { + double tw = (double)texture->width / texture->internal_width; + double th = (double)texture->height / texture->internal_height; + int i; + va_list va; + va_start(va, npair); - gf_graphic_begin_2d(draw); + if(dim == 2) gf_graphic_begin_2d(draw); + gf_draw_driver_begin_texture_2d(draw, texture); gf_draw_driver_set_color(draw, color); - gf_draw_driver_begin_texture_2d(draw, texture); - glBegin(GL_QUADS); + glBegin(GL_TRIANGLE_FAN); - glTexCoord2d(0, 0); - glVertex2f(x, y); - - glTexCoord2d(0, th); - glVertex2f(x, y + h); - - glTexCoord2d(tw, th); - glVertex2f(x + w, y + h); - - glTexCoord2d(tw, 0); - glVertex2f(x + w, y); + for(i = 0; i < npair; i++) { + float tx = va_arg(va, double) * tw; + float ty = va_arg(va, double) * th; + float x = va_arg(va, double); + float y = va_arg(va, double); + glTexCoord2f(tx, ty); + if(dim == 2) { + glVertex2f(x, y); + } else if(dim == 3) { + float z = va_arg(va, double); + glVertex3f(x, y, z); + } + } glEnd(); - gf_draw_driver_end_texture_2d(draw); - gf_graphic_end_2d(draw); + gf_draw_driver_end_texture_2d(draw); + if(dim == 2) gf_graphic_end_2d(draw); + + va_end(va); } void gf_graphic_fill_polygon(gf_draw_t* draw, gf_color_t color, int npair, ...) { diff --git a/engine/include/gf_graphic.h b/engine/include/gf_graphic.h index 3ed4b18..b240036 100644 --- a/engine/include/gf_graphic.h +++ b/engine/include/gf_graphic.h @@ -34,12 +34,13 @@ GF_EXPORT void gf_graphic_clear(gf_draw_t* draw); GF_EXPORT void gf_graphic_begin_2d(gf_draw_t* draw); GF_EXPORT void gf_graphic_end_2d(gf_draw_t* draw); -GF_EXPORT void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_color_t color); +GF_EXPORT void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_color_t color, int dim, int npair, ...); GF_EXPORT void gf_graphic_fill_polygon(gf_draw_t* draw, gf_color_t color, int npair, ...); /* Common */ GF_EXPORT float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text); GF_EXPORT void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_color_t color); GF_EXPORT void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_color_t color); +GF_EXPORT void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_color_t color); #endif diff --git a/engine/include/gf_texture.h b/engine/include/gf_texture.h index 33a7329..49265da 100644 --- a/engine/include/gf_texture.h +++ b/engine/include/gf_texture.h @@ -18,7 +18,7 @@ /* Standard */ -GF_EXPORT gf_texture_t* gf_register_texture(gf_draw_t* draw, int width, int height, unsigned char* data); -GF_EXPORT void gf_destroy_texture(gf_texture_t* texture); +GF_EXPORT gf_texture_t* gf_texture_register(gf_draw_t* draw, int width, int height, unsigned char* data); +GF_EXPORT void gf_texture_destroy(gf_texture_t* texture); #endif diff --git a/engine/include/gf_type/draw.h b/engine/include/gf_type/draw.h index b2aa6d2..a60f25b 100644 --- a/engine/include/gf_type/draw.h +++ b/engine/include/gf_type/draw.h @@ -30,6 +30,7 @@ GF_DECLARE_TYPE(draw, { int width; int height; int running; + int draw_3d; char title[128]; gf_texture_t* font[128]; gf_vector_t light;