1. Code part
OPenGL.pro
QT += core gui opengl greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = OPenGL12 TEMPLATE = app SOURCES += main.cpp\ openglwidget.cpp HEADERS += \ openglwidget.h LIBS += -lopengl32
OpenglWidget.h
#ifndef OPENGLWIDGET_H #define OPENGLWIDGET_H #include <QtOpenGL> class OpenglWidget : public QGLWidget { public: OpenglWidget(QWidget *parent=0); protected: void initializeGL(); void initWidget(); void paintGL(); void resizeGL(int width, int height); private: GLfloat mx; GLfloat my; GLfloat mz; GLfloat m_rotateTriangle; GLuint *texture; }; #endif // OPENGLWIDGET_H
OpenglWidget.cpp
#include "openglwidget.h" OpenglWidget::OpenglWidget(QWidget *parent):QGLWidget(parent) { mx = 0; my = 0; mz = 0; initWidget(); initializeGL(); } void OpenglWidget::initializeGL() { //load image QImage mpic; QImage newPic; mpic.load("D://Documents//OPenGL12//haha.jpg"); newPic=QGLWidget::convertToGLFormat(mpic); glGenTextures(1,&textur[0]); glBindTexture(GL_TEXTURE_2D,textur[0]); glTexImage2D(GL_TEXTURE_2D,0,3,newPic.width(),newPic.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,newPic.bits()); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); QImage mpic1; QImage newPic1; mpic1.load("D://Documents//OPenGL12//haha1.jpg"); newPic1=QGLWidget::convertToGLFormat(mpic1); glGenTextures(1,&textur[1]); glBindTexture(GL_TEXTURE_2D,textur[1]); glTexImage2D(GL_TEXTURE_2D,0,3,newPic1.width(),newPic1.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,newPic1.bits()); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //Start the texture, responsible for all white glEnable(GL_TEXTURE_2D); //Set the shading mode, smooth glShadeModel(GL_SMOOTH); // clear all previous colors glClearColor(0.0,0.0,0.0,0.5); //Depth buffer, set the initial value to 1.0, the part less than 1.0 is visible glClearDepth(1.0); //Start the related functions of OpenGL, which is determined by the parameters, here refers to //(After it is enabled, OpenGL will check when drawing to see if there are other pixels in front of the current pixel. If other pixels block it, it will not draw, that is to say, OpenGL will only draw the most front layer) glEnable(GL_DEPTH_TEST); / / Set the depth buffer comparison value //This parameter refers to passing if the input depth value is less than or equal to the reference value glDepthFunc (GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } void OpenglWidget::initWidget() { //From the screen (400, 400) as the starting point, display a 640*400 interface setGeometry(400,200,640,480); setWindowTitle("My OPenGL"); } void OpenglWidget::paintGL() { //Clear color buffer and depth buffer glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //Move the current point to the center of the screen, which is equivalent to the reset operation glLoadIdentity(); //Translation function, the parameters refer to the translation from the X-axis, Y-axis, Z-axis respectively glTranslatef(-1.5,0.0,-6.0); glRotatef( mx,1.0, 0.0,0.0 ); glRotatef(my,0.0,1.0,0.0 ); glRotatef(mz,0.0,0.0,1.0 ); glBindTexture( GL_TEXTURE_2D, textur[0] ); glBegin( GL_QUADS ); glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glEnd(); glLoadIdentity(); glTranslatef( 1.5, 0.0, -6.0 ); glBindTexture( GL_TEXTURE_2D, textur[1] ); glRotatef( m_rotateTriangle, 0.0, 1.0, 0.0 ); glBegin(GL_TRIANGLES); glTexCoord2f( 1, 1 ); glVertex3f( 0, 1, 0 ); glTexCoord2f( 0, 0 ); glVertex3f( 1, -1, 1 ); glTexCoord2f( 1, 0 ); glVertex3f( -1, -1, 1 ); glTexCoord2f( 1, 1 ); glVertex3f( 0, 1, 0 ); glTexCoord2f( 0, 0 ); glVertex3f( -1.0, -1.0, 1.0 ); glTexCoord2f( 1, 0 ); glVertex3f( -1.0, -1.0, -1.0 ); glTexCoord2f( 1, 1 ); glVertex3f( 0, 1, 0 ); glTexCoord2f( 0, 0 ); glVertex3f( -1.0, -1.0, -1.0 ); glTexCoord2f( 1, 0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 1, 1 ); glVertex3f( 0, 1, 0 ); glTexCoord2f( 0, 0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 1, 0 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 0, 0 ); glVertex3f( -1.0, -1.0, -1.0 ); glTexCoord2f( 1, 0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 1, 1 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 1, 0 ); glVertex3f( 1.0, -1.0, -1.0 ); glTexCoord2f( 1, 1 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 0, 1 ); glVertex3f( -1.0, -1.0, 1.0 ); glTexCoord2f( 1, 1 ); glVertex3f( 1.0, -1.0, 1.0 ); glTexCoord2f( 0, 1 ); glVertex3f( -1.0, -1.0, 1.0 ); glTexCoord2f( 0, 0 ); glVertex3f( -1.0, -1.0, -1.0 ); glEnd(); mx+=70; my+=0; mz+=0; m_rotateTriangle+=10; } void OpenglWidget::resizeGL(int width, int height) { if(0==height) height=1; //Tell where to draw to the form glViewport(0,0,width,height); // Set the matrix mode, the parameter is set to the projection matrix glMatrixMode(GL_PROJECTION); // reset operation glLoadIdentity(); GLdouble aspectRatio=(GLfloat)width/(GLfloat)height; GLdouble rFov=45.0*3.14159265/180.0; GLdouble zNear=0.1; GLdouble zFar=100.0; //Call glFrustum to multiply the generated matrix with the current matrix to generate a perspective effect glFrustum(-zNear*tan(rFov/2.0)*aspectRatio, zNear*tan(rFov/2.0)*aspectRatio, -zNear*tan(rFov/2.0), zNear*tan(rFov/2.0), zNear,zFar); // switch back to the model view matrix glMatrixMode(GL_MODELVIEW); // reset glLoadIdentity(); }
2. Pay attention
glTexCoord2f( x, y ); glVertex3f( x, y, z );
glTexCoord2f is used to represent the coordinates of the original image, so it is easier to understand, that is, in the plane of the x, y axis, (0, 0) represents the lower left corner of the image, (0, 1) represents the upper left corner, (1, 1) ) represents the upper right corner, and (1, 0) represents the lower right corner. The corresponding glVertex3f corresponds to (-1, -1) , (-1, 1) , (1, 1), (1, -1). How do we understand this correspondence? Then the coordinates of the map must be in one-to-one correspondence, that is, if we write clockwise, the corresponding seats must be clockwise, and if we write counterclockwise, the corresponding coordinates must be in counter-clockwise one-to-one correspondence.
Our texture is an image, so it must be a plane. So coordinates start at (0, 0), which is easy to use. And we need the three-dimensional graphics of the texture. Although each surface is a plane graphics, it always takes the center of the three-dimensional graphics as the coordinate origin in the coordinate system. Therefore, it is a three-dimensional coordinate, and the three-dimensional coordinate corresponds to the two-dimensional. Only in the same plane vertex rotation order to correspond.