2011-04-27 15:49:30 +00:00
|
|
|
#include "hellowindow.h"
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
#include <QOpenGLContext>
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <qmath.h>
|
|
|
|
|
2011-08-18 08:50:18 +00:00
|
|
|
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share)
|
2011-06-07 15:25:22 +00:00
|
|
|
: m_initialized(false)
|
2011-08-18 08:50:18 +00:00
|
|
|
, m_format(format)
|
2011-06-07 15:25:22 +00:00
|
|
|
{
|
2011-08-31 07:14:00 +00:00
|
|
|
m_context = new QOpenGLContext(this);
|
2011-08-18 08:50:18 +00:00
|
|
|
m_context->setFormat(format);
|
|
|
|
if (share)
|
|
|
|
m_context->setShareContext(share->m_context);
|
2011-07-21 11:50:28 +00:00
|
|
|
m_context->create();
|
2011-06-07 15:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HelloWindow::HelloWindow(Renderer *renderer)
|
|
|
|
: m_colorIndex(0)
|
|
|
|
, m_renderer(renderer)
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
2011-06-22 11:33:27 +00:00
|
|
|
setSurfaceType(QWindow::OpenGLSurface);
|
2011-08-18 08:50:18 +00:00
|
|
|
setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
setGeometry(QRect(10, 10, 640, 480));
|
|
|
|
|
2011-08-18 08:50:18 +00:00
|
|
|
setFormat(renderer->format());
|
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
create();
|
|
|
|
|
|
|
|
QTimer *timer = new QTimer(this);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(render()));
|
2011-06-16 10:56:00 +00:00
|
|
|
timer->start(10);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-08-18 08:50:18 +00:00
|
|
|
connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
|
|
|
|
renderer, SLOT(render(QSurface *, const QColor &, const QSize &)));
|
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void HelloWindow::mousePressEvent(QMouseEvent *)
|
2011-04-28 11:50:07 +00:00
|
|
|
{
|
2011-06-07 15:25:22 +00:00
|
|
|
updateColor();
|
2011-04-28 11:50:07 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 08:50:18 +00:00
|
|
|
void HelloWindow::render()
|
|
|
|
{
|
|
|
|
emit needRender(this, m_color, size());
|
|
|
|
}
|
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
void HelloWindow::updateColor()
|
|
|
|
{
|
2011-06-07 15:25:22 +00:00
|
|
|
QColor colors[] =
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
2011-06-07 15:25:22 +00:00
|
|
|
QColor(100, 255, 0),
|
|
|
|
QColor(0, 100, 255)
|
2011-04-27 15:49:30 +00:00
|
|
|
};
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
m_color = colors[m_colorIndex];
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
m_colorIndex++;
|
|
|
|
if (m_colorIndex >= int(sizeof(colors) / sizeof(colors[0])))
|
|
|
|
m_colorIndex = 0;
|
2011-04-27 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
2011-06-21 11:39:26 +00:00
|
|
|
void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
|
2011-06-07 15:25:22 +00:00
|
|
|
{
|
2011-06-21 11:39:26 +00:00
|
|
|
if (!m_context->makeCurrent(surface))
|
|
|
|
return;
|
2011-06-07 15:25:22 +00:00
|
|
|
|
|
|
|
if (!m_initialized) {
|
|
|
|
initialize();
|
|
|
|
m_initialized = true;
|
|
|
|
}
|
2011-05-02 10:00:00 +00:00
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
glViewport(0, 0, viewSize.width(), viewSize.height());
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
glFrontFace(GL_CW);
|
|
|
|
glCullFace(GL_FRONT);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
QMatrix4x4 modelview;
|
|
|
|
modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
|
|
|
|
modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
|
|
|
|
modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
|
|
|
|
modelview.translate(0.0f, -0.2f, 0.0f);
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program->bind();
|
|
|
|
m_program->setUniformValue(matrixUniform, modelview);
|
|
|
|
m_program->setUniformValue(colorUniform, color);
|
2011-04-27 15:49:30 +00:00
|
|
|
paintQtLogo();
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program->release();
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
m_context->swapBuffers(surface);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
m_fAngle += 1.0f;
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::paintQtLogo()
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program->enableAttributeArray(normalAttr);
|
|
|
|
m_program->enableAttributeArray(vertexAttr);
|
|
|
|
m_program->setAttributeArray(vertexAttr, vertices.constData());
|
|
|
|
m_program->setAttributeArray(normalAttr, normals.constData());
|
2011-04-27 15:49:30 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program->disableAttributeArray(normalAttr);
|
|
|
|
m_program->disableAttributeArray(vertexAttr);
|
2011-04-27 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::initialize()
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
|
2011-04-27 15:49:30 +00:00
|
|
|
const char *vsrc =
|
|
|
|
"attribute highp vec4 vertex;\n"
|
|
|
|
"attribute mediump vec3 normal;\n"
|
|
|
|
"uniform mediump mat4 matrix;\n"
|
|
|
|
"uniform lowp vec4 sourceColor;\n"
|
|
|
|
"varying mediump vec4 color;\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
|
|
|
|
" float angle = max(dot(normal, toLight), 0.0);\n"
|
|
|
|
" vec3 col = sourceColor.rgb;\n"
|
|
|
|
" color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
|
|
|
|
" color = clamp(color, 0.0, 1.0);\n"
|
|
|
|
" gl_Position = matrix * vertex;\n"
|
|
|
|
"}\n";
|
|
|
|
vshader->compileSourceCode(vsrc);
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
|
2011-04-27 15:49:30 +00:00
|
|
|
const char *fsrc =
|
|
|
|
"varying mediump vec4 color;\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_FragColor = color;\n"
|
|
|
|
"}\n";
|
|
|
|
fshader->compileSourceCode(fsrc);
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program = new QOpenGLShaderProgram;
|
|
|
|
m_program->addShader(vshader);
|
|
|
|
m_program->addShader(fshader);
|
|
|
|
m_program->link();
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
vertexAttr = m_program->attributeLocation("vertex");
|
|
|
|
normalAttr = m_program->attributeLocation("normal");
|
|
|
|
matrixUniform = m_program->uniformLocation("matrix");
|
|
|
|
colorUniform = m_program->uniformLocation("sourceColor");
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
m_fAngle = 0;
|
|
|
|
createGeometry();
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::createGeometry()
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
vertices.clear();
|
|
|
|
normals.clear();
|
|
|
|
|
|
|
|
qreal x1 = +0.06f;
|
|
|
|
qreal y1 = -0.14f;
|
|
|
|
qreal x2 = +0.14f;
|
|
|
|
qreal y2 = -0.06f;
|
|
|
|
qreal x3 = +0.08f;
|
|
|
|
qreal y3 = +0.00f;
|
|
|
|
qreal x4 = +0.30f;
|
|
|
|
qreal y4 = +0.22f;
|
|
|
|
|
|
|
|
quad(x1, y1, x2, y2, y2, x2, y1, x1);
|
|
|
|
quad(x3, y3, x4, y4, y4, x4, y3, x3);
|
|
|
|
|
|
|
|
extrude(x1, y1, x2, y2);
|
|
|
|
extrude(x2, y2, y2, x2);
|
|
|
|
extrude(y2, x2, y1, x1);
|
|
|
|
extrude(y1, x1, x1, y1);
|
|
|
|
extrude(x3, y3, x4, y4);
|
|
|
|
extrude(x4, y4, y4, x4);
|
|
|
|
extrude(y4, x4, y3, x3);
|
|
|
|
|
|
|
|
const qreal Pi = 3.14159f;
|
|
|
|
const int NumSectors = 100;
|
|
|
|
|
|
|
|
for (int i = 0; i < NumSectors; ++i) {
|
|
|
|
qreal angle1 = (i * 2 * Pi) / NumSectors;
|
|
|
|
qreal x5 = 0.30 * qSin(angle1);
|
|
|
|
qreal y5 = 0.30 * qCos(angle1);
|
|
|
|
qreal x6 = 0.20 * qSin(angle1);
|
|
|
|
qreal y6 = 0.20 * qCos(angle1);
|
|
|
|
|
|
|
|
qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors;
|
|
|
|
qreal x7 = 0.20 * qSin(angle2);
|
|
|
|
qreal y7 = 0.20 * qCos(angle2);
|
|
|
|
qreal x8 = 0.30 * qSin(angle2);
|
|
|
|
qreal y8 = 0.30 * qCos(angle2);
|
|
|
|
|
|
|
|
quad(x5, y5, x6, y6, x7, y7, x8, y8);
|
|
|
|
|
|
|
|
extrude(x6, y6, x7, y7);
|
|
|
|
extrude(x8, y8, x5, y5);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0;i < vertices.size();i++)
|
|
|
|
vertices[i] *= 2.0f;
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
vertices << QVector3D(x1, y1, -0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, -0.05f);
|
|
|
|
vertices << QVector3D(x4, y4, -0.05f);
|
|
|
|
|
|
|
|
vertices << QVector3D(x3, y3, -0.05f);
|
|
|
|
vertices << QVector3D(x4, y4, -0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, -0.05f);
|
|
|
|
|
|
|
|
QVector3D n = QVector3D::normal
|
|
|
|
(QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
vertices << QVector3D(x4, y4, 0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, 0.05f);
|
|
|
|
vertices << QVector3D(x1, y1, 0.05f);
|
|
|
|
|
|
|
|
vertices << QVector3D(x2, y2, 0.05f);
|
|
|
|
vertices << QVector3D(x4, y4, 0.05f);
|
|
|
|
vertices << QVector3D(x3, y3, 0.05f);
|
|
|
|
|
|
|
|
n = QVector3D::normal
|
|
|
|
(QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
vertices << QVector3D(x1, y1, +0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, +0.05f);
|
|
|
|
vertices << QVector3D(x1, y1, -0.05f);
|
|
|
|
|
|
|
|
vertices << QVector3D(x2, y2, -0.05f);
|
|
|
|
vertices << QVector3D(x1, y1, -0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, +0.05f);
|
|
|
|
|
|
|
|
QVector3D n = QVector3D::normal
|
|
|
|
(QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
}
|