#include "glwidget.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDebug>
#include <math.h>
{
initializeWindow(); // storede some data
}
//------------------------------------------------------------------------------------------------------------------------
GlWidget::~GlWidget()
{
}
//------------------------------------------------------------------------------------------------------------------------
QSize GlWidget
::sizeHint() const {
}
//------------------------------------------------------------------------------------------------------------------------
void GlWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
qglClearColor
(QColor(Qt
::black));
shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
shaderProgram.link();
}
//------------------------------------------------------------------------------------------------------------------------
void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(alpha, 0, 1, 0);
cameraTransformation.rotate(beta, 1, 0, 0);
QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
// X_axes_BODY
//shaderProgram.setUniformValue("color", QColor(Qt::red));
shaderProgram.
setUniformValue("color",
QColor(255,
0,
0,
127));
shaderProgram.setAttributeArray("vertex", x_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDepthMask(GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, x_axes_body.size());
glDepthMask(GL_TRUE);
// Y_axes_BODY
shaderProgram.
setUniformValue("color",
QColor(Qt
::white));
shaderProgram.setAttributeArray("vertex", y_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLE_STRIP, 0, y_axes_body.size());
// Z_axes_BODY
shaderProgram.
setUniformValue("color",
QColor(Qt
::green));
shaderProgram.setAttributeArray("vertex", z_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLE_STRIP, 0, z_axes_body.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.release();
}
#include "glwidget.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDebug>
#include <math.h>
GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(/* Additional format options */), parent)
{
initializeWindow(); // storede some data
}
//------------------------------------------------------------------------------------------------------------------------
GlWidget::~GlWidget()
{
}
//------------------------------------------------------------------------------------------------------------------------
QSize GlWidget::sizeHint() const
{
return QSize(1024,768);
}
//------------------------------------------------------------------------------------------------------------------------
void GlWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
qglClearColor(QColor(Qt::black));
shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
shaderProgram.link();
}
//------------------------------------------------------------------------------------------------------------------------
void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(alpha, 0, 1, 0);
cameraTransformation.rotate(beta, 1, 0, 0);
QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
// X_axes_BODY
//shaderProgram.setUniformValue("color", QColor(Qt::red));
shaderProgram.setUniformValue("color", QColor(255, 0, 0, 127));
shaderProgram.setAttributeArray("vertex", x_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDepthMask(GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, x_axes_body.size());
glDepthMask(GL_TRUE);
// Y_axes_BODY
shaderProgram.setUniformValue("color", QColor(Qt::white));
shaderProgram.setAttributeArray("vertex", y_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLE_STRIP, 0, y_axes_body.size());
// Z_axes_BODY
shaderProgram.setUniformValue("color", QColor(Qt::green));
shaderProgram.setAttributeArray("vertex", z_axes_body.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLE_STRIP, 0, z_axes_body.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.release();
}
To copy to clipboard, switch view to plain text mode
fragmentShader.fsh
Bookmarks