• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

openGL 初试 绘制三角形 和添加鼠标键盘事件


code:

 

#include <gl/glut.h>
#include <stdlib.h>

void render(void);
void keyboard(unsigned char c,int x,int y);
void mouse(int button,int state,int x,int y);

int main(int argc , char** argv){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(640,480);
	glutCreateWindow("simple glut application");

	glutDisplayFunc(render);
	glutKeyboardFunc(keyboard);
	glutMouseFunc(mouse);

	glutMainLoop();
}

//鼠标事件
void mouse(int button,int state,int x,int y){
	if(button == GLUT_RIGHT_BUTTON ){
		exit(0);
	}
}

//键盘事件
void keyboard(unsigned char c,int x,int y){
	if(c==27){
		exit(0);
	}
}

void render(void){
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glBegin(GL_TRIANGLES);

	//分别设置三点的颜色是整体产生变化色

	//设置颜色
	glColor3f(1,0,0);
	//绘制点
	glVertex2f(-0.5,-0.5);
	//设置颜色
	glColor3f(0,1,0);
	//绘制点
	glVertex2f(0.5,-0.5);
	//设置颜色
	glColor3f(0,0,1);
	//绘制点
	glVertex2f(0.0,0.5);

	glEnd();

	glutSwapBuffers();

}

 

 


效果:


 

posted @ 2013-08-26 20:27  Class Xman  阅读(503)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3