Glyph output -> single Glyphs;

read bmp file (via stb, m_header is coming later)
 -> stb_image.h;
added monochrom imagetype (load_tga_file); 

git-svn-id: svn://ammerhai.com/home/mike/pokemon_repo@19 24008968-59e6-ed4c-a10b-0b2c954b24ab
This commit is contained in:
mikeb
2021-07-18 13:09:44 +00:00
parent 70a9074415
commit b603d997cf
17 changed files with 8168 additions and 30 deletions
+111 -21
View File
@@ -18,6 +18,9 @@
#include "math_graphics.h"
#include "load_tga_file.h"
#include "load_entire_file.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "glyphs.h"
typedef uint8_t uint8;
typedef uint16_t uint16;
@@ -75,10 +78,10 @@ struct Vertex {
};
Vertex vertices[] = {
{{ -1, 1, 1, 1 }, {0, 1}},
{{ 1, -1, 1, 1 }, {1, 0}},
{{ -1, -1, 1, 1 }, {0, 0}},
{{ 1, 1, 1, 1 }, {1, 1}},
{{ -1, 1, 1, 1 }, {0, 0}},
{{ 1, -1, 1, 1 }, {1, 1}},
{{ -1, -1, 1, 1 }, {0, 1}},
{{ 1, 1, 1, 1 }, {1, 0}},
};
uint16 indices[] = {
@@ -627,13 +630,13 @@ ID3D11ShaderResourceView* create_shader_texture_array(int num_textures, char** b
defer(for (int i = 0; i < num_textures; i++) { free(bmp_textures[i].pixel); });
for (int i = 1; i < num_textures; i++) {
assert(bmp_textures[i].bmp_width == bmp_textures[i - 1].bmp_width);
assert(bmp_textures[i].bmp_height == bmp_textures[i - 1].bmp_height);
assert(bmp_textures[i].width == bmp_textures[i - 1].width);
assert(bmp_textures[i].height == bmp_textures[i - 1].height);
}
D3D11_TEXTURE2D_DESC texture_desc = {
.Width = (UINT) bmp_textures[0].bmp_width,
.Height = (UINT) bmp_textures[0].bmp_height,
.Width = (UINT) bmp_textures[0].width,
.Height = (UINT) bmp_textures[0].height,
.MipLevels = 1,
.ArraySize = (UINT) num_textures,
.Format = DXGI_FORMAT_B8G8R8A8_UNORM,
@@ -650,7 +653,7 @@ ID3D11ShaderResourceView* create_shader_texture_array(int num_textures, char** b
for (int i = 0; i < num_textures; i++) {
initial_data[i] = {
.pSysMem = bmp_textures[i].pixel,
.SysMemPitch = (UINT)bmp_textures[i].bmp_width * 4,
.SysMemPitch = (UINT)bmp_textures[i].width * 4,
};
}
@@ -673,12 +676,16 @@ ID3D11ShaderResourceView* create_shader_texture_array(int num_textures, char** b
// 1 Texture erzeugen
ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) {
Image_Info bmp_texture = load_tga_file(bmp_texture_path);
ID3D11ShaderResourceView* create_shader_texture(char* tga_texture_path) {
Image_Info tga_texture = load_tga_file(tga_texture_path);
if (!tga_texture.pixel) {
log_error("Load file has failed.");
return 0;
}
D3D11_TEXTURE2D_DESC texture_desc = {
.Width = (UINT)bmp_texture.bmp_width,
.Height = (UINT)bmp_texture.bmp_height,
.Width = (UINT)tga_texture.width,
.Height = (UINT)tga_texture.height,
.MipLevels = 1,
.ArraySize = 1,
.Format = DXGI_FORMAT_B8G8R8A8_UNORM,
@@ -690,8 +697,66 @@ ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) {
};
D3D11_SUBRESOURCE_DATA initial_data = {
.pSysMem = bmp_texture.pixel,
.SysMemPitch = (UINT)bmp_texture.bmp_width * 4,
.pSysMem = tga_texture.pixel,
.SysMemPitch = (UINT)tga_texture.width * 4,
};
ID3D11Texture2D* texture;
if ((hresult = device->CreateTexture2D(&texture_desc, &initial_data, &texture)) != S_OK) {
log_error("CreateTexture2D has failed. %ld", hresult);
return 0;
}
DX11SetDebugName(texture, { strlen(tga_texture_path), tga_texture_path });
ID3D11ShaderResourceView* resource_view;
if ((hresult = device->CreateShaderResourceView(texture, 0, &resource_view)) != S_OK) {
log_error("CreateShaderResourceView failed. %ld", hresult);
return 0;
}
DX11SetDebugName(resource_view, {strlen(tga_texture_path), tga_texture_path });
free(tga_texture.pixel);
return resource_view;
}
ID3D11ShaderResourceView* create_shader_texture_bmp(char* bmp_texture_path) {
//Image_Info bmp_texture = load_tga_file(bmp_texture_path);
int x, y, n;
unsigned char* bmp_texture = stbi_load(bmp_texture_path, &x, &y, &n, 0);
if (!bmp_texture) {
log_error("Load file has failed.");
return 0;
}
DXGI_FORMAT bmp_format;
if (n == 1)
bmp_format = DXGI_FORMAT_R8_UNORM;
else if (n == 2)
bmp_format = DXGI_FORMAT_R8G8_UNORM;
else if (n == 3) {
log_error("3 channel dxgi format not supported");
return 0;
} else
bmp_format = DXGI_FORMAT_B8G8R8A8_UNORM;
D3D11_TEXTURE2D_DESC texture_desc = {
.Width = (UINT)x,
.Height = (UINT)y,
.MipLevels = 1,
.ArraySize = 1,
.Format = bmp_format,
.SampleDesc = {.Count = 1, .Quality = 0},
.Usage = D3D11_USAGE_DEFAULT,
.BindFlags = D3D11_BIND_SHADER_RESOURCE,
.CPUAccessFlags = 0,
.MiscFlags = 0,
};
D3D11_SUBRESOURCE_DATA initial_data = {
.pSysMem = bmp_texture,
.SysMemPitch = (UINT)x * n,
};
ID3D11Texture2D* texture;
@@ -706,9 +771,9 @@ ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) {
log_error("CreateShaderResourceView failed. %ld", hresult);
return 0;
}
DX11SetDebugName(resource_view, {strlen(bmp_texture_path), bmp_texture_path});
DX11SetDebugName(resource_view, { strlen(bmp_texture_path), bmp_texture_path });
free(bmp_texture.pixel);
free(bmp_texture);
return resource_view;
}
@@ -790,8 +855,28 @@ int main() {
return 1;
}
ID3D11ShaderResourceView* glyph_texture = 0;
if (!(glyph_texture = create_shader_texture_bmp("../assets/fonts/glyph_atlas_lexend.tga"))) {
log_error("CreateShaderTexture has failed.");
return 1;
}
load_map();
String file = load_entire_file("../assets/fonts/glyph_coords_lexend.co");
if (!file.length) {
log_error("Loading glyph_coords_lexend has failed.");
return 1;
}
int glyphs_num = file.length / sizeof(Glyph_Coord);
if (glyphs_num != '~' - ' ' + 1) {
log_error("Wrong number of glyphs.");
return 1;
}
Glyph_Coord* glyph_coords = (Glyph_Coord*) file.data;
V2 quad_pos = { 0.101f, 0.101f };
V2 quad_size = { 0.1f, 0.1f };
@@ -873,12 +958,17 @@ int main() {
//Moving Quad
//quad_pos += {0.001f, 0.001f};
render_quad(quad_pos, quad_size, quad_texture, { 0, 0 }, { 0.5, 1 });
render_quad({ 0.1, 0.2 }, { 0.01, 0.01 }, quad_texture, { 0, 0 }, { 1, 1 });
devicecontext->VSSetShader(font_vertex_shader, 0, 0);
devicecontext->PSSetShader(font_pixel_shader, 0, 0);
render_quad(quad_pos, quad_size, quad_texture, { 0, 0 }, { 0.5, 1 });
render_quad({ 0.1, 0.2 }, { 0.01, 0.01 }, quad_texture, { 0, 0 }, { 1, 1});
//
//Glyph test rendering
int buchstabe = 'M' - ' ';
render_quad({ 0.5, 0.7 }, { glyph_coords[buchstabe].width * ( 512.0f / window_width), glyph_coords[buchstabe].height * (512.0f / window_height)}, glyph_texture, { glyph_coords[buchstabe].x, glyph_coords[buchstabe].y }, { glyph_coords[buchstabe].x + glyph_coords[buchstabe].width, glyph_coords[buchstabe].y + glyph_coords[buchstabe].height } );
swap_chain->Present(1, 0);
}