画一些点、线、面的基本图形
// Test1OpenGl.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <gl\glut.h>
#include <cmath>
void display(void) {
glClear(GL_COLOR_BUFFER_BIT); //清除颜色
//画点
//glPointSize(5); //以像素为单位设置点的大小
//glBegin(GL_POINTS);
//glVertex3f(0.0, 0.0, 0.0);
//glVertex3f(5.0, 5.0, 0.0);
//glEnd();
////绘制两条直线
//glColor3f(1.0, 0.0, 0.0);
//glLineWidth(2.0);
//glBegin(GL_LINES);
//glVertex2f(0, 0);
//glVertex2f(50, 50);
//glVertex3f(50.0, 100.0, 0.0);
//glVertex3f(100.0, 150.0, 0.0);
//glEnd();
////相连直线
//glColor3f(1.0, 0.0, 0.0);
//glLineWidth(2.0);
//glBegin(GL_LINE_STRIP);
//glVertex2f(0, 0);
//glVertex2f(50, 50);
//glVertex3f(50.0, 100.0, 0.0);
//glVertex3f(100.0, 150.0, 0.0);
//glEnd();
////封闭线
//glColor3f(1.0, 0.0, 0.0);
//glLineWidth(2.0);
//glBegin(GL_LINE_LOOP);
//glVertex2f(0, 0);
//glVertex2f(50, 50);
//glVertex3f(50.0, 100.0, 0.0);
//glVertex3f(100.0, 150.0, 0.0);
//glEnd();
//画三角形
/*glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(55.0, 55.0, 0.0);
glVertex3f(100.0, 0.0, 0.0);
glEnd();*/
//画四边形
/*glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(60.0, 30.0, 0.0);
glVertex3f(70.0, 45.0, 0.0);
glVertex3f(40.0, 75.0, 0.0);
glEnd();*/
//画多边形
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(60.0, 30.0, 0.0);
glVertex3f(70.0, 45.0, 0.0);
glVertex3f(40.0, 75.0, 0.0);
glVertex3f(10.0, 60.0, 0.0);
glEnd();
//画圆
/*glColor3f(1.0, 0.0, 0.0);
glPointSize(2);
int i;
int n = 1000, R = 10;
float Pi = 3.14;
glBegin(GL_LINE_LOOP);
for (i = 0; i < n; ++i) {
glVertex2f(60 + R*cos(2 * Pi / n*i), 60 + R*sin(2 * Pi / n*i));
}
glEnd();*/
//画虚线
/*glColor3f(1.0, 0.0, 0.0);
glLineStipple(2, 0xAAAA); //16位,从低位开始看,1表示画 0表示不画
glBegin(GL_LINES);
glVertex2f(0.0, 0.0); glVertex2f(100.0, 100.0);
glEnd();
*/
glFlush();
}
void myreshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(0.0, 200.0, 0.0, 200.0*(GLfloat)h / (GLfloat)w);
else
gluOrtho2D(0.0, 200.0*(GLfloat)h / (GLfloat)w, 0.0, 200.0);
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc, (char**)argv); //初始化glut
glutInitWindowPosition(100,100); //设置窗口位置,左上角
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); //设置显示方式
glutInitWindowSize(400, 400); //窗口大小
glutCreateWindow("测试程序");
glutReshapeFunc(&myreshape);
glutDisplayFunc(&display);//绘制窗口是调用自己的绘制函数
glutMainLoop(); //消息循环
//设置相对坐标系
return 0;
}