opengl绘制线

#include "stdafx.h"
#include "GL\glut.h"


void RenderScene()
{
//清楚颜色缓冲区,填充的颜色由glClearColor(0,0,0,1);指定为黑色
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3);//设置线段的宽度,里面是以像素点为单位,此线线宽为3个像素点
    //glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINES);//开始画线
 glVertex3f(0.0f,0.0f,0.0f);
 glVertex3f(0.5f,0.0f,0.0f);
    glEnd();//画线结束


glBegin(GL_LINES);//开始画线
 glVertex3f(0.5f,0.0f,0.0f);
 glVertex3f(0.5f,0.5f,0.0f);
    glEnd();//画线结束


glutSwapBuffers();//使用双缓冲,绘制完图后交换两个缓冲区指针,
//从而把绘制的图复制到屏幕上,这样就可以看到图像出现在屏幕上了
}


void SetupRC()
{
glClearColor(0.0f,0.0f,0.0f,1);//以黑色来清空颜色缓冲区
glColor3f(1.0f,0.0f,0.0f);//以红色来绘制图形
}


void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat nRange=200.0f;
//防止h为0,便于h为除数
if(h==0)
h=1;
//设立视口到窗口之间的范围
glViewport(0,0,w,h);
//重设投影矩阵
glMatrixMode(GL_PROJECTION);//声明对投影矩阵进行操作
glLoadIdentity();//更新
//正交投影变换
if(w<=h)
glOrtho(-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);
    else
glOrtho(-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);


glMatrixMode(GL_PROJECTION);//声明对模型视景矩阵进行操作
glLoadIdentity();


}


int main(int argc,char*argv[])
{
glutInit(&argc,argv);//初始化glut库
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);//设置图形显示模式
glutInitWindowSize(200,200);//设置图形窗口大小
glutCreateWindow("line example");//设置窗口名
glutDisplayFunc(RenderScene);//注册绘图函数
glutReshapeFunc(ChangeSize);//重建窗口大小
SetupRC();//设置渲染环境
glutMainLoop();//循环绘图
return 0;

}


你可能感兴趣的:(图形,OpenGL,lines)