我的OpenGL项目的性能水平非常低,有时速度慢到足以使应用程序崩溃。它以大约每秒1帧的速度运行,但是我希望20,或者如果可能的话我希望60。所以我的程序要做的是,它需要一个位图文件,并从其中一个像素中获取红色,绿色和蓝色。 ,将相同颜色的像素存储在HDC中,重置坐标,依此类推,直到完成所有像素并将其打印在屏幕上为止。

这是我的代码:

#include "bitmap_image.h"
#include <windows.h>
#include <gl/gl.h>
#include <iostream>
using namespace std;
HDC Image(HDC hDC, string File_Name, int x_position, int y_position, int length, int height)
{
File_Name = "C:/Users/David/Pictures/" + File_Name + ".bmp";
bitmap_image image(File_Name);      // Open the bitmap
unsigned char red;
unsigned char green;
unsigned char blue;
restart:
image.get_pixel(x_position, y_position, red, green, blue);     // Get the red green and blue from x_position and y_position and     store it in red green and blue.
glBegin (GL_TRIANGLES);                                        // Make a pixel at x_position and y_position with red green and blue.
glColor3ub (red, green, blue);
glVertex2f (-1 + 0.0015 * x_position, 1 - 0.003 * y_position);
glVertex2f (-1 + 0.0015 * x_position, 0.997 - 0.003 * y_position);
glVertex2f (-0.9985 + 0.0015 * x_position, 1 - 0.003 * y_position);
glEnd();
glBegin (GL_TRIANGLES);
glColor3ub (red, green, blue);
glVertex2f (-1 + 0.0015 * x_position, 0.997 - 0.003 * y_position);
glVertex2f (-0.9985 + 0.0015 * x_position, 1 - 0.003 * y_position);
glVertex2f (-0.9985 + 0.0015 * x_position, 0.997 - 0.003 * y_position);
glEnd();
if (x_position==length)      // If x_position equals to length of bmp set x_position to 0 and add 1 to y_position.
{
if (y_position==height)      // If bmp is done loading go to done.
{
goto done;
}
x_position = 0;
y_position = y_position + 1;
}
x_position = x_position + 1;
goto restart;
done:
return hDC;
}

void Load_Image(HDC hDC)
{
SwapBuffers(hDC);                                  // Load the image
}

int main()
{
int x = 0;
int y = 500;
HDC River = Image(hDC, "River", 0, 0, 1340, 678);                // make the river
HDC Turtle_1 = Image(River, "Turtle", x, y, 95, 65);            // make the turtle 10 pixels away on the river
x = x + 10;                                                                                              // push the turtle 10 more pixels
HDC Turtle_2 = Image(River, "Turtle", x, y, 95, 65);            // make the turtle 20 pixels away on the river
x = x + 10;                                                                                              // and so on...
HDC Turtle_3 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_4 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_5 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_6 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_7 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_8 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_9 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_10 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_11 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_12 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_13 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_14 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_15 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_16 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_17 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_18 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_19 = Image(River, "Turtle", x, y, 95, 65);
x = x + 10;
HDC Turtle_20 = Image(River, "Turtle", x, y, 95, 65);
Load_Image(Turtle_1);               // Load the images to make the video
Load_Image(Turtle_2);
Load_Image(Turtle_3);
Load_Image(Turtle_4);
Load_Image(Turtle_5);
Load_Image(Turtle_6);
Load_Image(Turtle_7);
Load_Image(Turtle_8);
Load_Image(Turtle_9);
Load_Image(Turtle_10);
Load_Image(Turtle_11);
Load_Image(Turtle_12);
Load_Image(Turtle_13);
Load_Image(Turtle_14);
Load_Image(Turtle_15);
Load_Image(Turtle_16);
Load_Image(Turtle_17);
Load_Image(Turtle_18);
Load_Image(Turtle_19);
Load_Image(Turtle_20);
}

关于代码的注释#1:您可以在http://partow.net/programming/bitmap/上找到bitmap_image.h

关于代码的注释#2:我省略了OpenGL函数,例如:EnableOpenGL,DisableOpenGL等。

有什么解决方案可以加快速度吗?谢谢!

最佳答案

使用该代码,您可以使用glBegin(GL_TRIANGLE_STRIP)并摆脱大约一半的gl调用。

但是,为什么要操纵位图的各个像素,而不是将该位图设置为纹理,然后让视频硬件完成所有工作呢?

10-04 18:18