Use geometry shader for widgets.

This commit is contained in:
2021-07-11 14:47:39 +02:00
parent 2305180272
commit 87c60187bf
11 changed files with 90 additions and 48 deletions

View File

@@ -23,7 +23,8 @@
{
"unique_name": "menuProgram",
"vertexPath": "data/shaders/menu.vert",
"fragmentPath": "data/shaders/menu.frag"
"fragmentPath": "data/shaders/menu.frag",
"geometryPath": "data/shaders/menu.geom"
},
{
"unique_name": "directionalShadowDepthProgram",

View File

@@ -13,4 +13,5 @@ uniform Material u_material;
void main()
{
f_color = texture(u_material.texture_diffuse0, v_texCoord);
// f_color = vec4(1.0, 0.0, 0.0, 0.5);
}

41
data/shaders/menu.geom Normal file
View File

@@ -0,0 +1,41 @@
#version 330 core
layout(points) in;
layout(triangle_strip, max_vertices = 4) out;
struct WidgetData
{
vec2 position;
vec2 dimensions;
};
uniform WidgetData u_widgetData;
out vec2 v_texCoord;
void main()
{
float glWidth = u_widgetData.dimensions.x * 2.0f;
float glHeight = u_widgetData.dimensions.y * 2.0f;
vec4 offset = vec4(0.0, 0.0, 0.0, 0.0);
gl_Position = gl_in[0].gl_Position + offset;
v_texCoord = vec2(0.0, 0.0);
EmitVertex();
offset = vec4(glWidth, 0.0, 0.0, 0.0);
gl_Position = gl_in[0].gl_Position + offset;
v_texCoord = vec2(1.0, 0.0);
EmitVertex();
offset = vec4(0.0, glHeight, 0.0, 0.0);
gl_Position = gl_in[0].gl_Position + offset;
v_texCoord = vec2(0.0, 1.0);
EmitVertex();
offset = vec4(glWidth, glHeight, 0.0, 0.0);
gl_Position = gl_in[0].gl_Position + offset;
v_texCoord = vec2(1.0, 1.0);
EmitVertex();
EndPrimitive();
}

View File

@@ -1,12 +1,16 @@
#version 330 core
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec2 a_texCoord;
out vec2 v_texCoord;
struct WidgetData
{
vec2 position;
vec2 dimensions;
};
uniform WidgetData u_widgetData;
void main()
{
v_texCoord = a_texCoord;
gl_Position = vec4(a_position, 1.0);
float glPosX = u_widgetData.position.x * 2.0f - 1.0f;
float glPosY = u_widgetData.position.y * 2.0f - 1.0f;
gl_Position = vec4(glPosX, glPosY, 0, 1);
}

View File

@@ -2,10 +2,11 @@
out vec2 v_tex_coords;
void main() {
void main()
{
float x = -1.0 + float((gl_VertexID & 1) << 2);
float y = -1.0 + float((gl_VertexID & 2) << 1);
v_tex_coords.x = (x+1.0)*0.5;
v_tex_coords.y = (y+1.0)*0.5;
v_tex_coords.x = (x + 1.0) * 0.5;
v_tex_coords.y = (y + 1.0) * 0.5;
gl_Position = vec4(x, y, 0, 1);
}

View File

@@ -41,6 +41,7 @@ Controller::Controller() : m_gameWindow(std::unique_ptr<Window>(new Window))
for (auto &prototype : shaderProgramPrototypes) {
m_shaderPrograms.push_back(new ShaderProgram(prototype));
std::cout << "Loaded ShaderProgram \"" << prototype.name << "\"" << std::endl;
}
m_postProcessFrameBuffer = new FrameBuffer(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight(),

View File

@@ -1,11 +1,4 @@
#include "JsonParser.h"
#include "Entity.h"
#include "Light.h"
#include "Model.h"
#include "Screen.h"
#include "ShaderProgram.h"
#include "Texture.h"
#include "Widget.h"
#include <fstream>
#include <iostream>
@@ -103,7 +96,6 @@ std::vector<ShaderProgram::Prototype> JsonParser::getShaderProgramPrototypes()
shaderProgram_geometryPath};
prototypes.push_back(prototype);
// std::cout << "Loaded ShaderProgram \"" << shaderProgram_name << "\"" << std::endl;
}
return prototypes;

View File

@@ -128,6 +128,12 @@ void ShaderProgram::setUniform(const std::string &name, float value) const
glUniform1f(location, value);
}
void ShaderProgram::setUniform(const std::string &name, glm::vec2 vector) const
{
GLint location = retrieveUniformLocation(name);
glUniform2f(location, vector.x, vector.y);
}
void ShaderProgram::setUniform(const std::string &name, glm::vec3 vector) const
{
GLint location = retrieveUniformLocation(name);

View File

@@ -28,6 +28,7 @@ public:
void setUniform(const std::string &name, bool value) const;
void setUniform(const std::string &name, int value) const;
void setUniform(const std::string &name, float value) const;
void setUniform(const std::string &name, glm::vec2 vector) const;
void setUniform(const std::string &name, glm::vec3 vector) const;
void setUniform(const std::string &name, glm::mat3 matrix) const;
void setUniform(const std::string &name, glm::mat4 matrix) const;

View File

@@ -12,35 +12,12 @@ Widget::Widget(Prototype prototype, Texture *texture)
m_callbackId(prototype.callBackId)
{
m_widgetTextures.push_back(texture);
double widgetVerticesData[12] = {
2 * (m_position.x + m_dimensions.x) - 1.0f,
2 * (m_position.y) - 1.0f,
0.0f, // Bottom right
2 * (m_position.x) - 1.0f,
2 * (m_position.y + m_dimensions.y) - 1.0f,
0.0f, // Top left
2 * (m_position.x) - 1.0f,
2 * (m_position.y) - 1.0f,
0.0f, // Bottom left
2 * (m_position.x + m_dimensions.x) - 1.0f,
2 * (m_position.y + m_dimensions.y) - 1.0f,
0.0f // Top right
};
unsigned int widgetIndicesData[6] = {0, 1, 2, 0, 3, 1};
float widgetTextureCoordinates[8] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f};
m_widgetVertices = VertexArray::createVertices(widgetVerticesData, 12, widgetTextureCoordinates);
m_widgetIndices.assign(widgetIndicesData, widgetIndicesData + 6);
m_widgetMesh = new Mesh(m_widgetVertices, m_widgetIndices, m_widgetTextures);
m_widgetMesh->initializeOnGPU();
}
Widget::~Widget()
{
delete m_widgetMesh;
for (auto &texture : m_widgetTextures)
delete texture;
}
std::string Widget::getUniqueName()
@@ -56,7 +33,28 @@ uint16_t Widget::getCallbackId()
void Widget::draw(ShaderProgram *shaderProgram)
{
shaderProgram->bind();
m_widgetMesh->draw(shaderProgram);
shaderProgram->setUniform("u_widgetData.position", m_position);
shaderProgram->setUniform("u_widgetData.dimensions", m_dimensions);
GLint wireframe;
glGetIntegerv(GL_POLYGON_MODE, &wireframe);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_widgetTextures[0]->getTextureId());
GLint location = glGetUniformLocation(shaderProgram->getShaderProgramId(), "u_texture");
glUniform1i(location, 0);
// A VAO is necessary although no data is stored in it
GLuint temp_vao;
glGenVertexArrays(1, &temp_vao);
glBindVertexArray(temp_vao);
glDrawArrays(GL_POINTS, 0, 3);
glBindVertexArray(0);
glPolygonMode(GL_FRONT_AND_BACK, wireframe);
shaderProgram->unbind();
}

View File

@@ -43,9 +43,5 @@ private:
uint16_t m_callbackId;
std::vector<Vertex> m_widgetVertices;
std::vector<uint32_t> m_widgetIndices;
std::vector<Texture *> m_widgetTextures;
Mesh *m_widgetMesh;
};