Toggle Wrap Mode for 2 Cubes - OpenGL - c++

I have rendered successfully 2 cubes with textures. The first cube is rendered with wrap mode GL_REPEAT. For the second cube I want to be able to toggle the wrap mode with a button (for example, when I press 2).
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cube1tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glDrawArrays(GL_TRIANGLES, 0, 36); // Render the first cube
if (mirrored) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
glDrawArrays(GL_TRIANGLES, 0, 36); // Render the second cube
I used this code after I draw the first cube, but when I press 2 it toggles the wrap mode for the first cube too. How can I avoid that?

Set the wrap mode for the first cube, before drawing it, as well.

Related

OpenGL texture gets worse when moving camera away from object

The texture gets generally worse more I move the camera away from the object. I need to be really close to the object for the texture to be fine. Does anyone know what would cause it and how to fix it?
this is how I load the texture
stbi_set_flip_vertically_on_load(1);
m_Local_buffer = stbi_load(path.c_str(), &m_width, &m_height, &m_bpp, 4);
glGenTextures(1, &m_rendererID);
glBindTexture(GL_TEXTURE_2D, m_rendererID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_Local_buffer);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
image of the bad texture
Edit: updated the code.
For mipmapping you have to use one of the mipmap minifying filters. e.g.: GL_LINEAR_MIPMAP_LINEAR (see glTexParameter):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
Problem was that the faces in the model were too close to each other which caused Z-fighting and that caused flickering. It was not caused by texture or my or by OpenGL code like I originally assumed. When I changed the model that had not this issue it worked correctly without any fliggering.

OpenGL Binding Points

What are the advantages of using binding points in OpenGL? Why the following code:
glBindTexture(GL_TEXTURE_2D_ARRAY, textureName);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Is not something like?
glTexParameteri(textureName, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

Error when attaching depth/stencil buffer to the framebuffer

I create a depth/stencil buffer by issuing this series of command to OpenGL:
glBindTexture(GL_TEXTURE_2D, 0)
glGenTextures(1, &TextureId)
glBindTexture(GL_TEXTURE_2D, TextureId)
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_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0)
Then I try and attach it to the framebuffer with
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, TextureId, 0)
But a call to glCheckFramebufferStatusEXT returns GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT. If I don’t attach the depth-stencil buffer, this very test goes fine (but obviously the display is screwed).
Do you guys have any clue?
EDIT:
Changed: I have simplified the texture creation to a basic texture’s format.
Okay, the problem disappeared when I removed all the trailing EXT…
The texture you're attaching to GL_DEPTH_STENCIL_ATTACHMENT has neither depth nor stencil information in it. If you want to attach a texture to GL_DEPTH_STENCIL_ATTACHMENT, you make sure it's image format has depth and stencil data.
Now OpenGL versions does not need EXT extension~~~

Bind Texture in Repeat Mode

I am trying to store an octree in a 3D texture in OpenGL for use on the GPU using Cg, from a chapter in GPU Gems 2 found here http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter37.html. However the results I am getting are incorrect. I think this is because of how I create the octree.
In the appendix of that chapter, it says "If we bind the indirection pool texture (octree texture) in repeat mode (GL_REPEAT)...".
Does this simply mean set the the filters and wrapping to repeat, or do I need to do something else? This is my code so far
glGenTextures(1, &octree_texture);
glBindTexture(GL_TEXTURE_3D, octree_texture);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, WIDTH, HEIGHT, DEPTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, octreeData);
Thanks for the help :)
The filters can't be repeat, that will generate a GL error, only the wrap mode can be GL_REPEAT, and that's probably what the book means.

OpenGL DevIL c++ source code

After spending hours searching for a simple way to to add an image to an openGL application written in C++, i failed, miserably. It is also apparent that many others have too.
I am fairly new to OpenGL, I have written my own win32 app with OpenGL using DevIL and you guessed it, i failed.
The only way i could get DevIL to work was using Glut, which i dont want to use, or SDL or glaux.h
What i am looking for is code or a tutorial for a simple image loader in C++ with OpenGL and DevIL, Any help would be greatly appreciated.
If you're asking OpenGL to filter using mapmaps make sure to give it mipmaps to filter with.
Also, I'm pretty sure glTexParameteri() and glTexEnvf() only apply to the currently bound texture.
Try replacing your InitTextures() with this:
Tex1 = ilutGLLoadImage("tex1.bmp");
glBindTexture( GL_TEXTURE_2D, Tex1 );
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Tex2 = ilutGLLoadImage("tex2.bmp");
glBindTexture( GL_TEXTURE_2D, Tex2 );
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);