VBO的像素操作,绘制24位位图

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <fstream>
#include <string>
#include <gl/glew.h>
#include <GL/glut.h>



#pragma comment(lib,"glew32.lib")
#pragma comment(lib,"glut32.lib")

using namespace std;
/*	Create checkerboard image	*/
#define	checkImageWidth 64
#define	checkImageHeight 64
GLubyte checkImage[checkImageHeight][checkImageWidth][3];

static GLdouble zoomFactor = 1.0;
//static GLint height;

void makeCheckImage(void)
{
	int i, j, c;

	for (i = 0; i < checkImageHeight; i++) {
		for (j = 0; j < checkImageWidth; j++) {
			c = ((((i&0x8)==0)^((j&0x8))==0))*255;
			checkImage[i][j][0] = (GLubyte) c;
			checkImage[i][j][1] = (GLubyte) c;
			checkImage[i][j][2] = (GLubyte) c;
		}
	}
}

static GLuint unpackBuffer = 0;
static int width=0, height = 0, totals=0;
void readBitmp24(const std::string& filename, void* &ptr)
{
	fstream in(filename.c_str(), ios_base::in|ios_base::binary);
	if (!in)
	{
		ptr = NULL;
		return;
	}
	
	//int width=0, height = 0;
	in.seekg(0x12); //18个字节开始读取宽度和高度
	in.read((char*)&width, sizeof(int));
	in.read((char*)&height,sizeof(int));

	//BMP的每行按照4个字节对齐
	int lineLength = width*3;
	while (lineLength % 4 != 0)
		++lineLength;
	
	totals = lineLength * height;
	ptr = malloc(totals);

	in.seekg(0x36); //54开始是数据,按照行读取
	in.read((char*)ptr, totals);

	in.close();
}


void init(void)
{    
	glClearColor (0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
	makeCheckImage();

	//默认按照4个字节对齐
	//glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
	void* ptrData = NULL;
	
	readBitmp24(std::string("e:/test.bmp"), ptrData);
	glewInit();
	glGenBuffers(1,&unpackBuffer);
	assert(unpackBuffer != 0);
	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, unpackBuffer);
	glBufferData(GL_PIXEL_UNPACK_BUFFER,totals, ptrData,GL_STATIC_DRAW);
	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

	free(ptrData);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glRasterPos2i(0, 0);

	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, unpackBuffer);
	//glPixelZoom(4,4);
	glDrawPixels(width, height, GL_BGR_EXT, 
		GL_UNSIGNED_BYTE, 0);
	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

	glFlush();
}

void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	//height = (GLint) h;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void motion(int x, int y)
{
	//static GLint screeny;

	//screeny = height - (GLint) y;
	//glRasterPos2i (x, screeny);
	//glPixelZoom (zoomFactor, zoomFactor);
	//glCopyPixels (0, 0, checkImageWidth, checkImageHeight, GL_COLOR);
	//glPixelZoom (1.0, 1.0);
	//glFlush ();
}

void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
	case 'r':
	case 'R':
		zoomFactor = 1.0;
		glutPostRedisplay();
		printf ("zoomFactor reset to 1.0\n");
		break;
	case 'z':
		zoomFactor += 0.5;
		if (zoomFactor >= 3.0) 
			zoomFactor = 3.0;
		printf ("zoomFactor is now %4.1f\n", zoomFactor);
		break;
	case 'Z':
		zoomFactor -= 0.5;
		if (zoomFactor <= 0.5) 
			zoomFactor = 0.5;
		printf ("zoomFactor is now %4.1f\n", zoomFactor);
		break;
	case 27:
		exit(0);
		break;
	default:
		break;
	}
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(600, 600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutMotionFunc(motion);
	glutMainLoop();
	return 0; 
}

运行效果


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值