如何使用OpenGL直接在屏幕上绘制像素数组? [英] How to draw an array of pixels directly to the screen with OpenGL?

查看:120
本文介绍了如何使用OpenGL直接在屏幕上绘制像素数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接将像素写入屏幕(不使用顶点和多边形)。我已经调查了类似问题的各种答案,其中最着名的答案是此处和< a href =https://stackoverflow.com/questions/6644248/pixelart-with-opencl-opengl>这里。



我看到一对夫妇绘制像素到屏幕上的方法可能是可能的,但它们似乎都是间接的,并且使用不必要的浮点运算:


  1. Draw为屏幕上的每个像素设置 GL_POINT 。我已经尝试了这一点,它的工作原理,但这似乎是一个低效率的方式来绘制像素到屏幕上。为什么要在将要转换为像素数据数组时将数据写入浮点。

  2. 创建跨越整个屏幕的2d四元组并写入一个纹理。像第一个选项一样,这似乎是将像素放在屏幕上的一种迂回方式。在投入屏幕之前,纹理仍然必须经过光栅化处理。纹理必须是正方形的,大多数屏幕不是方形的,所以我必须处理这个问题。 我如何得到一个颜色矩阵,其中像素[0] [0] 对应左上角,像素[1920] [1080 ] 对应右下方,以最直接和最有效的方式在屏幕上使用OpenGL?



    直接写入framebuffer看起来像最有希望的选择,但我只看到人们使用framebuffer进行着色。

    第一次:OpenGL是一个绘图API旨在利用一个摄取同质坐标的光栅化系统来定义几何图元,这些图元得到转换并进行良好的光栅化处理。仅仅绘制像素并不是OpenGL API所关心的。大多数GPU本质上都是浮点处理器,实际上可以比整数更有效地处理浮点数据。

    当它将要被转换成一个像素数据数组时。


    因为OpenGL是一个光栅化API,也就是说它需要原始的几何图形数据并将其转换为像素。它不处理像素作为输入数据,除了图像对象(纹理)的形式。


    纹理必须是正方形,并且大部分屏幕都不是正方形的,所以我必须处理这个问题。


    无论谁告诉过你,来自:他们错了。 OpenGL-1.x有这样的约束,即纹理必须在任何方向都是2的幂次方,但宽度和高度可能不同。自从OpenGL-2的纹理尺寸完全是任意的。



    然而,纹理可能不是直接更新屏幕上单个像素的最有效方法。然而,首先绘制像素缓冲区的像素是一个好主意,该像素缓冲区用于显示加载到纹理中,然后绘制到完整的视口四元组上。如果您的目标是直接操纵屏幕上的像素,而不使用光栅化器,那么OpenGL不适合作业。还有其他2D图形API,可让您直接将像素推送到屏幕。

    然而,推动单个像素的效率并不高。我强烈推荐在像素缓冲区上进行操作,然后将其作为一个整体进行显示。并且使用OpenGL进行绘制,绘制完整的视口,纹理四元组与其他绘图API一样有效。


    I want to write pixels directly to to screen (not using vertices and polygons). I have investigated a variety of answers to similar questions, the most notable ones here and here.

    I see a couple ways drawing pixels to the screen might be possible, but they both seem to be indirect and use unnecessary floating point operations:

    1. Draw a GL_POINT for each pixel on the screen. I've tried this and it works, but this seems like an inefficient way to draw pixels onto the screen. Why write my data in floating-points when it's going to be transformed into an array of pixel data.

    2. Create a 2d quad that spans the entire screen and write a texture to it. Like the first options, this seems to be a roundabout way of putting pixels on the screen. The texture would still have to go through rasterization before getting put on the screen. Also textures must be square, and most screens are not square, so I'd have to handle that problem.

    How do I get, a matrix of colors, where pixels[0][0] corresponds to the upper left corner and pixels[1920][1080] corresponds to the bottom right, onto the screen in the most direct and efficient way possible using OpenGL?

    Writing directly to the framebuffer seems like the most promising choice, but I have only seen people using the framebuffer for shading.

    解决方案

    First off: OpenGL is a drawing API designed to make use of a rasterizer system that ingests homogenous coordinates to define geometric primitives, which get transformed and, well rasterized. Merely drawing pixels is not what the OpenGL API is concerned with. Also most GPUs are floating point processors by nature and in fact can process floating point data more efficiently than integers.

    Why write my data in floating-points when it's going to be transformed into an array of pixel data.

    Because OpenGL is a rasterizer API, i.e. it takes primitive geometrical data and turns it into pixels. It doesn't deal with pixels as input data, except in the form of image objects (textures).

    Also textures must be square, and most screens are not square, so I'd have to handle that problem.

    Whoever told you that, or whereever you got that from: They are wrong. OpenGL-1.x had that constraint that textures had to be power-of-2 sized in either direction, but width and height may differ. Ever since OpenGL-2 texture sizes are completely arbitrary.

    However a texture might not be the most efficient way to directly update single pixels on the screen either. It is however a great idea to first draw pixels of an pixel buffer, which for display is loaded into a texture, that then gets drawn onto a full viewport quad.

    However if your goal is direct manipulation of on-screen pixels, without a rasterizer inbetween, then OpenGL is not the right API for the job. There are other, 2D graphics APIs that allow you to directly push pixels to the screen.

    However pushing individual pixels is very inefficient. I strongly recomment operating on a pixel buffer, which is then blited or drawn as a whole for display. And doing it with OpenGL, drawing a full viewport, textured quad is as good for this, and as efficient as any other graphics API.

    这篇关于如何使用OpenGL直接在屏幕上绘制像素数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