load test texture

This commit is contained in:
NishiOwO 2025-04-15 09:21:13 +09:00
parent 07ee1076df
commit 01bfee71fc
No known key found for this signature in database
GPG Key ID: 27EF69B208EB9343
8 changed files with 65 additions and 35 deletions

View File

@ -3,6 +3,7 @@
#include <gf_pre.h>
/* External library */
#include <stb_image.h>
/* Interface */
#include <gf_draw.h>
@ -12,6 +13,7 @@
#include <gf_log.h>
#include <gf_draw_platform.h>
#include <gf_draw_driver.h>
#include <gf_texture.h>
#include <gf_graphic.h>
#include <gf_gui.h>
@ -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);
}

View File

@ -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); }

View File

@ -14,7 +14,7 @@
/* Standard */
#include <stdlib.h>
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);
}

View File

@ -25,6 +25,7 @@
#include <math.h>
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);

View File

@ -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, ...) {

View File

@ -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

View File

@ -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

View File

@ -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;