Skip to content

Instantly share code, notes, and snippets.

@jarvisniu
Last active July 15, 2025 22:44
Show Gist options
  • Select an option

  • Save jarvisniu/63d14ba9ec79fb3450209dc0695dcf49 to your computer and use it in GitHub Desktop.

Select an option

Save jarvisniu/63d14ba9ec79fb3450209dc0695dcf49 to your computer and use it in GitHub Desktop.
opengl-step-by-step
// gcc 01.hello-world.c -o 01.hello-world && ./01.hello-world
#include <stdio.h>
int main(int argc, char ** argv) {
printf("Hello World!");
return 0;
}
// gcc 02.blank-window.c -o 02.blank-window -framework OpenGL -framework GLUT && ./02.blank-window
#include <stdio.h>
#include <GL/glew.h>
#include "GLUT/glut.h"
static void render(void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
int main(int argc, char** argv) {
// 初始化 GLUT,可以接收参数
glutInit(&argc, argv);
// 设置双缓冲、颜色模式
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
// 窗口的位置、大小、标题
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 100);
glutCreateWindow("02.Blank Window");
// 设置渲染回调函数
glutDisplayFunc(&render);
// 主循环
glutMainLoop();
return 0;
}
// gcc 03.triangle.c -o 03.triangle -framework OpenGL -framework GLUT && ./03.triangle
#include <stdio.h>
#include <GL/glew.h>
#include "GLUT/glut.h"
static void render(void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f( 1, 0, 0 ); // red
glVertex2f( 0.0, 0.0 );
glColor3f( 0, 1, 0 ); // green
glVertex2f( 1.0, 0.0 );
glColor3f( 0, 0, 1 ); // blue
glVertex2f( 1.0, 1.0 );
glColor3f( 1, 0, 0 ); // red
glVertex2f( 0.0, 0.0 );
glColor3f( 0, 0, 1 ); // blue
glVertex2f( 1.0, 1.0 );
glColor3f( 0, 1, 0 ); // green
glVertex2f( 0.0, 1.0 );
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv) {
// 初始化 GLUT,可以接收参数
glutInit(&argc, argv);
// 设置双缓冲、颜色模式
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
// 窗口的位置、大小、标题
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 100);
glutCreateWindow("02.Blank Window");
// 设置渲染回调函数
glutDisplayFunc(&render);
// 主循环
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment