当前位置:网站首页>OpenGL 绘制彩色的三角形
OpenGL 绘制彩色的三角形
2022-07-03 11:06:00 【wb175208】
使用openGL绘制图形需要三步:
1.创建一个VBO,使内存中的数据,存到显卡缓存中;VBO的任务就是这一件事;
2.创建一VAO,也就是属性的数组,负责把现存中的的数据,绑定属性,指定数据的使用规则,使着色器明白那些数据是颜色,那些是坐标;
3.着色器程序,运行在GPU中,负责把现存中的数据,通过计算渲染出图片;
绘制一个顶点颜色不同的三角形,需要给三角形的每隔顶点设置一个颜色,这也就需要一些颜色数据。我们可以单独定义一下顶点数据和颜色数据。
GLfloat ver[] = {
//如果话两个三角形的话,常规画法,定义两个三角形的位置点
//第一个三角形
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
GLfloat verColor[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
看一下绘制效果:
#pragma once
#include <QOpenGLWindow>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
class QOpenGLFunctions_3_3_Core;
class HelloShaderSelf : public QOpenGLWindow {
Q_OBJECT
public:
HelloShaderSelf();
~HelloShaderSelf();
private:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
QOpenGLFunctions_3_3_Core* _openGLCore;
GLuint _VBO;//顶点位置VBO
GLuint _VBOColor;//顶点颜色VBO
GLuint _VAO;
QOpenGLShaderProgram _shaderProgram;//着色器程序,所里系统所有的着色器
};
#include <QOpenGLFunctions_3_3_Core>
#include <QVector4D>
#include <QMath.h>
#include "HelloShaderSelf.h"
HelloShaderSelf::HelloShaderSelf() {
}
HelloShaderSelf::~HelloShaderSelf() {
}
void HelloShaderSelf::initializeGL() {
_openGLCore = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
GLfloat ver[] = {
//如果话两个三角形的话,常规画法,定义两个三角形的位置点
//第一个三角形
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
GLfloat verColor[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
_openGLCore->glGenBuffers(1, &_VAO);
_openGLCore->glGenBuffers(1, &_VBO);
_openGLCore->glGenBuffers(1, &_VBOColor);
_openGLCore->glBindVertexArray(_VAO);
//把定位位置数据写入显卡缓存
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, _VBO);
_openGLCore->glBufferData(GL_ARRAY_BUFFER, sizeof(ver), ver, GL_STATIC_DRAW);
_openGLCore->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
_openGLCore->glEnableVertexAttribArray(0);
//把顶点颜色数据写入显卡缓存
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, _VBOColor);
_openGLCore->glBufferData(GL_ARRAY_BUFFER, sizeof(verColor), verColor, GL_STATIC_DRAW);
_openGLCore->glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)0);
_openGLCore->glEnableVertexAttribArray(1);
_openGLCore->glBindVertexArray(0);
QOpenGLShader vertexShager(QOpenGLShader::Vertex);//顶点着色器
vertexShager.compileSourceFile("E:/Projects/QtGuiTest/OPenGLApp/shader/HelloShaderSelf.vert");
QOpenGLShader fragmentShager(QOpenGLShader::Fragment);//片段着色器
fragmentShager.compileSourceFile("E:/Projects/QtGuiTest/OPenGLApp/shader/HelloShaderSelf.frag");
_shaderProgram.addShader(&vertexShager);
_shaderProgram.addShader(&fragmentShager);
_shaderProgram.link();
}
void HelloShaderSelf::resizeGL(int w, int h) {
}
void HelloShaderSelf::paintGL() {
_openGLCore->glClearColor(0.6f, 0.6f, 0.6f, 1.0);
_openGLCore->glClear(GL_COLOR_BUFFER_BIT);
_shaderProgram.bind();
_openGLCore->glBindVertexArray(_VAO);
_openGLCore->glDrawArrays(GL_TRIANGLES, 0, 3);
update();
}
顶点着色器:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec4 aColor;
out vec4 outColor;
void main(){
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
outColor = aColor;
}
片元着色器:
#version 330 core
out vec4 fragColor;
in vec4 outColor;//从顶点着色器中传过来的颜色
void main(){
fragColor = outColor;
}
aaa
边栏推荐
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
- POI excel cell wrap
- Programmers' entrepreneurial trap: taking private jobs
- Duplicate numbers in the array of sword finger offer 03
- 【学习笔记】dp 状态与转移
- Excel快速跨表复制粘贴
- Kubernetes 三打探针及探针方式
- 银泰百货点燃城市“夜经济”
- 2022年湖南工学院ACM集训第二次周测题解
- Qt+VTK+OCCT读取IGES/STEP模型
猜你喜欢
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
cgroup简介
机器学习 3.2 决策树模型 学习笔记(待补)
PHP基础
Groovy测试类 和 Junit测试
AOSP ~ NTP ( 网络时间协议 )
Machine learning 3.2 decision tree model learning notes (to be supplemented)
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
Hongmeng third training (project training)
外插散点数据
随机推荐
Dynamic programming (interval DP)
Excel快速跨表复制粘贴
Yintai department store ignites the city's "night economy"
解决msvcp120d.dll和msvcr120d.dll缺失
836. Merge sets (day 63) and search sets
. \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
Some common terms
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
STL Tutorial 9 deep copy and shallow copy of container elements
C language AES encryption and decryption
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
libvirt 中体验容器
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
Vulnhub geminiinc V2
VS2015的下载地址和安装教程
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
How should intermediate software designers prepare for the soft test
How to make others fear you
vulnhub之Ripper
《剑指offer 04》二维数组查找