5.模板缓存,绘制一个正方形只能在三角形内移动

#define GLUT_DISABLE_ATEXIT_HACK

#include "windows.h"
#include <gl/glut.h>
#include "math.h"
float alpha = 0.5;
float qqx1=-0.6;
float qqy1=-0.6;
float qqz1=-4.0;


BYTE* gltReadBMPBits(const char* szFileName, int* nWidth, int* nHeight)
{
	HANDLE hFileHandle;
	BITMAPINFO* pBitmapInfo = NULL;
	unsigned long lInfoSize = 0;
	unsigned long lBitSize = 0;
	BYTE* pBits = NULL;					// Bitmaps bits
	BITMAPFILEHEADER	bitmapHeader;
	DWORD dwBytes;

	// Open the Bitmap file
	hFileHandle = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,
		NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

	// Check for open failure (most likely file does not exist).
	if (hFileHandle == INVALID_HANDLE_VALUE)
		return NULL;

	// File is Open. Read in bitmap header information
	ReadFile(hFileHandle, &bitmapHeader, sizeof(BITMAPFILEHEADER),
		&dwBytes, NULL);

	// Check for a couple of simple errors	
	if (dwBytes != sizeof(BITMAPFILEHEADER))
		return FALSE;

	// Check format of bitmap file
	if (bitmapHeader.bfType != 'MB')
		return FALSE;

	// Read in bitmap information structure
	lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
	pBitmapInfo = (BITMAPINFO*)malloc(sizeof(BYTE) * lInfoSize);
	ReadFile(hFileHandle, pBitmapInfo, lInfoSize, &dwBytes, NULL);

	if (dwBytes != lInfoSize)
	{
		free(pBitmapInfo);
		CloseHandle(hFileHandle);
		return FALSE;
	}

	// Save the size and dimensions of the bitmap
	*nWidth = pBitmapInfo->bmiHeader.biWidth;
	*nHeight = pBitmapInfo->bmiHeader.biHeight;
	lBitSize = pBitmapInfo->bmiHeader.biSizeImage;

	// If the size isn't specified, calculate it anyway	
	if (pBitmapInfo->bmiHeader.biBitCount != 24)
	{
		free(pBitmapInfo);
		return FALSE;
	}

	if (lBitSize == 0)
		lBitSize = (*nWidth *
			pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *
		abs(*nHeight);

	// Allocate space for the actual bitmap
	free(pBitmapInfo);
	pBits = (BYTE*)malloc(sizeof(BYTE) * lBitSize);

	// Read in the bitmap bits, check for corruption
	if (!ReadFile(hFileHandle, pBits, lBitSize, &dwBytes, NULL) ||
		dwBytes != (sizeof(BYTE) * lBitSize))
		pBits = NULL;

	// Close the bitmap file now that we have all the data we need
	CloseHandle(hFileHandle);

	return pBits;
}

//初始化OpenGL
void init(void)
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//设置背景颜色
	glShadeModel(GL_SMOOTH);//设置明暗处理,有两种选择模式:GL_FLAT(不渐变)和GL_SMOOTH(渐变过渡)

	// 获取位图数据
	BYTE* pBytes;
	int nWidth, nHeight;
	pBytes = gltReadBMPBits("D:\\TEST.BMP", &nWidth, &nHeight);

	定义二维纹理
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, nWidth, nHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes);

	//控制滤波
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	//说明映射方式
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	

}
//主要的绘制过程
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除颜色缓存    
	glLoadIdentity();
	glEnable(GL_DEPTH_TEST);


	glEnable(GL_STENCIL_TEST);
	glStencilFunc(GL_ALWAYS,1,0xFFFFFFFF);
	glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);
	//绘制三角形
	glPushMatrix();
	glTranslatef(-2, -2, -5.0);
	glScalef(2.0f,2.0f,2.0f);
	glShadeModel(GL_FLAT);
	glEnable(GL_TEXTURE_2D);
	glColor4f(1, 0, 0,1);
	glBegin(GL_TRIANGLES);
	glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(2.0, 0.0, 0.0);
	glVertex3f(1.0, 2.0, 0.0);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);


	glStencilFunc(GL_EQUAL,1,0xFFFFFFFF);
	glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
    //绘制正方形
	//glLoadIdentity();
	glPushMatrix();
	glTranslatef(qqx1, qqy1, qqz1);
	glScalef(0.5f,0.5f,0.5f);
	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	glColor4f(1.0, 1.0, 0.0, 0.5);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDisable(GL_BLEND);
	glBegin(GL_QUADS);
	glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(2.0, 0.0, 0.0);
	glVertex3f(2.0, 2.0, 0.0);
	glVertex3f(0.0, 2.0, 0.0);
	glEnd();
	glPopMatrix();
	glFlush();
	glutSwapBuffers();
}
//在窗口改变大小时调用
void reshape(int w, int h) {
	glViewport(0, 0, w, h);//设置视口
	glMatrixMode(GL_PROJECTION);//设置当前为投影变换模式
	glLoadIdentity();//用单位矩阵替换当前变换矩阵
	gluPerspective(90, (float)w / h, 4, 10.0);//设置正交投影视图体
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
//处理键盘
void keyboard(unsigned char key, int x, int y) {
	switch (key) {
	case 27://esc键退出
		exit(0);
		break;
	case 'a':
	case 'A':
		qqx1 -= 0.1;
		glutPostRedisplay();
		break;
	case 'd':
	case 'D':
		qqx1 += 0.1;
		glutPostRedisplay();
		break;
	case 'w':
	case 'W':
		qqy1 += 0.1;
		glutPostRedisplay();
		break;
	case 'S':
	case 's':
		qqy1 -= 0.1;
		glutPostRedisplay();
		break;


	default:
		break;
	}
}
int main(int argc, char* argv[])							//主函数: 参数数量&参数值
{
	glutInit(&argc, argv);
	glutInitWindowSize(640, 480);
	glutCreateWindow("Basic");//设置窗口标题
	init();//初始化OpenGL
	glutDisplayFunc(display);//设置显示回调函数 
	glutReshapeFunc(reshape);//设置reshape回调函数
	glutKeyboardFunc(keyboard);//设置键盘回调函数
	glutMainLoop();//进入主循环	
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值