aboutsummaryrefslogtreecommitdiff
path: root/src/opengl_widget.hh
blob: 04e2ffcaf1a88a833a3e6ba20d6e917abc118840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef OPENGL_WIDGET_HH
#define OPENGL_WIDGET_HH

#include "opengl_mesh.hh"

#include <QOpenGLWidget>
#include <QMatrix4x4>
#include <QOpenGLExtraFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>

#define FOV 70


class Painter {
public:
	virtual void draw(QOpenGLExtraFunctions *f) const = 0;
};


class OpenGLWidget : public QOpenGLWidget, public QOpenGLExtraFunctions {
	Q_OBJECT

	QMatrix4x4 rot, rot_start, proj;
	GLfloat cam_dist = 1;
	QPoint mouse_pos;

	QOpenGLShaderProgram main_program;
	QOpenGLShaderProgram skybox_program;
	QOpenGLShaderProgram line_program;
	GLuint skybox_tex;
	GLuint skybox_vao;
	GLuint skybox_vbo;
	OpenGLMesh *ground;

	void loadSkybox();
	void loadGround();

	const Painter *painter = nullptr;

protected:
    virtual void mousePressEvent(QMouseEvent *e);
    virtual void mouseReleaseEvent(QMouseEvent *e);
    virtual void mouseMoveEvent(QMouseEvent *e);
    virtual void wheelEvent(QWheelEvent *e);

public:
	static OpenGLWidget *instance;

	QVector<OpenGLMesh> meshes;

	OpenGLWidget(QWidget *parent=nullptr);
	~OpenGLWidget();
	void initializeGL() override;
	void resizeGL(int w, int h) override;
	void paintGL() override;
	void setPainter(const Painter *p);
	QOpenGLShaderProgram *getMainProgram();
	QOpenGLShaderProgram *getLineProgram();

signals:
	void initialized();
};


#endif