OpenGL texture wrap ignored when using glBindImageTexture - opengl

I am using a GL_R32F format texture to pass a 2D array of floats to an OpenGL shader. I want the texture to be wrap around so I specify the GL_REPEAT, but it seems to be ignored for textures I create with glBindImageTexture. If I try and access a pixel value beyond the image edge in the shader it just returns 0 and does not wrap around. This is the code I am using...
glGenTextures(1,#imagetexture1);
glBindTexture(GL_TEXTURE_2D,imagetexture1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
setlength(arrayvalues,imwidth*imheight);
for loop:=0 to imwidth*imheight-1 do arrayvalues[loop]:=random;
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, imwidth, imheight, 0, GL_RED, GL_FLOAT, arrayvalues);
setlength(arrayvalues,0);
glBindImageTexture( 1, //unit
imagetexture1, //texture
0, //level
false, //layered
0, //layer
GL_READ_WRITE, //access
GL_R32F); //format
The GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER do work, it is just the GL_TEXTURE_WRAP parameters that get ignored. Is there some other way I am supposed to declare wrap when using glBindImageTexture?

Wrapping behavior (and min/mag modes for that matter) only make sense when accessing a texture. When accessing an image, those things don't exist. You are fetching a pixel of data from a specific piece of the image. If you try to access outside of the range of the image, then you get undefined behavior, exactly as would happen if you access outside of the bounds of an array.

Related

Setting up GL_TEXTURE_2D_ARRAY, framebuffer incomplete

For cascaded shadow mapping I'm trying to use a GL_TEXTURE_2D_ARRAY for the individual shadow maps. However following tutorials found online and even looking up things in a textbook I can't seem to create a working framebuffer, as it always errors with GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT.
The code:
glGenFramebuffers(1, &m_DepthMap.m_FrameBuffer);
glGenTextures(1, &m_DepthMap.m_DepthTexture);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_DepthMap.m_DepthTexture);
glTexImage3D(
GL_TEXTURE_2D_ARRAY,
0,
GL_DEPTH_COMPONENT32F,
Renderer::m_ShadowMapResolution,
Renderer::m_ShadowMapResolution,
3,
0,
GL_DEPTH_COMPONENT,
GL_FLOAT,
nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glBindFramebuffer(GL_FRAMEBUFFER, m_DepthMap.m_FrameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_DepthMap.m_DepthTexture, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
const int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!";
throw 0;
}
Sources that tell me that this should be correct:
https://people.inf.elte.hu/plisaai/pdf/OpenGL%20Insights.pdf , page 264
https://johanmedestrom.wordpress.com/2016/03/18/opengl-cascaded-shadow-maps/
What am I doing wrong here? Why is the framebuffer incomplete?
The tutorial you cited has led you astray.
A 2D texture is not the same thing as a 2D array texture. You can either attach a specific array layer (of a specific mipmap level) to a framebuffer, or attach all of the array images in a mipmap. In neither of these cases can you call glFramebufferTexture2D to do this for an array texture (which is why you should be checking for OpenGL errors in one way or another, as this function should have errored out).
In any case, if you want to attach a specific array layer to the framebuffer, then you want to use glFramebufferTextureLayer. If you want to attach it as a layered attachment (because you're doing layered rendering), then you'd use glFramebufferTexture.

Texture getting pixelated instead of blurry

I have created a simple OpenGL application.
When zooming into a textured quad, the texture becomes pixelated instead of blurry. I would guess that is due to missing mipmaps?
I create my texture like this:
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_2D, mTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
And I update it from a PBO like this:
glBindTexture(GL_TEXTURE_2D, mTexture);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mPboIds[mPboIndex]);
glTexSubImage2D(
GL_TEXTURE_2D,
0, 0, 0,
frame->GetWidth(),
frame->GetHeight(),
GL_RGB,
GL_UNSIGNED_BYTE,
0);
I thought that GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER would tell OpenGL to generate the mipmaps. Ain't that the case?
How can I enable mipmap generation for my texture?
The magnification filter is used when you increase the zoom on a texture, and can have two values:
GL_NEAREST - Returns the value of the texture element that is nearest (in Manhattan distance) to the specified texture coordinates.
GL_LINEAR - Returns the weighted average of the texture elements that are closest to the specified texture coordinates. These can include items wrapped or repeated from other parts of a texture, depending on the values of GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T, and on the exact mapping.
In your case, you are use the wrong magnification filter. It must be GL_LINEAR to minimize the pixelated effect.
MipMaps, for the other hand is used when you want to decrease the zoom and want to get a smooth transition when the texture start to become too far away from the viewer. To get more information about MipMaps, you can look at glTexParameter manual pages, at section GL_TEXTURE_MIN_FILTER, and how to generate in glGenerateMipmap manual page.

Framebuffer Incomplete Attachment for Texture with Internal Format?

So i am trying to create a FrameBuffer where i render to a texture, but i can't seem to get it to work with the format i need. That is GL_RGB32F. It works for GL_RGB16F and GL_RGBA32F so i don't understand why GL_RGB32F is giving me GL_FRAMEBUFFER_INCOMEPLETE_ATTACHMENT from glCheckFramebufferStatus. I get no errors from the calls creating the texture either. Is there a special requirement to use that internal format? Can i see if i have support for it?
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1, &positionTexture);
glBindTexture(GL_TEXTURE_2D, positionTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionTexture, 0);
GL_RGB16F andGL_RGB32F are not required color buffer formats in the GL, while GL_RGBA32F is. You can have a look in Table 8.12 of the OpenGL 4.5 core profile specification (pages 198-200, assuming june 2017 revision of said document). It tells you that all these mentioned formats are color renderable but only GL_RGBA32F out of that set is a required render format. Implementations might support the other ones optionally, but you can't rely on that.

Why does unbinding a texture from a FBO clear the texture?

I'm having some trouble with a render-to-texture operation. I create a FBO, attach a texture, render to it, and everything's fine, but when I try to change the attached texture, either to 0 or to a new handle, by calling glFramebufferTexture2DEXT again, the texture I had attached becomes blank (all pixel values reset to (0, 0, 0, 0)).
The documentation doesn't say that this is supposed to happen, and it's a bit troublesome for me because I need to retain the information in this texture. Does anyone know why this is happening, and how to prevent or work around it?
Just ran into a nearly the same situation. The end solution was when we were creating the texture we had to make sure to set up the TexParameters.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
According to https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexParameter.xhtml, GL_TEXTURE_MIN_FILTER defaults to GL_NEAREST_MIPMAP_LINEAR but we weren't mipmapping. I am not sure why that would cause the texture to appear to be erased, but that is what we did to fix it.

OpenGL GL_BGR not working for texture internal format

From what I've read in the OpenGL documentation, OpenGL prefers BGR format over RGB. However, when setting the texture's internal format as BGR, the texture is all white when rendering. When the internal format is set to RGB or RGBA it displays properly. The source format is BGR to begin with (loaded directly from DIB).
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, nClamp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, nClamp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nMagFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nMinFilter);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
The following line is the problem... setting the internal format to GL_BGR makes everything all white... changing to GL_RGB makes it render correctly
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGR, pTex->nWidth, pTex->nHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, pTex->pBuffer);
From the glTexImage2D man page, GL_BGR is not a internalFormat, just a format.
You should check for GL errors since that would've catched this error. What you read about "OpenGL prefering BGR" is false.