QT+OpenGL绘制

QT中已经集成了OpenGL的功能,下面利用OpenGL来实现第一个三角形的绘制。
先看效果:
QT+OpenGL绘制_第1张图片

#pragma once

#include 
#include 
#include 

class BBBOpenGLWgt : public QOpenGLWidget, protected QOpenGLFunctions {
	Q_OBJECT

public:
	BBBOpenGLWgt(QWidget *parent);
	~BBBOpenGLWgt();
private:
	void init();
private:
	virtual void initializeGL() override;
	virtual void paintGL()override;
	virtual void resizeGL(int w, int h)override;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
private:

	//三角形的三点坐标
	GLfloat triangle[9] = {
		-1.0, 0.0, 0.0,
		1.0, 0.0,0.0,
		0.0, 1.0,0.0
	};

	//三角形三个顶点的颜色值
	GLfloat color[9] = {
		1.0,0.0,0.0,
		0.0,1.0,0.0,
		0.0,0.0,1.0,
	};

	QOpenGLShaderProgram* _shaderProgram;
};

#include "BBBOpenGLWgt.h"
#include 
#include 

#include 	

BBBOpenGLWgt::BBBOpenGLWgt(QWidget *parent)
	: QOpenGLWidget(parent) {
	
}

BBBOpenGLWgt::~BBBOpenGLWgt() {
}

void BBBOpenGLWgt::init() {
	//顶点着色器
	QOpenGLShader* vertShader = new QOpenGLShader(QOpenGLShader::Vertex, this);
	bool b = vertShader->compileSourceFile("E:\\Projects\\Test\\QtTest64\\QtGuiOPenGL\\shader\\test.vert");

	//片元着色器
	QOpenGLShader* fragShader = new QOpenGLShader(QOpenGLShader::Fragment, this);
	fragShader->compileSourceFile("shader\\test.frag");

	_shaderProgram = new QOpenGLShaderProgram;
	//添加着色器
	_shaderProgram->addShader(vertShader);
	_shaderProgram->addShader(fragShader);

	//对着色器进行链接
	_shaderProgram->link();
	//把着色器绑定到上下文
	_shaderProgram->bind();
}

void BBBOpenGLWgt::initializeGL() {
	//qDebug() << "initializeGL";
	initializeOpenGLFunctions();

	//glClearColor(0.98, 0.625, 0.12, 0.5);
	glClearColor(0.0, 0.0, 0.0, 0.5);

	init();
}

void BBBOpenGLWgt::paintGL() {
	//qDebug() << "paintGL";
	QOpenGLBuffer buffer;
	//创建缓存区对象
	buffer.create();
	//绑定上下文
	buffer.bind();

	buffer.allocate(triangle, 18 * sizeof(GLfloat));
	GLuint vPosition = _shaderProgram->attributeLocation("VertexPosition");
	_shaderProgram->setAttributeBuffer(vPosition, GL_FLOAT, 0, 3, 0);
	glEnableVertexAttribArray(vPosition);

	buffer.write(9 * sizeof(GLfloat), color, 9 * sizeof(GLfloat));
	GLuint vColor = _shaderProgram->attributeLocation("VertexColor");
	_shaderProgram->setAttributeBuffer(vColor, GL_FLOAT, 9 * sizeof(GLfloat), 3, 0);
	glEnableVertexAttribArray(vColor);
	glDrawArrays(GL_TRIANGLES, 0, 3);
}

void BBBOpenGLWgt::resizeGL(int w, int h) {
	//qDebug() << "resizeGL";

}

shader文件
test.frag

#version 130
in vec3 Color;
out vec4 FragColor;

void main()
{
    FragColor = vec4( Color, 1.0);
}

test.vert

#version 130
in vec3 VertexPosition;
in vec3 VertexColor;

out vec3 Color;

void main()
{
    Color = VertexColor;
    gl_Position = vec4( VertexPosition, 1.0 );
}

aaa

你可能感兴趣的:(qt,OpenGL)