当前位置:网站首页>OpenGL learning (IV) glut 3D image rendering
OpenGL learning (IV) glut 3D image rendering
2022-07-24 18:46:00 【Pony Baby】
List of articles
For three-dimensional objects , The most important thing is the problem of coordinate transformation , That is to say, there is a problem of perspective
1. Draw a rotating cube ( Ordinary perspective transformation )
The following program just turns the direction we are looking at , It's not that the object is really rotating
#include <gl/glut.h>
#include <iostream>
#include <vector>
// The angle of rotation about each axis
GLfloat angle_x = 30;
GLfloat angle_y = 30;
GLfloat angle_z = 30;
// Point structure
struct Point {
GLfloat x, y, z;// Location
GLfloat r, g, b;// Color
Point() = default;
};
// Define a Point Array is face , Each bread contains four dots
using face=std::vector<Point>;
//faces All faces are stored in the array
std::vector<face> Faces;
// Cube vertex
GLfloat vertexes[8][3] = {
{
1.0, 1.0, 1.0},
{
1.0,-1.0, 1.0},
{
-1.0,-1.0, 1.0},
{
-1.0, 1.0, 1.0},
{
1.0, 1.0,-1.0},
{
1.0,-1.0,-1.0},
{
-1.0,-1.0,-1.0},
{
-1.0, 1.0,-1.0} };
// Six cubes
int facesId[6][4] = {
{
0, 1, 2, 3},
{
4, 5, 6, 7},
{
0, 4, 7, 3},
{
1, 5, 6, 2},
{
0, 4, 5, 1},
{
3, 7, 6, 2} };
// The colors of the six sides
GLfloat facesColor[6][3] = {
{
1.0, 1.0, 0.0},
{
1.0, 0.0, 1.0},
{
0.0, 1.0, 1.0},
{
1.0, 0.0, 0.0},
{
0.0, 1.0, 0.0},
{
0.0, 0.0, 1.0}};
// Draw a face
void drawFace(face& points)
{
glBegin(GL_POLYGON);
for(auto &pt:points)
{
glColor3f(pt.r, pt.g, pt.b);
glVertex3f(pt.x, pt.y, pt.z);
}
glEnd();
}
// When the window changes ( Such as changing the size ) Automatically call
void mydisplay()
{
// Clear color cache
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto &tmp_face : Faces)
{
drawFace(tmp_face);
}
// Modify the perspective
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle_x, 1, 0, 0);
glRotatef(angle_y, 0, 1, 0);
glRotatef(angle_z, 0, 0, 1);
// Use DOUBLE_BUFFER after , Use the following code to swap the front and back memory
glutSwapBuffers();
}
// Initialization function , Generally, it includes perspective, etc
void init()
{
// The full screen color turns black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Opening depth , Block the elements behind
glEnable(GL_DEPTH_TEST);
// Change the projection view ,
glMatrixMode(GL_PROJECTION);
//opengl Is a state machine , First clear the previous transformation matrix data , Therefore, each perspective operation must first be changed into an identity matrix
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
// Modify the perspective
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle_x, 1, 0, 0);
glRotatef(angle_y, 0, 1, 0);
glRotatef(angle_z, 0, 0, 1);
}
// Initialize six face arrays
void initVertexes()
{
for (int i = 0; i < 6; i++)
{
face tmp_face;
for (int j = 0; j < 4; j++)
{
Point pt;
pt.x = vertexes[facesId[i][j]][0];
pt.y = vertexes[facesId[i][j]][1];
pt.z = vertexes[facesId[i][j]][2];
pt.r = facesColor[i][0];
pt.g = facesColor[i][1];
pt.b = facesColor[i][2];
tmp_face.emplace_back(pt);
}
Faces.push_back(tmp_face);
}
}
// Rotation function
void rotate(int x)
{
angle_x += 1;
if (angle_x >= 360)angle_x = 0;
// Draw now
glutPostRedisplay();
// Set a new timer
glutTimerFunc(10, rotate, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//displayMode, increase GLUT_DEPTH Make the depth
glutInitDisplayMode( GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
// Set window name
glutCreateWindow("Cubic");
// Initialize six faces
initVertexes();
// Set the timing function
glutTimerFunc(10, rotate, 0);
// binding display function
glutDisplayFunc(mydisplay);
// Set up opengl The initial state
init();
// Open window loop
glutMainLoop();
return 0;
}

2. Draw a rotating cube ( Perspective changes )
The perspective function parameters are shown in the figure 
#include <gl/glut.h>
#include <iostream>
#include <vector>
// The angle of rotation about each axis
GLfloat angle_x = 30;
GLfloat angle_y = 30;
GLfloat angle_z = 30;
// Point structure
struct Point {
GLfloat x, y, z;// Location
GLfloat r, g, b;// Color
Point() = default;
};
// Define a Point Array is face , Each bread contains four dots
using face=std::vector<Point>;
//faces All faces are stored in the array
std::vector<face> Faces;
// Cube vertex
GLfloat vertexes[8][3] = {
{
1.0, 1.0, 1.0},
{
1.0,-1.0, 1.0},
{
-1.0,-1.0, 1.0},
{
-1.0, 1.0, 1.0},
{
1.0, 1.0,-1.0},
{
1.0,-1.0,-1.0},
{
-1.0,-1.0,-1.0},
{
-1.0, 1.0,-1.0} };
// Six cubes
int facesId[6][4] = {
{
0, 1, 2, 3},
{
4, 5, 6, 7},
{
0, 4, 7, 3},
{
1, 5, 6, 2},
{
0, 4, 5, 1},
{
3, 7, 6, 2} };
// The colors of the six sides
GLfloat facesColor[6][3] = {
{
1.0, 1.0, 0.0},
{
1.0, 0.0, 1.0},
{
0.0, 1.0, 1.0},
{
1.0, 0.0, 0.0},
{
0.0, 1.0, 0.0},
{
0.0, 0.0, 1.0}};
// Draw a face
void drawFace(face& points)
{
glBegin(GL_POLYGON);
for(auto &pt:points)
{
glColor3f(pt.r, pt.g, pt.b);
glVertex3f(pt.x, pt.y, pt.z);
}
glEnd();
// Modify the perspective
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 4, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle_x, 1, 0, 0);
glRotatef(angle_y, 0, 1, 0);
glRotatef(angle_z, 0, 0, 1);
}
// When the window changes ( Such as changing the size ) Automatically call
void mydisplay()
{
// Clear color cache
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto &tmp_face : Faces)
{
drawFace(tmp_face);
}
// Use DOUBLE_BUFFER after , Use the following code to swap the front and back memory
glutSwapBuffers();
}
// Initialization function , Generally, it includes perspective, etc
void init()
{
// The full screen color turns black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Opening depth , Block the elements behind
glEnable(GL_DEPTH_TEST);
// Change the projection view ,
glMatrixMode(GL_PROJECTION);
//opengl Is a state machine , First clear the previous transformation matrix data , Therefore, each perspective operation must first be changed into an identity matrix
glLoadIdentity();
// Using perspective transformation , You can also use gluPerspective function , The parameters are different
glFrustum(-2.0, 2.0, -2.0, 2.0, 2.0, 20.0);
// Modify the perspective
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 4, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle_x, 1, 0, 0);
glRotatef(angle_y, 0, 1, 0);
glRotatef(angle_z, 0, 0, 1);
}
// Initialize six face arrays
void initVertexes()
{
for (int i = 0; i < 6; i++)
{
face tmp_face;
for (int j = 0; j < 4; j++)
{
Point pt;
pt.x = vertexes[facesId[i][j]][0];
pt.y = vertexes[facesId[i][j]][1];
pt.z = vertexes[facesId[i][j]][2];
pt.r = facesColor[i][0];
pt.g = facesColor[i][1];
pt.b = facesColor[i][2];
tmp_face.emplace_back(pt);
}
Faces.push_back(tmp_face);
}
}
// Rotation function
void rotate(int x)
{
angle_x += 1;
if (angle_x >= 360)angle_x = 0;
// Draw now
glutPostRedisplay();
// Set a new timer
glutTimerFunc(10, rotate, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//displayMode, increase GLUT_DEPTH Make the depth
glutInitDisplayMode( GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
// Set window name
glutCreateWindow("Cubic");
// Initialize six faces
initVertexes();
// Set the timing function
glutTimerFunc(10, rotate, 0);
// binding display function
glutDisplayFunc(mydisplay);
// Set up opengl The initial state
init();
// Open window loop
glutMainLoop();
return 0;
}

3. Draw two cubes with different rotation directions
The core is to understand this code :
The following code shows us ( − 1.1 , − 1.1 , 0 ) (-1.1,-1.1,0) (−1.1,−1.1,0) A cube is rotating around its axis .
M1,M2,M3,M4 Represent four transformation matrices respectively , p ′ = M 1 M 2 M 3 M 4 p p'=M_1M_2M_3M_4p p′=M1M2M3M4p, The later code acts on p, It is a process of transferring from the object coordinate system to the perspective coordinate system .M4、M3、M2 Can be understood as an operation based on the object coordinate system ( That is, directly operate the object )M4 Move it to the origin ,M3 Wrap it around y Shaft rotation ,M2 Move it back , Connecting is rotating in place . Last M1 Project it into our view coordinate system .
glLoadIdentity();
gluLookAt(0, 0, 6, 0, 0, 0.0, 0.0, 1.0, 0.0); //M1
glTranslatef(-1.1,-1.1,0.0); //M2
glRotatef(angle_x, 0, 1, 0); //M3
glTranslatef(1.1, 1.1, 0.0); //M4
Complete code :
#include <gl/glut.h>
#include <iostream>
#include <vector>
// The angle of rotation about each axis
GLfloat angle_x = 30;
GLfloat angle_y = 30;
GLfloat angle_z = 30;
// Point structure
struct Point {
GLfloat x, y, z;// Location
GLfloat r, g, b;// Color
Point() = default;
};
// Define a Point Array is face , Each bread contains four dots
using face=std::vector<Point>;
//faces All faces are stored in the array
std::vector<face> Cube1;
std::vector<face> Cube2;
// Cube vertex
GLfloat vertexes[8][3] = {
{
1.0, 1.0, 1.0},
{
1.0,-1.0, 1.0},
{
-1.0,-1.0, 1.0},
{
-1.0, 1.0, 1.0},
{
1.0, 1.0,-1.0},
{
1.0,-1.0,-1.0},
{
-1.0,-1.0,-1.0},
{
-1.0, 1.0,-1.0} };
// Six cubes
int facesId[6][4] = {
{
0, 1, 2, 3},
{
4, 5, 6, 7},
{
0, 4, 7, 3},
{
1, 5, 6, 2},
{
0, 4, 5, 1},
{
3, 7, 6, 2} };
// The colors of the six sides
GLfloat facesColor[6][3] = {
{
1.0, 1.0, 0.0},
{
1.0, 0.0, 1.0},
{
0.0, 1.0, 1.0},
{
1.0, 0.0, 0.0},
{
0.0, 1.0, 0.0},
{
0.0, 0.0, 1.0}};
// Draw a face
void drawFace(face& points)
{
glBegin(GL_POLYGON);
for(auto &pt:points)
{
glColor3f(pt.r, pt.g, pt.b);
glVertex3f(pt.x, pt.y, pt.z);
}
glEnd();
}
// When the window changes ( Such as changing the size ) Automatically call
void mydisplay()
{
// Clear color cache
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Modify the perspective
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 6, 0, 0, 0.0, 0.0, 1.0, 0.0);
glTranslatef(-1.1,-1.1,0.0);
glRotatef(angle_x, 0, 1, 0);
glTranslatef(1.1, 1.1, 0.0);
for (auto &tmp_face : Cube1)
{
drawFace(tmp_face);
}
// Modify the perspective
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 6, 0, 0, 0.0, 0.0, 1.0, 0.0);
glTranslatef(1.1, 1.1, 0.0);
glRotatef(angle_x, 0, -1, 0);
glTranslatef(-1.1, -1.1, 0.0);
for (auto& tmp_face : Cube2)
{
drawFace(tmp_face);
}
// Use DOUBLE_BUFFER after , Use the following code to swap the front and back memory
glutSwapBuffers();
}
// Initialization function , Generally, it includes perspective, etc
void init()
{
// The full screen color turns black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Opening depth , Block the elements behind
glEnable(GL_DEPTH_TEST);
// Change the projection view ,
glMatrixMode(GL_PROJECTION);
//opengl Is a state machine , First clear the previous transformation matrix data , Therefore, each perspective operation must first be changed into an identity matrix
glLoadIdentity();
// Using perspective transformation , You can also use gluPerspective function , The parameters are different
glFrustum(-2.0, 2.0, -2.0, 2.0, 2.0, 20.0);
}
// Initialize six face arrays
void initVertexes()
{
for (int i = 0; i < 6; i++)
{
face tmp_face1;
face tmp_face2;
for (int j = 0; j < 4; j++)
{
Point pt;
pt.x = vertexes[facesId[i][j]][0]-1.1;
pt.y = vertexes[facesId[i][j]][1]-1.1;
pt.z = vertexes[facesId[i][j]][2];
pt.r = facesColor[i][0];
pt.g = facesColor[i][1];
pt.b = facesColor[i][2];
tmp_face1.emplace_back(pt);
pt.x = vertexes[facesId[i][j]][0]+1.1;
pt.y = vertexes[facesId[i][j]][1]+1.1;
pt.r = facesColor[i][0];
pt.g = facesColor[i][1];
pt.b = facesColor[i][2];
tmp_face2.emplace_back(pt);
}
Cube1.push_back(tmp_face1);
Cube2.push_back(tmp_face2);
}
}
// Rotation function
void rotate(int x)
{
angle_x += 1;
if (angle_x >= 360)angle_x = 0;
// Draw now
glutPostRedisplay();
// Set a new timer
glutTimerFunc(10, rotate, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//displayMode, increase GLUT_DEPTH Make the depth
glutInitDisplayMode( GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
// Set window name
glutCreateWindow("Cubic");
// Initialize six faces
initVertexes();
// Set the timing function
glutTimerFunc(10, rotate, 0);
// binding display function
glutDisplayFunc(mydisplay);
// Set up opengl The initial state
init();
// Open window loop
glutMainLoop();
return 0;
}

边栏推荐
- 【微信小程序开发】自定义tabBar案例(定制消息99+小红心)
- 深度学习中Dropout原理解析
- 16. What is the difference between target and currenttarget?
- Cf. bits and pieces (subset pressing DP + pruning)
- MySQL configuration file
- Attack and defense world novice zone PWN
- leetcode-记忆化深搜/动态规划v2
- What are the benefits of knowledge management in enterprises?
- Encapsulate function basedata.js
- Ionic4 learning notes 10 rotation map of an East Project
猜你喜欢

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)

Wechat applet reverse

Eternal Blue ms17-010exp reappears

Ionic4 learning notes 8 -- UI component 2 list (no practice, direct excerpt)

Why is gradient the fastest changing direction of function

【微信小程序开发】自定义tabBar案例(定制消息99+小红心)

National vocational college skills competition network security competition -- detailed explanation of Apache security configuration

【历史上的今天】7 月 24 日:Caldera 诉微软案;AMD 宣布收购 ATI;谷歌推出 Chromecast

7. Character coding?

Analysis of dropout principle in deep learning
随机推荐
缺失值处理
Ionic4 learning notes 8 -- UI component 2 list (no practice, direct excerpt)
MySQL optimization series (2) -- InnoDB important parameter optimization
Ionic4 learning notes 4 -- add a tab page
[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast
EasyUI framework dialog repeated loading problem
Introduction to nipple music theory and personal perception
4. Basic type and reference type?
Principle and application of database
Generate publickey with der format public key and report an error
无关的表进行关联查询及null=null条件
The problem that files cannot be uploaded to the server using TFTP is solved
KiB、MiB与KB、MB的区别
Introduction to VIM
Today's sleep quality record 79 points
Vsftpd2.3.4-端口渗透 6200 irc_3281_backdoor
Some buckles
EasyUI adds row level buttons to the DataGrid
2020-2021 new technology lecture course
Encapsulate function basedata.js