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

来源:互联网 发布:怎样提高淘宝店信誉度 编辑:程序博客网 时间:2024/05/22 01:51
#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*/#definecheckImageWidth 64#definecheckImageHeight 64GLubyte 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; }

运行效果


原创粉丝点击