[toc]

C++使用OpenGl画点线面

1.点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <glut.h>
#include <windows.h>


#define WINDOW_NAME "Drawing Points"

// Vertex data for 5 points
GLfloat points[][2] = {
{100.0f, 100.0f},
{200.0f, 100.0f},
{300.0f, 100.0f},
{400.0f, 100.0f},
{500.0f, 100.0f}
};

// Function to render the scene
void RenderScene() {
// Clear the color buffer to white
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set clear color to white
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Set the color for the points (magenta)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta color

// Draw points
glBegin(GL_POINTS);
for (int i = 0; i < 5; i++) {
glVertex2f(points[i][0], points[i][1]);
}
glEnd();

// Swap buffers (double buffering)
glutSwapBuffers();
}

// Function to handle window resizing
void Resize(int width, int height) {
glViewport(0, 0, width, height); // Set the viewport to the new window dimensions
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix
glLoadIdentity(); // Load the identity matrix
gluOrtho2D(0, width, 0, height); // Set the orthographic projection
glMatrixMode(GL_MODELVIEW); // Switch back to the modelview matrix
RenderScene();
}

// Function to handle keyboard input
void Keyboard(unsigned char key, int x, int y) {
if (key == 27) { // ESC key
exit(0); // Exit the program
}
}

// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // Enable double buffering and RGB color
glutInitWindowSize(640, 480); // Set the window size
glutCreateWindow(WINDOW_NAME); // Create the window

// Set the callback functions
glutDisplayFunc(RenderScene); // Set the display callback
glutReshapeFunc(Resize); // Set the reshape callback
glutKeyboardFunc(Keyboard); // Set the keyboard callback

// Start the main loop
glutMainLoop();

return 0;
}

2.线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <glut.h>
#include <windows.h>

#define WINDOW_NAME "Drawing Lines"

// Vertex data for 5 points
GLfloat points[][2] = {
{100.0f, 100.0f},
{200.0f, 200.0f},
{300.0f, 100.0f},
{400.0f, 200.0f},
{500.0f, 100.0f}
};

// Function to render the scene
void RenderScene() {
// Clear the color buffer to white
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set clear color to white
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Set the color for the lines (magenta)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta color

// Draw lines
glBegin(GL_LINE_STRIP); // Use GL_LINE_STRIP to connect points with lines
for (int i = 0; i < 5; i++) {
glVertex2f(points[i][0], points[i][1]);
}
glEnd();

// Swap buffers (double buffering)
glutSwapBuffers();
}

// Function to handle window resizing
void Resize(int width, int height) {
glViewport(0, 0, width, height); // Set the viewport to the new window dimensions
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix
glLoadIdentity(); // Load the identity matrix
gluOrtho2D(0, width, 0, height); // Set the orthographic projection
glMatrixMode(GL_MODELVIEW); // Switch back to the modelview matrix
RenderScene();
}

// Function to handle keyboard input
void Keyboard(unsigned char key, int x, int y) {
if (key == 27) { // ESC key
exit(0); // Exit the program
}
}

// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // Enable double buffering and RGB color
glutInitWindowSize(640, 480); // Set the window size
glutCreateWindow(WINDOW_NAME); // Create the window

// Set the callback functions
glutDisplayFunc(RenderScene); // Set the display callback
glutReshapeFunc(Resize); // Set the reshape callback
glutKeyboardFunc(Keyboard); // Set the keyboard callback

// Start the main loop
glutMainLoop();

return 0;
}

3.矩形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <glut.h>
#include <windows.h>

#define WINDOW_NAME "Drawing Rectangle"

// Function to render the scene
void RenderScene() {
// Clear the color buffer to white
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set clear color to white
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Set the color for the rectangle (magenta)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta color

// Draw a rectangle
glBegin(GL_QUADS); // Use GL_QUADS to draw a rectangle
glVertex2f(100.0f, 100.0f); // Bottom left
glVertex2f(300.0f, 100.0f); // Bottom right
glVertex2f(300.0f, 300.0f); // Top right
glVertex2f(100.0f, 300.0f); // Top left
glEnd();

// Swap buffers (double buffering)
glutSwapBuffers();
}

// Function to handle window resizing
void Resize(int width, int height) {
glViewport(0, 0, width, height); // Set the viewport to the new window dimensions
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix
glLoadIdentity(); // Load the identity matrix
gluOrtho2D(0, width, 0, height); // Set the orthographic projection
glMatrixMode(GL_MODELVIEW); // Switch back to the modelview matrix

// Clear the color buffer to white when resizing
RenderScene(); // Call RenderScene to clear the background and redraw the rectangle
}

// Function to handle keyboard input
void Keyboard(unsigned char key, int x, int y) {
if (key == 27) { // ESC key
exit(0); // Exit the program
}
}

// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // Enable double buffering and RGB color
glutInitWindowSize(640, 480); // Set the window size
glutCreateWindow(WINDOW_NAME); // Create the window

// Set the callback functions
glutDisplayFunc(RenderScene); // Set the display callback
glutReshapeFunc(Resize); // Set the reshape callback
glutKeyboardFunc(Keyboard); // Set the keyboard callback

// Start the main loop
glutMainLoop();

return 0;
}