Clean opengl rendering.
This commit is contained in:
parent
4849d83fcf
commit
798cb365d7
19 changed files with 483 additions and 274 deletions
|
@ -3,15 +3,18 @@
|
|||
#include "DrawingContext.h"
|
||||
#include "DrawingSurface.h"
|
||||
#include "Scene.h"
|
||||
|
||||
#include "TriMesh.h"
|
||||
|
||||
#include "FontsManager.h"
|
||||
#include "FontGlyph.h"
|
||||
|
||||
#include "OpenGlFontTexture.h"
|
||||
#include "OpenGlShaderProgram.h"
|
||||
#include "TextData.h"
|
||||
|
||||
#include "OpenGlShaderProgram.h"
|
||||
#include "OpenGlMeshPainter.h"
|
||||
#include "OpenGlTextPainter.h"
|
||||
#include "OpenGlFontTexture.h"
|
||||
|
||||
#include "File.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
@ -30,218 +33,12 @@
|
|||
#include <iostream>
|
||||
|
||||
OpenGlPainter::OpenGlPainter()
|
||||
: mMeshPainter(std::make_unique<OpenGlMeshPainter>()),
|
||||
mTextPainter(std::make_unique<OpenGlTextPainter>())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OpenGlPainter::initializeMeshShader()
|
||||
{
|
||||
if (auto context = glXGetCurrentContext())
|
||||
{
|
||||
std::cout << "has valid context" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "no valid context" << std::endl;
|
||||
}
|
||||
|
||||
int major_version{0};
|
||||
int minor_version{0};
|
||||
glGetIntegerv(GL_MAJOR_VERSION, &major_version);
|
||||
glGetIntegerv(GL_MINOR_VERSION, &minor_version);
|
||||
std::cout << "Using opengl version " << major_version << "|" << minor_version << std::endl;
|
||||
|
||||
auto vert_shader_path = std::filesystem::path(__FILE__).parent_path() / "shaders/default.vert";
|
||||
auto frag_shader_path = std::filesystem::path(__FILE__).parent_path() / "shaders/default.frag";
|
||||
|
||||
mMeshShaderProgram = std::make_unique<OpenGlShaderProgram>(vert_shader_path, frag_shader_path);
|
||||
}
|
||||
|
||||
void OpenGlPainter::initializeTextShader()
|
||||
{
|
||||
auto vert_shader_path = std::filesystem::path(__FILE__).parent_path() / "shaders/text.vert";
|
||||
auto frag_shader_path = std::filesystem::path(__FILE__).parent_path() / "shaders/text.frag";
|
||||
|
||||
mTextShaderProgram = std::make_unique<OpenGlShaderProgram>(vert_shader_path, frag_shader_path);
|
||||
}
|
||||
|
||||
void OpenGlPainter::renderTextLayer(const TextData& textData, DrawingContext* context)
|
||||
{
|
||||
if (!mTextShaderProgram)
|
||||
{
|
||||
initializeTextShader();
|
||||
}
|
||||
|
||||
for (auto c : textData.mContent)
|
||||
{
|
||||
if (auto iter = mFontTextures.find(c); iter == mFontTextures.end())
|
||||
{
|
||||
auto glyph = context->getFontsManager()->getGlyph(textData.mFont.getFaceName(), textData.mFont.getSize(), c);
|
||||
auto new_texture = std::make_unique<OpenGlFontTexture>(glyph);
|
||||
mFontTextures[c] = std::move(new_texture);
|
||||
}
|
||||
}
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
const auto width = float(context->getSurface()->getWidth());
|
||||
const auto height = float(context->getSurface()->getHeight());
|
||||
|
||||
glm::mat4 projection = glm::ortho(0.0f, width, 0.0f, height);
|
||||
|
||||
unsigned int VAO, VBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glUseProgram(mTextShaderProgram->getHandle());
|
||||
glUniform3f(glGetUniformLocation(mTextShaderProgram->getHandle(), "textColor"), 0.0, 0.0, 0.0);
|
||||
glUniformMatrix4fv(glGetUniformLocation(mTextShaderProgram->getHandle(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
// iterate through all characters
|
||||
float x = textData.mLocation.GetX();
|
||||
const float y = textData.mLocation.GetY();
|
||||
|
||||
for (auto c : textData.mContent)
|
||||
{
|
||||
auto texture = mFontTextures[c].get();
|
||||
|
||||
float xpos = x + texture->getGlyph()->getBearingX();
|
||||
float ypos = y - (texture->getGlyph()->getHeight() - texture->getGlyph()->getBearingY());
|
||||
|
||||
float w = texture->getGlyph()->getWidth();
|
||||
float h = texture->getGlyph()->getHeight();
|
||||
// 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 }
|
||||
};
|
||||
|
||||
// render glyph texture over quad
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getHandle());
|
||||
// update content of VBO memory
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
// render quad
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
// now advance cursors for next glyph (note that advance is number of 1/64 pixels)
|
||||
x += (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64)
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void OpenGlPainter::renderMeshLayer(TriMesh* mesh, DrawingContext* context)
|
||||
{
|
||||
if (!mMeshShaderProgram)
|
||||
{
|
||||
initializeMeshShader();
|
||||
}
|
||||
|
||||
float vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
};
|
||||
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3 // second triangle
|
||||
};
|
||||
unsigned int vbo{0};
|
||||
glGenBuffers(1, &vbo);
|
||||
|
||||
unsigned int EBO;
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
unsigned int VAO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
int vertexColorLocation = glGetUniformLocation(mMeshShaderProgram->getHandle(), "ourColor");
|
||||
|
||||
glUseProgram(mMeshShaderProgram->getHandle());
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glUniform4f(vertexColorLocation, 0.0f, 1.0, 0.0f, 1.0f);
|
||||
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
/*
|
||||
std::cout << "Bounds are : " << width << " | " << height << std::endl;
|
||||
|
||||
for (std::size_t idx=0; idx<num_mesh; idx++)
|
||||
{
|
||||
auto mesh = context->getScene()->getMesh(idx);
|
||||
const auto faces = mesh->getFaceVertices();
|
||||
const auto colors = mesh->getFaceVectorAttributes("Color");
|
||||
|
||||
std::size_t counter{0};
|
||||
for (const auto& face : faces)
|
||||
{
|
||||
const auto r = colors[counter][0];
|
||||
const auto g = colors[counter][1];
|
||||
const auto b = colors[counter][2];
|
||||
|
||||
glColor3f(r, g, b);
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
double x0 = 2.0*face[0].getX()/width - 1.0;
|
||||
double y0 = 1.0 - 2.0*face[0].getY()/height;
|
||||
|
||||
double x1 = 2.0*face[1].getX()/width - 1.0;
|
||||
double y1 = 1.0 - 2.0*face[1].getY()/height;
|
||||
|
||||
double x2 = 2.0*face[2].getX()/width - 1.0;
|
||||
double y2 = 1.0 - 2.0*face[2].getY()/height;
|
||||
|
||||
glVertex3f(x0, y0, 0);
|
||||
glVertex3f(x1, y1, 0);
|
||||
glVertex3f(x2, y2, 0);
|
||||
|
||||
std::cout << "Verts0| " << x0 << " | " << y0 << " | "<< std::endl;
|
||||
std::cout << "Verts1| " << x1 << " | " << y1 << " | "<< std::endl;
|
||||
std::cout << "Verts2| " << x2 << " | " << y2 << " | "<< std::endl;
|
||||
std::cout << "****************" << std::endl;
|
||||
|
||||
glEnd();
|
||||
|
||||
counter++;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void OpenGlPainter::paint(DrawingContext* context)
|
||||
{
|
||||
auto surface = context->getSurface();
|
||||
|
@ -249,24 +46,23 @@ void OpenGlPainter::paint(DrawingContext* context)
|
|||
const auto height = double(surface->getHeight());
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
//glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
|
||||
glOrtho(0, width, 0, height, -1.0, 1.0);
|
||||
|
||||
//glScissor(0, 0, width, height);
|
||||
//glMatrixMode(GL_PROJECTION);
|
||||
//glLoadIdentity();
|
||||
|
||||
glClearColor(0.5, 0.5, 1.0, 0.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
//glMatrixMode(GL_MODELVIEW);
|
||||
//glLoadIdentity();
|
||||
|
||||
const auto num_mesh = context->getScene()->getNumMeshes();
|
||||
//renderMeshLayer(nullptr, context);
|
||||
for (std::size_t idx=0 ; idx< num_mesh; idx++)
|
||||
{
|
||||
auto mesh = context->getScene()->getMesh(idx);
|
||||
mMeshPainter->paint(mesh, context);
|
||||
}
|
||||
|
||||
auto text_data = context->getScene()->getTextData();
|
||||
renderTextLayer(text_data[0], context);
|
||||
|
||||
for (const auto& text_item : context->getScene()->getTextData())
|
||||
{
|
||||
mTextPainter->paint(text_item, context);
|
||||
break;
|
||||
}
|
||||
glFlush();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue