OpenGL 中的每像素绘图

发布于 2024-12-11 12:13:38 字数 269 浏览 0 评论 0原文

我曾经用“每像素”绘图编写小游戏, 我的意思是使用一些 SetPixel(x,y,color) 函数或类似的函数。

我对OpenGL也很感兴趣,但不太了解。

这是在 OpenGL 中进行每像素绘图的好(快)方法吗?

例如,使用纹理四边形作为精灵会很好, 或整个应用程序背景屏幕,可以 使用我自己的 SetPixel 例程设置不同的像素 我会写......或任何其他方式 - 但它应该是有效的 尽可能多

(特别是我对 OGL 的基本基础 1.0 版本感兴趣)

I used to write small games with 'per pixel' drawing,
I mean with some SetPixel(x,y,color) function or such.

I am also interested in OpenGL but do not know it much.

Is it a good (fast) way to do a per pixel drawing in OpenGL ?

It would be good for example to use textured quads as a sprites,
or whole application background screen, with possibility to
set distinct pixel with some kind of my own SetPixel routine
i would write... or any other way - but it should be efficient
as much as it cans

(especialy im interested in basic fundamenta 1.0 version of OGL)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞笑我太脆弱 2024-12-18 12:13:38

您可以设置一个投影,将顶点坐标 1:1 映射到像素坐标:

glViewport(0, 0, window_width, window_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

从这里开始,顶点 X,Y 坐标以像素为单位,原点位于左下角。理论上,您可以将立即模式与 GL_POINT 原语一起使用。但将事情分批处理是一个更好的主意。不是单独发送每个点,而是创建一个包含您想要绘制的所有点的数组:

struct Vertex
{
    GLfloat x,y,red,green,blue;
};

std::vector<Vertex> vertices;
/* fill the vertices vector */

您可以 OpenGL 指向…………

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

/* Those next two calls don't copy the data, they set a pointer, so vertices must not be deallocated, as long OpenGL points to it! */
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].red);

并通过一次调用让它访问并绘制所有点:

glDrawArrays(GL_POINTS, 0, vertices.size();

You can set a projection that will map vertex coordinates 1:1 to pixel coordinates:

glViewport(0, 0, window_width, window_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

From here on, vertex X,Y coordinates are in pixels with the origin in the lower left corner. In theory you could use the immediate mode with GL_POINT primitives. But it's a much better idea to batch things up. Instead of sending each point indivisually create an array of all the points you want to draw:

struct Vertex
{
    GLfloat x,y,red,green,blue;
};

std::vector<Vertex> vertices;
/* fill the vertices vector */

This you can OpenGL point to…

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

/* Those next two calls don't copy the data, they set a pointer, so vertices must not be deallocated, as long OpenGL points to it! */
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].red);

…and have it access and draw it all with a single call:

glDrawArrays(GL_POINTS, 0, vertices.size();
北斗星光 2024-12-18 12:13:38

你真的不想这样做。

这是在 OpenGL 中进行每像素绘图的好(快)方法吗?

没有好的或快速的方法可以做到这一点。由于速度的原因,它是非常不鼓励的。

尽管在 OGL 1 中并不容易(或在某些情况下可能),但正确的方法是使用像素着色器或混合模式。这是唯一正确的方法,其他任何方法都是对整个系统的破坏。

根据数据需要修改的方式,顶点颜色和混合模式可能能够解决某些用途。它不会单独对每个像素着色,但您可以更快地修改纹理。

为此,您可以绘制单像素四边形(尽管必须注意偏移它们并处理过滤以防止模糊),或者您可以获取纹理数据并稍后对其进行操作。两者都会慢得令人难以置信,但可以正常工作。

使用纹理数据可能更简单并且可能稍微快一些。

You really don't want to do this.

Is it a good (fast) way to do a per pixel drawing in OpenGL ?

There is no good or fast way to do this. It is highly discouraged due to the speed.

The proper way, although not easy (or in some cases possible) in OGL 1, is to use pixel shaders or blend modes. That is the only correct way, anything else is hacking around the entire system.

Depending on how the data needs modified, vertex colors and blend modes may be able to solve the some uses. It won't tint each pixel individually, but you can modify the texture must faster.

To do it, you can draw single-pixel quads (although care must be taken to offset them and handle filtering to prevent blurring) or you can get texture data and manipulate it later. Both will be unbelievably slow, but could function.

Working with the texture data is probably simpler and may be slightly faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文