Created
May 31, 2015 06:26
-
-
Save arthurzam/02e1700a08857bb14ca8 to your computer and use it in GitHub Desktop.
Mini OpenGL-Qt app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "window.h" | |
#include <QApplication> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
MainWindow w; | |
w.show(); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// myglwidget.cpp | |
#include <QtWidgets> | |
#include <QtOpenGL> | |
#include "myglwidget.h" | |
MyGLWidget::MyGLWidget(QWidget *parent) | |
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent) | |
{ | |
xRot = 0; | |
yRot = 0; | |
zRot = 0; | |
state = 0; | |
} | |
MyGLWidget::~MyGLWidget() | |
{ | |
} | |
QSize MyGLWidget::minimumSizeHint() const | |
{ | |
return QSize(50, 50); | |
} | |
QSize MyGLWidget::sizeHint() const | |
{ | |
return QSize(400, 400); | |
} | |
static void qNormalizeAngle(int &angle) | |
{ | |
while (angle < 0) | |
angle += 360 * 16; | |
while (angle > 360) | |
angle -= 360 * 16; | |
} | |
void MyGLWidget::setXRotation(int angle) | |
{ | |
qNormalizeAngle(angle); | |
if (angle != xRot) { | |
xRot = angle; | |
emit xRotationChanged(angle); | |
updateGL(); | |
} | |
} | |
void MyGLWidget::setYRotation(int angle) | |
{ | |
qNormalizeAngle(angle); | |
if (angle != yRot) { | |
yRot = angle; | |
emit yRotationChanged(angle); | |
updateGL(); | |
} | |
} | |
void MyGLWidget::setZRotation(int angle) | |
{ | |
qNormalizeAngle(angle); | |
if (angle != zRot) { | |
zRot = angle; | |
emit zRotationChanged(angle); | |
updateGL(); | |
} | |
} | |
void MyGLWidget::initializeGL() | |
{ | |
qglClearColor(Qt::black); | |
glEnable(GL_DEPTH_TEST); | |
} | |
void MyGLWidget::paintGL() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
glTranslatef(0.0, 0.0, -10.0); //////////////////////////////// move zero x,y,z | |
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0); | |
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0); | |
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0); | |
draw(); | |
} | |
void MyGLWidget::resizeGL(int width, int height) | |
{ | |
double Aspect; | |
if(height == 0) | |
height = 1; | |
glViewport(0, 0, width, height); | |
Aspect=(double)width/(double)height; | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
double Range = 10; | |
if (width <= height) | |
glOrtho(-(Range),Range,-Range/Aspect,Range/Aspect,-(Range*2),Range*2); | |
else | |
glOrtho(-(Range*Aspect),Range*Aspect,-Range,Range,-(Range*2),Range*2); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
} | |
void MyGLWidget::mousePressEvent(QMouseEvent *event) | |
{ | |
lastPos = event->pos(); | |
} | |
void MyGLWidget::mouseMoveEvent(QMouseEvent *event) | |
{ | |
int dx = event->x() - lastPos.x(); | |
int dy = event->y() - lastPos.y(); | |
if (event->buttons() & Qt::LeftButton) { | |
setXRotation(xRot + 8 * dy); | |
setYRotation(yRot + 8 * dx); | |
} else if (event->buttons() & Qt::RightButton) { | |
setXRotation(xRot + 8 * dy); | |
setZRotation(zRot + 8 * dx); | |
} | |
lastPos = event->pos(); | |
} | |
void drawSphere(float r) | |
{ | |
glBegin(GL_LINES); | |
const float JUMP_ANGLE = M_PI / 18.0f; | |
const float HEIG_ANGLE = M_PI / 25.0f; | |
float tempR, z; | |
for (float angle_z = -(M_PI / 2.0f); angle_z <= M_PI / 2.0f; angle_z += HEIG_ANGLE) | |
{ | |
glBegin(GL_LINE_STRIP); | |
z = r * sin(angle_z); | |
tempR = r * cos(angle_z); | |
for (float angle = 0; angle < 2 * M_PI; angle += JUMP_ANGLE) | |
{ | |
float x = tempR * cos(angle), | |
y = tempR * sin(angle); | |
glVertex3f(x, y, z); | |
} | |
glEnd(); | |
} | |
for(float b = 0; b<= 2 * M_PI; b += 6 * JUMP_ANGLE) | |
{ | |
glBegin(GL_LINE_STRIP); | |
for(float a = -(M_PI / 2.0f); a<= M_PI / 2.0f; a += JUMP_ANGLE) | |
{ | |
glVertex3f(r * cos(a) * cos (b), r * cos(a) * sin(b), r * sin(a)); | |
} | |
glEnd(); | |
} | |
} | |
void drawCube(float a, float b, float c, float x, float y, float z) | |
{ | |
glBegin(GL_QUADS); | |
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green | |
glVertex3f( x, y + b,z); // Top Right Of The Quad (Top) | |
glVertex3f(x + a, y + b,z); // Top Left Of The Quad (Top) | |
glVertex3f(x + a, y + b,z + c); // Bottom Left Of The Quad (Top) | |
glVertex3f( x, y + b,z + c); // Bottom Right Of The Quad (Top) = 6 | |
glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange | |
glVertex3f( x,y, z); // Top Right Of The Quad (Bottom) | |
glVertex3f(x + a,y, z); // Top Left Of The Quad (Bottom) | |
glVertex3f(x + a,y, z + c); // Bottom Left Of The Quad (Bottom) | |
glVertex3f(x,y, z + c); // Bottom Right Of The Quad (Bottom) = 5 | |
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red | |
glVertex3f( x, y, z); // Top Right Of The Quad (Front) | |
glVertex3f(x+ a, y, z); // Top Left Of The Quad (Front) | |
glVertex3f(x+a,y+b, z); // Bottom Left Of The Quad (Front) | |
glVertex3f(x,y+b, z); // Bottom Right Of The Quad (Front) = 1 | |
glColor3f (1.0f,1.0f,0.0f); // Set The Color To Yellow | |
glVertex3f( x, y, z+c); // Bottom Left Of The Quad (Back) | |
glVertex3f(x+ a, y, z+c); // Bottom Right Of The Quad (Back) | |
glVertex3f(x+a,y+b, z+c); // Top Right Of The Quad (Back) | |
glVertex3f(x,y+b, z+c); // Top Left Of The Quad (Back) = 2 | |
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue | |
glVertex3f(x, y, z); // Top Right Of The Quad (Left) | |
glVertex3f(x, y+b,z); // Top Left Of The Quad (Left) | |
glVertex3f(x, y+b,z+c); // Bottom Left Of The Quad (Left) | |
glVertex3f(x, y,z+c); // Bottom Right Of The Quad (Left) = 3 | |
glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet | |
glVertex3f(x+a, y, z); // Top Right Of The Quad (Right) | |
glVertex3f(x+a, y+b,z); // Top Left Of The Quad (Right) | |
glVertex3f(x+a, y+b,z+c); // Bottom Left Of The Quad (Right) | |
glVertex3f(x+a, y,z+c); // Bottom Right Of The Quad (Right) | |
glEnd(); | |
} | |
float degreeToRadian(float deg) | |
{ | |
return ((deg / 180.0) * M_PI); | |
} | |
void MyGLWidget::draw() | |
{ | |
/*float a = 0.5f, b = 0.5f, c = 0.5f; | |
float r = 6; | |
int n = 3600; | |
float C, S; | |
for(int i = 0; i < n; ++i) | |
{ | |
C = cos(degreeToRadian(i * 360.0 / n)) * r; | |
S = sin(degreeToRadian(i * 360.0 / n)) * r; | |
drawCube(a,b,c, C, S, 0); | |
drawCube(a,b,c, C, 0, S); | |
drawCube(a,b,c, 0, C, S); | |
}*/ | |
drawSphere(5.0f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// myglwidget.h | |
#ifndef MYGLWIDGET_H | |
#define MYGLWIDGET_H | |
#include <QGLWidget> | |
class MyGLWidget : public QGLWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit MyGLWidget(QWidget *parent = 0); | |
~MyGLWidget(); | |
signals: | |
public slots: | |
protected: | |
void initializeGL(); | |
void paintGL(); | |
void resizeGL(int width, int height); | |
QSize minimumSizeHint() const; | |
QSize sizeHint() const; | |
void mousePressEvent(QMouseEvent *event); | |
void mouseMoveEvent(QMouseEvent *event); | |
public slots: | |
// slots for xyz-rotation slider | |
void setXRotation(int angle); | |
void setYRotation(int angle); | |
void setZRotation(int angle); | |
signals: | |
// signaling rotation from mouse movement | |
void xRotationChanged(int angle); | |
void yRotationChanged(int angle); | |
void zRotationChanged(int angle); | |
private: | |
void draw(); | |
int xRot; | |
int yRot; | |
int zRot; | |
int state; | |
QPoint lastPos; | |
}; | |
#endif // MYGLWIDGET_H | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2014-07-03T15:02:32 | |
# | |
#------------------------------------------------- | |
QT += core gui opengl | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = OpenGL | |
TEMPLATE = app | |
SOURCES += main.cpp\ | |
window.cpp \ | |
myglwidget.cpp | |
HEADERS += window.h \ | |
myglwidget.h | |
FORMS += window.ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "window.h" | |
#include "ui_window.h" | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
void MainWindow::on_xRot_valueChanged(int value) | |
{ | |
ui->myGLWidget->setXRotation(value); | |
ui->xRot_lb->setText(QString::number(value / 16)); | |
} | |
void MainWindow::on_xRot_sliderMoved(int position) | |
{ | |
// sorry, can't delete function | |
} | |
void MainWindow::on_yRot_valueChanged(int value) | |
{ | |
ui->myGLWidget->setYRotation(value); | |
ui->yRot_lb->setText(QString::number(value / 16)); | |
} | |
void MainWindow::on_zRot_valueChanged(int value) | |
{ | |
ui->myGLWidget->setZRotation(value); | |
ui->zRot_lb->setText(QString::number(value / 16)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WINDOW_H | |
#define WINDOW_H | |
#include <QMainWindow> | |
namespace Ui { | |
class MainWindow; | |
} | |
class MainWindow : public QMainWindow | |
{ | |
Q_OBJECT | |
public: | |
explicit MainWindow(QWidget *parent = 0); | |
~MainWindow(); | |
private slots: | |
void on_xRot_sliderMoved(int position); | |
void on_yRot_valueChanged(int value); | |
void on_xRot_valueChanged(int value); | |
void on_zRot_valueChanged(int value); | |
private: | |
Ui::MainWindow *ui; | |
}; | |
#endif // WINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>MainWindow</class> | |
<widget class="QMainWindow" name="MainWindow"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>643</width> | |
<height>479</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>OpenGL in QT</string> | |
</property> | |
<widget class="QWidget" name="centralWidget"> | |
<widget class="MyGLWidget" name="myGLWidget" native="true"> | |
<property name="geometry"> | |
<rect> | |
<x>10</x> | |
<y>0</y> | |
<width>621</width> | |
<height>351</height> | |
</rect> | |
</property> | |
<property name="toolTip"> | |
<string>OpenGL canvas</string> | |
</property> | |
<property name="toolTipDuration"> | |
<number>5000</number> | |
</property> | |
</widget> | |
<widget class="QSlider" name="xRot"> | |
<property name="geometry"> | |
<rect> | |
<x>10</x> | |
<y>360</y> | |
<width>501</width> | |
<height>19</height> | |
</rect> | |
</property> | |
<property name="toolTip"> | |
<string>Rotate on X</string> | |
</property> | |
<property name="maximum"> | |
<number>5760</number> | |
</property> | |
<property name="singleStep"> | |
<number>16</number> | |
</property> | |
<property name="pageStep"> | |
<number>15</number> | |
</property> | |
<property name="orientation"> | |
<enum>Qt::Horizontal</enum> | |
</property> | |
<property name="tickPosition"> | |
<enum>QSlider::TicksAbove</enum> | |
</property> | |
<property name="tickInterval"> | |
<number>150</number> | |
</property> | |
</widget> | |
<widget class="QSlider" name="yRot"> | |
<property name="geometry"> | |
<rect> | |
<x>10</x> | |
<y>390</y> | |
<width>501</width> | |
<height>19</height> | |
</rect> | |
</property> | |
<property name="toolTip"> | |
<string>Rotate on Y</string> | |
</property> | |
<property name="maximum"> | |
<number>5760</number> | |
</property> | |
<property name="singleStep"> | |
<number>16</number> | |
</property> | |
<property name="pageStep"> | |
<number>15</number> | |
</property> | |
<property name="orientation"> | |
<enum>Qt::Horizontal</enum> | |
</property> | |
<property name="tickPosition"> | |
<enum>QSlider::TicksAbove</enum> | |
</property> | |
<property name="tickInterval"> | |
<number>150</number> | |
</property> | |
</widget> | |
<widget class="QSlider" name="zRot"> | |
<property name="geometry"> | |
<rect> | |
<x>10</x> | |
<y>420</y> | |
<width>501</width> | |
<height>19</height> | |
</rect> | |
</property> | |
<property name="toolTip"> | |
<string>Rotate on Z</string> | |
</property> | |
<property name="maximum"> | |
<number>5760</number> | |
</property> | |
<property name="singleStep"> | |
<number>16</number> | |
</property> | |
<property name="pageStep"> | |
<number>15</number> | |
</property> | |
<property name="orientation"> | |
<enum>Qt::Horizontal</enum> | |
</property> | |
<property name="tickPosition"> | |
<enum>QSlider::TicksAbove</enum> | |
</property> | |
<property name="tickInterval"> | |
<number>150</number> | |
</property> | |
</widget> | |
<widget class="QLabel" name="xRot_lb"> | |
<property name="geometry"> | |
<rect> | |
<x>530</x> | |
<y>360</y> | |
<width>31</width> | |
<height>16</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>0</string> | |
</property> | |
</widget> | |
<widget class="QLabel" name="yRot_lb"> | |
<property name="geometry"> | |
<rect> | |
<x>530</x> | |
<y>390</y> | |
<width>31</width> | |
<height>16</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>0</string> | |
</property> | |
</widget> | |
<widget class="QLabel" name="zRot_lb"> | |
<property name="geometry"> | |
<rect> | |
<x>530</x> | |
<y>410</y> | |
<width>31</width> | |
<height>16</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>0</string> | |
</property> | |
</widget> | |
</widget> | |
<widget class="QMenuBar" name="menuBar"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>643</width> | |
<height>25</height> | |
</rect> | |
</property> | |
</widget> | |
<widget class="QToolBar" name="mainToolBar"> | |
<attribute name="toolBarArea"> | |
<enum>TopToolBarArea</enum> | |
</attribute> | |
<attribute name="toolBarBreak"> | |
<bool>false</bool> | |
</attribute> | |
</widget> | |
<widget class="QStatusBar" name="statusBar"/> | |
</widget> | |
<layoutdefault spacing="6" margin="11"/> | |
<customwidgets> | |
<customwidget> | |
<class>MyGLWidget</class> | |
<extends>QWidget</extends> | |
<header>myglwidget.h</header> | |
<container>1</container> | |
</customwidget> | |
</customwidgets> | |
<resources/> | |
<connections/> | |
</ui> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment