OpenGL入门学习(一)

本文介绍了使用OpenGL进行基本图形绘制的方法,包括点、直线、圆的绘制,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一天学习OpenGL,

以下程序用来画点、直线、圆等。

#include <GL/glut.h>
#include <math.h>

const int n = 20;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;

void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
	glFlush();
}

// The part is used to draw point.
void myDisplay1(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPointSize(5.0f);
	glBegin(GL_POINTS);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.0f);
	glEnd();
	glFlush();
}

//The part is used to draw lines
void myDisplay2(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	// The function is used to set the size of line.
	glLineWidth(2.0f);
	// Start the mode od stipple.
	glEnable(GL_LINE_STIPPLE);
	// The function is used to set the pattern of line.
	glLineStipple(2,0x0F0F);
	glBegin(GL_LINES);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.0f);
	glEnd();
	glFlush();
}

//This part is used to draw circle.
void myDisplay3(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();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(400,400);
	glutCreateWindow("The first OpenGL program");
	glutDisplayFunc(&myDisplay2);
	glutMainLoop();

	return 0;
}

学习的书籍是《OpenGL入门学习》。代码也来自这里
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值