From 50e8606b59d9f55042a984ecfcf01c5663399635 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 19 Dec 2023 21:42:52 +0100 Subject: [PATCH] Initial texture loading setup. --- compile_linux.sh | 3 +- game_scene.cpp | 52 ++++--- game_scene.h | 11 +- icon.png | Bin 0 -> 12259 bytes sfw/application/texture.cpp | 225 +++++++++++++++++++++++++---- sfw/application/texture.h | 45 ++++-- sfw/application/texture_material.h | 4 +- 7 files changed, 265 insertions(+), 75 deletions(-) create mode 100644 icon.png diff --git a/compile_linux.sh b/compile_linux.sh index d9e3374..82ce770 100755 --- a/compile_linux.sh +++ b/compile_linux.sh @@ -52,6 +52,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/object_2d.cpp -o sfw/application/object_2d.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/sprite.cpp -o sfw/application/sprite.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/tile_map.cpp -o sfw/application/tile_map.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/texture.cpp -o sfw/application/texture.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c game_scene.cpp -o game_scene.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c main.cpp -o main.o @@ -65,7 +66,7 @@ ccache g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/aabb.o sfw/basis. sfw/application/application.o sfw/application/scene.o sfw/application/window.o \ sfw/application/shader.o sfw/application/material.o sfw/application/mesh.o \ sfw/application/camera.o sfw/application/mesh_instance.o sfw/application/object_2d.o \ - sfw/application/sprite.o sfw/application/tile_map.o \ + sfw/application/sprite.o sfw/application/tile_map.o sfw/application/texture.o \ game_scene.o main.o \ -o game diff --git a/game_scene.cpp b/game_scene.cpp index d1f5c2b..9151679 100644 --- a/game_scene.cpp +++ b/game_scene.cpp @@ -5,11 +5,6 @@ #include "3rd_glad.h" #include "memory.h" - - -//#include "camera.h" -//#include "sprite.h" - void GameScene::event() { /* switch (ev.type) { @@ -109,8 +104,10 @@ void GameScene::render() { //sprite->render(); - material->bind(); - mesh->render(); + //material->bind(); + //mesh->render(); + + sprite->render(); } GameScene::GameScene() { @@ -119,16 +116,17 @@ GameScene::GameScene() { right = false; up = false; down = false; + */ - int w; - int h; - SDL_GetWindowSize(Application::get_singleton()->window, &w, &h); + //int w; + //int h; + //SDL_GetWindowSize(Application::get_singleton()->window, &w, &h); - float ar = static_cast(w) / static_cast(h); - camera->width = camera->height * ar; + //float ar = static_cast(w) / static_cast(h); + //camera->width = camera->height * ar; texture = new Texture(); - texture->load_image("download.bmp"); + texture->load_image("icon.png"); //ha a textúrának van alpha csatornája: //texture->load_image("download.bmp", GL_RGBA, GL_RGBA); @@ -137,14 +135,14 @@ GameScene::GameScene() { sprite = new Sprite(); sprite->mesh_instance->material = material; - sprite->position.x = 8; - sprite->position.y = 8; - sprite->region_x = 7.0 * (1.0 / 16.0); - sprite->region_y = 7.0 * (1.0 / 16.0); - sprite->region_width = 1.0 / 16.0; - sprite->region_height = 1.0 / 16.0; + sprite->position.x = 0; + sprite->position.y = 0; + //sprite->region_x = 7.0 * (1.0 / 16.0); + //sprite->region_y = 7.0 * (1.0 / 16.0); + //sprite->region_width = 1.0 / 16.0; + //sprite->region_height = 1.0 / 16.0; sprite->update_mesh(); - + /* tile_map = new TileMap(); tile_map->material = material; tile_map->atlas_size_x = 16; @@ -173,8 +171,8 @@ GameScene::GameScene() { //camera->position.z = -2; mesh = memnew(Mesh(2)); - material = memnew(ColoredMaterial()); - material->color = glm::vec4(1, 1, 0, 1); + cmaterial = memnew(ColoredMaterial()); + cmaterial->color = glm::vec4(1, 1, 0, 1); mesh->clear(); @@ -201,7 +199,6 @@ GameScene::GameScene() { //mesh->add_uv(region_x + region_width, region_y); //mesh->add_vertex2(w2, h2); - mesh->add_vertex2(0, 0.5); mesh->add_vertex2(-0.5, -0.5); mesh->add_vertex2(0.5, -0.5); @@ -214,12 +211,13 @@ GameScene::GameScene() { GameScene::~GameScene() { /* - delete camera; - delete texture; - delete material; delete tile_map; - delete sprite; */ + memdelete(camera); + memdelete(texture); + memdelete(material); + + memdelete(sprite); memdelete(camera); memdelete(mesh); diff --git a/game_scene.h b/game_scene.h index 2bb9ae0..7daa8d0 100644 --- a/game_scene.h +++ b/game_scene.h @@ -15,6 +15,11 @@ #include "colored_material.h" #include "mesh.h" #include "camera.h" +#include "texture.h" +#include "texture_material.h" +#include "camera.h" +#include "sprite.h" + class GameScene : public Scene { public: @@ -29,16 +34,16 @@ public: bool right; bool up; bool down; + */ Texture *texture; TextureMaterial *material; - TileMap *tile_map; + //TileMap *tile_map; Sprite *sprite; - */ Camera *camera; Mesh *mesh; - ColoredMaterial *material; + ColoredMaterial *cmaterial; }; #endif \ No newline at end of file diff --git a/icon.png b/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a263d7572781d43daccb5cf0171afde14093cf83 GIT binary patch literal 12259 zcmZ{KWmFwauL#9Ah-pGaB#N-cXti$?|k>Ze{a3D zX4cH~R9Dwj_3W$S(4F?f?K= z``-y-=)W3|pYqV=z@|~^Fuaq+=Udo?) z5-V>)!eMk2twmzaTcIx9l>Y#&mMy$!k+)6}BU3+kV!fBUIR5nDZgU%uXX%}nyDbVx zV!{3Yk1fK#!XQbc{pXXDEeyRjOtimLNsZHa2Ln9HXS(d1(AFE zJ2}*d3#WYxf>5YXNfzeM*i;_rJZ7&Xwd?3!5TZp5e7?dJs>+^V8zi%|gs; ze89N=URo4wCvKFS74c$v59tE=<_01l3lZ#^X{=x!eLzaz*;*hqSiAmFSwaIhChZ}v zO04box9isk%hzZgFyqTYn(*JT14n|k6@!LNDVH zM~R=5SvH~a2l526&I2A7ut1SW*UZB#jNh3^>Q#*W)yae>?WWGbCT23nMvzMJH(zuO zppbKNuLEAfdQ#tvK-^X)lk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siKY* zP`=KmLdZC69ZwP^3lZu%&49WL#DE_6#VmX|Kzh{F%4_h(^pB`rKq8dER%uu1_c6W+ zn1DC^kH-fmQs~yOY{}%ZdlV6&rsYHks2_PGP*GH;5?O=Q;if%>&N$7D&40y88e1u- zeeuz=vVz^m`_Y=&szir856l#01|ltVjBLNF&^9;h8oN44m_$0k!_T5Nzt)4;wE6$H zt&hK?X-Q>zS}UYP7d@UHHGr$@swS>32^+>PF9<=RP^5n-!@s%7KhDJg3_(BUEO*6N zhCRsZ7MvhFkmf(zA5Vn`8-Z_&Ib^2tXOg(wuvMr@=l^C^RPf8`=3r5|V-X(4atRf= zvsEFS{>z<Dj)l#YM0QoUxKe^G3-jg6fYbvF3GDpF{Jlnth1 zis|;T?^<100C!Xr(vR(v_{;6XYH^)1OlcK5Iy&vMgC+gB8jJ^SVYgdY;)4S=jP`ctsvlKS-A&~ z{Y>m*6p+~Y7G(w024`dv{ehU$eZaAhw}!)AXinscPC`?^vsa;~1K~Y3FnCbOcX?A2 zB++_+z{&@fXku8;rnXN`>Gbqc^b!0BM{pM8nO{k0XZ4Zz0yQ_}fuCrz97WC9*|`B( zwApzePVSS0xKmc=N|Nug$_?-08O=hpsAx`fV*uCy0JUAC>NwlyZnd%UdyNhJjsG3( zao-}wpkQpis1k$`?%^waA$y~_8w(Jh*A}#RGJydwz4@j3!<~RGJ|qvm9B@l`jN|)u z@;Tk50|R5;f{$>|K3Jv=u4602aXUd}__0B91#unyme38bVUs8mUrF`7{|M`<;@LP! nV{DrP3`ilA|DR(D^+lZ8YsE0@DWxAu&<7M`RHdswn1=ix_DE9( literal 0 HcmV?d00001 diff --git a/sfw/application/texture.cpp b/sfw/application/texture.cpp index 697f44e..e414a50 100644 --- a/sfw/application/texture.cpp +++ b/sfw/application/texture.cpp @@ -1,51 +1,216 @@ #include "texture.h" +#include "memory.h" #include -void Texture::load_image(const char * file_name, const int format, const int internal_components) { - if (image) { - SDL_FreeSurface(image); - image = NULL; - glDeleteTextures(1, &texture); +#define STB_IMAGE_IMPLEMENTATION // stbi +#define STB_IMAGE_WRITE_IMPLEMENTATION // stbi_write +#define STB_SPRINTF_IMPLEMENTATION // stb_sprintf +#define STB_SPRINTF_NOUNALIGNED // stb_sprintf + +#include "3rd_stb_image.h" + +//{{FILE:3rd_stb_image_write.h}} +//--- +#undef freelist +#define STBTT_malloc(x, u) ((void)(u), MALLOC(x)) +#define STBTT_free(x, u) ((void)(u), FREE(x)) +#define NK_ASSERT ASSERT +#define NK_DTOA(s, n) strcpy(s, va("%f", n)) // override cos built-in nk_dtoa() will freeze while parsing UINT_MAX otherwise + +void Texture::image_data_load(const char *file_name, int flags) { + //stbi_set_flip_vertically_on_load(flags & IMAGE_FLIP ? 1 : 0); + + int img_n = 0; + //if (flags & IMAGE_R) + // n = 1; + //if (flags & IMAGE_RG) + // n = 2; + //if (flags & IMAGE_RGB) + // n = 3; + //if (flags & IMAGE_RGBA) + img_n = 4; + //if (flags & IMAGE_FLOAT) + // img.pixels = stbi_loadf_from_file((const stbi_uc *)data, size, (int *)&img.x, (int *)&img.y, (int *)&img.n, n); + //else + + FILE *fp = fopen(file_name, "r"); + + pixels = stbi_load_from_file(fp, &x, &y, &n, img_n); + + fclose(fp); + + //if (img.pixels) { + // PRINTF("Loaded image (%dx%d %.*s->%.*s)\n", img.w, img.h, img.n, "RGBA", n ? n : img.n, "RGBA"); + //} else { + // PANIC("Error loading image (%s)\n", pathfile); + //} + + n = img_n ? img_n : n; +} + +void Texture::load_image(const char *file_name, const int format, const int internal_components) { + if (pixels) { + //TODO + //memdelete(pixels); + pixels = NULL; + glDeleteTextures(1, &texture); + } + + image_data_load(file_name, 0); + + if (!pixels) { + printf("Couldn't load %s.\n", file_name); + } else { + //image = SDL_ConvertSurfaceFormat(img, SDL_PIXELFORMAT_RGBA32, 0); + //SDL_FreeSurface(img); + + glGenTextures(1, &texture); + + texture_update(0); + + //glBindTexture(GL_TEXTURE_2D, texture); + //glTexImage2D(GL_TEXTURE_2D, 0, format, image->w, image->h, 0, internal_components, GL_UNSIGNED_BYTE, image->pixels); + //apply_filter(); + } +} + +void Texture::texture_update(int flags) { + if (!pixels) { + return; + } + + //if (t && !t->id) { + // glGenTextures(1, &t->id); + // return texture_update(t, w, h, n, pixels, flags); + //} + + //ASSERT(t && t->id); + //ASSERT(n <= 4); + + //GLuint pixel_types[] = { GL_RED, GL_RED, GL_RG, GL_RGB, GL_RGBA, GL_R32F, GL_R32F, GL_RG32F, GL_RGB32F, GL_RGBA32F }; + //GLenum pixel_storage = flags & TEXTURE_FLOAT ? GL_FLOAT : GL_UNSIGNED_BYTE; + GLenum pixel_storage = GL_UNSIGNED_BYTE; + //GLuint pixel_type = pixel_types[n]; + GLuint pixel_type = GL_RGBA; + //GLuint texel_type = t->texel_type = pixel_types[n + 5 * !!(flags & TEXTURE_FLOAT)]; + GLuint texel_type = GL_RGBA; + GLenum wrap = GL_CLAMP_TO_EDGE; + GLenum min_filter = GL_NEAREST, mag_filter = GL_NEAREST; + // GLfloat color = (flags&7)/7.f, border_color[4] = { color, color, color, 1.f }; + + /* + if (flags & TEXTURE_BGR) + if (pixel_type == GL_RGB) + pixel_type = GL_BGR; + if (flags & TEXTURE_BGR) + if (pixel_type == GL_RGBA) + pixel_type = GL_BGRA; + if (flags & TEXTURE_SRGB) + if (texel_type == GL_RGB) + texel_type = GL_SRGB; + if (flags & TEXTURE_SRGB) + if (texel_type == GL_RGBA) + texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? + */ + /* + if (flags & TEXTURE_BC1) + texel_type = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; + if (flags & TEXTURE_BC2) + texel_type = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; + if (flags & TEXTURE_BC3) + texel_type = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + if (flags & TEXTURE_DEPTH) + texel_type = pixel_type = GL_DEPTH_COMPONENT; // GL_DEPTH_COMPONENT32 + + if (flags & TEXTURE_REPEAT) + wrap = GL_REPEAT; + if (flags & TEXTURE_BORDER) + wrap = GL_CLAMP_TO_BORDER; + if (flags & TEXTURE_LINEAR) + min_filter = GL_LINEAR, mag_filter = GL_LINEAR; + if (flags & TEXTURE_MIPMAPS) + min_filter = flags & TEXTURE_LINEAR ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_LINEAR; // : GL_LINEAR_MIPMAP_NEAREST; maybe? + if (flags & TEXTURE_MIPMAPS) + mag_filter = flags & TEXTURE_LINEAR ? GL_LINEAR : GL_NEAREST; + */ + +#if 0 + if( 0 ) { // flags & TEXTURE_PREMULTIPLY_ALPHA ) + uint8_t *p = pixels; + if(n == 2) for( unsigned i = 0; i < 2*w*h; i += 2 ) { + p[i] = (p[i] * p[i+1] + 128) >> 8; + } + if(n == 4) for( unsigned i = 0; i < 4*w*h; i += 4 ) { + p[i+0] = (p[i+0] * p[i+3] + 128) >> 8; + p[i+1] = (p[i+1] * p[i+3] + 128) >> 8; + p[i+2] = (p[i+2] * p[i+3] + 128) >> 8; + } } +#endif - SDL_Surface *img = SDL_LoadBMP(file_name); + //GLenum texture_type = t->flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // @fixme: test GL_TEXTURE_2D_ARRAY + GLenum texture_type = GL_TEXTURE_2D; // @fixme: test GL_TEXTURE_2D_ARRAY - if (!img) { - printf("Couldn't load %s.\n", file_name); - } else { - image = SDL_ConvertSurfaceFormat(img, SDL_PIXELFORMAT_RGBA32, 0); - SDL_FreeSurface(img); + //glPixelStorei( GL_UNPACK_ALIGNMENT, n < 4 ? 1 : 4 ); // for framebuffer reading + //glActiveTexture(GL_TEXTURE0 + (flags&7)); + glBindTexture(texture_type, texture); + glTexImage2D(texture_type, 0, texel_type, w, h, 0, pixel_type, pixel_storage, pixels); + glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, wrap); + glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, wrap); + glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, min_filter); + glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, mag_filter); +#if 0 // only for sampler2DShadow + if( flags & TEXTURE_DEPTH ) glTexParameteri(texture_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + if( flags & TEXTURE_DEPTH ) glTexParameteri(texture_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); +#endif + // if( flags & TEXTURE_BORDER ) glTexParameterfv(texture_type, GL_TEXTURE_BORDER_COLOR, border_color); + /* + if (flags & TEXTURE_MIPMAPS) + glGenerateMipmap(texture_type); - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, format, image->w, image->h, 0, internal_components, GL_UNSIGNED_BYTE, image->pixels); - apply_filter(); - } + if (flags & TEXTURE_MIPMAPS) { + GLfloat max_aniso = 0; + // glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &max_aniso); + max_aniso = 4; + // glTexParameterf(texture_type, GL_TEXTURE_MAX_ANISOTROPY, max_aniso); + } + + */ + + // glBindTexture(texture_type, 0); // do not unbind. current code expects texture to be bound at function exit + /* + t->w = w; + t->h = h; + t->n = n; + t->flags = flags; + t->filename = t->filename ? t->filename : ""; + */ } void Texture::apply_filter() { - GLint params = GL_NEAREST; + GLint params = GL_NEAREST; - if (filter == TEXTURE_FILTER_LINEAR) { - params = GL_LINEAR; - } + if (filter == TEXTURE_FILTER_LINEAR) { + params = GL_LINEAR; + } - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, params); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, params); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, params); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, params); } Texture::Texture() { - filter = TEXTURE_FILTER_NEAREST; - texture = 0; - image = NULL; + filter = TEXTURE_FILTER_NEAREST; + texture = 0; + pixels = NULL; } Texture::~Texture() { - if (image) { - SDL_FreeSurface(image); + if (pixels) { + //todo + //delete (pixels); - glDeleteTextures(1, &texture); - } + glDeleteTextures(1, &texture); + } } diff --git a/sfw/application/texture.h b/sfw/application/texture.h index 594f319..899446f 100644 --- a/sfw/application/texture.h +++ b/sfw/application/texture.h @@ -1,24 +1,45 @@ #ifndef TEXTURE_H #define TEXTURE_H -#include +#include "3rd_glad.h" class Texture { public: - enum TextureFilter { - TEXTURE_FILTER_NEAREST = 0, - TEXTURE_FILTER_LINEAR, - }; + enum TextureFilter { + TEXTURE_FILTER_NEAREST = 0, + TEXTURE_FILTER_LINEAR, + }; - void load_image(const char * file_name, const int format = GL_RGBA, const int internal_components = GL_RGBA); - void apply_filter(); + void load_image(const char *file_name, const int format = GL_RGBA, const int internal_components = GL_RGBA); + void apply_filter(); - TextureFilter filter; - GLuint texture; - SDL_Surface *image; + TextureFilter filter; + GLuint texture; - Texture(); - virtual ~Texture(); + // from fwk, temporary + union { + int x, w; + }; + union { + int y, h; + }; + union { + int n, comps; + }; + union { + void *pixels; + uint8_t *pixels8; + uint16_t *pixels16; + uint32_t *pixels32; + float *pixelsf; + }; + + // From FWK + void image_data_load(const char *file_name, int flags); + void texture_update(int flags); + + Texture(); + virtual ~Texture(); }; #endif // TEXTURE_H diff --git a/sfw/application/texture_material.h b/sfw/application/texture_material.h index 304909a..29d6c9d 100644 --- a/sfw/application/texture_material.h +++ b/sfw/application/texture_material.h @@ -4,8 +4,8 @@ #include "material.h" #include "texture.h" -#include "./libs/glm/vec4.hpp" -#include "./libs/glm/gtc/type_ptr.hpp" +#include "../../libs/glm/vec4.hpp" +#include "../../libs/glm/gtc/type_ptr.hpp" #include "camera.h"