mod_geo-tp/src/mesh_viewer.cpp

211 lines
4.9 KiB
C++
Raw Normal View History

2021-09-20 20:36:29 +02:00
#include "mesh_viewer.h"
#include <tgmath.h>
2021-10-02 20:47:02 +02:00
#include <algorithm>
2021-09-20 20:36:29 +02:00
const GLchar *vertex_shader_source = R"glsl(
attribute vec3 pos;
attribute vec3 col;
2021-09-20 20:36:29 +02:00
varying vec3 frag_col;
2021-09-20 20:36:29 +02:00
uniform mat4 proj;
uniform mat4 view;
uniform mat4 model;
void main() {
gl_Position = proj * view * model * vec4(pos, 1.0);
frag_col = col;
}
)glsl";
const GLchar *fragment_shader_source = R"glsl(
varying vec3 frag_col;
2021-09-20 20:36:29 +02:00
uniform vec3 wf_col;
uniform bool wireframe;
uniform float alpha;
void main() {
2021-09-30 17:20:25 +02:00
if (!wireframe)
// gl_FragColor = vec4(wf_col, alpha);
gl_FragColor = vec4(.5, .5, .5, 1);
2021-09-20 20:36:29 +02:00
else
gl_FragColor = vec4(frag_col, alpha);
2021-09-20 20:36:29 +02:00
}
)glsl";
MeshViewer::MeshViewer(QWidget *parent) : QOpenGLWidget(parent) {
setMouseTracking(true);
setFocus();
}
QSize MeshViewer::sizeHint() const {
return QSize(640, 480);
}
2021-09-20 20:36:29 +02:00
void GLAPIENTRY
opengl_debug_cb(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam) {
(void) source;
(void) type;
(void) id;
(void) severity;
(void) length;
(void) userParam;
qDebug() << "OpenGL debug output:" << message;
}
void MeshViewer::initializeGL() {
// initializeOpenGLFunctions();
2021-10-02 20:47:02 +02:00
qDebug("OpenGL version %d.%d",
context()->format().majorVersion(),
context()->format().minorVersion());
2021-09-20 20:36:29 +02:00
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(opengl_debug_cb, 0);
/* Compile the vertex shader. */
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
GLint status;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char log[1024];
glGetShaderInfoLog(vertex_shader, sizeof log, NULL, log);
fprintf(stderr, "Failed to compile the vertex shader: %s\n", log);
exit(1);
}
/* Compile the fragment shader. */
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char log[1024];
glGetShaderInfoLog(fragment_shader, sizeof log, NULL, log);
fprintf(stderr, "Failed to compile the fragment shader: %s\n", log);
exit(1);
}
/* Link the shader program. */
GLuint shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glBindFragDataLocation(shader_program, 0, "out_color");
glLinkProgram(shader_program);
glGetProgramiv(shader_program, GL_LINK_STATUS, &status);
if (status != GL_TRUE) {
char log[1024];
glGetProgramInfoLog(shader_program, sizeof log, NULL, log);
fprintf(stderr, "Failed to link the shader program: %s\n", log);
exit(1);
}
/* Use it. */
glUseProgram(shader_program);
/* Get the position attribute. */
pos_attr = glGetAttribLocation(shader_program, "pos");
col_attr = glGetAttribLocation(shader_program, "col");
proj_attr = glGetUniformLocation(shader_program, "proj");
view_attr = glGetUniformLocation(shader_program, "view");
model_attr = glGetUniformLocation(shader_program, "model");
wf_col_attr = glGetUniformLocation(shader_program, "wf_col");
wireframe_attr = glGetUniformLocation(shader_program, "wireframe");
alpha_attr = glGetUniformLocation(shader_program, "alpha");
glUniform1f(alpha_attr, 1);
glUniform3f(wf_col_attr, WIREFRAME_COLOR);
glClearColor(1, 1, 1, 0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
2021-10-02 20:47:02 +02:00
qDebug("Mesh viewer initialized");
2021-09-20 20:36:29 +02:00
emit initialized();
}
void MeshViewer::resizeGL(int w, int h) {
QMatrix4x4 projection;
projection.perspective(FOV, (float) w/h, .01, 100);
glUniformMatrix4fv(proj_attr, 1, GL_FALSE, projection.data());
}
void MeshViewer::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 trans;
trans.translate(0, 0, -cam_dist);
QMatrix4x4 view = trans * rot;
glUniformMatrix4fv(view_attr, 1, GL_FALSE, view.data());
2021-10-02 20:47:02 +02:00
for (const MeshView m : meshes) {
m.paint(context(), model_attr, wireframe_attr);
2021-09-20 20:36:29 +02:00
}
}
2021-10-02 20:47:02 +02:00
void MeshViewer::addMesh(const MyMesh &mesh) {
2021-09-20 20:36:29 +02:00
makeCurrent();
2021-10-02 20:47:02 +02:00
meshes.emplace_back(context()->extraFunctions(), mesh,
pos_attr, col_attr);
2021-09-20 20:36:29 +02:00
doneCurrent();
update();
}
2021-10-02 20:47:02 +02:00
void MeshViewer::removeMesh(const MyMesh &mesh) {
makeCurrent();
meshes.remove_if([&](const MeshView &mv) {
return &mv.mesh == &mesh;
});
doneCurrent();
update();
2021-09-20 20:36:29 +02:00
}
void MeshViewer::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton) {
mouse_pos = e->pos();
}
}
void MeshViewer::mouseReleaseEvent(QMouseEvent *e) {
(void) e;
rot_start = rot;
}
void MeshViewer::mouseMoveEvent(QMouseEvent *e) {
if (e->buttons() & Qt::LeftButton) {
QPoint delta = e->pos() - mouse_pos;
rot = rot_start;
rot.rotate(delta.x() / 5., 0, 1, 0);
rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot);
update();
}
}
void MeshViewer::wheelEvent(QWheelEvent *e) {
cam_dist -= e->angleDelta().y() / 1000. * cam_dist;
update();
}