QT openglwidget实现简单的3D绘制显示和纹理贴图

QT openglwidget实现简单的3D绘制显示和纹理贴图_第1张图片`

头文件

#ifndef OPENWIDGET_H
#define OPENWIDGET_H

#include 
#include"qopenglwidget.h"
#include
#include
#include
class QOpenGLShaderProgram;
class QOpenGLTexture;
class OpenWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit OpenWidget(QWidget *parent = nullptr);

//signals:

//public slots:
public:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void keyPressEvent(QKeyEvent*event);
private:
  QOpenGLShaderProgram *program;
  QOpenGLBuffer vbo;
  QOpenGLTexture* texture[2];
  GLfloat translate,xRot,yRot,zRot;
};

#endif // OPENWIDGET_H`

cpp

#include "openwidget.h"
#include
#include
#include
#include
OpenWidget::OpenWidget(QWidget *parent) : QOpenGLWidget(parent)
{
translate=-6.0;
xRot=zRot=0.0;
yRot=-30.0;
}

void OpenWidget::initializeGL()
{
	initializeOpenGLFunctions();
    glEnable(GL_DEPTH_TEST);
     texture[0]=new QOpenGLTexture(QImage(QString(":/myopengl/side%1.bmp")));
     texture[1]=new QOpenGLTexture(QImage(QString(":/myopengl/side%2.bmp")));
   QOpenGLShader *vshader=new QOpenGLShader(QOpenGLShader::Vertex,this);
   const char *vsrc=
           "#version 150                               \n"
           "in vec4 vPosition;                         \n"
           "in vec4 vTexCoord;                         \n"
           "out vec4 texCoord;                         \n"
           "uniform mat4 matrix;                       \n"
           "void main(){                               \n"
           "texCoord=vTexCoord;                        \n"
           "gl_Position=matrix*vPosition;              \n"
           "}                                          \n";
   if (!vshader->compileSourceCode(vsrc))
   {
	   return;
   }
   QOpenGLShader *fshader=new QOpenGLShader(QOpenGLShader::Fragment,this);
   const char *fsrc=
           "#version 150                              \n"
           "uniform sampler2D tex;                    \n"
           "in vec4 texCoord;                         \n"
           "out vec4 fColor;                          \n"
           "void main(){                              \n"
           "fColor=texture(tex,vec2(texCoord));       \n"
           "}                                         \n";
   if (!fshader->compileSourceCode(fsrc))
  {
  return;
  }
  program=new QOpenGLShaderProgram(this);
  if (!program->addShader(vshader))
  {
	  return;
  }
  if (!program->addShader(fshader))
  {
	  return;
  }
  if (!program->link())
  {
	  return;
  }
  if (!program->bind())
  {
	  return;
  }

}

void OpenWidget::resizeGL(int w, int h)
{

}

void OpenWidget::paintGL()
{
        int w=width();
        int h=height();
        int side=qMin(w,h);
        glViewport((w-side)/2,(h-side)/2,side,side);
        glClear(GL_COLOR_BUFFER_BIT|GL_ACCUM_BUFFER_BIT);
    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}},
    };
    vbo.create();
    vbo.bind();
    vbo.allocate(vertices,48*sizeof(GLfloat));
   GLuint vPosition =program->attributeLocation("vPosition");
   //glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, vertices);
   glEnableVertexAttribArray(vPosition);
   program->setAttributeBuffer(vPosition,GL_FLOAT,0,3,0);
  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}}
  };
  vbo.write(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);
  //透视投影效果
  QMatrix4x4 matrix;
  matrix.perspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
  matrix.translate(0,0,translate);
  matrix.rotate(xRot,1.0,0.0,0.0);
  matrix.rotate(yRot,0.0,1.0,0.0);
  matrix.rotate(zRot,0.0,0.0,1.0);
  program->setUniformValue("matrix",matrix);
  for(int i=0;i<2;i++)
 { texture[i]->bind();
  glDrawArrays(GL_TRIANGLE_FAN, i*4, 4);
 }
  //    glDrawArrays(GL_POINTS,0,1);
}

void OpenWidget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {case Qt::Key_Up:
        xRot+=10;
        break;
    case Qt::Key_Left:
            yRot+=10;
            break;
    case Qt::Key_Right:
            zRot+=10;
            break;
    case Qt::Key_Down:
            translate-=1;
            break;
    case Qt::Key_Space:
            translate+=1;
            break;
    default:
        break;

    }
    update();
    QOpenGLWidget::keyPressEvent(event);
}

版本环境QT 5.13.2 msvc2015-64

你可能感兴趣的:(qt,opengl)