3 Star 3 Fork 1

Forever Sun/NeHe_OpenGL_Qt5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
polygonwindow.cpp 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
蔡万苍 提交于 2014-06-30 23:38 . Fix delete buffers.
#include "polygonwindow.h"
PolygonWindow::PolygonWindow(QWindow *parent) :
OpenGLWindow(parent), m_program(NULL)
{
}
PolygonWindow::~PolygonWindow()
{
glDeleteBuffers(2,&m_vboIds[0]);
}
void PolygonWindow::initialize()
{
initGeometry();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepthf(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shader/vertshader.glsl");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shader/fragshader.glsl");
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
}
void PolygonWindow::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_program->bind();
m_modelView.setToIdentity();
m_modelView.translate(-1.5f, 0.0f, -6.0f);
m_program->setUniformValue("mvpMatrix", m_projection * m_modelView);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
m_program->enableAttributeArray(m_posAttr);
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, 3);
m_modelView.translate(3.0f, 0.0f, 0.0f);
m_program->setUniformValue("mvpMatrix", m_projection * m_modelView);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
m_program->enableAttributeArray(m_posAttr);
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 3);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
m_program->release();
}
void PolygonWindow::initGeometry()
{
glGenBuffers(2, &m_vboIds[0]);
GLfloat triangleVertices[] = {
0.0f, 1.0f, 0.0f,
-1.0f,-1.0f, 0.0f,
1.0f,-1.0f, 0.0f,
};
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
GLfloat quadVertices[] = {
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f,-1.0f, 0.0f,
-1.0f,-1.0f, 0.0f
};
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/forsun/NeHe_OpenGL_Qt5.git
[email protected]:forsun/NeHe_OpenGL_Qt5.git
forsun
NeHe_OpenGL_Qt5
NeHe_OpenGL_Qt5
master

搜索帮助