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

View File

Before

Width:  |  Height:  |  Size: 768 KiB

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

Binary file not shown.

View File

@ -8,7 +8,7 @@ struct PixelShaderOutput {
float4 color : SV_TARGET;
};
Texture2DArray<float4> tex1 : register(t0);
Texture2D<float> tex1 : register(t0);
SamplerState texture_sampler : register(s0);
@ -16,7 +16,7 @@ PixelShaderOutput main(PixelShaderInput input) {
PixelShaderOutput output;
#if 1
output.color = tex1.Sample(texture_sampler, float3(input.uvst.xy, input.tile_type));
output.color = float4(1, 1, 1, tex1.Sample(texture_sampler, float2(input.uvst.xy)));
#else
output.color = float4(1, 0, 1, 1);
#endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -150,6 +150,8 @@
<ClCompile Include="src\log.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\glyphs.h" />
<ClInclude Include="src\stb_image.h" />
<ClInclude Include="src\stb_image_write.h" />
<ClInclude Include="src\stb_rect_pack.h" />
<ClInclude Include="src\stb_truetype.h" />

View File

@ -35,5 +35,11 @@
<ClInclude Include="src\stb_rect_pack.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\stb_image.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\glyphs.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -111,11 +111,17 @@
<AdditionalOptions>-Wno-missing-braces -Wno-parentheses -Wno-reorder-init-list -Wno-unused-variable -Wno-format %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\create_window.cpp" />

View File

@ -7,6 +7,8 @@
#include "stb_image_write.h"
#include "load_entire_file.h"
#include "log.h"
#include "defer.h"
#include "glyphs.h"
struct Glyph {
int x_off;
@ -14,6 +16,8 @@ struct Glyph {
char* data;
};
Glyph_Coord glyph_coords[512];
int main() {
stbtt_fontinfo font_info;
stbrp_context packer_context;
@ -63,6 +67,10 @@ int main() {
for (int i = 0; i < rect_number; i++) {
glyph_coords[i].x = packer_rects[i].x / 512.0f;
glyph_coords[i].y = packer_rects[i].y / 512.0f;
glyph_coords[i].width = packer_rects[i].w / 512.0f;
glyph_coords[i].height = packer_rects[i].h / 512.0f;
for (int x = 0; x < packer_rects[i].w; x++) {
for (int y = 0; y < packer_rects[i].h; y++) {
glyph_atlas[(y + packer_rects[i].y) * 512 + x + packer_rects[i].x] = glyphs[i].data[y * packer_rects[i].w + x];
@ -70,9 +78,23 @@ int main() {
}
}
char file_name_test[256];
sprintf(file_name_test, "../test_bild_text.bmp");
stbi_write_bmp(file_name_test, 512, 512, 1, glyph_atlas);
FILE* file = fopen("../assets/fonts/glyph_coords_lexend.co", "wb");
if (!file) {
log_error("Glyph coords file creation has failed.");
return 1;
}
defer(fclose(file));
if (fwrite(glyph_coords, sizeof(*glyph_coords) * rect_number, 1, file) != 1) {
log_error("fwrite for glyph_coords has failed.");
return 1;
}
if (!stbi_write_bmp("../assets/fonts/glyph_atlas_lexend.bmp", 512, 512, 1, glyph_atlas)) {
log_error("stbi_write_bmp has failed.");
return 1;
}
return 0;
}

8
src/glyphs.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
struct Glyph_Coord {
float x;
float y;
float width;
float height;
};

View File

@ -107,7 +107,11 @@ Image_Info load_tga_file(const char* path) {
expect(color_map_type, 0, "wrong color map type");
auto image_type = read<uint8_t>(file);
expect(image_type, 10, "wrong image type");
//expect(image_type, 10, "wrong image type");
if (image_type != 10 && image_type != 11) {
log_error("wrong image type, not monochrome or not rgb");
return{ 0, 0 };
}
auto color_map_info = read<TGA_Color_Map_Info >(file);
expect(color_map_info.size, 0, "no existing color map");
@ -159,7 +163,7 @@ Image_Info load_tga_file(const char* path) {
}
}
if (image_origin == 32) {
if (image_origin != 32) {
for (int y = 0; y < (image_specification.height / 2); y++) {
for (int x = 0; x < image_specification.width; x++)
swap(start_pixel[y * image_specification.width + x], start_pixel[image_specification.width * (image_specification.height - y - 1) + x]);

View File

@ -2,8 +2,8 @@
#include <stdint.h>
struct Image_Info {
int64_t bmp_width;
int64_t bmp_height;
int64_t width;
int64_t height;
uint32_t* pixel;
};

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

8000
src/stb_image.h Normal file

File diff suppressed because it is too large Load Diff