Skip to content

Commit

Permalink
added new tests for Texture class and Text class
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacey committed Sep 29, 2023
1 parent fe4b68b commit 2baa3ca
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 27 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ if(NOT DEFINED PYNGL_ONLY)
${CMAKE_SOURCE_DIR}/tests/PointBakeTests.cpp
${CMAKE_SOURCE_DIR}/tests/MessageTests.cpp
${CMAKE_SOURCE_DIR}/tests/NGLStreamTest.cpp
${CMAKE_SOURCE_DIR}/tests/TextureTests.cpp

)
add_compile_definitions(GLM_ENABLE_EXPERIMENTAL)
Expand Down
4 changes: 2 additions & 2 deletions include/ngl/Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ class NGL_DLLEXPORT Text
/// @brief set the colour of the font from an Colour
/// @param[in] _c the colour to set for the font (alpha is overridden by the texture)
//----------------------------------------------------------------------------------------------------------------------
void setColour(const Vec3 &_c) noexcept;
void setColour(const Vec3 &_c) const noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief set the colour of the font from three floats as a convenience method
/// @param[in] _r the red component of the colour for the font
/// @param[in] _g the green component of the colour for the font
/// @param[in] _b the blue component of the colour for the font
//----------------------------------------------------------------------------------------------------------------------
void setColour(Real _r, Real _g, Real _b) noexcept;
void setColour(Real _r, Real _g, Real _b) const noexcept;

protected:
//----------------------------------------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions include/ngl/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ class NGL_DLLEXPORT Texture
/// @brief Get the width of the texture
/// @return width of the texture
//----------------------------------------------------------------------------------------------------------------------
GLuint getWidth() const noexcept
GLuint width() const noexcept
{
return m_width;
}
//----------------------------------------------------------------------------------------------------------------------
/// @brief Get the height of the texture
/// @return height of the texture
//----------------------------------------------------------------------------------------------------------------------
GLuint getHeight() const noexcept
GLuint height() const noexcept
{
return m_height;
}
//----------------------------------------------------------------------------------------------------------------------
/// @brief Get the pixel format
/// @return pixel format of the texture
//----------------------------------------------------------------------------------------------------------------------
GLuint getFormat() const noexcept
GLuint format() const noexcept
{
return m_format;
}
Expand Down
37 changes: 16 additions & 21 deletions src/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <unordered_map>
#include FT_FREETYPE_H

//#include <QPainter>
#include "NGLStream.h"
#include "ShaderLib.h"
#include "Text.h"
Expand Down Expand Up @@ -79,8 +78,6 @@ Text::Text(std::string_view _name, int _size)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// glGenerateMipmap(GL_TEXTURE_2D);

fc.sizex = face->glyph->bitmap.width;
fc.sizey = face->glyph->bitmap.rows;
fc.bearingx = face->glyph->bitmap_left;
Expand All @@ -102,15 +99,15 @@ Text::Text(std::string_view _name, int _size)
Text::~Text()
{
// our dtor should clear out the textures and remove the VAO's
for(auto &m : m_characters)
for(auto [c,font] : m_characters)
{
glDeleteTextures(1, &m.second.textureID);
glDeleteTextures(1, &font.textureID);
}
}

void Text::renderText(float _x, float _y, const char *_text) const noexcept
{
renderText(_x, _y, std::string(_text));
renderText(_x, _y, std::string_view(_text));
}

//---------------------------------------------------------------------------
Expand All @@ -131,31 +128,29 @@ void Text::renderText(float _x, float _y, std::string_view _text) const noexcept
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
// now loop for each of the char and draw our billboard
int textLength = _text.length();
float scale = 1.0f;
m_texVAO->bind();

