1. Method:
Establish a three-dimensional coordinate system, then color according to the face of each cube, and then draw the points on the face. The drawing of the face should follow the principle of counterclockwise.
2.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
3.OpenglWidget
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 angle; GLfloat angle1; }; #endif // OPENGLWIDGET_HOpenglWidget.cpp
#include "openglwidget.h" OpenglWidget::OpenglWidget(QWidget *parent):QGLWidget(parent) { initWidget(); initializeGL(); } void OpenglWidget::initializeGL() { //Set the shading mode, smooth glShadeModel(GL_SMOOTH); // clear all previous colors glClearColor(0.0,0.0,0.0,0.0); //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); glRotated(angle,0.0,1.0,0.0); //Create a primitive, which is the interface for drawing what kind of graphics, the parameters are the meaning of drawing polygons glBegin(GL_QUADS); //Set the vertex value (f stands for floating point), three-dimensional space coordinates glColor3f( 1.0, 0.0, 0.0 ); glBegin( GL_QUADS ); glColor3f(1, 0, 0); glVertex3f(-1,1,1); glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1); glColor3f(0, 0, 1); glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(1,-1,-1); glVertex3f(1,1,-1); glColor3f(0, 1, 0); glVertex3f(1,1,-1); glVertex3f(1,-1,-1); glVertex3f(1,-1,1); glVertex3f(1,1,1); glColor3f(0, 0, 1); glVertex3f(1,-1,1); glVertex3f(1,1,1); glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glColor3f(0, 1, 0); glVertex3f(-1,1,1); glVertex3f(1,1,1); glVertex3f(1,1,-1); glVertex3f(-1,1,-1); glColor3f(0, 0, 1); glVertex3f(-1,-1,1); glVertex3f(-1,-1,-1); glVertex3f(1,-1,-1); glVertex3f(1,-1,1); glEnd(); glLoadIdentity(); glTranslatef(1.5,0.0,-6.0); glRotatef( angle1,0.0,1.0,0.0); glBegin( GL_TRIANGLES ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glEnd(); angle+=70; angle1+=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(); }
4.main
#include"openglwidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); // Widget w; // w.show(); OpenglWidget window; window.show(); return a.exec(); }