Clean opengl rendering.

This commit is contained in:
James Grogan 2022-11-16 09:39:05 +00:00
parent 4849d83fcf
commit 798cb365d7
19 changed files with 483 additions and 274 deletions

View file

@ -7,12 +7,14 @@ list(APPEND graphics_LIB_INCLUDES
DrawingContext.cpp
DrawingSurface.cpp
RasterPainter.cpp
PainterFactory.cpp
${platform_LIB_INCLUDES}
)
list(APPEND graphics_HEADERS
${platform_HEADERS}
RasterPainter.h
PainterFactory.h
)
set(OpenGL_GL_PREFERENCE "GLVND")
@ -21,10 +23,15 @@ if (OpenGL_FOUND)
list(APPEND platform_LIBS OpenGL::GL OpenGL::GLU)
list(APPEND graphics_LIB_INCLUDES
opengl/OpenGlPainter.cpp
opengl/OpenGlTextPainter.cpp
opengl/OpenGlMeshPainter.cpp
opengl/OpenGlFontTexture.cpp
opengl/OpenGlShaderProgram.cpp)
opengl/OpenGlShaderProgram.cpp
)
list(APPEND graphics_HEADERS
opengl/OpenGlPainter.h
opengl/OpenGlTextPainter.h
opengl/OpenGlMeshPainter.h
opengl/OpenGlFontTexture.h
opengl/OpenGlShaderProgram.h)
else()

View file

@ -1,10 +1,8 @@
#include "DrawingContext.h"
#include "AbstractPainter.h"
#include "OpenGlPainter.h"
#include "OpenGlShaderProgram.h"
#include "OpenGlFontTexture.h"
#include "RasterPainter.h"
#include "PainterFactory.h"
#include "Grid.h"
#include "TriMesh.h"
@ -17,14 +15,7 @@ DrawingContext::DrawingContext(DrawingSurface* surface, FontsManager* fontsManag
mScene(std::make_unique<Scene>()),
mFontsManager(fontsManager)
{
if (mDrawingMode == DrawingMode::GRAPH)
{
mPainter = std::make_unique<OpenGlPainter>();
}
else
{
mPainter = std::make_unique<RasterPainter>();
}
mPainter = PainterFactory::Create(mDrawingMode);
}
std::unique_ptr<DrawingContext> DrawingContext::Create(DrawingSurface* surface, FontsManager* fontsManager, DrawingMode requestedDrawingMode)

View file

@ -0,0 +1,27 @@
#include "PainterFactory.h"
#include "OpenGlPainter.h"
#include "OpenGlMeshPainter.h"
#include "OpenGlTextPainter.h"
#include "OpenGlShaderProgram.h"
#include "OpenGlFontTexture.h"
#include "Grid.h"
#include "RasterPainter.h"
#include "DrawingContext.h"
std::unique_ptr<AbstractPainter> PainterFactory::Create(DrawingMode drawMode)
{
if (drawMode == DrawingMode::GRAPH)
{
return std::make_unique<OpenGlPainter>();
}
else
{
return std::make_unique<RasterPainter>();
}
}

View file

@ -0,0 +1,13 @@
#pragma once
#include "AbstractPainter.h"
#include <memory>
enum class DrawingMode;
class PainterFactory
{
public:
static std::unique_ptr<AbstractPainter> Create(DrawingMode drawMode);
};

View file

@ -0,0 +1,111 @@
#include "OpenGlMeshPainter.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "TriMesh.h"
#include "OpenGlShaderProgram.h"
#ifdef _WIN32
#include <windows.h>
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
OpenGlMeshPainter::OpenGlMeshPainter()
{
}
void OpenGlMeshPainter::initializeShader()
{
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";
mShaderProgram = std::make_unique<OpenGlShaderProgram>(vert_shader_path, frag_shader_path);
}
void OpenGlMeshPainter::initializeBuffers()
{
glGenBuffers(1, &mVertexBuffer);
glGenBuffers(1, &mElementBuffer);
glGenVertexArrays(1, &mVertexArray);
}
void OpenGlMeshPainter::paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color)
{
glBindVertexArray(mVertexArray);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(float), verts.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned), elements.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//glm::mat4 projection = glm::ortho(0.0f, width, 0.0f, height);
//glUniformMatrix4fv(glGetUniformLocation(mShaderProgram->getHandle(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUseProgram(mShaderProgram->getHandle());
glBindVertexArray(mVertexArray);
int vertexColorLocation = glGetUniformLocation(mShaderProgram->getHandle(), "ourColor");
glUniform4f(vertexColorLocation, float(color[0]), float(color[1]), float(color[2]), float(color[3]));
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void OpenGlMeshPainter::paint(TriMesh* mesh, DrawingContext* context)
{
if (!mShaderProgram)
{
initializeShader();
}
if (mVertexArray == 0)
{
initializeBuffers();
}
auto surface = context->getSurface();
const auto width = float(surface->getWidth());
const auto height = float(surface->getHeight());
auto vertices = mesh->getVerticesFlat<float>();
for (std::size_t idx = 0; idx<vertices.size(); idx++)
{
if (idx % 3 == 0)
{
vertices[idx] = 2*vertices[idx]/width - 1.0;
}
else if(idx%3 == 1)
{
vertices[idx] = 1.0 - 2*vertices[idx]/height;
}
}
const auto indices = mesh->getFaceNodeIds();
std::vector<float> color(4, 1.0f);
if (mesh->hasVectorAttribute("Color"))
{
auto mesh_color = mesh->getVectorAttribute("Color");
color = {float(mesh_color[0]), float(mesh_color[1]), float(mesh_color[2]), float(mesh_color[3])};
}
paint(vertices, indices, color);
}