for(int i = 0; i < textLength; ++i)
for (auto c : _text)
{
auto fc = m_characters.find(_text[i]);
auto fc = m_characters.find(c) ;
float xpos = _x + fc->second.bearingx * scale;
float ypos = _y - (fc->second.sizey - fc->second.bearingy) * scale;

float w = fc->second.sizex * scale;
float h = fc->second.sizey * scale;
// update VBO for each character
float vertices[6][4] = {
{xpos, ypos + h, 0.0f, 0.0f},
{xpos, ypos, 0.0f, 1.0f},
{xpos + w, ypos, 1.0f, 1.0f},

{xpos, ypos + h, 0.0f, 0.0f},
{xpos + w, ypos, 1.0f, 1.0f},
{xpos + w, ypos + h, 1.0f, 0.0f}};
std::array<float,24> vertices = {
xpos, ypos + h, 0.0f, 0.0f,
xpos, ypos, 0.0f, 1.0f,
xpos + w, ypos, 1.0f, 1.0f,
xpos, ypos + h, 0.0f, 0.0f,
xpos + w, ypos, 1.0f, 1.0f,
xpos + w, ypos + h, 1.0f, 0.0f};

// // bind the pre-generated texture
glBindTexture(GL_TEXTURE_2D, fc->second.textureID);
m_texVAO->setData(SimpleVAO::VertexData(sizeof(vertices), vertices[0][0], GL_DYNAMIC_DRAW));
m_texVAO->setData(SimpleVAO::VertexData(sizeof(vertices), vertices[0], GL_DYNAMIC_DRAW));
// now we set the attribute pointer to be 0 (as this matches vertIn in our shader)
m_texVAO->setVertexAttributePointer(0, 4, GL_FLOAT, 0, 0);
// say how many indices to be rendered
Expand All @@ -177,7 +172,7 @@ void Text::setScreenSize(int _w, int _h) noexcept

ShaderLib::use("nglTextShader");

auto orth = ngl::ortho(0, _w, 0, _h);
auto orth = ngl::ortho(0, static_cast<float>(_w), 0, static_cast<float>(_h));
ShaderLib::setUniform("projection", orth);
}

Expand All @@ -187,7 +182,7 @@ void Text::setScreenSize(int _w, int _h) noexcept
// it is default to black but this will change it
// the shader uses the following code

void Text::setColour(const Vec3 &_c) noexcept
void Text::setColour(const Vec3 &_c) const noexcept
{
// get shader instance
ShaderLib::use("nglTextShader");
Expand All @@ -196,7 +191,7 @@ void Text::setColour(const Vec3 &_c) noexcept
}

//---------------------------------------------------------------------------
void Text::setColour(Real _r, Real _g, Real _b) noexcept
void Text::setColour(Real _r, Real _g, Real _b)const noexcept
{
ShaderLib::use("nglTextShader");
ShaderLib::setUniform("textColour", _r, _g, _b);
Expand Down
1 change: 0 additions & 1 deletion src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Texture::Texture(std::string_view _fname)
m_height = m_image.height();
m_channels = m_image.channels();
m_format = m_image.format();
m_multiTextureID = 0;
}

bool Texture::loadImage(std::string_view _fname)
Expand Down
34 changes: 34 additions & 0 deletions tests/TextureTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <gtest/gtest.h>
#include <ngl/Texture.h>

TEST(Texture,construct)
{
auto t=ngl::Texture();
EXPECT_TRUE(t.width()==0);
EXPECT_TRUE(t.height()==0);
EXPECT_TRUE(t.format()==GL_RGB);
EXPECT_TRUE(t.getImage().width()==0);
EXPECT_TRUE(t.getImage().height()==0);
}

TEST(Texture,loadRGB)
{
auto t=ngl::Texture("files/simpleRGB.bmp");
EXPECT_TRUE(t.width()==4);
EXPECT_TRUE(t.height()==4);
EXPECT_TRUE(t.format()==GL_RGB);
EXPECT_TRUE(t.getImage().width()==4);
EXPECT_TRUE(t.getImage().height()==4);

}

TEST(Texture,loadRGBA)
{
auto t=ngl::Texture("files/simpleRGBA.bmp");
EXPECT_TRUE(t.width()==4);
EXPECT_TRUE(t.height()==4);
EXPECT_TRUE(t.format()==GL_RGBA);
EXPECT_TRUE(t.getImage().width()==4);
EXPECT_TRUE(t.getImage().height()==4);

}
Binary file added tests/files/Arial.ttf
Binary file not shown.

0 comments on commit 2baa3ca

Please sign in to comment.