代码拉取完成,页面将自动刷新
#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);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。