worked on OpenGL ES

This commit is contained in:
2016-07-15 16:39:38 +02:00
parent 719a66937e
commit 69dfbe6693
10 changed files with 319 additions and 53 deletions

View File

@@ -46,22 +46,30 @@ int main(int argc, char *argv[]) {
// std::this_thread::sleep_for(std::chrono::seconds(10000)); // std::this_thread::sleep_for(std::chrono::seconds(10000));
//QGuiApplication app(argc, argv); //QGuiApplication app(argc, argv);
QApplication app(argc, argv); QApplication app(argc, argv);
QMainWindow* win = new QMainWindow(); // OpenGL Setup
QSurfaceFormat format;
format.setDepthBufferSize(24);
QSurfaceFormat::setDefaultFormat(format);
QVBoxLayout* lay = new QVBoxLayout(); // QMainWindow* win = new QMainWindow();
win->setLayout(lay);
MapView* map = new MapView(win); // QVBoxLayout* lay = new QVBoxLayout();
lay->addWidget(map); // win->setLayout(lay);
MapView* map = new MapView();
// lay->addWidget(map);
map->setMinimumHeight(200); map->setMinimumHeight(200);
map->setMinimumWidth(200); map->setMinimumWidth(200);
map->show();
win->setMinimumWidth(400); // win->setMinimumWidth(400);
win->setMinimumHeight(400); // win->setMinimumHeight(400);
win->show(); // win->show();
// QQmlApplicationEngine engine; // QQmlApplicationEngine engine;
// engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); // engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

124
map/Geometry.cpp Normal file
View File

@@ -0,0 +1,124 @@
#include "Geometry.h"
#include <QVector2D>
#include <QVector3D>
struct VertexData
{
QVector3D position;
QVector2D texCoord;
};
Geometry::Geometry() : indexBuf(QOpenGLBuffer::IndexBuffer) {
initializeOpenGLFunctions();
// Generate 2 VBOs
arrayBuf.create();
indexBuf.create();
// Initializes cube geometry and transfers it to VBOs
initCubeGeometry();
}
Geometry::~Geometry() {
arrayBuf.destroy();
indexBuf.destroy();
}
void Geometry::initCubeGeometry() {
// For cube we would need only 8 vertices but we have to
// duplicate vertex for each face because texture coordinate
// is different.
VertexData vertices[] = {
// Vertex data for face 0
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)}, // v0
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)}, // v1
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.0f, 0.5f)}, // v2
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v3
// Vertex data for face 1
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D( 0.0f, 0.5f)}, // v4
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.5f)}, // v5
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.0f, 1.0f)}, // v6
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v7
// Vertex data for face 2
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v8
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(1.0f, 0.5f)}, // v9
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)}, // v10
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(1.0f, 1.0f)}, // v11
// Vertex data for face 3
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v12
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(1.0f, 0.0f)}, // v13
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v14
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(1.0f, 0.5f)}, // v15
// Vertex data for face 4
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.0f)}, // v16
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v17
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v18
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v19
// Vertex data for face 5
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v20
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v21
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v22
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)} // v23
};
// Indices for drawing cube faces using triangle strips.
// Triangle strips can be connected by duplicating indices
// between the strips. If connecting strips have opposite
// vertex order then last index of the first strip and first
// index of the second strip needs to be duplicated. If
// connecting strips have same vertex order then only last
// index of the first strip needs to be duplicated.
GLushort indices[] = {
0, 1, 2, 3, 3, // Face 0 - triangle strip ( v0, v1, v2, v3)
4, 4, 5, 6, 7, 7, // Face 1 - triangle strip ( v4, v5, v6, v7)
8, 8, 9, 10, 11, 11, // Face 2 - triangle strip ( v8, v9, v10, v11)
12, 12, 13, 14, 15, 15, // Face 3 - triangle strip (v12, v13, v14, v15)
16, 16, 17, 18, 19, 19, // Face 4 - triangle strip (v16, v17, v18, v19)
20, 20, 21, 22, 23 // Face 5 - triangle strip (v20, v21, v22, v23)
};
// Transfer vertex data to VBO 0
arrayBuf.bind();
arrayBuf.allocate(vertices, 24 * sizeof(VertexData));
// Transfer index data to VBO 1
indexBuf.bind();
indexBuf.allocate(indices, 34 * sizeof(GLushort));
}
void Geometry::drawCubeGeometry(QOpenGLShaderProgram *program) {
// Tell OpenGL which VBOs to use
arrayBuf.bind();
indexBuf.bind();
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
// Offset for texture coordinate
offset += sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program->attributeLocation("a_texcoord");
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, 0);
}

23
map/Geometry.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
class Geometry : protected QOpenGLFunctions
{
public:
Geometry();
virtual ~Geometry();
void drawCubeGeometry(QOpenGLShaderProgram *program);
private:
void initCubeGeometry();
QOpenGLBuffer arrayBuf;
QOpenGLBuffer indexBuf;
};
#endif // GEOMETRY_H

