130 lines
2.9 KiB
C++
130 lines
2.9 KiB
C++
#include "MapView.h"
|
|
|
|
#include <QGLShaderProgram>
|
|
#include "Geometry.h"
|
|
|
|
// http://doc.qt.io/qt-5/qtopengl-cube-example.html
|
|
|
|
MapView::MapView(QWidget* parent) : QOpenGLWidget(parent) {
|
|
|
|
|
|
};
|
|
|
|
void MapView::timerEvent(QTimerEvent *) {
|
|
update();
|
|
}
|
|
|
|
void MapView::initializeGL() {
|
|
|
|
//qglClearColor(Qt::black);
|
|
|
|
//glEnable(GL_DEPTH_TEST);
|
|
//glEnable(GL_CULL_FACE);
|
|
//glShadeModel(GL_SMOOTH);
|
|
//glEnable(GL_LIGHTING);
|
|
//glEnable(GL_LIGHT0);
|
|
|
|
//static GLfloat lightPosition[4] = { 0, 0, 10, 1.0 };
|
|
//glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
|
|
|
initializeOpenGLFunctions();
|
|
|
|
geo = new Geometry();
|
|
|
|
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/res/gl/vertex1.glsl")) {throw "1";}
|
|
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/res/gl/fragment1.glsl")) {throw "2";}
|
|
if (!program.link()) {throw "3";}
|
|
if (!program.bind()) {throw "4";}
|
|
|
|
timer.start(60, this);
|
|
|
|
}
|
|
|
|
void MapView::paintGL() {
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
//glLoadIdentity();
|
|
//glTranslatef(0.0, 0.0, -10.0);
|
|
//glRotatef(20 / 16.0, 1.0, 0.0, 0.0);
|
|
//glRotatef(30 / 16.0, 0.0, 1.0, 0.0);
|
|
//glRotatef(60 / 16.0, 0.0, 0.0, 1.0);
|
|
draw();
|
|
}
|
|
|
|
void MapView::resizeGL(int w, int h) {
|
|
|
|
// Calculate aspect ratio
|
|
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
|
|
|
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
|
|
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
|
|
|
|
// Reset projection
|
|
projection.setToIdentity();
|
|
|
|
// Set perspective projection
|
|
projection.perspective(fov, aspect, zNear, zFar);
|
|
|
|
}
|
|
|
|
void MapView::draw() {
|
|
|
|
|
|
static float angularSpeed = 0;
|
|
angularSpeed += 0.5;
|
|
|
|
// Clear color and depth buffer
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
//texture->bind();
|
|
QVector3D rotationAxis(1,1,1);
|
|
QQuaternion rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed);
|
|
|
|
// Calculate model view transformation
|
|
QMatrix4x4 matrix;
|
|
matrix.translate(0.0, 0.0, -5.0);
|
|
matrix.rotate(rotation);
|
|
|
|
// Set modelview-projection matrix
|
|
program.setUniformValue("mvp_matrix", projection * matrix);
|
|
|
|
// Use texture unit 0 which contains cube.png
|
|
program.setUniformValue("texture", 0);
|
|
|
|
// Draw cube geometry
|
|
geo->drawCubeGeometry(&program);
|
|
|
|
//qglColor(Qt::red);
|
|
/*glBegin(GL_QUADS);
|
|
glNormal3f(0,0,-1);
|
|
glVertex3f(-1,-1,0);
|
|
glVertex3f(-1,1,0);
|
|
glVertex3f(1,1,0);
|
|
glVertex3f(1,-1,0);
|
|
|
|
glEnd();
|
|
glBegin(GL_TRIANGLES);
|
|
glNormal3f(0,-1,0.707);
|
|
glVertex3f(-1,-1,0);
|
|
glVertex3f(1,-1,0);
|
|
glVertex3f(0,0,1.2);
|
|
glEnd();
|
|
glBegin(GL_TRIANGLES);
|
|
glNormal3f(1,0, 0.707);
|
|
glVertex3f(1,-1,0);
|
|
glVertex3f(1,1,0);
|
|
glVertex3f(0,0,1.2);
|
|
glEnd();
|
|
glBegin(GL_TRIANGLES);
|
|
glNormal3f(0,1,0.707);
|
|
glVertex3f(1,1,0);
|
|
glVertex3f(-1,1,0);
|
|
glVertex3f(0,0,1.2);
|
|
glEnd();
|
|
glBegin(GL_TRIANGLES);
|
|
glNormal3f(-1,0,0.707);
|
|
glVertex3f(-1,1,0);
|
|
glVertex3f(-1,-1,0);
|
|
glVertex3f(0,0,1.2);
|
|
glEnd();*/
|
|
}
|