简体   繁体   English

如何在 OpenGL 中将像素作为纹理绘制到多边形?

[英]How to I draw pixels as a texture to a polygon in OpenGL?

In C++ OpenGL, I want to draw each pixel manually (as a texture I assume) on to a simple square primitive, or indeed 2 polygons forming a square.在 C++ OpenGL 中,我想手动将每个像素(作为我假设的纹理)绘制到一个简单的正方形图元上,或者实际上是 2 个多边形形成一个正方形。

I have no idea where to start, or what phrases to look for.我不知道从哪里开始,或者要寻找什么短语。 Am I looking for texture mapping or creating textures?我是在寻找纹理映射还是创建纹理? Most examples are to load from a file, but I dont want to do that.大多数示例是从文件加载,但我不想这样做。

I've tried reading my OpenGL Programming Guide book, but its just a maze as I'm quite new to OpenGL.我试过阅读我的 OpenGL 编程指南书,但这只是一个迷宫,因为我对 OpenGL 很陌生。 Please help.请帮忙。

Take a close look at glTexImage2D.仔细看看 glTexImage2D。 This is the call that loads the image in OpenGL for a 2D Texture.这是在 OpenGL 中为 2D 纹理加载图像的调用。

glTexImage2D actually takes a raw pointer to the pixel data in the image. glTexImage2D 实际上采用指向图像中像素数据的原始指针。 You can allocate memory yourself, and set the image data directly (however you want), then call this function to pass the image to OpenGL.你可以自己分配memory,直接设置图片数据(随便你),然后调用这个function把图片传给OpenGL。

Once you've created the texture, you can bind it to the state so that your triangles use that texture.创建纹理后,您可以将其绑定到 state 以便您的三角形使用该纹理。

There is nothing in OpenGL that directly requires images used as textures to be loaded from files. OpenGL 中没有任何内容直接要求从文件中加载用作纹理的图像。 A good resource for textures can be found in this GameDev article.可以在这篇 GameDev 文章中找到一个很好的纹理资源。

If the square you are drawing to is 2 dimensional and not rotated, you may be looking for glDrawPixels .如果您要绘制的正方形是二维的且未旋转,则您可能正在寻找glDrawPixels It allows you to draw a rectangular region of pixels directly to the buffer without using any polygons.它允许您在不使用任何多边形的情况下直接将像素的矩形区域绘制到缓冲区。

If you are new then I would recommend starting from tutorial one on from NeHe .如果您是新手,那么我建议您从NeHe的教程一开始。

Do note that while glTexImage2D() takes a pointer to the pixels to load, it does not care about any changes to that data after the call.请注意,虽然glTexImage2D()需要一个指向要加载的像素的指针,但它并不关心调用后对该数据的任何更改。 That is, the data is really loaded from the memory area you specify, and OpenGL creates a copy of its own.也就是说,数据实际上是从您指定的 memory 区域加载的,并且 OpenGL 会创建自己的副本。

This means that if the data changes, you will need to either re-do the glTexImage2D() call, or use something like glTexSubImage2D() to do a partial update.这意味着如果数据发生变化,您将需要重新调用 glTexImage2D() 或使用glTexSubImage2D()之类的方法进行部分更新。 I would recommend testing and profiling to see if sub-texture updating is quick, not sure how actual drivers optimize for that case.我建议测试和分析以查看子纹理更新是否快速,不确定实际驱动程序如何针对这种情况进行优化。

Thanks to Reed Copsey for pointing me towards glTexImage2D .感谢 Reed Copsey 将我指向glTexImage2D Turns out this is very simple;事实证明这很简单; just pass an array of GLubyte to the glTexImage2D function (as well as all the functions needed to bind the texture, etc).只需将 GLubyte 数组传递给glTexImage2D function (以及绑定纹理所需的所有函数等)。 Haven't tried this exact snippet of code, but it should work fine.还没有尝试过这个确切的代码片段,但它应该可以正常工作。 The array elements represent a serial version of the rows, columns and channels.数组元素表示行、列和通道的串行版本。

int pixelIndex = 0;
GLubyte pixels[400];

for (int x = 0; x < 10; x++)
{
    for (int y = 0; y < 10; x++)
    {
         for (int channel = 0; channel < 4; channel++)
         {
             // 0 = black, 255 = white
             pixels[pixelIndex++] = 255;
         }
    }
}

glTexImage2D(
    GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
    GL_RGBA, GL_UNSIGNED_BYTE, pixels);

I've read in the OpenGL book you can use a 2D array for monochrome images, so I assume you could use a 3D array also.我在 OpenGL 书中读过,您可以将二维数组用于单色图像,因此我假设您也可以使用 3D 数组。

What you are looking for is generally called "render to texture."您正在寻找的内容通常称为“渲染到纹理”。

http://developer.nvidia.com/object/gdc_oglrtt.html http://developer.nvidia.com/object/gdc_oglrtt.html

That tutorial is pretty old.那个教程很老了。 I'd guess that some of those extensions aren't actually extensions in newer versions of OGL.我猜这些扩展中的一些实际上并不是新版本的 OGL 中的扩展。 Sorry I can't be more help.抱歉,我无法提供更多帮助。 I don't work directly with graphics API's much any more, and if I did I'd use D3D if I could (to avoid the extension soup, and for other reasons I won't bore you with here.)我不再直接使用图形 API,如果可以的话,我会使用 D3D(为了避免扩展汤,出于其他原因,我不会在这里让你厌烦。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM