简体   繁体   English

OpenGL 2DTexture 来自 c++ 无符号字节数组

[英]OpenGL 2DTexture from c++ unsigned byte array

I'm trying to process a 2D array in a fragment shader - so to learn that, I started building my array:我正在尝试在片段着色器中处理二维数组 - 为了了解这一点,我开始构建我的数组:

int const _s = 512;
std::array<GLubyte,_s*_s*4> hitmap;
for(unsigned j = 0; j < _s; j++) {
  for(unsigned i = 0; i < _s; i+=4) {
   hitmap[i+j*_s] = j%256;                  //R     
   if(j>33 && j < 45) hitmap[i+j*_s] = 0;   //R
   hitmap[i+j*_s+1] = i%256;                //G
   if(i>33 && i < 45) hitmap[i+j*_s+1] = 0; //G
   hitmap[i+j*_s+2] = 0;                    //B
   hitmap[i+j*_s+3] = 255;                  //A
  }
}

And as a first step, just push that as a texture and display that on a surface.作为第一步,只需将其作为纹理推送并显示在表面上。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _s, _s, 0, GL_RGBA, GL_UNSIGNED_BYTE, hitmap.data());

I'm drawing two triangles with following coordinates to get a square:我正在绘制两个具有以下坐标的三角形以获得正方形:

    // positions           // texture coords
     1.f,  1.f, 0.0f,      1.0f, 1.0f, // top right
     1.f, -1.f, 0.0f,      1.0f, 0.0f, // bottom right
    -1.f, -1.f, 0.0f,      0.0f, 0.0f, // bottom left
    -1.f,  1.f, 0.0f,      0.0f, 1.0f  // top left 

And my fragment shader is pretty stupid:我的片段着色器非常愚蠢:

#version 150 core
uniform sampler2D ourTexture;
out vec4 FragColor;
in vec2 TexCoord;
void main()
{
FragColor = texture(ourTexture, TexCoord);
};

(yes, I'm using a pretty old version here:/) (是的,我在这里使用的是相当旧的版本:/)

The result I get is the following:我得到的结果如下:

结果

There is some weird stuff right at the top, and the texture seems to repeat (although, not mirrored).顶部有一些奇怪的东西,纹理似乎在重复(尽管不是镜像)。 I seriously can't figure out what I'm doing wrong - it looks like I don't provide enough data.我真的无法弄清楚我做错了什么 - 看起来我没有提供足够的数据。 I expect to get a 2x2 square pattern, given I'm mapping 512 px into the range of 256 via mod divisions.我希望得到一个 2x2 的正方形图案,因为我通过 mod 分割将 512 px 映射到 256 的范围内。

Edit: Like this:编辑:像这样:

预期模式

You've misunderstood the concept of GL_MIRRORED_REPEAT .您误解了GL_MIRRORED_REPEAT的概念。 See OpenGL wiki glTexParameter请参阅 OpenGL wiki glTexParameter

GL_MIRRORED_REPEAT causes the s coordinate to be set to the fractional part of the texture coordinate if the integer part of s is even;如果s的 integer 部分是偶数,则GL_MIRRORED_REPEAT使 s 坐标设置为纹理坐标的小数部分; if the integer part of s is odd, then the s texture coordinate is set to 1−frac(s) , where frac(s) represents the fractional part of s .如果s的 integer 部分是奇数,则s纹理坐标设置为1−frac(s) ,其中frac(s)表示s的小数部分。

This means that the texture is mirrored if the texture coordinates are in the range [1.0, 2.0], [3.0, 4.0], ...;这意味着如果纹理坐标在 [1.0, 2.0], [3.0, 4.0], ... 范围内,则纹理被镜像; If the texture coordinates are in range [0.0, 1.0], [2.0, 3.0], ... it will not be mirrored.如果纹理坐标在 [0.0, 1.0], [2.0, 3.0], ... 范围内,则不会被镜像。
Since all of your texture coordinates are in the range [0.0, 1.0], nothing is mirrored at all.由于所有纹理坐标都在 [0.0, 1.0] 范围内,因此根本没有镜像。

Scale the the texture coordinates in the fragment shader to see the concept of GL_MIRRORED_REPEAT :缩放片段着色器中的纹理坐标以查看GL_MIRRORED_REPEAT的概念:

void main()
{
    FragColor = texture(ourTexture, TexCoord * 2.0);
};

There is also a mistake in the condition statement of the inner for -loop.内部for循环的条件语句也有错误。 The inner loop must run from 0 to _s*4 rather than form 0 to _s :内部循环必须从0运行到_s*4而不是从0_s

for(unsigned i = 0; i < _s; i+=4) {

for(unsigned i = 0; i < _s*4; i+=4) {

The size of a line in bytes is _s*4 and not _s :以字节为单位的行大小是_s*4而不是_s

int const _s = 512;
std::array<GLubyte,_s*_s*4> hitmap;
for(unsigned j = 0; j < _s; j++) {
    for(unsigned i = 0; i < _s*4; i+=4) {
        hitmap[i + j*_s*4] = j%256;                    //R     
        if(j>33 && j < 45) hitmap[i + j*_s*4] = 0;     //R
        hitmap[i + j*_s*4 + 1] = i%256;                //G
        if(i>33 && i < 45) hitmap[i + j*_s*4 + 1] = 0; //G
        hitmap[i + j*_s*4 + 2] = 0;                    //B
        hitmap[i + j*_s*4 + 3] = 255;                  //A
    }
}

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

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