基础代码#
#include <GL/GLUT.h>
#include<time.h>
void init() {
glClearColor(0.1, 0.1, 0.4, 0.0);
glShadeModel(GL_SMOOTH);
}
void DrawMyObjects(void) {
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-10.0, 11.0);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(-9.0, 10.0);
glColor3f(0.0, 1.0, 1.0);
glVertex2f(-8.0, 12.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(-11.0, 8.0);
glVertex2f(-7.0, 7.0);
glColor3f(1.0, 0.0, 1.0);
glVertex2f(-11.0, 9.0);
glVertex2f(-8.0, 6.0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-3.0, 9.0);
glVertex2f(2.0, 6.0);
glVertex2f(3.0, 8.0);
glVertex2f(-2.5, 6.5);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0.0, 1.0, 1.0);
glVertex2f(7.0, 7.0);
glVertex2f(8.0, 8.0);
glVertex2f(9.0, 6.5);
glVertex2f(10.3, 7.5);
glVertex2f(11.5, 6.0);
glVertex2f(7.5, 6.0);
glEnd();
glBegin(GL_POLYGON);
glColor3f(0.5, 0.3, 0.7);
glVertex2f(-7.0, 2.0);
glVertex2f(-8.0, 3.0);
glVertex2f(-10.3, 0.5);
glVertex2f(-7.5, -2.0);
glVertex2f(-6.0, -1.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.7, 0.5, 0.2);
glVertex2f(0.0, 2.0);
glVertex2f(-1.0, 3.0);
glVertex2f(-3.3, 0.5);
glVertex2f(-0.5, -1.0);
glColor3f(0.5, 0.7, 0.2);
glVertex2f(3.0, 2.0);
glVertex2f(2.0, 3.0);
glVertex2f(0.0, 0.5);
glVertex2f(2.5, -1.0);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex2f(6.0, -2.0);
glVertex2f(5.5, 1.0);
glVertex2f(8.0, -1.0);
glColor3f(0.8, 0.0, 0.0);
glVertex2f(9.0, 2.0);
glVertex2f(11.0, -2.0);
glColor3f(0.0, 0.0, 0.8);
glVertex2f(11.0, 2.0);
glVertex2f(13.0, -1.0);
glColor3f(0.0, 0.8, 0.0);
glVertex2f(14.0, 1.0);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.2, 0.5, 0.7);
glVertex2f(-10.0, -5.0);
glVertex2f(-12.3, -7.5);
glVertex2f(-8.5, -6.0);
glColor3f(0.2, 0.7, 0.5);
glVertex2f(-8.0, -7.0);
glVertex2f(-7.0, -4.5);
glVertex2f(-5.5, -9.0);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(-1.0, -8.0);
glVertex2f(-2.5, -5.0);
glColor3f(0.8, 0.8, 0.0);
glVertex2f(1.0, -7.0);
glColor3f(0.0, 0.8, 0.8);
glVertex2f(2.0, -4.0);
glColor3f(0.8, 0.0, 0.8);
glVertex2f(4.0, -6.0);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex2f(8.0, -6.0);
glVertex2f(10.0, -3.0);
glColor3f(0.8, 0.2, 0.5);
glVertex2f(12.5, -4.5);
glColor3f(0.2, 0.5, 0.8);
glVertex2f(13.0, -7.5);
glColor3f(0.8, 0.5, 0.2);
glVertex2f(10.5, -9.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 0, 0);
glVertex2f(-15, 0);
glVertex2f(15.0, 0);
glVertex2f(0, 15.0);
glVertex2f(0, -15.0);
glEnd();
glBegin(GL_LINES);
glColor3f(62 / 255.0, 62 / 255.0, 62 / 255.0);
for (float i = 1; i < 15; i++) {
glVertex2f(i, 15);
glVertex2f(i, -15);
glVertex2f(-i, 15);
glVertex2f(-i, -15);
glVertex2f(15, i);
glVertex2f(-15, i);
glVertex2f(15, -i);
glVertex2f(-15, -i);
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
DrawMyObjects();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.1, 100000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 25, 0, 0, -1, 0, 1, 0);
}
int main(int argc, const char* argv[]) {
glutInit(&argc, const_cast<char**>(argv));
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
参考资料#
opengl入门
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)