计算机图形学OpenGL基础(2)

本文基于OpenGL基础,介绍了如何使用glBegin和glEnd等函数绘制点、线、多边形和三角形,以及如何在一个窗口中显示这些图形。
摘要由CSDN通过智能技术生成

OpenGL基础(2)

依据上篇文章(计算机图形学OpenGL基础)为基础向下扩展在上篇代码的基础上我们今天加入了
一、点的代码

	glColor3f(1.0f,0.0f,0.0f);
	glPointSize(10.0f);
	glBegin(GL_POINTS);
		glVertex2i(100,100);
		glVertex2i(50,50);
	glEnd();

二、线的代码

	glColor3f(1.0f,0.0f,0.0f);
	glPointSize(10.0f);
	glBegin(GL_LINES);
		glVertex2i(100,100);
		glVertex2i(50,50);
	glEnd();

三、多边形的代码

glColor3f(1.0f,0.0f,0.0f);
	glPointSize(10.0f);
	glBegin(GL_POLYGON);
		glVertex2i(100,100);
		glVertex2i(50,50);
	glEnd();

如果我们把GL_POLYGON改成GL_TRIANGLES就变成了三角形的代码

下面我们根据基础代码+以上说的代码
进行一个代码模块儿的练习
要求:
窗口里面含有点、线、多边形和三角形

#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
#include <gl/glut.h>

void SetupRC(){
	glClearColor(1.0f,1.0f,1.0f,1.0f);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0,400.0,0.0,300.0);
	
	
}
void showTime(){
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f,0.0f,0.0f);
	glPointSize(10.0f);
	glBegin(GL_POINTS);
		glVertex2i(100,100);
		glVertex2i(50,50);
	glEnd();
	glColor3f(0.0f,1.0f,0.0f);
	glPointSize(10.0f);
	glBegin(GL_LINES);
		glVertex2i(100,100);
		glVertex2i(50,50);
	glEnd();
	int p1[]={100,130};
	int p2[]={130,100};
	int p3[]={160,100};
	int p4[]={170,130};
	int p5[]={160,160};
	int p6[]={130,160};
	glColor3f(0.5f,1.0f,0.0f);
	glPointSize(10.0f);
	glBegin(GL_POLYGON);
		glVertex2iv(p1);
		glVertex2iv(p2);
		glVertex2iv(p3);
		glVertex2iv(p4);
		glVertex2iv(p5);
		glVertex2iv(p6);
	glEnd();
	int p11[]={200,230};
	int p12[]={230,200};
	int p13[]={260,200};
	int p14[]={270,230};
	int p15[]={260,260};
	int p16[]={230,260};
	glColor3f(0.5f,0.5f,0.5f);
	glPointSize(10.0f);
	glBegin(GL_TRIANGLES);
		glVertex2iv(p11);
		glVertex2iv(p12);
		glVertex2iv(p13);
		glVertex2iv(p14);
		glVertex2iv(p15);
		glVertex2iv(p16);
	glEnd();
	
	glFlush();
}
int main(int argc,char *argv[]){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(400,300);
	glutInitWindowPosition(100,120); 
	glutCreateWindow("  Ŀһ") ;
	glutDisplayFunc(showTime);
	SetupRC();
	glutMainLoop(); 
	 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值