程序代写案例-CS 8001/ECE
时间:2021-04-24
Joshua Fraser and K. Palaniappan
OpenGL and Qt
Visual Computing CS 8001/ECE 8001
OpenGL and Qt
Creating an OpenGL Widget
• Similar to our first Qt project:

• Create GUI main window project

• Add OpenGL widget and layout to main window

• Create a subclass of QOpenGLWidget

• Promote central widget to be of our derived class
OpenGL and Qt
Subclassing QOpenGLWidget
• QOpenGLWidget is base class for rending OpenGL in Qt applications

• Three main functions will be overridden:

• void paintGL()

• void resizeGL(int w, int h)

• void initializeGL()

• To use any OpenGL functionality outside of these functions, the OpenGL context must first be
made current

• makeCurrent() and doneCurrent()

• Context may not be valid until initializeGL() is called. Don’t try to use OpenGL in constructor
OpenGL and Qt
OpenGL Function Access
• Qt abstracts access to OpenGL functions

• Don’t need OpenGL includes

• Two ways to access functions:

• Through context

• QOpenGLContext::currentContext()->functions()->glClearColor(0, 0, 0, 0);

• By also deriving from QOpenGLFunctions and resolving in initializeGL() with
initializeOpenGLFunctions();
OpenGL and Qt
Sample Drawing Code
• Sample paintGL() taken from
QOpenGLShaderProgram
documentation
void ViewWidget::paintGL()
{
QOpenGLShaderProgram program;
program.addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute highp vec4 vertex;\n"
"uniform highp mat4 matrix;\n"
"void main(void)\n"
"{\n"
" gl_Position = matrix * vertex;\n"
"}");
program.addShaderFromSourceCode(QOpenGLShader::Fragment,
"uniform mediump vec4 color;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = color;\n"
"}");
program.link();
program.bind();

int vertexLocation = program.attributeLocation("vertex");
int matrixLocation = program.uniformLocation("matrix");
int colorLocation = program.uniformLocation("color");

static GLfloat const triangleVertices[] = {
60.0f, 10.0f, 0.0f,
110.0f, 110.0f, 0.0f,
10.0f, 110.0f, 0.0f
};

QColor color(0, 255, 0, 255);

QMatrix4x4 pmvMatrix;
pmvMatrix.ortho(rect());

program.enableAttributeArray(vertexLocation);
program.setAttributeArray(vertexLocation, triangleVertices, 3);
program.setUniformValue(matrixLocation, pmvMatrix);
program.setUniformValue(colorLocation, color);

glDrawArrays(GL_TRIANGLES, 0, 3);

program.disableAttributeArray(vertexLocation);
}






























































































学霸联盟


essay、essay代写