Skip to content

Instantly share code, notes, and snippets.

@MiLk
Created August 29, 2013 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiLk/6374829 to your computer and use it in GitHub Desktop.
Save MiLk/6374829 to your computer and use it in GitHub Desktop.
OpenGL w/ Qt5
#include "mainwindow.h"
#include <QtGui/QMatrix4x4>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QScreen>
#include <QGLContext>
MainWindow::MainWindow() :
m_program(0)
, m_frame(0)
{
}
MainWindow::~MainWindow()
{
if ( m_texture )
{
glDeleteTextures( 1, &m_texture );
m_texture = 0;
}
}
/*static const char *vertexShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute lowp vec4 colAttr;\n"
"varying lowp vec4 col;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";*/
static const char *vertexTxtShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute mediump vec4 texCoord;\n"
"varying mediump vec4 texc;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" texc = texCoord;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";
/*static const char *fragmentShaderSource =
"varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";*/
static const char *fragmentTxtShaderSource =
"uniform sampler2D texture;\n"
"varying mediump vec4 texc;\n"
"void main() {\n"
" gl_FragColor = texture2D(texture, texc.st);\n"
"}\n";
GLuint MainWindow::loadShader(GLenum type, const char *source)
{
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, 0);
glCompileShader(shader);
return shader;
}
void MainWindow::initialize()
{
glEnable(GL_TEXTURE_2D);
m_program = new QOpenGLShaderProgram(this);
//m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
//m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexTxtShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentTxtShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
//m_colAttr = m_program->attributeLocation("colAttr");
m_texCoord = m_program->attributeLocation("texCoord");
m_matrixUniform = m_program->uniformLocation("matrix");
m_program->setUniformValue("texture", 0);
m_texture = QGLContext::fromOpenGLContext(getContext())->bindTexture(QImage("Caisse.jpg"), GL_TEXTURE_2D);
}
void MainWindow::render()
{
const qreal retinaScale = devicePixelRatio();
glViewport(0, 0, width() * retinaScale, height() * retinaScale);
glClear(GL_COLOR_BUFFER_BIT);
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60, 4.0/3.0, 0.1, 100.0);
matrix.translate(0, 0, -2);
matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0.1f);
m_program->setUniformValue(m_matrixUniform, matrix);
GLfloat vertices[] = {
0.0f, 0.707f,
-0.5f, -0.5f,
0.5f, -0.5f,
};
/*GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};*/
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(m_texCoord, 2, GL_FLOAT, GL_FALSE, 0, vertices);
//glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindTexture(GL_TEXTURE_2D, 0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
m_program->release();
++m_frame;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment