在OpenGL中,我可以在坐标(5, 5)处精确地绘制一个像素吗?

发布于 2024-11-10 05:30:59 字数 117 浏览 2 评论 0原文

我所说的 (5, 5) 正是指第五行第五列。

我发现使用屏幕坐标来绘制东西非常困难,OpenGL 中的所有坐标都是相对的,通常范围从 -1.0 到 1.0。为什么阻止程序员使用屏幕坐标/窗口坐标如此严重?

By (5, 5) I mean exactly the fifth row and fifth column.

I found it very hard to draw things using screen coordinates, all the coordinates in OpenGL is relative, and usually ranging from -1.0 to 1.0. Why it is so serious to prevent programmers from using screen coordinates / window coordinates?

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

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

发布评论

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

评论(6

开始看清了 2024-11-17 05:30:59

最简单的方法可能是通过 glOrtho。那么顶点可以是像素坐标。缺点是调整窗口大小可能会导致问题,并且您主要浪费了加速变换。

假设窗口大小为 640x480:

// You can reverse the 0,480 arguments depending on you Y-axis 
// direction preference
glOrtho(0, 640, 0, 480, -1, 1);

帧缓冲区对象和纹理是另一种途径,但您必须创建自己的光栅化例程(绘制线条、圆形、位图等)。有可能用于此目的的库。

The simplest way is probably to set the projection to match the pixel dimensions of the rendering space via glOrtho. Then vertices can be in pixel coordinates. The downside is that resizing the window could cause problems and you're mostly wasting the accelerated transforms.

Assuming a window that is 640x480:

// You can reverse the 0,480 arguments depending on you Y-axis 
// direction preference
glOrtho(0, 640, 0, 480, -1, 1);

Frame buffer objects and textures are another avenue but you'll have to create your own rasterization routines (draw line, circle, bitmap, etc). There are problaby libs for this.

淡淡绿茶香 2024-11-17 05:30:59

@dandan78 OpenGL 不是矢量图形渲染器。是一个光栅化器。更准确地说,是通过C语言接口描述的标准。光栅化器将 3D 协调空间(汽车、树、球体、龙)中表示的对象映射到 2D 协调空间(例如平面、应用程序窗口或显示器),这些 2D 坐标属于离散协调平面。光栅化的反渲染方法是光线追踪。

矢量图形是一种通过数学函数以非离散方式表示一组曲线、直线或类似几何图元的方法。所以矢量图形属于“模型表示”领域而不是“渲染”领域。

@dandan78 OpenGL is not a Vector Graphics renderer. Is a Rasterizer. And in a more precise way is a Standard described by means of a C language interface. A rasterizer, maps objects represented in 3D coordinated spaces (a car, a tree, a sphere, a dragon) into 2D coordinated spaces (say a plane, your app window or your display), these 2d coordinates belong to a discrete coordinated plane. The counter rendering method of rasterization is Ray Tracing.

Vector graphics is a way to represent by means of mathematical functions a set of curves, lines or similar geometrical primitives, in a nondiscrete way. So Vector graphics is in the "model representation" field rather than "rendering" field.

稀香 2024-11-17 05:30:59

您只需更改“相机”即可通过将模型视图矩阵设置为恒等并将投影设置为正交投影来使 3D 坐标与屏幕坐标匹配(请参阅我在 这个问题)。然后您可以在所需的屏幕坐标处绘制一个单点图元。

您还可以使用 glWindowPos 设置光栅位置(与 glRasterPos 不同,它在屏幕坐标中工作),然后仅使用 glDrawPixels 绘制 1x1 像素图像。

You can just change the "camera" to make 3D coordinates match screen coordinates by setting the modelview matrix to identity and the projection to an orthographic projection (see my answer on this question). Then you can just draw a single point primitive at the required screen coordinates.

You can also set the raster position with glWindowPos (which works in screen coordinates, unlike glRasterPos) and then just use glDrawPixels to draw a 1x1 pixel image.

隱形的亼 2024-11-17 05:30:59
glEnable( GL_SCISSOR_TEST );
glScissor( 5, 5, 1, 1 ); /// position of pixel
glClearColor( 1.0f, 1.0f, 1.0f, 0.0f ); /// color of pixel
glClear( GL_COLOR_BUFFER_BIT );
glDisable( GL_SCISSOR_TEST );

通过更改 glScissor 的最后 2 个参数,您还可以绘制像素完美的矩形。

glEnable( GL_SCISSOR_TEST );
glScissor( 5, 5, 1, 1 ); /// position of pixel
glClearColor( 1.0f, 1.0f, 1.0f, 0.0f ); /// color of pixel
glClear( GL_COLOR_BUFFER_BIT );
glDisable( GL_SCISSOR_TEST );

By changing last 2 arguments of glScissor you can also draw pixel perfect rectangle.

羁〃客ぐ 2024-11-17 05:30:59

几年前我做了一些 3D 编程,虽然我远非专家,但我认为您忽略了经典位图 DrawPixel(x, y) 图形和类型之间的一个非常重要的区别使用 Direct3D 和 OpenGL 完成的图形。

早在 3D 出现之前,计算机图形学主要涉及位图,即彩色点的集合。这些点与显示器上的像素具有 1:1 的关系。

然而,这有很多缺点,包括使 3D 变得非常困难,并且需要不同尺寸的位图来适应不同的显示分辨率。

在 OpenGL/D3D 中,您正在处理矢量图形。线由 3 维坐标空间中的点定义,形状由线定义,等等。表面可以有纹理,可以添加灯光,以及各种类型的灯光效果等。然后可以通过虚拟相机查看整个场景或场景的一部分。

通过这个虚拟相机,您“看到”的是场景在 2D 表面上的投影。此时我们仍在处理矢量图形。然而,由于计算机显示器由离散像素组成,因此必须对该矢量图像进行光栅化,从而将矢量转换为具有实际像素的位图。

总而言之,您不能使用屏幕/窗口坐标,因为 OpenGL 基于矢量图形。

I did a bit of 3D programming several years back and, while I'm far from an expert, I think you are overlooking a very important difference between classical bitmapped DrawPixel(x, y) graphics and the type of graphics done with Direct3D and OpenGL.

Back in the days before 3D, computer graphics was mostly about bitmaps, which is to say collections of colored dots. These dots had a 1:1 relationship with the pixels on your monitor.

However, that had numerous drawbacks, including making 3D very difficult and requiring bitmaps of different sizes for different display resolutions.

In OpenGL/D3D, you are dealing with vector graphics. Lines are defined by points in a 3-dimensional coordinate space, shapes are defined by lines and so on. Surfaces can have textures, lights can be added, as can various types of lighting effects etc. This entire scene, or a part of it, can then be viewed through a virtual camera.

What you 'see' though this virtual camera is a projection of the scene onto a 2D surface. We're still dealing with vector graphics at this point. However, since computer displays consist of discrete pixels, this vector image has to be rasterized, which transforms the vector into a bitmap with actual pixels.

To summarize, you can't use screen/window coordinates because OpenGL is based on vector graphics.

请持续率性 2024-11-17 05:30:59

我知道我参加聚会已经很晚了,但以防万一将来有人提出这个问题。我使用以下方法将屏幕坐标转换为 OpenGL 矩阵坐标:

double converterX (double x, int window_width) {
    return 2 * (x / window_width) - 1;
}

double converterY (double y, int window_height) {
    return -2 * (y / window_height) + 1;
}

这基本上是重新缩放方法。

I know I'm very late to the party, but just in case someone has this question in the future. I converted screen coordinates to OpenGL matrix coordinates using these:

double converterX (double x, int window_width) {
    return 2 * (x / window_width) - 1;
}

double converterY (double y, int window_height) {
    return -2 * (y / window_height) + 1;
}

Which are basically re-scaling methods.

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