store and pass normal values to viewport

This commit is contained in:
parker
2025-07-30 23:34:07 +01:00
parent e72e6bce96
commit afd3559a4d
3 changed files with 59 additions and 40 deletions

View File

@@ -1,6 +1,9 @@
#include "Gui/Viewport/GLMesh.h"
#include <GL/gl.h>
#include <iostream>
#include "Engine/Operator/AttributeHandle.h"
GLMesh::GLMesh()
{
@@ -34,30 +37,35 @@ void GLMesh::initBuffers()
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
// gives the shader a way to read buffer data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
// read normal
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
// disable vertex attrib array
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
}
void GLMesh::setPosBuffer(std::vector<enzo::bt::Vector3> data)
void GLMesh::setPosBuffer(enzo::geo::Geometry& geometry)
{
bind();
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
vertexPosData.clear();
std::cout << "pos data\n-------\n";
for(auto vector : data)
vertices.clear();
std::shared_ptr<enzo::ga::Attribute> PAttr = geometry.getAttribByName(enzo::ga::AttrOwner::POINT, "P");
enzo::ga::AttributeHandleVector3 PAttrHandle = enzo::ga::AttributeHandleVector3(PAttr);
auto pointPositions = PAttrHandle.getAllValues();
for(auto position : pointPositions)
{
vertexPosData.push_back(vector.x());
vertexPosData.push_back(vector.y());
vertexPosData.push_back(vector.z());
std::cout << vector.x() << " " << vector.y() << " " << vector.z() << "\n";
vertices.push_back({{position.x(), position.y(), position.z()}, {position.x(), position.y(), position.z()}});
std::cout << position.x() << " " << position.y() << " " << position.z() << "\n";
}
glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
unbind();
}
@@ -110,7 +118,7 @@ void GLMesh::draw()
bind();
// wireframe
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
// glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
glDrawElements(GL_TRIANGLES, indexData.size(), GL_UNSIGNED_INT, 0);
}

View File

@@ -2,6 +2,14 @@
#include "Engine/Types.h"
#include <GL/gl.h>
#include <QOpenGLFunctions_3_2_Core>
#include <glm/ext/vector_float3.hpp>
#include "Engine/Operator/Geometry.h"
struct Vertex
{
glm::vec3 position;
glm::vec3 normal;
};
class GLMesh
: protected QOpenGLFunctions_3_2_Core
@@ -12,12 +20,13 @@ public:
GLuint vertexBuffer;
GLuint indexBuffer;
std::vector<GLfloat> vertexPosData;
// std::vector<GLfloat> vertexPosData;
std::vector<Vertex> vertices;
std::vector<GLint> indexData;
void init();
void initBuffers();
void setPosBuffer(std::vector<enzo::bt::Vector3> data);
void setPosBuffer(enzo::geo::Geometry& geometry);
void setIndexBuffer(std::vector<int> pointIndices, std::vector<int> primVertexCounts);
void bind();
void unbind();

View File

@@ -39,14 +39,21 @@ void ViewportGLWidget::initializeGL()
// vertex shader
const std::string vertexShaderSource = "#version 330 core\n"
"uniform mat4 uView;\n"
"uniform mat4 uProj;\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = uProj * uView * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\n";
const std::string vertexShaderSource = R"(
#version 330 core
uniform mat4 uView;
uniform mat4 uProj;
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec3 Normal;
void main()
{
Normal = aNormal;
gl_Position = uProj * uView * vec4(aPos, 1.0);
}
)";
// shader type
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
// convert source
@@ -74,12 +81,18 @@ void ViewportGLWidget::initializeGL()
// fragment shader
const std::string fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n";
const std::string fragmentShaderSource = R"(
#version 330 core
in vec3 Normal;
out vec4 FragColor;
void main()
{
// FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
FragColor = vec4(Normal, 1.0f);
}
)";
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* fragmentShaderSourceC = fragmentShaderSource.c_str();
glShaderSource(fragmentShader, 1, &fragmentShaderSourceC, NULL);
@@ -150,26 +163,15 @@ std::unique_ptr<GLMesh> ViewportGLWidget::meshFromGeo(enzo::geo::Geometry& geome
std::shared_ptr<ga::Attribute> PAttr = geometry.getAttribByName(ga::AttrOwner::POINT, "P");
ga::AttributeHandleVector3 PAttrHandle = ga::AttributeHandleVector3(PAttr);
PAttrHandle.addValue(bt::Vector3(1.0f, -1.0f, 0.0f));
PAttrHandle.addValue(bt::Vector3(-1.0f, -1.0f, 0.0f));
PAttrHandle.addValue(bt::Vector3(-1.0f, 1.0f, 0.0f));
PAttrHandle.addValue(bt::Vector3(0.0f, 2.0f, 0.0f));
PAttrHandle.addValue(bt::Vector3(1.0f, 1.0f, 0.0f));
mesh->setPosBuffer(PAttrHandle.getAllValues());
mesh->setPosBuffer(geometry);
std::shared_ptr<ga::Attribute> pointAttr = geometry.getAttribByName(ga::AttrOwner::VERTEX, "point");
ga::AttributeHandleInt pointAttrHandle = ga::AttributeHandleInt(pointAttr);
pointAttrHandle.addValue(0);
pointAttrHandle.addValue(1);
pointAttrHandle.addValue(2);
pointAttrHandle.addValue(3);
pointAttrHandle.addValue(4);
std::shared_ptr<ga::Attribute> vertexCountAttr = geometry.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount");
ga::AttributeHandleInt vertexCountHandle = ga::AttributeHandleInt(vertexCountAttr);
vertexCountHandle.addValue(5);
mesh->setIndexBuffer(pointAttrHandle.getAllValues(), vertexCountHandle.getAllValues());
@@ -184,7 +186,7 @@ void ViewportGLWidget::geometryChanged(enzo::geo::Geometry& geometry)
std::shared_ptr<ga::Attribute> PAttr = geometry.getAttribByName(ga::AttrOwner::POINT, "P");
ga::AttributeHandleVector3 PAttrHandle = ga::AttributeHandleVector3(PAttr);
triangleMesh_->setPosBuffer(PAttrHandle.getAllValues());
triangleMesh_->setPosBuffer(geometry);
std::shared_ptr<ga::Attribute> pointAttr = geometry.getAttribByName(ga::AttrOwner::VERTEX, "point");
ga::AttributeHandleInt pointAttrHandle = ga::AttributeHandleInt(pointAttr);