Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float texID = 0.0f;
- Uint color;
- };
- enum
- {
- MAX_SPRITES = 60000,
- MAX_ELEMENTS = MAX_SPRITES * 6,
- MAX_TEXTURES = 32,
- SPRITE_SIZE = sizeof(Vertex) * 4,
- };
- enum BUFFER_SIZE
- {
- VERTEX = SPRITE_SIZE * MAX_SPRITES,
- ELEMENT = MAX_ELEMENTS * sizeof(GLuint),
- };
- enum SHADER_OUT
- {
- POSITION = 0,
- COLOR = 1,
- UV = 2,
- TEXTURE = 3
- };
- void Renderer2D::Create()
- {
- vao = vao->Create();
- //create array buffer with dynamic draw
- vbo = vbo->Create(API::BUFFER::ARRAY, API::DRAW::DYNAMIC);
- //create element array buffer with static draw
- ebo = ebo->Create(API::BUFFER::ELEMENT, API::DRAW::STATIC);
- vbo->AddData(BUFFER_SIZE::VERTEX, nullptr);
- ebo->AddData(BUFFER_SIZE::ELEMENT, setIndices());
- vbo->AddAttribute(SHADER_OUT::POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, position));
- vbo->AddAttribute(SHADER_OUT::COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (const void*)offsetof(Vertex, color));
- vbo->AddAttribute(SHADER_OUT::UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, uv));
- vbo->AddAttribute(SHADER_OUT::TEXTURE, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, texID));
- vao->UnbindVertexArray();
- }
- void Renderer2D::Begin()
- {
- //map the buffer
- mappedVertex = vbo->getDataPointer<Vertex>();
- }
- void Renderer2D::DrawString(const std::string& text, const Vector2& position, Uint color, const Font& font)
- {
- Texture* texture = font.getTexture();
- float textureSlot = FindTexture(texture);
- const Vector2& scale = 1.0f;// font.getScale();
- float x = position.x;
- texture_font_t* ftFont = font.getFTFont();
- for (Uint i = 0; i < text.length(); ++i)
- {
- texture_glyph_t* glyph = texture_font_get_glyph(ftFont, text.c_str() + i);
- if (glyph)
- {
- if (i > 0)
- {
- float kerning = texture_glyph_get_kerning(glyph, text.c_str() + i - 1);
- x += kerning / scale.x;
- }
- float x0 = x + glyph->offset_x / scale.x,
- y0 = position.y + glyph->offset_y / scale.y,
- x1 = x0 + glyph->width / scale.x,
- y1 = y0 - glyph->height / scale.y,
- u0 = glyph->s0,
- v0 = glyph->t0,
- u1 = glyph->s1,
- v1 = glyph->t1;
- mappedVertex->position = Vector3(x0, y0, 0);
- mappedVertex->uv = Vector2(u0, v0);
- mappedVertex->texID = textureSlot;
- mappedVertex->color = color;
- mappedVertex++;
- mappedVertex->position = Vector3(x0, y1, 0);
- mappedVertex->uv = Vector2(u0, v1);
- mappedVertex->texID = textureSlot;
- mappedVertex->color = color;
- mappedVertex++;
- mappedVertex->position = Vector3(x1, y1, 0);
- mappedVertex->uv = Vector2(u1, v1);
- mappedVertex->texID = textureSlot;
- mappedVertex->color = color;
- mappedVertex++;
- mappedVertex->position = Vector3(x1, y0, 0);
- mappedVertex->uv = Vector2(u1, v0);
- mappedVertex->texID = textureSlot;
- mappedVertex->color = color;
- mappedVertex++;
- indexCount += 6;
- x += glyph->advance_x;
- }
- }
- }
- void Renderer2D::End()
- {
- //Unmap the buffer
- vbo->releaseDataPointer();
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
- void Renderer2D::Render()
- {
- End();
- for (Uint i = 0; i < texture.size(); ++i)
- {
- //active texture 'i' and bind it
- texture[i]->BindTexture(i);
- }
- vao->BindVertexArray();
- vao->DrawElements(API::ELEMENTS, indexCount);
- vao->UnbindVertexArray();
- for (Uint i = 0; i < texture.size(); ++i)
- {
- //active texture 'i' and bind as a 0
- texture[i]->UnbindTexture(i);
- }
- indexCount = 0;
- texture.clear();
- //glDisable(GL_BLEND);
- }
- Uint* Renderer2D::setIndices()
- {
- Uint* indices = new Uint[BUFFER_SIZE::ELEMENT],
- offset = 0;
- for (int i = 0; i < BUFFER_SIZE::ELEMENT; i += 6)
- {
- indices[i] = offset;
- indices[i + 1] = offset + 1;
- indices[i + 2] = offset + 2;
- indices[i + 3] = offset + 2;
- indices[i + 4] = offset + 3;
- indices[i + 5] = offset;
- offset += 4;
- }
- return indices;
- }
- float Renderer2D::FindTexture(Texture* t)
- {
- float result = 0.0f;
- bool found = false;
- for (Uint i = 0; i < texture.size(); ++i)
- {
- if (texture[i] == t)
- {
- result = (float)(i + 1);
- found = true;
- break;
- }
- }
- if (!found)
- {
- if (texture.size() >= MAX_TEXTURES)
- {
- Present();
- Begin();
- }
- texture.push_back(t);
- result = (float)(texture.size());
- }
- return result;
- }
- Font::Font(const std::string& name, const std::string& filename, float size)
- : name(name), filename(filename), size(size), texture(nullptr)
- {
- FTAtlas = ftgl::texture_atlas_new(512, 512, 1);
- FTFont = ftgl::texture_font_new_from_file(FTAtlas, size, filename.c_str());
- TextureParameters parameters = { TextureFormat::LUMINANCE_ALPHA, TextureFilter::LINEAR, TextureFilter::LINEAR, TextureWrap::CLAMP_TO_EDGE };
- //Creates texture w = 512; h = 512; texImage data = nullptr;
- texture = texture->Create(512, 512, parameters);
- //set texSubImage data to FTAtlas->Data
- texture->setData(FTAtlas->data);
- }
- Texture* Font::getTexture() const
- {
- UpdateAtlas();
- return texture;
- }
- void Font::UpdateAtlas() const
- {
- texture->setData(FTAtlas->data);
- }
- int main(int argc, char* args[])
- {
- Window window;
- window.Create(1280, 720, "window");
- Renderer renderer;
- renderer.Create();
- //Shader stuff
- //Set 32 uniform texture samplers
- //Set mvp
- //Font name, font path, font size
- FontManager::getInstance().AddFont(new Font("font", "font.ttf", 30));
- Event event;
- bool quit = false;
- while(!quit)
- {
- while(PollEvent(event))
- {
- if(event.type == QUIT)
- {
- quit = true;
- }
- }
- renderere.Begin();
- //Color is reversed alpha, blue, green, red
- //getFont() returns font which is named "font"
- renderer.DrawString("TEST", Vector2(0.0f, 0.0f), 0xff00ffff, FontManager::getInstance().getFont(font"));
- renderer.Present();
- window.Swap();
- }
- //Free all if it is needed
- return 0;
- }
- Texture::Texture(Uint width, Uint height, TextureParameters parameters, TextureTarget target)
- : filePath("")
- {
- w = width;
- h = height;
- textureParameters = parameters;
- textureTarget = target;
- texture = LoadFromFile();
- }
- Texture* Texture::Create(uint width, Uint height, TextureParameters parameters, TextureTarget target /*Texture2D default*/)
- {
- return new Texture(width, height, parameters, target);
- }
- Uint Texture::LoadFromFile()
- {
- Uint tex = 0;
- Uint format = textureParameters.format != TextureFormat::NONE ? ConvertFormatToOpenGL(textureParameters.format) : GL_RGBA;
- Uint target = ConvertTargetToOpenGL(textureTarget);
- glCall(glGenTextures(1, &tex));
- glCall(glBindTexture(target, tex));
- glCall(glTexImage2D(target, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, nullptr));
- glCall(glTexParameteri(target, GL_TEXTURE_WRAP_S, ConvertWrapToOpenGL(textureParameters.wrap)));
- glCall(glTexParameteri(target, GL_TEXTURE_WRAP_T, ConvertWrapToOpenGL(textureParameters.wrap)));
- glCall(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, ConvertFilterToOpenGL(textureParameters.filterMAG)));
- glCall(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, ConvertFilterToOpenGL(textureParameters.filterMIN)));
- glCall(glBindTexture(target, 0));
- return tex;
- }
- void Texture::setData(const void* pixels)
- {
- Uint format = textureParameters.format != TextureFormat::NONE ? ConvertFormatToOpenGL(textureParameters.format) : GL_RGBA;
- glCall(glBindTexture(ConvertTargetToOpenGL(textureTarget), texture));
- glCall(glTexSubImage2D(ConvertTargetToOpenGL(textureTarget), 0, 0, 0, w, h, format, GL_UNSIGNED_BYTE, pixels));
- }
- Texture::Texture(Uint tex)
- {
- texture = tex;
- }
- Texture* Texture::Create(Uint tex)
- {
- return new Texture(tex);
- }
- Font::Font(const std::string& name, const std::string& filename, float size)
- : name(name), filename(filename), size(size), texture(nullptr)
- {
- FTAtlas = ftgl::texture_atlas_new(512, 512, 1);
- FTFont = ftgl::texture_font_new_from_file(FTAtlas, size, filename.c_str());
- glCall(glGenTextures(1, &tex));
- glCall(glBindTexture(GL_TEXTURE_2D, tex));
- glCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, FTAtlas->width, FTAtlas->height, 0, GL_RED, GL_UNSIGNED_BYTE, FTAtlas->data));
- glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
- glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
- glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
- glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
- //Set created texture to texture class as if it was a texture class instance that it can be activated in renderer
- texture = texture->Create(tex);
- glCall(glBindTexture(GL_TEXTURE_2D, 0));
- }
- void Font::UpdateAtlas() const
- {
- glBindTexture(GL_TEXTURE_2D, tex);
- glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512, GL_RED, GL_UNSIGNED_BYTE, FTAtlas->data);
- }
- layout (location = 0) in vec2 position;
- layout (location = 1) in vec4 color;
- layout (location = 2) in vec2 uv;
- layout (location = 3) in float texID;
- out vec2 fragmentUV;
- out vec4 fragmentColor;
- out vec2 fragmenPosition;
- out float fragmentTexID;
- layout (location = 1) uniform mat4 mvp;
- void main()
- {
- gl_Position = mvp * vec4(a_position, 0.0, 1.0);
- fragmenPosition = position;
- fragmentColor = color;
- fragmentTexID= texID;
- fragmentUV = vec2(uv.x, 1 - uv.y);
- }
- #version 330 core
- out vec4 finalColor;
- in vec2 fragmentUV;
- in vec4 fragmentColor;
- in vec2 fragmenPosition;
- in float fragmentTexID;
- layout (binding = 0) uniform sampler2D textureSampler[32];
- void main()
- {
- vec4 texColor = fragmentColor;
- if (fragmentTexID > 0.0) {
- int texID = int(fragmentTexID - 0.5);
- texColor = texture(textureSampler[texID], fragmentUV);
- }
- finalColor = texColor * fragmentColor;
- }
Add Comment
Please, Sign In to add comment