Initial texture loading setup.

This commit is contained in:
Relintai 2023-12-19 21:42:52 +01:00
parent 0da5c5e530
commit 50e8606b59
7 changed files with 265 additions and 75 deletions

View File

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

View File

@ -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<float>(w) / static_cast<float>(h);
camera->width = camera->height * ar;
//float ar = static_cast<float>(w) / static_cast<float>(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);

View File

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

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -1,29 +1,193 @@
#include "texture.h"
#include "memory.h"
#include <stdio.h>
#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 (image) {
SDL_FreeSurface(image);
image = NULL;
if (pixels) {
//TODO
//memdelete(pixels);
pixels = NULL;
glDeleteTextures(1, &texture);
}
SDL_Surface *img = SDL_LoadBMP(file_name);
image_data_load(file_name, 0);
if (!img) {
if (!pixels) {
printf("Couldn't load %s.\n", file_name);
} else {
image = SDL_ConvertSurfaceFormat(img, SDL_PIXELFORMAT_RGBA32, 0);
SDL_FreeSurface(img);
//image = SDL_ConvertSurfaceFormat(img, SDL_PIXELFORMAT_RGBA32, 0);
//SDL_FreeSurface(img);
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();
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
//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
//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);
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;
@ -39,12 +203,13 @@ void Texture::apply_filter() {
Texture::Texture() {
filter = TEXTURE_FILTER_NEAREST;
texture = 0;
image = NULL;
pixels = NULL;
}
Texture::~Texture() {
if (image) {
SDL_FreeSurface(image);
if (pixels) {
//todo
//delete (pixels);
glDeleteTextures(1, &texture);
}

View File

@ -1,7 +1,7 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <SDL.h>
#include "3rd_glad.h"
class Texture {
public:
@ -15,7 +15,28 @@ public:
TextureFilter filter;
GLuint texture;
SDL_Surface *image;
// 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();

View File

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