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
发布者:admin,转转请注明出处:http://www.yc00.com/news/1704447391a1351720.html
评论列表(0条)