Home
简单三角形绘制
OpenGL
简单三角形绘制
姜睿
姜睿
April 29, 2023
1 min

代码

  • 那么我们现在就可以写代码啦!
1
2#include "GL/freeglut.h"
3
4const GLsizei windowWidth = 800;
5const GLsizei windowHeight = 800;
6
7// This function renders the scene using OpenGL.
8void RenderScene() {
9// Clear the frame buffer by setting it to the clear color.
10glClear(GL_COLOR_BUFFER_BIT);
11
12// Select the Modelview Matrix and reset it to its default state.
13glMatrixMode(GL_MODELVIEW);
14glLoadIdentity();
15
16// Draw the triangle.
17glBegin(GL_TRIANGLES);
18glColor3f(1.0, 0.0, 0.0); // red
19glVertex2f(0.0, 1.0);
20
21glColor3f(0.0, 1.0, 0.0); // green
22glVertex2f(1.0, -1.0);
23
24glColor3f(0.0, 0.0, 1.0); // blue
25glVertex2f(-1.0, -1.0);
26glEnd();
27
28// Flush the graphic buffers &
29// swap them (double buffering).
30glFlush();
31glutSwapBuffers();
32}
33
34// This is the entry point of the program.
35int main(int argc, char* argv[]) {
36// Initialize GLUT.
37glutInit(&argc, argv);
38
39// Set the display mode to use double buffering.
40// GLUT_SINGLE will came flichering.
41glutInitDisplayMode(GLUT_DOUBLE);
42
43// Create a window with the title "Hello OpenGL".
44int windowID = glutCreateWindow("Hello OpenGL");
45glutReshapeWindow(windowWidth, windowHeight);
46
47// Set the display function to render the scene.
48glutDisplayFunc(RenderScene);
49
50// Set the clear color to white.
51glClearColor(1.0, 1.0, 1.0, 1.0);
52
53// Transfer control over to GLUT.
54glutMainLoop();
55
56// This code is never reached.
57return 0;
58}
59

姜睿

姜睿

学生

游戏设计学生

Expertise

游戏开发
平面设计

Related Posts

向量类
向量类
May 22, 2023
1 min

Legal Stuff

Privacy NoticeCookie PolicyTerms Of Use