View file

@ -0,0 +1,27 @@
#pragma once
#include <memory>
#include <vector>
class DrawingContext;
class OpenGlShaderProgram;
class TriMesh;
class OpenGlMeshPainter
{
public:
OpenGlMeshPainter();
void paint(TriMesh* mesh, DrawingContext* context);
private:
void initializeShader();
void initializeBuffers();
void paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color);
unsigned mVertexBuffer{0};
unsigned mElementBuffer{0};
unsigned mVertexArray{0};
std::unique_ptr<OpenGlShaderProgram> mShaderProgram;
};

View file

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

View file

@ -6,29 +6,20 @@
#include <memory>
class DrawingContext;
class OpenGlFontTexture;
class OpenGlShaderProgram;
class OpenGlMeshPainter;
class OpenGlTextPainter;
class TextData;
class TriMesh;
class OpenGlPainter : public AbstractPainter
{
public:
OpenGlPainter();
void paint(DrawingContext* context) override;
private:
void initializeMeshShader();
void initializeTextShader();
void renderTextLayer(const TextData& textData, DrawingContext* context);
void renderMeshLayer(TriMesh* mesh, DrawingContext* context);
std::unique_ptr<OpenGlShaderProgram> mMeshShaderProgram;
std::unique_ptr<OpenGlShaderProgram> mTextShaderProgram;
std::unordered_map<char, std::unique_ptr<OpenGlFontTexture> > mFontTextures;
std::unique_ptr<OpenGlMeshPainter> mMeshPainter;
std::unique_ptr<OpenGlTextPainter> mTextPainter;
};

View file

@ -0,0 +1,134 @@
#include "OpenGlTextPainter.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "FontsManager.h"
#include "FontGlyph.h"
#include "OpenGlFontTexture.h"
#include "OpenGlShaderProgram.h"
#include "TextData.h"
#include "File.h"
#ifdef _WIN32
#include <windows.h>
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
OpenGlTextPainter::OpenGlTextPainter()
{
}
void OpenGlTextPainter::initializeShader()
{
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";
mShaderProgram = std::make_unique<OpenGlShaderProgram>(vert_shader_path, frag_shader_path);
}
void OpenGlTextPainter::initializeTextures(const TextData& textData, DrawingContext* context)
{
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);
}
}
}
void OpenGlTextPainter::initializeBuffers()
{
glGenVertexArrays(1, &mVertexArray);
glGenBuffers(1, &mVertexBuffer);
glBindVertexArray(mVertexArray);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, nullptr, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void OpenGlTextPainter::paint(const TextData& textData, DrawingContext* context)
{
if (!mShaderProgram)
{
initializeShader();
}
if (mVertexArray == 0)
{
initializeBuffers();
}
initializeTextures(textData, context);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(mShaderProgram->getHandle());
glUniform3f(glGetUniformLocation(mShaderProgram->getHandle(), "textColor"), 0.0, 0.0, 0.0);
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);
glUniformMatrix4fv(glGetUniformLocation(mShaderProgram->getHandle(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(mVertexArray);
float x = textData.mLocation.GetX();
const float y = height - 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();
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 }
};
glBindTexture(GL_TEXTURE_2D, texture->getHandle());
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <unordered_map>
#include <memory>
class DrawingContext;
class OpenGlFontTexture;
class OpenGlShaderProgram;
class TextData;
class OpenGlTextPainter
{
public:
OpenGlTextPainter();
void paint(const TextData& textData, DrawingContext* context);
private:
void initializeShader();
void initializeTextures(const TextData& textData, DrawingContext* context);
void initializeBuffers();
unsigned mVertexBuffer{0};
unsigned mVertexArray{0};
std::unique_ptr<OpenGlShaderProgram> mShaderProgram;
std::unordered_map<char, std::unique_ptr<OpenGlFontTexture> > mFontTextures;
};

View file

@ -1,10 +1,7 @@
#version 330 core
layout (location = 0) in vec3 aPos; // the position variable has attribute position 0
layout (location = 0) in vec3 aPos;
out vec4 vertexColor; // specify a color output to the fragment shader
void main()
{
gl_Position = vec4(aPos, 1.0); // see how we directly give a vec3 to vec4's constructor
vertexColor = vec4(aPos.x, aPos.y, 0.0, 1.0); // set the output variable to a dark-red color
gl_Position = vec4(aPos, 1.0);
}