我的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等。
任何使它更快的解决方案?谢谢!
答案 0 :(得分:0)
使用该代码,您可以使用glBegin(GL_TRIANGLE_STRIP)
并删除大约一半的gl调用。
但是为什么要操作位图的单个像素而不是将该位图设置为纹理并让视频硬件完成所有工作呢?
答案 1 :(得分:-1)
通过gsplice()以保守顺序添加位图来预加载图像,并使用valarray()
并使用AI进行指导,其中最慢的fps来自非预先格式化的动作会更快。
这样想。你有18个号码。你能用多少种方法把它们整理好?什么时候可以从这些订单之间的位置开始,记住你已经看过这个订单,并开始相同的序列?尝试连续3次。然后是4,然后是5,依此类推。