QtOPenGL入门教程之六基于QOpenGLWidget3D立方体纹理贴图

QtOPenGL入门教程之六基于QOpenGLWidget3D立方体纹理贴图


2024年1月5日发(作者:)

// program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/"); program->addShader(vshader); program->addShader(fshader); program->bindAttributeLocation("vPosition", PROGRAM_VERTEX_ATTRIBUTE); program->bindAttributeLocation("vTexCoord", PROGRAM_TEXCOORD_ATTRIBUTE); program->link(); program->bind(); program->setUniformValue("texture", 0);}void MyOpenGLWidget::resizeGL(int width , int height){ int side = qMin(width, height); glViewport((width - side) / 2, (height - side) / 2, side, side);}void MyOpenGLWidget::paintGL(){ int w = width(); int h = height(); int side = qMin(w, h); glViewport((w - side) / 2, (h - side) / 2, side, side); glClearColor((), (), (), ()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);#ifdef PROGRAM_2D // 顶点位置 GLfloat vertices[2][4][3] = { { {-0.8f, 0.8f, 0.8f}, {-0.8f, -0.8f, 0.8f}, {0.8f, -0.8f, 0.8f}, {0.8f, 0.8f, 0.8f} }, { {0.8f, 0.8f, 0.8f}, {0.8f, -0.8f, 0.8f}, {0.8f, -0.8f, -0.8f}, {0.8f, 0.8f, -0.8f} } }; GLfloat vertices1[2][4][5] = { { {-0.8f, 0.8f, 0.8f,0.0f, 1.0f}, {-0.8f, -0.8f, 0.8f,0.0f, 0.0f}, {0.8f, -0.8f, 0.8f, 1.0f, 0.0f}, {0.8f, 0.8f, 0.8f, 1.0f, 1.0f} }, { {0.8f, 0.8f, 0.8f, 0.0f, 1.0f}, {0.8f, -0.8f, 0.8f, 0.0f, 0.0f}, {0.8f, -0.8f, -0.8f, 1.0f, 0.0f}, {0.8f, 0.8f, -0.8f, 1.0f, 1.0f} } }; (); (); te(vertices1, 40*sizeof(GLfloat)); GLuint attr = -1; attr = program->attributeLocation("vPosition");// program->setAttributeBuffer(vPosition, GL_FLOAT, 0, 3, 0); program->setAttributeBuffer(attr, GL_FLOAT, 0, 3, sizeof(GLfloat) * 5); glEnableVertexAttribArray(attr); attr = program->attributeLocation("vTexCoord"); program->setAttributeBuffer(attr, GL_FLOAT, 3*sizeof(GLfloat), 2, sizeof(GLfloat) * 5); glEnableVertexAttribArray(attr);// GLuint vPosition = program->attributeLocation("vPosition");// program->setAttributeBuffer(vPosition, GL_FLOAT, 0, 3, 0);// glEnableVertexAttribArray(vPosition); // 纹理坐标 GLfloat coords[2][4][2] = { { {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f} }, { {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f} } };// (24*sizeof(GLfloat), coords, 16*sizeof(GLfloat));

// (24*sizeof(GLfloat), coords, 16*sizeof(GLfloat));// GLuint vTexCoord = program->attributeLocation("vTexCoord");// program->setAttributeBuffer(vTexCoord, GL_FLOAT, 24*sizeof(GLfloat), 2, 0);// glEnableVertexAttribArray(vTexCoord); program->setUniformValue("tex", 0);#endif QMatrix4x4 matrix; ctive(45.0f, (GLfloat)w/(GLfloat)h, 0.1f, 100.0f); ate(0, 0, translate); (-60, 0, 1, 0); //绕Y轴逆时针旋转 (xRot / 16.0f, 1.0f, 0.0f, 0.0f); (yRot / 16.0f, 0.0f, 1.0f, 0.0f); (zRot / 16.0f, 0.0f, 0.0f, 1.0f); program->setUniformValue("matrix", matrix);#ifdef PROGRAM_2D#else program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE); program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat)); program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));#endif // 绘制#ifdef PROGRAM_2D for(int i=0; i<2; i++)#else for(int i=0; i<6; i++)#endif { textures[i]->bind(); glDrawArrays(GL_TRIANGLE_FAN, i*4, 4); }}void MyOpenGLWidget::mousePressEvent(QMouseEvent *event){ lastPos = event->pos();}void MyOpenGLWidget::mouseMoveEvent(QMouseEvent *event){ int dx = event->x() - lastPos.x(); int dy = event->y() - lastPos.y(); if (event->buttons() & Qt::LeftButton) { rotateBy(8 * dy, 8 * dx, 0); } else if (event->buttons() & Qt::RightButton) { rotateBy(8 * dy, 0, 8 * dx); } lastPos = event->pos();}void MyOpenGLWidget::mouseReleaseEvent(QMouseEvent *event){ emit clicked();}void MyOpenGLWidget::makeObject(){ static const int coords[6][4][3] = { { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } }, { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },

{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } }, { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } }, { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } }, { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } }, { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } }; for (int j = 0; j < 6; ++j) textures[j] = new QOpenGLTexture(QImage(QString(":/side%").arg(j + 1)).mirrored()); QVector vertData; for (int i = 0; i < 6; ++i) { for (int j = 0; j < 4; ++j) { // vertex position (0.5 * coords[i][j][0]); (0.5 * coords[i][j][1]); (0.5 * coords[i][j][2]); // texture coordinate (j == 0 || j == 3); (j == 0 || j == 1); } } (); (); te(ata(), () * sizeof(GLfloat));}void MyOpenGLWidget::printContextInformation(){ QString glType; QString glVersion; QString glProfile; // Get Version Information glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL"; glVersion = reinterpret_cast(glGetString(GL_VERSION)); // Get Profile Information #define CASE(c) case QSurfaceFormat::c: glProfile = #c; break switch (format().profile()) { CASE(NoProfile); CASE(CoreProfile); CASE(CompatibilityProfile); } #undef CASE // qPrintable() will print our QString w/o quotes around it. qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")";}#include #include "myopenglwidget.h"int main(int argc, char *argv[]){ Q_INIT_RESOURCE(textures); QApplication app(argc,argv); QSurfaceFormat format; thBufferSize(24); QSurfaceFormat::setDefaultFormat(format); MyOpenGLWidget w;


发布者:admin,转转请注明出处:http://www.yc00.com/news/1704447391a1351720.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信