View File

@@ -1,53 +1,100 @@
#include "MapView.h" #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); //qglClearColor(Qt::black);
glEnable(GL_CULL_FACE);
//glShadeModel(GL_SMOOTH);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//static GLfloat lightPosition[4] = { 0, 0, 10, 1.0 }; //glEnable(GL_DEPTH_TEST);
//glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); //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); 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) { //texture->bind();
int side = qMin(width, height); QVector3D rotationAxis(1,1,1);
glViewport((width - side) / 2, (height - side) / 2, side, side); QQuaternion rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed);
//glMatrixMode(GL_PROJECTION); // Calculate model view transformation
//glLoadIdentity(); QMatrix4x4 matrix;
#ifdef QT_OPENGL_ES_1 matrix.translate(0.0, 0.0, -5.0);
glOrthof(-2, +2, -2, +2, 1.0, 15.0); matrix.rotate(rotation);
#else
//glOrtho(-2, +2, -2, +2, 1.0, 15.0);
#endif
//glMatrixMode(GL_MODELVIEW);
}
void MapView::draw() // Set modelview-projection matrix
{ program.setUniformValue("mvp_matrix", projection * matrix);
qglColor(Qt::red);
/*glBegin(GL_QUADS); // 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); glNormal3f(0,0,-1);
glVertex3f(-1,-1,0); glVertex3f(-1,-1,0);
glVertex3f(-1,1,0); glVertex3f(-1,1,0);
@@ -79,4 +126,4 @@ MapView::MapView(QWidget* parent) : QGLWidget(parent) {
glVertex3f(-1,-1,0); glVertex3f(-1,-1,0);
glVertex3f(0,0,1.2); glVertex3f(0,0,1.2);
glEnd();*/ glEnd();*/
} }

View File

@@ -1,9 +1,22 @@
#ifndef MAPVIEW_H #ifndef MAPVIEW_H
#define MAPVIEW_H #define MAPVIEW_H
#include <QGLWidget> #include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QBasicTimer>
class MapView : public QGLWidget { class Geometry;
class MapView : public QOpenGLWidget, protected QOpenGLFunctions {
private:
QMatrix4x4 projection;
QOpenGLShaderProgram program;
Geometry* geo;
QBasicTimer timer;
public: public:
@@ -11,6 +24,8 @@ public:
protected: protected:
void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE;
void initializeGL(); void initializeGL();
void paintGL(); void paintGL();

View File

@@ -1,6 +1,8 @@
#ifndef FIXC11_H #ifndef FIXC11_H
#define FIXC11_H #define FIXC11_H
#ifdef ANDROID
#include <cmath> #include <cmath>
#include <sstream> #include <sstream>
@@ -47,4 +49,6 @@ namespace std {
} }
#endif
#endif // FIXC11_H #endif // FIXC11_H

View File

@@ -1,3 +1,6 @@
<RCC> <RCC>
<qresource prefix="/"/> <qresource prefix="/">
<file>res/gl/fragment1.glsl</file>
<file>res/gl/vertex1.glsl</file>
</qresource>
</RCC> </RCC>

16
res/gl/fragment1.glsl Normal file
View File

@@ -0,0 +1,16 @@
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
uniform sampler2D texture;
varying vec2 v_texcoord;
void main()
{
// Set fragment color from texture
gl_FragColor = texture2D(texture, v_texcoord);
gl_FragColor = vec4(1,1,1,1);
}

22
res/gl/vertex1.glsl Normal file
View File

@@ -0,0 +1,22 @@
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
uniform mat4 mvp_matrix;
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
void main()
{
// Calculate vertex position in screen space
gl_Position = mvp_matrix * a_position;
// Pass texture coordinate to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces
v_texcoord = a_texcoord;
}

View File

@@ -3,8 +3,8 @@ TEMPLATE = app
QT += qml opengl QT += qml opengl
# android? # android?
QT += androidextras sensors #QT += androidextras sensors
DEFINES += ANDROID #DEFINES += ANDROID
CONFIG += c++11 CONFIG += c++11
@@ -21,7 +21,8 @@ OTHER_FILES += \
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
map/MapView.cpp map/MapView.cpp \
map/Geometry.cpp
RESOURCES += qml.qrc RESOURCES += qml.qrc
@@ -51,7 +52,10 @@ HEADERS += \
misc/Debug.h \ misc/Debug.h \
misc/fixc11.h \ misc/fixc11.h \
sensors/dummy/WiFiSensorDummy.h \ sensors/dummy/WiFiSensorDummy.h \
map/MapView.h map/MapView.h \
map/Geometry.h
DISTFILES += \ DISTFILES += \
android-sources/src/MyActivity.java android-sources/src/MyActivity.java \
res/gl/vertex1.glsl \
res/gl/fragment1.glsl