refactor: fix file case
This commit is contained in:
141
src/Gui/Viewport/GLCamera.cpp
Normal file
141
src/Gui/Viewport/GLCamera.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include "Gui/Viewport/GLCamera.h"
|
||||
#include <glm/ext/quaternion_geometric.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/geometric.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <iostream>
|
||||
#include <glm/ext/quaternion_trigonometric.hpp>
|
||||
#include <glm/ext/quaternion_float.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <QOpenGLFunctions>
|
||||
|
||||
void printMatrix(const glm::mat4& matrix) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
std::cout << matrix[i][j] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
void printVec(const glm::vec3& vector) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
std::cout << vector[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void GLCamera::changeCenter(float x, float y, float z)
|
||||
{
|
||||
camCenter_.x += x;
|
||||
camCenter_.y += y;
|
||||
camCenter_.z += z;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
}
|
||||
|
||||
void GLCamera::setCenter(float x, float y, float z)
|
||||
{
|
||||
camCenter_.x = x;
|
||||
camCenter_.y = y;
|
||||
camCenter_.z = z;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
}
|
||||
|
||||
glm::vec3 GLCamera::getUp()
|
||||
{
|
||||
return glm::normalize(glm::cross(getForward(), getRight()));
|
||||
}
|
||||
|
||||
|
||||
GLCamera::GLCamera()
|
||||
: GLCamera(0.0f, 0.0f, 10.0f)
|
||||
{
|
||||
}
|
||||
|
||||
GLCamera::GLCamera(float posX, float posY, float posZ)
|
||||
{
|
||||
setPos(posX, posY, posZ);
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
glm::mat4 GLCamera::getViewMatrix()
|
||||
{
|
||||
return viewMatrix_;
|
||||
}
|
||||
|
||||
void GLCamera::setPos(float x, float y, float z)
|
||||
{
|
||||
camPos_.x = x;
|
||||
camPos_.y = y;
|
||||
camPos_.z = z;
|
||||
}
|
||||
|
||||
void GLCamera::movePos(float x, float y, float z)
|
||||
{
|
||||
camPos_.x += x;
|
||||
camPos_.y += y;
|
||||
camPos_.z += z;
|
||||
}
|
||||
|
||||
|
||||
void GLCamera::rotateAroundCenter(float angle, glm::vec3 axis)
|
||||
{
|
||||
glm::mat4 rotMatrix = glm::rotate(glm::mat4(1.0f), angle, axis);
|
||||
|
||||
axis = glm::normalize(axis);
|
||||
// build a quaternion for this rotation
|
||||
glm::quat q = glm::angleAxis(angle, axis);
|
||||
// rotate the position vector
|
||||
camPos_ = q * camPos_;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
void GLCamera::changeRadius(float delta)
|
||||
{
|
||||
glm::vec3 centerDir = glm::normalize(camPos_-camCenter_);
|
||||
camPos_+=centerDir*delta;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
glm::vec3 GLCamera::getRight()
|
||||
{
|
||||
return glm::cross(getForward(), camUp_);
|
||||
};
|
||||
|
||||
glm::vec3 GLCamera::getForward()
|
||||
{
|
||||
return glm::normalize(camCenter_-camPos_);
|
||||
};
|
||||
|
||||
void GLCamera::setUniform(uint uniformLocation)
|
||||
{
|
||||
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
|
||||
f->glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(getViewMatrix()));
|
||||
}
|
||||
29
src/Gui/Viewport/GLCamera.h
Normal file
29
src/Gui/Viewport/GLCamera.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class GLCamera
|
||||
{
|
||||
public:
|
||||
GLCamera();
|
||||
GLCamera(float posX, float posY, float posZ);
|
||||
glm::mat4 getViewMatrix();
|
||||
void setPos(float x, float y, float z);
|
||||
void movePos(float x, float y, float z);
|
||||
void rotateAroundCenter(float angle, glm::vec3 axis);
|
||||
void changeRadius(float delta);
|
||||
void changeCenter(float x, float y, float z);
|
||||
void setCenter(float x, float y, float z);
|
||||
void setUniform(uint uniformLocation);
|
||||
|
||||
glm::vec3 getForward();
|
||||
glm::vec3 getRight();
|
||||
glm::vec3 getUp();
|
||||
|
||||
|
||||
private:
|
||||
glm::mat4 viewMatrix_{1.0f};
|
||||
glm::vec3 camPos_{0.0f,0.0f,0.0f};
|
||||
glm::vec3 camCenter_{0.0f,0.0f,0.0f};
|
||||
glm::vec3 camUp_{0.0f,1.0f,0.0f};
|
||||
};
|
||||
151
src/Gui/Viewport/GLGrid.cpp
Normal file
151
src/Gui/Viewport/GLGrid.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
#include "Gui/Viewport/GLGrid.h"
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <iostream>
|
||||
|
||||
GLGrid::GLGrid()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
init();
|
||||
}
|
||||
|
||||
void GLGrid::init()
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
initBuffers();
|
||||
initShaderProgram();
|
||||
|
||||
// unbind vertex array
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void GLGrid::initShaderProgram()
|
||||
{
|
||||
// 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"
|
||||
"out vec4 position;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||
" gl_Position = uProj * uView * position;\n"
|
||||
"}\n";
|
||||
// shader type
|
||||
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
// convert source
|
||||
const GLchar* vertexShaderSourceC = vertexShaderSource.c_str();
|
||||
// create shader object
|
||||
glShaderSource(vertexShader, 1, &vertexShaderSourceC, NULL);
|
||||
// compile shader object
|
||||
glCompileShader(vertexShader);
|
||||
|
||||
|
||||
// log shader error
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if(!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "success\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
// fragment shader
|
||||
const std::string fragmentShaderSource = "#version 330 core\n"
|
||||
"float remap(float value, float inMin, float inMax, float outMin, float outMax) {\n"
|
||||
" return outMin + (value - inMin) * (outMax - outMin) / (inMax - inMin);\n"
|
||||
"}\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"in vec4 position;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" float distance = distance(position, vec4(0.0f,0.0f,0.0f,1.0f));\n"
|
||||
" FragColor = vec4(0.53f, 0.53f, 0.53f, 0.2f*remap(distance, 0, 40, 1, 0));\n"
|
||||
"}\n";
|
||||
|
||||
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
const GLchar* fragmentShaderSourceC = fragmentShaderSource.c_str();
|
||||
glShaderSource(fragmentShader, 1, &fragmentShaderSourceC, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
if(!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "success\n";
|
||||
|
||||
}
|
||||
|
||||
// create shader program
|
||||
shaderProgram = glCreateProgram();
|
||||
// attach shaders
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
// link program
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
// delete shaders now that the program is complete
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
|
||||
void GLGrid::initBuffers()
|
||||
{
|
||||
constexpr int gridLen = 50;
|
||||
constexpr int gridLines = 40;
|
||||
float halfLinesCnt = (gridLines-1)*0.5f;
|
||||
for(int i=0; i<gridLines; ++i)
|
||||
{
|
||||
vertices.push_back(glm::vec3((i-halfLinesCnt)*2,0,-gridLen));
|
||||
vertices.push_back(glm::vec3((i-halfLinesCnt)*2,0,gridLen));
|
||||
|
||||
vertices.push_back(glm::vec3(-gridLen,0,(i-halfLinesCnt)*2));
|
||||
vertices.push_back(glm::vec3(gridLen,0,(i-halfLinesCnt)*2));
|
||||
|
||||
}
|
||||
|
||||
// create buffer of vertices
|
||||
glGenBuffers(1, &vbo);
|
||||
// set purpose
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
// store data in the buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
|
||||
|
||||
// gives the shader a way to read
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (void*)0);
|
||||
// disable vertex attrib array
|
||||
glEnableVertexAttribArray(0);
|
||||
}
|
||||
|
||||
void GLGrid::bind()
|
||||
{
|
||||
glBindVertexArray(vao);
|
||||
}
|
||||
|
||||
void GLGrid::unbind()
|
||||
{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void GLGrid::useProgram()
|
||||
{
|
||||
glUseProgram(shaderProgram);
|
||||
}
|
||||
|
||||
void GLGrid::draw()
|
||||
{
|
||||
bind();
|
||||
useProgram();
|
||||
glDrawArrays(GL_LINES, 0, vertices.size());
|
||||
}
|
||||
22
src/Gui/Viewport/GLGrid.h
Normal file
22
src/Gui/Viewport/GLGrid.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
|
||||
class GLGrid
|
||||
: protected QOpenGLFunctions_3_2_Core
|
||||
{
|
||||
public:
|
||||
GLGrid();
|
||||
GLuint vao;
|
||||
GLuint vbo;
|
||||
GLuint shaderProgram;
|
||||
std::vector<glm::vec3> vertices;
|
||||
|
||||
void init();
|
||||
void initBuffers();
|
||||
void initShaderProgram();
|
||||
void useProgram();
|
||||
void bind();
|
||||
void unbind();
|
||||
void draw();
|
||||
};
|
||||
116
src/Gui/Viewport/GLMesh.cpp
Normal file
116
src/Gui/Viewport/GLMesh.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "Gui/Viewport/GLMesh.h"
|
||||
#include <GL/gl.h>
|
||||
#include <iostream>
|
||||
|
||||
GLMesh::GLMesh()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
init();
|
||||
}
|
||||
|
||||
void GLMesh::init()
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
initBuffers();
|
||||
|
||||
|
||||
// store data in the buffer
|
||||
// glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW);
|
||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size()*sizeof(GLint), indexData.data(), GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// unbind vertex array
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void GLMesh::initBuffers()
|
||||
{
|
||||
|
||||
// create buffer of vertices
|
||||
glGenBuffers(1, &vertexBuffer);
|
||||
// set purpose
|
||||
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);
|
||||
// disable vertex attrib array
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glGenBuffers(1, &indexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
|
||||
|
||||
}
|
||||
|
||||
void GLMesh::setPosBuffer(std::vector<enzo::bt::Vector3> data)
|
||||
{
|
||||
bind();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||||
vertexPosData.clear();
|
||||
std::cout << "pos data\n-------\n";
|
||||
for(auto vector : data)
|
||||
{
|
||||
vertexPosData.push_back(vector.x());
|
||||
vertexPosData.push_back(vector.y());
|
||||
vertexPosData.push_back(vector.z());
|
||||
std::cout << vector.x() << " " << vector.y() << " " << vector.z() << "\n";
|
||||
}
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW);
|
||||
unbind();
|
||||
}
|
||||
|
||||
void GLMesh::setIndexBuffer(std::vector<int> pointIndices, std::vector<int> primVertexCounts)
|
||||
{
|
||||
bind();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
|
||||
indexData.clear();
|
||||
std::cout << "index pointIndices\n-------\n";
|
||||
size_t startIndex = 1;
|
||||
|
||||
// create triangle fan from potentially ngon inputs
|
||||
std::cout << "size: " << primVertexCounts.size() << "\n";
|
||||
for(size_t primNum=0; primNum<primVertexCounts.size(); ++primNum)
|
||||
{
|
||||
int primVertexCount = primVertexCounts[primNum];
|
||||
|
||||
std::cout << "startIndex: " << startIndex << "\n";
|
||||
std::cout << "prim vertex count: " << primVertexCount << "\n";
|
||||
|
||||
for(size_t pointIndex=startIndex; pointIndex+2<startIndex+primVertexCount; ++pointIndex)
|
||||
{
|
||||
indexData.push_back(pointIndices.at(startIndex-1));
|
||||
indexData.push_back(pointIndices.at(pointIndex));
|
||||
indexData.push_back(pointIndices.at(pointIndex+1));
|
||||
|
||||
std::cout << pointIndices.at(0) << " " << pointIndices.at(pointIndex) << " " << pointIndices.at(pointIndex+1) << "\n";
|
||||
}
|
||||
|
||||
startIndex += primVertexCount;
|
||||
}
|
||||
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size()*sizeof(GLint), indexData.data(), GL_STATIC_DRAW);
|
||||
unbind();
|
||||
}
|
||||
|
||||
|
||||
void GLMesh::bind()
|
||||
{
|
||||
glBindVertexArray(vao);
|
||||
}
|
||||
|
||||
void GLMesh::unbind()
|
||||
{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void GLMesh::draw()
|
||||
{
|
||||
bind();
|
||||
|
||||
// wireframe
|
||||
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
|
||||
|
||||
glDrawElements(GL_TRIANGLES, indexData.size(), GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
25
src/Gui/Viewport/GLMesh.h
Normal file
25
src/Gui/Viewport/GLMesh.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "Engine/Types.h"
|
||||
#include <GL/gl.h>
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
|
||||
class GLMesh
|
||||
: protected QOpenGLFunctions_3_2_Core
|
||||
{
|
||||
public:
|
||||
GLMesh();
|
||||
GLuint vao;
|
||||
GLuint vertexBuffer;
|
||||
GLuint indexBuffer;
|
||||
|
||||
std::vector<GLfloat> vertexPosData;
|
||||
std::vector<GLint> indexData;
|
||||
|
||||
void init();
|
||||
void initBuffers();
|
||||
void setPosBuffer(std::vector<enzo::bt::Vector3> data);
|
||||
void setIndexBuffer(std::vector<int> pointIndices, std::vector<int> primVertexCounts);
|
||||
void bind();
|
||||
void unbind();
|
||||
void draw();
|
||||
};
|
||||
141
src/Gui/Viewport/Viewport.cpp
Normal file
141
src/Gui/Viewport/Viewport.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include "Gui/Viewport/Viewport.h"
|
||||
#include "Gui/Viewport/GLCamera.h"
|
||||
#include <glm/common.hpp>
|
||||
#include <qboxlayout.h>
|
||||
#include <qevent.h>
|
||||
#include <qnamespace.h>
|
||||
#include <qpushbutton.h>
|
||||
#include <iostream>
|
||||
#include <QTimer>
|
||||
#include <QPainterPath>
|
||||
#include <QEvent>
|
||||
|
||||
Viewport::Viewport(QWidget *parent, Qt::WindowFlags f)
|
||||
: QWidget(parent, f)
|
||||
{
|
||||
mainLayout_=new QVBoxLayout();
|
||||
openGLWidget_ = new ViewportGLWidget(this); mainLayout_->addWidget(openGLWidget_);
|
||||
openGLWidget_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
// mainLayout_->addWidget(new QPushButton("hello world"));
|
||||
this->setLayout(mainLayout_);
|
||||
// this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
// QTimer::singleShot(100, this, [=] {
|
||||
// std::cout << "MyGLWidget size: " << openGLWidget_->width() << " x " << openGLWidget_->height() << std::endl;
|
||||
// });
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Viewport::geometryChanged(enzo::geo::Geometry& geometry)
|
||||
{
|
||||
openGLWidget_->geometryChanged(geometry);
|
||||
}
|
||||
|
||||
void Viewport::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QPainterPath path;
|
||||
constexpr float radius = 10;
|
||||
path.addRoundedRect(mainLayout_->contentsRect(), radius, radius);
|
||||
QRegion region = QRegion(path.toFillPolygon().toPolygon());
|
||||
this->setMask(region);
|
||||
}
|
||||
|
||||
bool Viewport::event(QEvent *event)
|
||||
{
|
||||
switch(event->type())
|
||||
{
|
||||
case QEvent::Wheel:
|
||||
case QEvent::MouseMove:
|
||||
case QEvent::MouseButtonPress:
|
||||
case QEvent::MouseButtonRelease:
|
||||
handleCamera(event);
|
||||
event->ignore();
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
return QWidget::event(event);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Viewport::handleCamera(QEvent *event)
|
||||
{
|
||||
switch(event->type())
|
||||
{
|
||||
case QEvent::Wheel:
|
||||
{
|
||||
QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
|
||||
float delta = wheelEvent->angleDelta().y();
|
||||
constexpr float mouseSpeed = 0.7;
|
||||
openGLWidget_->curCamera.changeRadius(-glm::sign(delta)*mouseSpeed);
|
||||
break;
|
||||
}
|
||||
case QEvent::MouseMove:
|
||||
{
|
||||
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
|
||||
QPointF mousePos = mouseEvent->position();
|
||||
GLCamera& camera = openGLWidget_->curCamera;
|
||||
constexpr float rotateSpeed = 0.01;
|
||||
constexpr float panSpeed = 0.01;
|
||||
constexpr float zoomSpeed = 0.01;
|
||||
|
||||
if(leftMouseDown_)
|
||||
{
|
||||
QPointF delta = mousePos-leftStartPos_;
|
||||
delta*=-rotateSpeed;
|
||||
camera.rotateAroundCenter(delta.x(), {0,1,0});
|
||||
camera.rotateAroundCenter(delta.y(),
|
||||
camera.getRight() * glm::vec3(1.0f,0.0f,1.0f));
|
||||
leftStartPos_=mousePos;
|
||||
}
|
||||
if(middleMouseDown_)
|
||||
{
|
||||
QPointF delta = mousePos-middleStartPos_;
|
||||
delta *= panSpeed;
|
||||
glm::vec3 up = camera.getUp()*static_cast<float>(-delta.y());
|
||||
glm::vec3 right = camera.getRight()*static_cast<float>(-delta.x());
|
||||
|
||||
camera.changeCenter(up.x+right.x, up.y+right.y, up.z+right.z);
|
||||
camera.movePos(up.x+right.x, up.y+right.y, up.z+right.z);
|
||||
middleStartPos_=mousePos;
|
||||
}
|
||||
if(rightMouseDown_)
|
||||
{
|
||||
QPointF delta = mousePos-rightStartPos_;
|
||||
delta*=zoomSpeed;
|
||||
camera.changeRadius(-delta.x()+delta.y());
|
||||
rightStartPos_=mousePos;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case QEvent::MouseButtonPress:
|
||||
case QEvent::MouseButtonRelease:
|
||||
{
|
||||
bool isDown=event->type()==QEvent::MouseButtonPress;
|
||||
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
|
||||
switch(mouseEvent->button())
|
||||
{
|
||||
case Qt::LeftButton:
|
||||
leftMouseDown_=isDown;
|
||||
if(isDown) leftStartPos_=mouseEvent->position();
|
||||
break;
|
||||
case Qt::MiddleButton:
|
||||
middleMouseDown_=isDown;
|
||||
if(isDown) middleStartPos_=mouseEvent->position();
|
||||
break;
|
||||
case Qt::RightButton:
|
||||
rightMouseDown_=isDown;
|
||||
if(isDown) rightStartPos_=mouseEvent->position();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
28
src/Gui/Viewport/Viewport.h
Normal file
28
src/Gui/Viewport/Viewport.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <qboxlayout.h>
|
||||
#include <qwidget.h>
|
||||
#include "Gui/Viewport/ViewportGLWidget.h"
|
||||
|
||||
class Viewport
|
||||
: public QWidget
|
||||
{
|
||||
public:
|
||||
Viewport(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
|
||||
private:
|
||||
QVBoxLayout* mainLayout_;
|
||||
ViewportGLWidget* openGLWidget_;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
bool event(QEvent *event) override;
|
||||
Qt::Key cameraMod_ = Qt::Key_Space;
|
||||
void handleCamera(QEvent *event);
|
||||
|
||||
// TODO: maybe simplify positions to mouseDownPos
|
||||
bool middleMouseDown_=false;
|
||||
QPointF middleStartPos_;
|
||||
bool leftMouseDown_=false;
|
||||
QPointF leftStartPos_;
|
||||
bool rightMouseDown_=false;
|
||||
QPointF rightStartPos_;
|
||||
public slots:
|
||||
void geometryChanged(enzo::geo::Geometry& geometry);
|
||||
};
|
||||
196
src/Gui/Viewport/ViewportGLWidget.cpp
Normal file
196
src/Gui/Viewport/ViewportGLWidget.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
#include "Gui/Viewport/ViewportGLWidget.h"
|
||||
#include "Engine/Operator/AttributeHandle.h"
|
||||
#include "Engine/Types.h"
|
||||
#include "Gui/Viewport/GLMesh.h"
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <memory>
|
||||
#include <qtimer.h>
|
||||
#include "Engine/Operator/Geometry.h"
|
||||
|
||||
void ViewportGLWidget::initializeGL()
|
||||
{
|
||||
using namespace enzo;
|
||||
initializeOpenGLFunctions();
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
enzo::geo::Geometry geo = enzo::geo::Geometry();
|
||||
// triangleMesh_ = meshFromGeo(geo);
|
||||
triangleMesh_ = std::make_unique<GLMesh>();
|
||||
gridMesh_ = std::make_unique<GLGrid>();
|
||||
|
||||
QSurfaceFormat fmt = context()->format();
|
||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGLES ? "GLES" : "Desktop") << "\n";
|
||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGL ? "true" : "false") << "\n";
|
||||
|
||||
// init loop
|
||||
QTimer* loopTimer = new QTimer(this);
|
||||
connect(loopTimer, &QTimer::timeout, this, QOverload<>::of(&QOpenGLWidget::update));
|
||||
loopTimer->start(16);
|
||||
|
||||
// init camera
|
||||
curCamera = GLCamera(-10, 5, -10);
|
||||
|
||||
|
||||
// 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";
|
||||
// shader type
|
||||
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
// convert source
|
||||
const GLchar* vertexShaderSourceC = vertexShaderSource.c_str();
|
||||
// create shader object
|
||||
glShaderSource(vertexShader, 1, &vertexShaderSourceC, NULL);
|
||||
// compile shader object
|
||||
glCompileShader(vertexShader);
|
||||
|
||||
|
||||
// log shader error
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if(!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "success\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 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";
|
||||
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
const GLchar* fragmentShaderSourceC = fragmentShaderSource.c_str();
|
||||
glShaderSource(fragmentShader, 1, &fragmentShaderSourceC, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
|
||||
// create shader program
|
||||
shaderProgram = glCreateProgram();
|
||||
// attach shaders
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
// link program
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
// delete shaders now that the program is complete
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
|
||||
|
||||
constexpr float clearValue = 0.19;
|
||||
glClearColor(clearValue, clearValue, clearValue, 1.0f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ViewportGLWidget::resizeGL(int w, int h)
|
||||
{
|
||||
}
|
||||
|
||||
void ViewportGLWidget::paintGL()
|
||||
{
|
||||
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||
|
||||
|
||||
glm::mat4 projMatrix = glm::perspective(
|
||||
glm::radians(45.0f), // FOV
|
||||
float(width()) / height(), // aspect ratio
|
||||
0.1f, // near plane
|
||||
1000.0f // far plane
|
||||
);
|
||||
|
||||
gridMesh_->useProgram();
|
||||
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "uProj"), 1, GL_FALSE, glm::value_ptr(projMatrix));
|
||||
curCamera.setUniform(glGetUniformLocation(shaderProgram, "uView"));
|
||||
|
||||
|
||||
gridMesh_->draw();
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
GLint projMLoc = glGetUniformLocation(shaderProgram, "uProj");
|
||||
glUniformMatrix4fv(projMLoc, 1, GL_FALSE, glm::value_ptr(projMatrix));
|
||||
|
||||
GLint viewMLoc = glGetUniformLocation(shaderProgram, "uView");
|
||||
curCamera.setUniform(viewMLoc);
|
||||
|
||||
triangleMesh_->draw();
|
||||
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<GLMesh> ViewportGLWidget::meshFromGeo(enzo::geo::Geometry& geometry)
|
||||
{
|
||||
using namespace enzo;
|
||||
|
||||
auto mesh = std::make_unique<GLMesh>();
|
||||
|
||||
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());
|
||||
|
||||
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());
|
||||
|
||||
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
void ViewportGLWidget::geometryChanged(enzo::geo::Geometry& geometry)
|
||||
{
|
||||
using namespace enzo;
|
||||
std::shared_ptr<ga::Attribute> PAttr = geometry.getAttribByName(ga::AttrOwner::POINT, "P");
|
||||
ga::AttributeHandleVector3 PAttrHandle = ga::AttributeHandleVector3(PAttr);
|
||||
|
||||
triangleMesh_->setPosBuffer(PAttrHandle.getAllValues());
|
||||
|
||||
std::shared_ptr<ga::Attribute> pointAttr = geometry.getAttribByName(ga::AttrOwner::VERTEX, "point");
|
||||
ga::AttributeHandleInt pointAttrHandle = ga::AttributeHandleInt(pointAttr);
|
||||
|
||||
std::shared_ptr<ga::Attribute> vertexCountAttr = geometry.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount");
|
||||
ga::AttributeHandleInt vertexCountHandle = ga::AttributeHandleInt(vertexCountAttr);
|
||||
|
||||
triangleMesh_->setIndexBuffer(pointAttrHandle.getAllValues(), vertexCountHandle.getAllValues());
|
||||
}
|
||||
30
src/Gui/Viewport/ViewportGLWidget.h
Normal file
30
src/Gui/Viewport/ViewportGLWidget.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#include <QOpenGLWidget>
|
||||
#include <iostream>
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
#include "Gui/Viewport/GLCamera.h"
|
||||
#include "Gui/Viewport/GLMesh.h"
|
||||
#include "Gui/Viewport/GLGrid.h"
|
||||
#include "Engine/Operator/Geometry.h"
|
||||
|
||||
class ViewportGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_2_Core
|
||||
{
|
||||
public:
|
||||
ViewportGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }
|
||||
QSize sizeHint() const override { return QSize(-1, -1); }
|
||||
GLuint shaderProgram;
|
||||
GLCamera curCamera;
|
||||
std::unique_ptr<GLMesh> triangleMesh_ ;
|
||||
std::unique_ptr<GLGrid> gridMesh_ ;
|
||||
|
||||
std::unique_ptr<GLMesh> meshFromGeo(enzo::geo::Geometry& geometry);
|
||||
|
||||
protected:
|
||||
void initializeGL() override;
|
||||
void resizeGL(int w, int h) override;
|
||||
void paintGL() override;
|
||||
|
||||
public slots:
|
||||
void geometryChanged(enzo::geo::Geometry& geometry);
|
||||
};
|
||||
Reference in New Issue
Block a user