- 论坛徽章:
- 0
|
本帖最后由 三月廿七 于 2011-03-12 16:25 编辑
绘制1000 张 240x320 的图片, 双缓冲 比 普通绘图快 5 倍左右,
同样是绘制1000 张 240x320 的图片, 整体绘制比零散绘制快 10倍左右,
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- GLubyte* readImage(const char*, GLsizei*, GLsizei* );
- GLubyte *pixels;
- GLsizei width, height;
- void init(void)
- {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glClearColor(0.0, 0.0, 0.0, 0.0);
-
- }
- void display(void)
- {
- GLubyte* buffer;
- int i;
- static clock_t start = 0, end = 0, t1 = 0, t2 = 0;
- buffer = (GLubyte*)malloc(width * height * 3);
- glClear(GL_COLOR_BUFFER_BIT);
- glRasterPos2i(0, 0);
-
- start = clock();
- for (i = 0; i < 1000; i++)
- {
- glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels );
- }
- end = clock();
- t1 = end - start;
- glRasterPos2i(240, 100);
- start = clock();
- for (i = 0; i < 1000; i++)
- {
- memcpy(buffer, pixels, width * height * 3);
- //glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels );
- }
- glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
- end = clock();
-
- t2 = end - start;
- glFlush();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho (0, w, 0, h, -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- /* Main Loop
- * Open window with initial window size, title bar,
- * RGBA display mode, and handle input events.
- */
- int main(int argc, char** argv)
- {
- pixels = readImage("blueguy\\blueguy.bmp", &width, &height);
-
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(100, 100);
- glutCreateWindow(argv[0]);
- init();
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
- GLubyte* readImage( const char* filename, GLsizei* width, GLsizei *height )
- {
- FILE* file;
- GLubyte* pixels;
- unsigned long size; /* size of the image in bytes.*/
- unsigned long i; /* standard counter.*/
- unsigned short int planes; /* number of planes in image (must be 1) */
- unsigned short int bpp; /* number of bits per pixel (must be 24)*/
- char temp; /* temporary color storage for bgr-rgb conversion.*/
-
- /* make sure the file is there.*/
- if ((file = fopen(filename, "rb"))==NULL)
- {
- printf("File Not Found : %s\n",filename);
- return 0;
- }
-
- /* seek through the bmp header, up to the width/height:*/
- fseek(file, 18, SEEK_CUR);
-
- /* No 100% errorchecking anymore!!!*/
-
- /* read the width*/
- *width = getInt(file);
- printf("Width of %s: %lu\n", filename, *width);
-
- /* read the height */
- *height = getInt(file);
- printf("Height of %s: %lu\n", filename, *height);
-
- /* calculate the size (assuming 24 bits or 3 bytes per pixel).*/
- size = (*width) * (*height) * 3;
-
- /* read the planes*/
- planes = getShort(file);
-
- if (planes != 1)
- {
- printf("Planes from %s is not 1: %u\n", filename, planes);
- return 0;
- }
-
- /* read the bpp*/
- bpp = getShort(file);
-
- if (bpp != 24)
- {
- printf("Bpp from %s is not 24: %u\n", filename, bpp);
- return 0;
- }
-
- /* seek past the rest of the bitmap header.*/
- fseek(file, 24, SEEK_CUR);
-
- /* read the data. */
- pixels = (GLubyte*)malloc(size);
-
- if (pixels == NULL)
- {
- printf("Error allocating memory for color-corrected image data");
- return 0;
- }
-
- if ((i = fread(pixels, size, 1, file)) != 1)
- {
- printf("Error reading image data from %s.\n", filename);
- return 0;
- }
-
-
- for (i = 0; i < size; i += 3)
- { /* reverse all of the colors. (bgr -> rgb)*/
- temp = pixels[i];
- pixels[i] = pixels[i+2];
- pixels[i+2] = temp;
- }
-
-
- /* we're done.*/
- return pixels;
- }
- static unsigned int getInt(FILE* fp)
- {
- int c, c1, c2, c3;
-
- /* get 4 bytes*/
- c = getc(fp);
- c1 = getc(fp);
- c2 = getc(fp);
- c3 = getc(fp);
-
- return ((unsigned int) c) +
- (((unsigned int) c1) << 8) +
- (((unsigned int) c2) << 16) +
- (((unsigned int) c3) << 24);
- }
- static unsigned int getShort(FILE* fp)
- {
- int c, c1;
-
- /*get 2 bytes*/
- c = getc(fp);
- c1 = getc(fp);
- return ((unsigned int) c) + (((unsigned int) c1) << 8);
- }
复制代码 |
|