glReadPixels glDrawPixels glCopyPixels与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, packBuffer = 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);

	glGenBuffers(1,&packBuffer);
	assert(packBuffer);
		

	free(ptrData);
}
void saveToBMP24(void* packBuffer,const std::string& img);
void display(void)
{
	static bool saveImg = true;

	glClear(GL_COLOR_BUFFER_BIT);
	glRasterPos2i(0, 0);

	glTranslatef(10,0,0);
	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, unpackBuffer);
	//glPixelTransferf(GL_RED_SCALE, 1.0);
	//glPixelTransferf(GL_GREEN_SCALE, 0.6);
	//glPixelTransferf(GL_BLUE_SCALE, 0.6);
	//glPixelZoom(.5,.5);
	glDrawPixels(width, height, GL_BGR, 
		GL_UNSIGNED_BYTE, 0);

	//将数据读出来保存成BMP的图片
	if (saveImg)
	{
		glBindBuffer(GL_PIXEL_PACK_BUFFER, packBuffer);
		glReadPixels(0,0,width, height, GL_BGR, GL_UNSIGNED_BYTE,0);
		void* packBuffer = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_STATIC_READ);
		
		saveToBMP24(packBuffer,"e:/sss.bmp");
		glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
		saveImg = false;
	}
	
	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

	glFlush();
}

void saveToBMP24(void* packBuffer,const std::string& img)
{
	fstream in("e:/test.bmp", ios_base::in|ios_base::binary);
	if (!in)
	{
		return;
	}
	char bmpHeader[54] = {0};
	in.read(bmpHeader, sizeof(bmpHeader));

	char *pBuffer = new char[totals];
	in.read(pBuffer, totals);
	in.close();

	fstream out(img.c_str(), ios_base::out|ios_base::binary|ios_base::trunc);
	if (!out)
	{
		in.close();
		delete[] pBuffer;
		return;
	}
	
	*((int*)&bmpHeader[0x12]) = width;
	*((int*)&bmpHeader[0x16]) = height;

	out.write(bmpHeader, sizeof(bmpHeader));
	out.write(pBuffer, totals);
	out.close();
	delete []pBuffer;
}

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, width, height, 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; 
}


你可能感兴趣的:(OpenGL,vbo,像素操作)