worked on OpenGL ES
This commit is contained in:
119
map/MapView.cpp
119
map/MapView.cpp
@@ -1,53 +1,100 @@
|
||||
#include "MapView.h"
|
||||
|
||||
#include <QGLShaderProgram>
|
||||
#include "Geometry.h"
|
||||
|
||||
// http://doc.qt.io/qt-5/qtopengl-cube-example.html
|
||||
|
||||
MapView::MapView(QWidget* parent) : QOpenGLWidget(parent) {
|
||||
|
||||
MapView::MapView(QWidget* parent) : QGLWidget(parent) {
|
||||
|
||||
};
|
||||
|
||||
void MapView::initializeGL() {
|
||||
void MapView::timerEvent(QTimerEvent *) {
|
||||
update();
|
||||
}
|
||||
|
||||
qglClearColor(Qt::black);
|
||||
void MapView::initializeGL() {
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
//glShadeModel(GL_SMOOTH);
|
||||
//glEnable(GL_LIGHTING);
|
||||
//glEnable(GL_LIGHT0);
|
||||
//qglClearColor(Qt::black);
|
||||
|
||||
//static GLfloat lightPosition[4] = { 0, 0, 10, 1.0 };
|
||||
//glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
||||
//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);
|
||||
|
||||
void MapView::paintGL() {
|
||||
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);
|
||||
//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 width, int height) {
|
||||
int side = qMin(width, height);
|
||||
glViewport((width - side) / 2, (height - side) / 2, side, side);
|
||||
//texture->bind();
|
||||
QVector3D rotationAxis(1,1,1);
|
||||
QQuaternion rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed);
|
||||
|
||||
//glMatrixMode(GL_PROJECTION);
|
||||
//glLoadIdentity();
|
||||
#ifdef QT_OPENGL_ES_1
|
||||
glOrthof(-2, +2, -2, +2, 1.0, 15.0);
|
||||
#else
|
||||
//glOrtho(-2, +2, -2, +2, 1.0, 15.0);
|
||||
#endif
|
||||
//glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
// Calculate model view transformation
|
||||
QMatrix4x4 matrix;
|
||||
matrix.translate(0.0, 0.0, -5.0);
|
||||
matrix.rotate(rotation);
|
||||
|
||||
void MapView::draw()
|
||||
{
|
||||
qglColor(Qt::red);
|
||||
/*glBegin(GL_QUADS);
|
||||
// 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);
|
||||
@@ -79,4 +126,4 @@ MapView::MapView(QWidget* parent) : QGLWidget(parent) {
|
||||
glVertex3f(-1,-1,0);
|
||||
glVertex3f(0,0,1.2);
|
||||
glEnd();*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user