opengl和Qt5.0.2
openGL的初学者,尝试把opengl与Qt结合
程式目的 : 画一个三角形
平台 : mac osx 10.8.3
编译器 : clang3.2
问题有二
1 : 无法显示三角形
2 : 关闭视窗后程式不会跟着关闭
.hpp
.cpp
main.cpp
程式目的 : 画一个三角形
平台 : mac osx 10.8.3
编译器 : clang3.2
问题有二
1 : 无法显示三角形
2 : 关闭视窗后程式不会跟着关闭
.hpp
#include <QGLWidget>
#include <QtGui/QOpenGLFunctions>
#include <QtGui/QOpenGLShaderProgram>
class QWidget;
class ch1HelloTriangle : public QGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit ch1HelloTriangle(QWidget *parent = 0);
protected:
virtual void initializeGL();
void initShaders();
void InitializeVertexBuffer();
virtual void resizeGL(int w, int h);
virtual void paintGL();
private:
QOpenGLShaderProgram program;
GLuint positionBufferObject;
};
.cpp
#include <locale.h>
#include <QWidget>
#include "ch1HelloTriangle.hpp"
namespace
{
float const vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};
}
ch1HelloTriangle::ch1HelloTriangle(QWidget *parent) :
QGLWidget(parent)
{
}
void ch1HelloTriangle::initializeGL()
{
initializeOpenGLFunctions();
InitializeVertexBuffer();
initShaders();
}
void ch1HelloTriangle::initShaders()
{
// Override system locale until shaders are compiled
setlocale(LC_NUMERIC, "C");
// Compile vertex shader
if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n"))
{
close();
}
// Compile fragment shader
if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment,
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
" outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\n"))
{
close();
}
// Link shader pipeline
if (!program.link())
close();
// Bind shader pipeline for use
if (!program.bind())
close();
// Restore system locale
setlocale(LC_ALL, "");
}
void ch1HelloTriangle::InitializeVertexBuffer()
{
glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void ch1HelloTriangle::resizeGL(int w, int h)
{
// Set OpenGL viewport to cover whole widget
glViewport(0, 0, w, h);
}
void ch1HelloTriangle::paintGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
int vertexLocation = program.attributeLocation("position");
program.enableAttributeArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
main.cpp
#include <QApplication>
#include "ch1HelloTriangle.hpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ch1HelloTriangle ch1;
ch1.show();
return a.exec();
}
...全文
相关推荐
vtk7.0-qt5.7-x86(32bit)-opengl2-vs2015-开发包debug版本
vtk7.0-qt5.7开发包debug版本,由cmake生成vs2015项目后编译而成,含bin, cmake, include, lib, plugins, share 6个子目录
基于Opencv2.4.11+OpenGL(Qt5.6.0)实现增强现实(四)
坐标系的变换实现增强现实 在进行下一步之前需要对相机进行标定,这里网上有很多标定的方案,本文采用的是基于平面方格点的方式求得相机的内参与畸变参数。求解的最终方程如下 其中u,v为物体在成像面上的坐标,Xw为物体在世界坐标系中的坐标,M1为相机的内部参数,M2为相机的外部参数会随着物体的位置或者相机的位置的变换而变化。通过标定可以求得相机的内部参数矩阵与畸变参数。如果我们反过来进行思
ubuntu下 qt5.5 利用opengl画3d坐标系
ubuntu16.04 下在qt 5.9.0 下采用opengl 利用glut画3维坐标系
vtk7.0-qt5.7-x86(32bit)-opengl2-vs2015-开发包
vtk7.0-qt5.7的 32位开发包,由cmake生成vs2015项目文件后编译出来,包含 bin, cmake, include, lib, plugins, share 6个子目录
Qt 5.2.0 正式版帮助文件(.qch)
从Qt5.2.0正式版中提取出的帮助文件(.qch) 主要用于自行编译Qt没有帮助文件的问题,在QtCreator中添加即可


