vbo.create(); vbo.bind(); vbo.allocate(vertices,sizeof(vertices));
时间: 2024-02-26 08:53:45 浏览: 20
这是使用OpenGL中的VBO(Vertex Buffer Object)创建和绑定缓冲区的代码。
VBO是OpenGL中一种用于加速渲染的机制,它可以将顶点数据等渲染数据存储在显存中,避免了每次渲染时从内存中读取数据的开销,从而提高了渲染效率。
在这段代码中,首先使用vbo.create()创建一个新的缓冲区对象,并将其绑定到OpenGL的当前上下文中。然后,使用vbo.allocate()将顶点数据vertices写入缓冲区中。其中,sizeof(vertices)表示vertices数组所占用的字节数,告诉OpenGL缓冲区需要分配多大的空间。最后,通过vbo.bind()将缓冲区绑定到当前OpenGL的顶点缓冲区对象中,以便在绘制时使用。
相关问题
Qt OpenGL 读取OBJ模型文件
要在Qt OpenGL中读取OBJ模型文件,可以使用Qt自带的QOpenGLFunctions库。
首先,需要在项目文件中添加以下依赖:
```
QT += opengl
```
然后,可以使用QOpenGLFunctions类来加载模型文件和绘制模型。
以下是一个简单的示例代码,可以读取和绘制一个OBJ模型文件:
```c++
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QVector3D>
#include <QVector2D>
#include <QFile>
#include <QStringList>
struct VertexData {
QVector3D position;
QVector2D texCoord;
};
class ObjModel : protected QOpenGLFunctions
{
public:
ObjModel();
virtual ~ObjModel();
void init(QString filename);
void render();
private:
QOpenGLShaderProgram m_program;
QOpenGLBuffer m_vbo;
int m_vertexCount;
};
ObjModel::ObjModel()
: m_vertexCount(0)
{
}
ObjModel::~ObjModel()
{
m_vbo.destroy();
}
void ObjModel::init(QString filename)
{
initializeOpenGLFunctions();
// Load OBJ file
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QVector<QVector3D> positions;
QVector<QVector2D> texCoords;
while (!file.atEnd()) {
QByteArray line = file.readLine().trimmed();
QList<QByteArray> tokens = line.split(' ');
if (tokens.isEmpty())
continue;
if (tokens[0] == "v") {
positions.append(QVector3D(tokens[1].toFloat(), tokens[2].toFloat(), tokens[3].toFloat()));
} else if (tokens[0] == "vt") {
texCoords.append(QVector2D(tokens[1].toFloat(), tokens[2].toFloat()));
} else if (tokens[0] == "f") {
for (int i = 1; i < tokens.size(); ++i) {
QList<QByteArray> face = tokens[i].split('/');
VertexData data;
data.position = positions[face[0].toInt() - 1];
data.texCoord = texCoords[face[1].toInt() - 1];
m_vertices.append(data);
}
}
}
m_vertexCount = m_vertices.size();
// Create VBO
m_vbo.create();
m_vbo.bind();
m_vbo.allocate(m_vertices.constData(), m_vertexCount * sizeof(VertexData));
// Load shader program
m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/obj.vert");
m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/obj.frag");
m_program.link();
}
void ObjModel::render()
{
m_program.bind();
m_vbo.bind();
m_program.enableAttributeArray("position");
m_program.enableAttributeArray("texCoord");
m_program.setAttributeBuffer("position", GL_FLOAT, offsetof(VertexData, position), 3, sizeof(VertexData));
m_program.setAttributeBuffer("texCoord", GL_FLOAT, offsetof(VertexData, texCoord), 2, sizeof(VertexData));
glDrawArrays(GL_TRIANGLES, 0, m_vertexCount);
m_vbo.release();
m_program.release();
}
```
在上面的示例代码中,我们使用QFile类来读取OBJ文件,然后使用QOpenGLBuffer类创建一个VBO,并将OBJ文件中的顶点数据存储到VBO中。最后,使用QOpenGLShaderProgram类加载并绑定着色器程序,并使用glDrawArrays函数绘制模型。
注意,上面的代码仅仅是一个简单的示例,不足以处理所有的OBJ文件。在实际开发中,还需要对OBJ文件中的各种情况进行判断和处理。
QOpenGLShaderProgram 空心矩形, 完善代码列子
以下是完整的代码示例:
```
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
class GLWidget : public QOpenGLWidget
{
public:
GLWidget(QWidget *parent = nullptr) : QOpenGLWidget(parent), m_program(this)
{}
protected:
void initializeGL() override
{
// Initialize the OpenGL function pointers.
initializeOpenGLFunctions();
// Compile the vertex shader.
m_program.addShaderFromSourceCode(QOpenGLShader::Vertex,
"#version 330\n"
"in vec2 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n");
// Compile the fragment shader.
m_program.addShaderFromSourceCode(QOpenGLShader::Fragment,
"#version 330\n"
"uniform vec4 color;\n"
"out vec4 fragmentColor;\n"
"void main()\n"
"{\n"
" fragmentColor = color;\n"
"}\n");
// Link the program.
m_program.link();
// Create a vertex array object.
m_vao.create();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
// Create a vertex buffer.
m_vbo.create();
m_vbo.bind();
float vertices[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f
};
m_vbo.allocate(vertices, sizeof(vertices));
m_program.setAttributeBuffer("position", GL_FLOAT, 0, 2);
m_program.enableAttributeArray("position");
// Set the clear color.
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
}
void paintGL() override
{
// Clear the framebuffer.
glClear(GL_COLOR_BUFFER_BIT);
// Bind the vertex array object and the shader program.
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_program.bind();
// Draw a hollow rectangle.
m_program.setUniformValue("color", QColor(Qt::white));
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_QUADS, 0, 4);
// Swap the buffers.
swapBuffers();
}
private:
QOpenGLShaderProgram m_program;
QOpenGLVertexArrayObject m_vao;
QOpenGLBuffer m_vbo;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GLWidget widget;
widget.show();
return app.exec();
}
```
此代码将创建一个空心矩形并使用OpenGL着色器绘制它。可以在 `initializeGL` 和 `paintGL` 函数中看到着色器的编写和如何将形状添加到顶点缓冲区中进行渲染。在 `paintGL` 函数中,`glPolygonMode` 函数被用于将渲染方式从填充到线框切换。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)