What is the scope of glTexParameter's in OpenGL? - opengl

Does glTexParamter act on all textures globally or only the texture that is currently bound.
For example, if I call this at the texture load:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
And this on another texture load:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
When I bind the first texture will it just use the last value I set (GL_CLAMP) or will it use the values originally set when the texture was bound?

From the OpenGL FAQ:
21.070 How do texture objects work?
Texture objects store texture maps and their associated texture parameter state. They allow switching between textures with a single call to glBindTexture().
(...)
The following functions affect and store state in texture objects: glTexImage*(), glTexSubImage*(), glCopyTexImage*(), glCopyTexSubImage*(), glTexParameter*(), and glPrioritizeTextures(). Since the GLU routines for building mipmap pyramids ultimately call glTexImage*(), they also affect texture object state.Noticeably absent from this list are glTexEnv*() and glTexGen*(); they do not store state in texture objects.
Ergo, glTexParameter* affects only the bound texture.

Related

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.

scale texture opengl 2

I have a rectangle in opengl 2 and I'm using a texture for it.
It Works, but the texture is repeated over the rectangle, and what I want is to adapt to the size of the rectangle.
I have read in this tutorial about the different parameters you can set to achieve this:
https://open.gl/textures
In my App I am using this:
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
According to the tutorial this should adapt the size of the texture to fill the rectangle, isn' it?
Any clues about why isn't working that way?
Actually stretching a texture over a rectangle works with the texture coordinates. But if you want to repeat it you have to set:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

OpenGL texture mapping glActivetexture vs glEnable

I was looking at OpenGL examples and before creating object these two examples follow different ways. What are the differences between these two examples?
1)
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,myImg);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,myImg2);
2)
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
in first example we activate two unit and assign one texture to each unit.but sample 2 is different .it enables 2D texture mapping on current active layer and set filtering parameters.the key concept is texture unit in OpenGL.

OpenGL Skybox Issue

There are seems on the edges of the cube that I have constructed and I cant understand why. I have set the following parameters...
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(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);
The lines only appear sometimes, it depends on what angle the camera is sitting at. Any ideas why the lines are still there?
It would help if you could show us a screen shot. But the problem is probably the fact that you're not interpolating the textels along the edge of each face with their neighbors on the other faces.
So you should add a border to each texture by copying the edges of the neighboring textures, and change the filter mode to GL_CLAMP. Or you can use a cubemap texture instead.
maybe you need to add:
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
in the code you provided there is only S and T coord setup, you need the R as well.
Turns out this was actually a problem with SOIL which is the texture loading program that I was using. After I changed the texture loading method the lines dissapeared

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.