OpenGL下使用异或画图

/* This program illustrates the use of the glut library for
interfacing with a Window System */

/* The program opens a window, clears it to black,
then draws a box at the location of the mouse each time the
left button is clicked. The right button exits the program

The program also reacts correctly when the window is
moved or resized by clearing the new window to black*/

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

/* globals */

GLsizei wh = 500, ww = 500; /* initial window size */
float xm, ym, xmm, ymm;
int first = 0;

void mouse(int btn, int state, int x, int y)
{
	if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
	{
		xm = x;
		ym = wh-y;
		glColor3f(0.0, 0.0, 1.0);
		glLogicOp(GL_XOR);
		first = 0;
	}
	if(btn==GLUT_LEFT_BUTTON && state==GLUT_UP)
	{
		glRectf(xm, ym, xmm, ymm);
		glFlush();
		glColor3f(0.0, 1.0, 0.0);
		glLogicOp(GL_COPY);
		xmm = x;
		ymm = wh-y;
		glLogicOp(GL_COPY);
		glRectf(xm, ym, xmm, ymm);
		glFlush();
	}
}

void move(int x, int y)
{
	if(first == 1)
	{
		glRectf(xm, ym, xmm, ymm);
		glFlush();
	}
	xmm = x;
	ymm = wh-y;
	glRectf(xm, ym, xmm, ymm);
	glFlush();
	first = 1;
}

void myReshape(GLsizei w, GLsizei h)
{
	
	/* adjust clipping box */
	
   	glMatrixMode(GL_PROJECTION);
   	glLoadIdentity(); 
   	glOrtho(0.0,(GLdouble)w, 0.0,(GLdouble)h, -1.0, 1.0);
	
   	glMatrixMode(GL_MODELVIEW);
   	glLoadIdentity(); 
	
	/* adjust viewport and clear */
	
	glViewport(0,0,w,h);
    glClearColor(1.0, 1.0, 1.0, 1.0);
   	glClear(GL_COLOR_BUFFER_BIT);
   	glFlush();
	
	/* set global size for use by drawing routine */
	
	ww = w;
   	wh = h; 
}

void myinit(void)
{
   	glViewport(0,0,ww,wh);
	
	/* Pick 2D clipping window to match size of screen window 
	This choice avoids having to scale object coordinates
	each time window is resized */
   	glMatrixMode(GL_PROJECTION);
   	glLoadIdentity(); 
   	glOrtho(0.0,(GLdouble) ww , 0.0,(GLdouble) wh , -1.0, 1.0);
	
	/* set clear color to black and clear window */
   	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glFlush();
	
	/* callback routine for reshape event */
   	glutReshapeFunc(myReshape);
	
}


/* display callback required by GLUT 3.0 */
void display(void)
{}

int main(int argc, char** argv)
{
   	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(ww, wh);
	glutCreateWindow("square");
   	myinit();
   	glutReshapeFunc(myReshape);
   	glutMouseFunc(mouse);
   	glutMotionFunc(move);
	glutDisplayFunc(display);
	
   	glutMainLoop();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值