1.利用opengl画一个背景为黑色,白色的圆–Circle.cpp
#include <GL/glut.h>
#include <math.h>
int main(int argc, char* argv[])
{
void myDisplay(void);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("圆");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
const int n = 50;
const GLfloat R = 0.5f;
const GLfloat PI = 3.1415926536f;
void myDisplay(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //使用多边形画圆
for (i = 0; i < n; ++i)
glVertex2f(R * cos(2 * PI / n * i), R * sin(2 * PI / n * i));
glEnd();
glFlush();
}
2.虚线–Lines1.cpp
#include<gl/glut.h>
int main(int argc, char* argv[])
{
void myDisplay(void);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("画线");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.5f,0.5f,0.0f);
glEnd();
glFlush();
glLineWidth(3.0);
glBegin(GL_LINE_STRIP);
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glEnd();
glEnable(GL_LINE_STIPPLE);
glLineStipple(1,0x00FF);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f, -0.8f, 0.0f);
glVertex3f(0.5f, -0.8f, 0.0f);
glVertex3f(0.25f, -0.2f, 0.0f);
glEnd();
glFlush();
}
3.画点–Points.cpp
#define NDEBUG
#include <GL/glut.h>
void myDisplay(void)
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);//将窗口的背景设置为当前清空颜色
glPointSize(10);//栅格化点,要用在glBegin之前
glBegin(GL_POINTS);//glBegin与glEnd结合起来使用,GL_POINTS把每个点作为一个点进行处理
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);//绿点
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, 0.0f);//红点
glColor3f(0.0f, 0.0f, 1.0f); glVertex4f(0.5f, 0.0f, 0.0f, 2.0f);//蓝点
glEnd();
glFlush();//强制刷新缓冲,保证绘图命令被执行,而不是存储在缓冲区中等待其他OpenGL命令
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("画点");
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
4.多边形–Polygon.cpp
#include <windows.h>
#include <Gl/glut.h>
int main(int argc, char* argv[])
{
void myDisplay(void);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("画多边形(正面为填充模式,反面为线形模式)");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT, GL_FILL); // 设置正面为填充模式
glPolygonMode(GL_BACK, GL_LINE); // 设置反面为线形模式
//glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); // 设置两面均为顶点绘制方式
//glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); // 设置两面均为顶点绘制方式
glFrontFace(GL_CCW); // 设置逆时针方向为正面
// glFrontFace(GL_CW); //设置顺时针方向为正面
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON); // 按逆时针绘制一个四边形,在左下方
glVertex2f(-0.4f, -0.6f);
glVertex2f(0.0f, -0.6f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, 0.0f);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON); // 按顺时针绘制一个四边形,在右上方
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.4f, 0.0f);
glEnd();
glFlush();
}
5.四边形–Rectangle.cpp
#include <windows.h>
#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>
int main(int argc, char* argv[])
{
void myDisplay(void);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("四边形");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
/*
glBegin(GL_QUADS); // 绘制四边形
glVertex3f(-0.5f, -0.5f, 0.0f); //v0
glVertex3f(-0.5f, 0.5f, 0.0f); //v1
glVertex3f(0.5f, 0.5f, 0.0f); //v2
glVertex3f(0.5f, -0.5f, 0.0f); //v3
glEnd();
*/
glBegin(GL_QUAD_STRIP);
glVertex3f(-0.5f, -0.5f, 0.0f); //v0
glVertex3f(-0.5f, 0.5f, 0.0f); //v1
glVertex3f(0.5f, -0.5f, 0.0f); //v2
glVertex3f(0.5f, 0.5f, 0.0f); //v3
glVertex3f(0.7f, 0.0f, 0.0f); //v4
glVertex3f(0.7f, 0.2f, 0.0f); //v5
glEnd();
glFlush();
}
6.三角形–Triangle.cpp
//#include <windows.h>
#include <gl/glut.h>
int main(int argc, char* argv[])
{
void myDisplay(void);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("三角形");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
void myDisplay(void)
{
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
/*
glBegin(GL_TRIANGLES); //绘制三角形
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
*/
/*
glBegin(GL_TRIANGLE_STRIP); //绘制相连三角形
glVertex3f(-0.2f, 0.0f, 0.0f);
glVertex3f(0.2f, 0.2f, 0.0f);
glVertex3f(0.5f, 0.8f, 0.0f);
glVertex3f(0.8f, 0.2f, 0.0f);
glVertex3f(1.0f, 0.8f, 0.0f);
glEnd();
*/
glBegin(GL_TRIANGLE_FAN); //绘制扇形三角形
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.2f, 0.0f);
glVertex3f(0.2f, 0.6f, 0.0f);
glVertex3f(-0.5f, 0.3f, 0.0f);
glEnd();
glFlush();}
以上是OpenGL的学习笔记 仅供参考哦!
Weak up,moving on