当前位置:网站首页>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;
}

边栏推荐
- Understand dynamic calculation diagram, requires_ grad、zero_ grad
- 理解动态计算图,requires_grad、zero_grad
- 4. Basic type and reference type?
- Missing value processing
- 知乎上的那些神回复
- Inoic4 learning notes 2
- MySQL - bufferpool related information
- redis 数据类型
- [today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast
- CF Lomsat gelral(启发式合并)
猜你喜欢
![[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast

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

Mysql——》BufferPool相关信息

OPENGL学习(四)GLUT三维图像绘制

Sqoop

PWN learning

Vsftpd2.3.4 port penetration 6200 IRC_ 3281_ backdoor

Tcl/tk file operation

L4L7负载均衡

Ionic4 learning notes 3
随机推荐
FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
Missing value processing
Analysis of dropout principle in deep learning
JS to achieve progress steps (small exercise)
Web
Thread lifecycle and basic methods
Ionic4 learning notes 5-- custom public module
Data analysis of network security competition of national vocational college skills competition digital forensics-a
2022暑期杭电多校第一场1012Alice and Bob(博弈论)
Type-C边充边听PD协议芯片
Ionic4 learning notes 3
L4l7 load balancing
CF. Bits And Pieces(子集状压dp + 剪枝)
Generate publickey with der format public key and report an error
redis 数据类型
Getting started with MySQL database
多线程与并发编程常见问题(未完待续)
MySQL - bufferpool related information
Make C #
Pytoch's Journey 2: gradient descent