我有以下函数,可以在窗口上绘制像素网格,我正在使用 sdl。
问题是速度太慢了!它使我的程序以 10fps 运行,所以我想我一定做错了什么。
这是我正在使用的代码
void rayTracing(SDL &sdl) {
int nx = 1440;
int ny = 810;
for (int x = 0; x < nx; x++) {
for (int y = 0; y < ny; y++) {
float r = float(x) / float(nx);
float g = float(y) / float(ny);
float b = 0.2;
int ir = int(255.99 * r);
int ig = int(255.99 * g);
int ib = int(255.99 * b);
SDL_SetRenderDrawColor(sdl.renderer.get(), ir, ig, ib, 255);
SDL_RenderDrawPoint(sdl.renderer.get(), x, ny - y);
}
}
SDL_SetRenderDrawColor(sdl.renderer.get(), 0, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
也许问题是我使用 SDL_RenderDrawPoint 的方式?
小智 5
最好的选择可能是创建一个单独的渲染纹理,您可以通过指针直接访问它。您进行更改,完成后,您调用一次 SDL 纹理更新,它将把内存中的纹理传输到显示器。
这就是创建 SDL 纹理的方法
theTexture = SDL_CreateTexture( theRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, nx, ny );
Run Code Online (Sandbox Code Playgroud)
然后,您还可以创建一个内存缓冲区来绘制
uint32_t *textureBuffer = new uint32_t[ nx * ny ];
Run Code Online (Sandbox Code Playgroud)
此时,绘制一个像素实际上只是将相应的 RGB8888 颜色值写入到textureBuffer数组中,有点像这样
textureBuffer[(yPos*nx) + xPos] = 0xFF000000 | (ir<<16) | (ib<<8) | ig;
Run Code Online (Sandbox Code Playgroud)
绘制后,您可以像这样一下子更新整个纹理
SDL_UpdateTexture( theTexture , NULL, textureBuffer, nx * sizeof (uint32_t));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3697 次 |
| 最近记录: |