Added UITexture and font caching
This commit is contained in:
parent
2af54c4d9e
commit
dff0d86558
BIN
DevScreenshots/5. UI Texture.png
Normal file
BIN
DevScreenshots/5. UI Texture.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
@ -9,7 +9,7 @@ MainState::MainState(nf::Application* app) :
|
|||||||
void MainState::onEnter() {
|
void MainState::onEnter() {
|
||||||
Log("MainState onEnter!");
|
Log("MainState onEnter!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainState::update(double deltaTime) {
|
void MainState::update(double deltaTime) {
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
out vec4 color;
|
in vec2 texCoord;
|
||||||
|
|
||||||
uniform sampler2D tex;
|
uniform sampler2D tex;
|
||||||
|
|
||||||
in vec2 texCoord;
|
out vec4 color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = texture(tex, texCoord);
|
color = texture(tex, texCoord);
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
out vec4 color;
|
in vec2 texCoord;
|
||||||
|
|
||||||
uniform sampler2D text;
|
uniform sampler2D text;
|
||||||
uniform vec3 textColor;
|
uniform vec3 textColor;
|
||||||
|
|
||||||
in vec2 texCoord;
|
out vec4 color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 temp = vec4(1.0, 1.0, 1.0, texture(text, texCoord).r);
|
vec4 temp = vec4(1.0, 1.0, 1.0, texture(text, texCoord).r);
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
layout(location = 0) in vec2 pos;
|
layout(location = 0) in vec2 pos;
|
||||||
layout(location = 1) in vec2 texCoords;
|
layout(location = 1) in vec2 texCoords;
|
||||||
|
|
||||||
//uniform mat4 model;
|
|
||||||
uniform mat4 proj;
|
uniform mat4 proj;
|
||||||
|
|
||||||
out vec2 texCoord;
|
out vec2 texCoord;
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
in vec2 texCoord;
|
||||||
|
|
||||||
|
uniform sampler2D tex;
|
||||||
|
|
||||||
|
out vec4 color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
color = texture(tex, texCoord);
|
||||||
|
}
|
13
NFPackCreator/AssetBuild/base/shaders/uiTextureVertex.shader
Normal file
13
NFPackCreator/AssetBuild/base/shaders/uiTextureVertex.shader
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec2 pos;
|
||||||
|
layout(location = 1) in vec2 texCoords;
|
||||||
|
|
||||||
|
uniform mat4 proj;
|
||||||
|
|
||||||
|
out vec2 texCoord;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = proj * vec4(pos, 0.0, 1.0);
|
||||||
|
texCoord = texCoords;
|
||||||
|
}
|
@ -205,6 +205,7 @@
|
|||||||
<ClCompile Include="src\Renderer\Drawable\Model.cpp" />
|
<ClCompile Include="src\Renderer\Drawable\Model.cpp" />
|
||||||
<ClCompile Include="src\Renderer\Drawable\Text.cpp" />
|
<ClCompile Include="src\Renderer\Drawable\Text.cpp" />
|
||||||
<ClCompile Include="src\Renderer\Drawable\UIElement.cpp" />
|
<ClCompile Include="src\Renderer\Drawable\UIElement.cpp" />
|
||||||
|
<ClCompile Include="src\Renderer\Drawable\UITexture.cpp" />
|
||||||
<ClCompile Include="src\Renderer\IndexBuffer.cpp" />
|
<ClCompile Include="src\Renderer\IndexBuffer.cpp" />
|
||||||
<ClCompile Include="src\Renderer\Renderer.cpp" />
|
<ClCompile Include="src\Renderer\Renderer.cpp" />
|
||||||
<ClCompile Include="src\Renderer\Shader.cpp" />
|
<ClCompile Include="src\Renderer\Shader.cpp" />
|
||||||
@ -231,6 +232,7 @@
|
|||||||
<ClInclude Include="src\include\Text.h" />
|
<ClInclude Include="src\include\Text.h" />
|
||||||
<ClInclude Include="src\include\Texture.h" />
|
<ClInclude Include="src\include\Texture.h" />
|
||||||
<ClInclude Include="src\include\UIElement.h" />
|
<ClInclude Include="src\include\UIElement.h" />
|
||||||
|
<ClInclude Include="src\include\UITexture.h" />
|
||||||
<ClInclude Include="src\include\Utility.h" />
|
<ClInclude Include="src\include\Utility.h" />
|
||||||
<ClInclude Include="src\include\VertexArray.h" />
|
<ClInclude Include="src\include\VertexArray.h" />
|
||||||
<ClInclude Include="src\include\VertexBuffer.h" />
|
<ClInclude Include="src\include\VertexBuffer.h" />
|
||||||
|
@ -66,6 +66,9 @@
|
|||||||
<ClCompile Include="src\Renderer\Drawable\Text.cpp">
|
<ClCompile Include="src\Renderer\Drawable\Text.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\Renderer\Drawable\UITexture.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="src\include\Config.h">
|
<ClInclude Include="src\include\Config.h">
|
||||||
@ -128,6 +131,9 @@
|
|||||||
<ClInclude Include="src\include\Text.h">
|
<ClInclude Include="src\include\Text.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\include\UITexture.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Natvis Include="NatvisFile.natvis" />
|
<Natvis Include="NatvisFile.natvis" />
|
||||||
|
@ -121,7 +121,7 @@ namespace nf {
|
|||||||
const Config& Application::getConfig() const {
|
const Config& Application::getConfig() const {
|
||||||
return m_currentConfig;
|
return m_currentConfig;
|
||||||
}
|
}
|
||||||
//TODO: Test this
|
|
||||||
int Application::getFPS() const {
|
int Application::getFPS() const {
|
||||||
return m_FPS;
|
return m_FPS;
|
||||||
}
|
}
|
||||||
|
@ -5,15 +5,15 @@
|
|||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
Asset::~Asset() {
|
Asset::~Asset() {
|
||||||
|
delete[] data;
|
||||||
}
|
}
|
||||||
|
|
||||||
AModel::~AModel() {
|
AModel::~AModel() {
|
||||||
delete[] data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ATexture::~ATexture() {
|
ATexture::~ATexture() {
|
||||||
delete[] data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AShader::~AShader() {
|
AShader::~AShader() {
|
||||||
|
@ -30,7 +30,7 @@ namespace nf {
|
|||||||
size_t ibCount = 0;
|
size_t ibCount = 0;
|
||||||
std::vector<float> tc;
|
std::vector<float> tc;
|
||||||
parseOBJ(obj, vb, ib, ibCount, tc);
|
parseOBJ(obj, vb, ib, ibCount, tc);
|
||||||
m_model->create(&vb[0], vb.size() * sizeof(float), &ib[0], ibCount, &tc[0], tc.size() * sizeof(float), texture.data, texture.size);
|
m_model->create(&vb[0], vb.size() * sizeof(float), &ib[0], ibCount, &tc[0], tc.size() * sizeof(float), &texture);
|
||||||
model.alreadyLoaded = true;
|
model.alreadyLoaded = true;
|
||||||
model.loadedModel = m_model;
|
model.loadedModel = m_model;
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setScale(double x) {
|
void Entity::setScale(double x) {
|
||||||
m_rotation = { x, x, x };
|
m_scale = { x, x, x };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setScale(double x, double y, double z) {
|
void Entity::setScale(double x, double y, double z) {
|
||||||
|
@ -13,17 +13,22 @@ namespace nf {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureData, size_t textureSize) {
|
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, ATexture* texture) {
|
||||||
m_vao = new VertexArray;
|
m_vao = new VertexArray;
|
||||||
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
|
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
|
||||||
m_vao->push<float>(3);
|
m_vao->push<float>(3);
|
||||||
m_vao->finishBufferLayout();
|
m_vao->finishBufferLayout();
|
||||||
if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureData) {
|
m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize);
|
||||||
m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize);
|
m_vao->push<float>(2);
|
||||||
m_vao->push<float>(2);
|
m_vao->finishBufferLayout();
|
||||||
m_vao->finishBufferLayout();
|
if (texture->alreadyLoaded) {
|
||||||
|
m_texture = texture->loadedTexture;
|
||||||
|
}
|
||||||
|
else {
|
||||||
m_texture = new Texture;
|
m_texture = new Texture;
|
||||||
m_texture->create(textureData, textureSize);
|
m_texture->create(texture->data, texture->size);
|
||||||
|
texture->alreadyLoaded = true;
|
||||||
|
texture->loadedTexture = m_texture;
|
||||||
}
|
}
|
||||||
m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
|
m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,7 @@
|
|||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
Text::Text() :
|
Text::Text() {
|
||||||
m_centered(false)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,24 +21,32 @@ namespace nf {
|
|||||||
Error("Could not initialize FreeType!");
|
Error("Could not initialize FreeType!");
|
||||||
FT_Face face;
|
FT_Face face;
|
||||||
AFont& newFont = *(AFont*)font;
|
AFont& newFont = *(AFont*)font;
|
||||||
if (FT_New_Memory_Face(ft, (const unsigned char*)newFont.data, newFont.size, 0, &face))
|
if (newFont.alreadyLoaded) {
|
||||||
Error("Could not load font!");
|
m_font = newFont.loadedFont;
|
||||||
FT_Set_Pixel_Sizes(face, 0, size);
|
}
|
||||||
for (unsigned char c = 0; c < 128; c++) {
|
else {
|
||||||
FT_Load_Char(face, c, FT_LOAD_RENDER);
|
m_font = new Font;
|
||||||
unsigned int tex;
|
if (FT_New_Memory_Face(ft, (const unsigned char*)newFont.data, newFont.size, 0, &face))
|
||||||
glGenTextures(1, &tex);
|
Error("Could not load font!");
|
||||||
glBindTexture(GL_TEXTURE_2D, tex);
|
FT_Set_Pixel_Sizes(face, 0, size);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
|
for (unsigned char c = 0; c < 128; c++) {
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
FT_Load_Char(face, c, FT_LOAD_RENDER);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
unsigned int tex;
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glGenTextures(1, &tex);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glBindTexture(GL_TEXTURE_2D, tex);
|
||||||
Character ch = { tex, Vec2((float)face->glyph->bitmap.width, (float)face->glyph->bitmap.rows), Vec2(face->glyph->bitmap_left, face->glyph->bitmap_top), (unsigned int)face->glyph->advance.x };
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
|
||||||
m_font[c] = ch;
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
Character ch = { tex, Vec2((float)face->glyph->bitmap.width, (float)face->glyph->bitmap.rows), Vec2(face->glyph->bitmap_left, face->glyph->bitmap_top), (unsigned int)face->glyph->advance.x };
|
||||||
|
m_font->m_characters[c] = ch;
|
||||||
|
}
|
||||||
|
FT_Done_Face(face);
|
||||||
|
FT_Done_FreeType(ft);
|
||||||
|
newFont.alreadyLoaded = true;
|
||||||
|
newFont.loadedFont = m_font;
|
||||||
}
|
}
|
||||||
FT_Done_Face(face);
|
|
||||||
FT_Done_FreeType(ft);
|
|
||||||
m_vao = new VertexArray;
|
m_vao = new VertexArray;
|
||||||
m_vao->addBuffer(nullptr, 0);
|
m_vao->addBuffer(nullptr, 0);
|
||||||
m_vao->push<float>(2);
|
m_vao->push<float>(2);
|
||||||
@ -50,8 +56,8 @@ namespace nf {
|
|||||||
m_vao->finishBufferLayout();
|
m_vao->finishBufferLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Text::centered(bool centered) {
|
void Text::setText(const std::string& string) {
|
||||||
m_centered = centered;
|
m_string = string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Text::identity() {
|
const char* Text::identity() {
|
||||||
@ -63,22 +69,24 @@ namespace nf {
|
|||||||
m_vao->bind();
|
m_vao->bind();
|
||||||
std::string::const_iterator si;
|
std::string::const_iterator si;
|
||||||
float currX = (float)m_position.x * windowWidth, currY = (float)m_position.y * windowHeight;
|
float currX = (float)m_position.x * windowWidth, currY = (float)m_position.y * windowHeight;
|
||||||
if (m_centered) {
|
if (m_centeredX || m_centeredY) {
|
||||||
float textWidth = 0.0f;
|
float textWidth = 0.0f;
|
||||||
float textHeight = 0.0f;
|
float textHeight = 0.0f;
|
||||||
for (si = m_string.begin(); si != m_string.end(); si++) {
|
for (si = m_string.begin(); si != m_string.end(); si++) {
|
||||||
Character& c = m_font[*si];
|
Character& c = m_font->m_characters[*si];
|
||||||
textWidth += (c.advance >> 6) * scale;
|
textWidth += (c.advance >> 6) * scale;
|
||||||
if (c.size.y >= textHeight)
|
if (c.size.y >= textHeight)
|
||||||
textHeight = (float)c.size.y * scale;
|
textHeight = (float)c.size.y * scale;
|
||||||
}
|
}
|
||||||
currX = ((float)windowWidth - textWidth) / 2;
|
if (m_centeredX)
|
||||||
currY = ((float)windowHeight - textHeight) / 2;
|
currX = ((float)windowWidth - textWidth) / 2;
|
||||||
|
if (m_centeredY)
|
||||||
|
currY = ((float)windowHeight - textHeight) / 2;
|
||||||
}
|
}
|
||||||
glm::vec3 color = { m_color.x, m_color.y, m_color.z };
|
glm::vec3 color = { m_color.x, m_color.y, m_color.z };
|
||||||
shader->setUniform("textColor", color);
|
shader->setUniform("textColor", color);
|
||||||
for (si = m_string.begin(); si != m_string.end(); si++) {
|
for (si = m_string.begin(); si != m_string.end(); si++) {
|
||||||
Character& c = m_font[*si];
|
Character& c = m_font->m_characters[*si];
|
||||||
float x = currX + (float)c.bearing.x * scale;
|
float x = currX + (float)c.bearing.x * scale;
|
||||||
float y = currY - float(c.size.y - c.bearing.y) * scale;
|
float y = currY - float(c.size.y - c.bearing.y) * scale;
|
||||||
float w = (float)c.size.x * scale;
|
float w = (float)c.size.x * scale;
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
#include "UIElement.h"
|
#include "UIElement.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
UIElement::UIElement() {
|
UIElement::UIElement() :
|
||||||
|
m_centeredX(false),
|
||||||
|
m_centeredY(false)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9,6 +12,18 @@ namespace nf {
|
|||||||
return "none";
|
return "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UIElement::setPosition(float x, float y) {
|
||||||
|
m_position = Vec2(x, y);
|
||||||
|
}
|
||||||
|
void UIElement::setPosition(const Vec2& position) {
|
||||||
|
m_position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UIElement::centered(bool x, bool y) {
|
||||||
|
m_centeredX = x;
|
||||||
|
m_centeredY = y;
|
||||||
|
}
|
||||||
|
|
||||||
void UIElement::render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) {
|
void UIElement::render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
76
NothinFancy/src/Renderer/Drawable/UITexture.cpp
Normal file
76
NothinFancy/src/Renderer/Drawable/UITexture.cpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#include "UITexture.h"
|
||||||
|
|
||||||
|
#include "GL/glew.h"
|
||||||
|
|
||||||
|
#include "Assets.h"
|
||||||
|
#include "Texture.h"
|
||||||
|
|
||||||
|
namespace nf {
|
||||||
|
UITexture::UITexture() :
|
||||||
|
m_texture(0)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UITexture::create(Asset* texture, const Vec2& position, double scale) {
|
||||||
|
ATexture* tex = (ATexture*)texture;
|
||||||
|
m_position = position;
|
||||||
|
m_scale = (float)scale;
|
||||||
|
if (tex->alreadyLoaded) {
|
||||||
|
m_texture = tex->loadedTexture;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
m_texture = new Texture;
|
||||||
|
m_texture->create(tex->data, tex->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_vao = new VertexArray;
|
||||||
|
m_vao->addBuffer(nullptr, 0);
|
||||||
|
m_vao->push<float>(2);
|
||||||
|
m_vao->finishBufferLayout();
|
||||||
|
m_vao->addBuffer(nullptr, 0);
|
||||||
|
m_vao->push<float>(2);
|
||||||
|
m_vao->finishBufferLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* UITexture::identity() {
|
||||||
|
return "texture";
|
||||||
|
}
|
||||||
|
|
||||||
|
void UITexture::render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) {
|
||||||
|
float posX = (float)m_position.x * windowWidth, posY = (float)m_position.y * windowHeight;
|
||||||
|
float scale = windowWidth / 5.0f;
|
||||||
|
|
||||||
|
Vec2 texDim = m_texture->getDimensions();
|
||||||
|
float height = scale * m_scale;
|
||||||
|
float width = ((float)texDim.x / (float)texDim.y) * scale * m_scale;
|
||||||
|
if (m_centeredX)
|
||||||
|
posX = ((float)windowWidth - width) / 2;
|
||||||
|
if (m_centeredY)
|
||||||
|
posY = ((float)windowHeight - height) / 2;
|
||||||
|
float vb[3][4] = {
|
||||||
|
posX, posY + height,
|
||||||
|
posX, posY,
|
||||||
|
posX + width, posY,
|
||||||
|
posX, posY + height,
|
||||||
|
posX + width, posY,
|
||||||
|
posX + width, posY + height
|
||||||
|
};
|
||||||
|
float tc[3][4] = {
|
||||||
|
0.0, 1.0,
|
||||||
|
0.0, 0.0,
|
||||||
|
1.0, 0.0,
|
||||||
|
0.0, 1.0,
|
||||||
|
1.0, 0.0,
|
||||||
|
1.0, 1.0
|
||||||
|
};
|
||||||
|
m_texture->bind();
|
||||||
|
m_vao->setBufferData(0, vb, sizeof(vb));
|
||||||
|
m_vao->setBufferData(1, tc, sizeof(tc));
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
UITexture::~UITexture() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -49,7 +49,6 @@ namespace nf {
|
|||||||
wglMakeCurrent(m_hdc, m_hglrc);
|
wglMakeCurrent(m_hdc, m_hglrc);
|
||||||
wglSwapIntervalEXT(0);
|
wglSwapIntervalEXT(0);
|
||||||
Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
|
Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LESS);
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
@ -64,6 +63,9 @@ namespace nf {
|
|||||||
const char* textVertex = baseAP["textVertex.shader"]->data;
|
const char* textVertex = baseAP["textVertex.shader"]->data;
|
||||||
const char* textFragment = baseAP["textFragment.shader"]->data;
|
const char* textFragment = baseAP["textFragment.shader"]->data;
|
||||||
m_textShader = new Shader(textVertex, textFragment);
|
m_textShader = new Shader(textVertex, textFragment);
|
||||||
|
const char* uiTextureVertex = baseAP["uiTextureVertex.shader"]->data;
|
||||||
|
const char* uiTextureFragment = baseAP["uiTextureFragment.shader"]->data;
|
||||||
|
m_uiTextureShader = new Shader(uiTextureVertex, uiTextureFragment);
|
||||||
|
|
||||||
BaseAssets::cube = (AModel*)baseAP["cube.obj"];
|
BaseAssets::cube = (AModel*)baseAP["cube.obj"];
|
||||||
BaseAssets::plane = (AModel*)baseAP["plane.obj"];
|
BaseAssets::plane = (AModel*)baseAP["plane.obj"];
|
||||||
@ -91,6 +93,7 @@ namespace nf {
|
|||||||
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
|
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
|
||||||
for (Entity* draw : m_lGame) {
|
for (Entity* draw : m_lGame) {
|
||||||
Entity& curr = *draw;
|
Entity& curr = *draw;
|
||||||
@ -102,6 +105,7 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
m_lGame.clear();
|
m_lGame.clear();
|
||||||
|
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
|
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
|
||||||
for (UIElement* draw : m_lUI) {
|
for (UIElement* draw : m_lUI) {
|
||||||
UIElement& curr = *draw;
|
UIElement& curr = *draw;
|
||||||
@ -111,7 +115,11 @@ namespace nf {
|
|||||||
curr.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
|
curr.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//TODO: Add else if for UI texture
|
if (curr.identity() == "texture") {
|
||||||
|
m_uiTextureShader->bind();
|
||||||
|
m_uiTextureShader->setUniform("proj", proj);
|
||||||
|
curr.render(m_uiTextureShader, m_app->getConfig().width, m_app->getConfig().height);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m_lUI.clear();
|
m_lUI.clear();
|
||||||
|
|
||||||
@ -123,6 +131,9 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Renderer::~Renderer() {
|
Renderer::~Renderer() {
|
||||||
|
delete m_entityShader;
|
||||||
|
delete m_textShader;
|
||||||
|
delete m_uiTextureShader;
|
||||||
ReleaseDC(m_app->getWindow(), m_hdc);
|
ReleaseDC(m_app->getWindow(), m_hdc);
|
||||||
wglMakeCurrent(NULL, NULL);
|
wglMakeCurrent(NULL, NULL);
|
||||||
wglDeleteContext(m_hglrc);
|
wglDeleteContext(m_hglrc);
|
||||||
|
@ -12,15 +12,15 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Texture::create(const char* textureData, size_t textureSize) {
|
void Texture::create(const char* textureData, size_t textureSize) {
|
||||||
int width, height, nChannels;
|
int nChannels;
|
||||||
stbi_set_flip_vertically_on_load(true);
|
stbi_set_flip_vertically_on_load(true);
|
||||||
unsigned char* texture = stbi_load_from_memory((unsigned char*)textureData, textureSize, &width, &height, &nChannels, 0);
|
unsigned char* texture = stbi_load_from_memory((unsigned char*)textureData, textureSize, &m_x, &m_y, &nChannels, 0);
|
||||||
if (!texture)
|
if (!texture)
|
||||||
Error("Texture failed to load from memory!");
|
Error("Texture failed to load from memory!");
|
||||||
glBindTexture(GL_TEXTURE_2D, m_id);
|
glBindTexture(GL_TEXTURE_2D, m_id);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_x, m_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
stbi_image_free(texture);
|
stbi_image_free(texture);
|
||||||
}
|
}
|
||||||
@ -29,6 +29,10 @@ namespace nf {
|
|||||||
glBindTexture(GL_TEXTURE_2D, m_id);
|
glBindTexture(GL_TEXTURE_2D, m_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vec2 Texture::getDimensions() {
|
||||||
|
return { (float)m_x, (float)m_y };
|
||||||
|
}
|
||||||
|
|
||||||
Texture::~Texture() {
|
Texture::~Texture() {
|
||||||
glDeleteTextures(1, &m_id);
|
glDeleteTextures(1, &m_id);
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,17 @@
|
|||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
class Model;
|
class Model;
|
||||||
|
class Texture;
|
||||||
|
struct Font;
|
||||||
|
|
||||||
struct Asset {
|
struct Asset {
|
||||||
char* data;
|
char* data;
|
||||||
|
bool alreadyLoaded = false;
|
||||||
|
|
||||||
virtual ~Asset();
|
virtual ~Asset();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AModel : Asset {
|
struct AModel : Asset {
|
||||||
bool alreadyLoaded;
|
|
||||||
Model* loadedModel;
|
Model* loadedModel;
|
||||||
|
|
||||||
~AModel() override;
|
~AModel() override;
|
||||||
@ -19,6 +21,7 @@ namespace nf {
|
|||||||
|
|
||||||
struct ATexture : Asset {
|
struct ATexture : Asset {
|
||||||
size_t size;
|
size_t size;
|
||||||
|
Texture* loadedTexture;
|
||||||
|
|
||||||
~ATexture() override;
|
~ATexture() override;
|
||||||
};
|
};
|
||||||
@ -29,6 +32,7 @@ namespace nf {
|
|||||||
|
|
||||||
struct AFont : Asset {
|
struct AFont : Asset {
|
||||||
size_t size;
|
size_t size;
|
||||||
|
Font* loadedFont;
|
||||||
|
|
||||||
~AFont() override;
|
~AFont() override;
|
||||||
};
|
};
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Drawable.h"
|
#include "Drawable.h"
|
||||||
|
#include "Assets.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
class Drawable;
|
class Drawable;
|
||||||
class Texture;
|
class Texture;
|
||||||
|
|
||||||
class Model : public Drawable {
|
class Model : public Drawable {
|
||||||
public:
|
public:
|
||||||
Model();
|
Model();
|
||||||
|
|
||||||
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates, size_t textureCoordinatesBufferSize, const char* textureData, size_t textureSize);
|
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates, size_t textureCoordinatesBufferSize, ATexture* texture);
|
||||||
void bind() override;
|
void bind() override;
|
||||||
|
|
||||||
~Model();
|
~Model();
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "IntroGamestate.h"
|
#include "IntroGamestate.h"
|
||||||
#include "Assets.h"
|
#include "Assets.h"
|
||||||
#include "Text.h"
|
#include "Text.h"
|
||||||
|
#include "UITexture.h"
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
|
@ -32,5 +32,6 @@ namespace nf {
|
|||||||
std::vector<UIElement*> m_lUI;
|
std::vector<UIElement*> m_lUI;
|
||||||
Shader* m_entityShader;
|
Shader* m_entityShader;
|
||||||
Shader* m_textShader;
|
Shader* m_textShader;
|
||||||
|
Shader* m_uiTextureShader;
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -13,22 +13,23 @@ namespace nf {
|
|||||||
unsigned int advance;
|
unsigned int advance;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Font {
|
||||||
|
std::map<char, Character> m_characters;
|
||||||
|
};
|
||||||
|
|
||||||
class Text : public UIElement {
|
class Text : public UIElement {
|
||||||
public:
|
public:
|
||||||
Text();
|
Text();
|
||||||
|
|
||||||
void create(const std::string& string, const Vec2& position, const Vec3& color = {1.0, 1.0, 1.0}, unsigned int size = 160, Asset* font = BaseAssets::defaultFont);
|
void create(const std::string& string, const Vec2& position, const Vec3& color = {1.0, 1.0, 1.0}, unsigned int size = 160, Asset* font = BaseAssets::defaultFont);
|
||||||
void centered(bool centered);
|
void setText(const std::string& string);
|
||||||
|
|
||||||
const char* identity() override;
|
const char* identity() override;
|
||||||
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;
|
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;
|
||||||
|
|
||||||
~Text();
|
~Text();
|
||||||
private:
|
private:
|
||||||
std::string m_string;
|
std::string m_string;
|
||||||
Vec2 m_position;
|
Font* m_font;
|
||||||
Vec3 m_color;
|
Vec3 m_color;
|
||||||
bool m_centered;
|
|
||||||
std::map<char, Character> m_font;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Utility.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
class Texture {
|
class Texture {
|
||||||
@ -7,9 +8,12 @@ namespace nf {
|
|||||||
|
|
||||||
void create(const char* textureData, size_t textureSize);
|
void create(const char* textureData, size_t textureSize);
|
||||||
void bind();
|
void bind();
|
||||||
|
Vec2 getDimensions();
|
||||||
|
|
||||||
~Texture();
|
~Texture();
|
||||||
private:
|
private:
|
||||||
unsigned int m_id;
|
unsigned int m_id;
|
||||||
|
int m_x;
|
||||||
|
int m_y;
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Drawable.h"
|
#include "Drawable.h"
|
||||||
|
|
||||||
|
#include "Utility.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
class Shader;
|
class Shader;
|
||||||
|
|
||||||
@ -9,10 +11,15 @@ namespace nf {
|
|||||||
UIElement();
|
UIElement();
|
||||||
|
|
||||||
virtual const char* identity();
|
virtual const char* identity();
|
||||||
|
void setPosition(float x, float y);
|
||||||
|
void setPosition(const Vec2& position);
|
||||||
|
void centered(bool x, bool y = false);
|
||||||
virtual void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight);
|
virtual void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight);
|
||||||
|
|
||||||
~UIElement();
|
~UIElement();
|
||||||
private:
|
protected:
|
||||||
|
Vec2 m_position;
|
||||||
|
bool m_centeredX;
|
||||||
|
bool m_centeredY;
|
||||||
};
|
};
|
||||||
}
|
}
|
22
NothinFancy/src/include/UITexture.h
Normal file
22
NothinFancy/src/include/UITexture.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "UIElement.h"
|
||||||
|
#include "Utility.h"
|
||||||
|
|
||||||
|
namespace nf {
|
||||||
|
class Texture;
|
||||||
|
struct Asset;
|
||||||
|
|
||||||
|
class UITexture : public UIElement {
|
||||||
|
public:
|
||||||
|
UITexture();
|
||||||
|
|
||||||
|
void create(Asset* texture, const Vec2& position, double scale = 1.0);
|
||||||
|
const char* identity() override;
|
||||||
|
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;
|
||||||
|
|
||||||
|
~UITexture();
|
||||||
|
private:
|
||||||
|
Texture* m_texture;
|
||||||
|
float m_scale;
|
||||||
|
};
|
||||||
|
}
|
Reference in New Issue
Block a user