Binding multiple textures OpenGL to different quads - c++

I've managed to get my game to read in a PNG file, and successfully texture my objects. To be honest, I can't 100% nail down how it's actually working - and now I'd like to extend it to loading several textures, and using the one I specify.
Here's my PNG loading function:
//Loads PNG to texture
GLuint loadPNG(string name) {
nv::Image img;
GLuint myTextureID;
if (img.loadImageFromFile(name.c_str())) {
glGenTextures(1, &myTextureID);
glBindTexture(GL_TEXTURE_2D, myTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, img.getInternalFormat(), img.getWidth(), img.getHeight(), 0, img.getFormat(), img.getType(), img.getLevel(0));
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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
}
else {
MessageBox(NULL, L"Failed to load texture", L"Sorry!", MB_OK | MB_ICONINFORMATION);
}
return myTextureID;
}
In my main function, I define the texture like this:
//Load in player texture
testTexture = loadPNG("test.png");
where testTexture is a global variable, of type GLuint. And drawing my rectangles in my main draw loop is done this way:
//Used to draw rectangles
void drawRect(gameObject &p) {
glEnable(GL_TEXTURE_2D);
//Sets PNG transparent background
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBindTexture(GL_TEXTURE_2D, myTexture);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(p.x, p.y);
glTexCoord2f(1.0, 0.0); glVertex2f(p.x + p.width, p.y);
glTexCoord2f(1.0, 1.0); glVertex2f(p.x + p.width, p.y + p.height);
glTexCoord2f(0.0, 1.0); glVertex2f(p.x, p.y + p.height);
glEnd();
glDisable(GL_TEXTURE_2D);
}
This works fine, texturing all my objects with the defined texture. However, I'd like to be able to define more textures, and use those. I tried moving:
glBindTexture(GL_TEXTURE_2D, myTextureID);
from the loadPNG function, into my drawRect, as:
glBindTexture(GL_TEXTURE_2D, testTexture);
However this doesn't apply any texture whatsoever. If anyone has any ideas, I'd really appreciate the help. Thanks!

You have to bind the texture in order to initialize it with glTexImage2D. Don't remove the call to glBindTexture from loadPNG. If you want to render with a different texture, simply bind the texture before rendering the quads.

Related

Can't bind a texture to a GL_QUADS

I try to do an animation in OpenGL, my vertices and animations are working, but I would like to put a background image to it, with a file such as a bmp, or whatever.
So after a few reads I try the quads technique, which is simply to show a quad and bind a texture to it.
I use the STB_Image library, and it seems that I correctly point to my file (if I make mistakes on the filename I definitely got a much faster response from my program).
And I implemented a print to see if it catches the right file and it does!
My code look like this, and my result is a white square that appears in the correct coordinates, but no texture appears, the file is correctly loaded (with correct sizes printed), it just doesn't bind or appear on the square...
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
int width, height, nrChannels;
unsigned char* data = stbi_load("test.jpg", &width, &height, &nrChannels, 0);
if (data == NULL) {
printf("Error in loading the image\n");
exit(1);
}
printf("Loaded image with a width of %dpx, a height of %dpx and %d channels\n", width, height, nrChannels);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glVertex2f(2.0, 1.0);
glTexCoord2f(2.0, 1.0);
glVertex2f(8.0, 1.0);
glTexCoord2f(8.0, 1.0);
glVertex2f(8.0, 7.0);
glTexCoord2f(8.0, 7.0);
glVertex2f(2.0, 7.0);
glTexCoord2f(2.0, 7.0);
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
Any ideas?
glTexImage2D specify the two-dimensional texture image for the texture object. You have to do that the quad is drawn.
Furthermore 2 dimensional texturing has to be enabled by glEnable(GL_TEXTURE_2D).
The texture coordinates have to be in range [0.0, 1.0] (See How do opengl texture coordinates work?).
glTexCoord2f has to be set before glVertex2f, because the current color, normal and texture coordinates are associated with the vertex when glVertex is called.
Since you don't generate mipmaps (glGenerateMipmap), the texture minifying function (GL_TEXTURE_MIN_FILTER) has to be set to GL_LINEAR or GL_NEAREST. Else the texture would be mipmap incomplete.
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
glBindTexture(GL_TEXTURE_2D, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0);
glVertex2f(2.0, 1.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(8.0, 1.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(8.0, 7.0);
glTexCoord2f(0.0, 0.0);
glVertex2f(2.0, 7.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f(1.0, 1.0, 1.0);

Open GL texture flickering with GL_TEXTURE_MIN_FILTER at GL_NEAREST

I have a simple OpenGL program built with GLUT that draws a few buildings, have a keyboard controlled moving camera, and I am trying to texture the ground. The animation is smooth.
The problem I have is with texturing mapping the ground. Two separate issues with two separate approaches
1) If I use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); then I have no
flickering when I zoom out to far away. But the issue is when I zoom out from far, it appears as a single solid color.
From far:
From close:
2) If I use, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); then I have a lot of flickering
when I zoom out to far away as I move, almost look like a TV noise when you are out of tune.
But stationary, it looks more natural.
From far:
From close:
Questions: What would be a simple way from my existing code to get the best of both worlds? Looking natural from far, and no flickering of the texture as I move. Thank you.
Here is the relevant code:
GLuint grassTextureId;
GLfloat GROUND_PLANE_WIDTH = 1000.0f;
void Display()
{
glLoadIdentity();
camera.Update();
...
FlatGroundPlane_Draw(void);
...
glutSwapBuffers();
}
void FlatGroundPlane_Draw(void)
{
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, grassTextureId); // call glBindTexture before glBegin
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glTexCoord2d(0, 0);
GLdouble textCoord = GROUND_PLANE_WIDTH;
glVertex3f( -GROUND_PLANE_WIDTH, 0, -GROUND_PLANE_WIDTH);
// go beyond 1 for texture coordinate so it repeats and tiles
glTexCoord2d(0, textCoord);
glVertex3f( -GROUND_PLANE_WIDTH, 0, GROUND_PLANE_WIDTH);
glTexCoord2d(textCoord, textCoord);
glVertex3f( GROUND_PLANE_WIDTH, 0, GROUND_PLANE_WIDTH);
glTexCoord2d(textCoord, 0);
glVertex3f( GROUND_PLANE_WIDTH, 0, -GROUND_PLANE_WIDTH);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void Initialise()
{
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
modelParser = new ModelParser();
// grass texture is 1024x1024
// from http://seamless-pixels.blogspot.ca/2012_10_01_archive.html
grassTextureId = modelParser->LoadTiledTextureFromFile("./Grass/Grass_1024.ppm");
}
void ModelParser::UploadTiledTexture(unsigned int &iTexture, const RGBImage &img)
{
glGenTextures(1, &iTexture); // create the texture
glBindTexture(GL_TEXTURE_2D, iTexture);
// Issue 1: no flickering, but appear solid color from far away
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Issue 2: flickering noise during movement but appear realistic from far away
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// the texture would wrap over at the edges (repeat)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.Width(), img.Height(), GL_RGB, GL_UNSIGNED_BYTE, img.Data());
}
int main(int argc, char** argv)
{
...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
...
glutDisplayFunc(&GLUT::Display);
...
glutMainLoop();
}

Shadow Mapping is faint

I am trying to do OpenGL GLSL based shadow mapping. The trouble is that after I have finished rendering the shadow map, I am rendering the map to the screen to test whether the rendering works correctly or not i.e I am simply using the newly generated texture as a texture which I am mapping on to the screen. The expected result is that I will see the new texture. But instead, what i am seeing is a white area with the texture drawn but extremely faint. That is, if I tilt the screen at a certain angle only then I can see the faint outlines of the shadow map.
Can anyone tell me if I am doing anything wrong?
Here is relevant parts of my code :
void Init_FBO()
{
//glActiveTexture(GL_TEXTURE3);
GLfloat border[] = {1.0f, 0.0f, 0.0f, 0.0f};
glGenTextures(1, &depthTex);
glBindTexture(GL_TEXTURE_2D, depthTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,900,900, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
glBindTexture(GL_TEXTURE_2D,0);
glGenFramebuffers(1, &shadowFBO);
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0); // go back to the default framebuffer
// check FBO status
GLenum FBOstatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
if(FBOstatus != GL_FRAMEBUFFER_COMPLETE)
{
printf("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n");
}
else
{
printf("Frame Buffer Done Succesfully\n");
}
}
void generateShadowTex()
{
//Calculate final ligting properties
glm::vec4 a_f=light_ambient*mat_ambient;
glm::vec4 d_f=light_diffuse*mat_diffuse;
glm::vec4 s_f=light_specular*mat_specular;
int counter=0;
glEnable(GL_DEPTH_TEST); // need depth test to correctly draw 3D objects
glClearColor(0,0,0,1);
//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glCullFace(GL_FRONT);
if(wframe)
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glUseProgram(programObject);
//Draw the stuff using Light Position as camera
//glutSwapBuffers();
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D,depthTex);
glUseProgram(0);
}
void generateScene()
{
//Calculate final ligting properties
glm::vec4 a_f=light_ambient*mat_ambient;
glm::vec4 d_f=light_diffuse*mat_diffuse;
glm::vec4 s_f=light_specular*mat_specular;
int counter=0;
glEnable(GL_DEPTH_TEST); // need depth test to correctly draw 3D objects
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//Draw the stuff using camera as camera position
}
glutSwapBuffers();
glUseProgram(0);
}
void display()
{
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);
generateShadowTex();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
generateScene();
}
your depth texture is looking normally, depth covers range from near to far clip planes, you can set some reasonable clipping planes using glFrustumf(...)

Multiple Textures OpenGL GLUT C++

Okay still having a few problems with it, this is what I have so far:
Bitmap Display::m_HeightMap;
unsigned int Display:: textures;
My initialise method:
glEnable(GL_TEXTURE_2D);
Bitmap image[2];
GLuint *textures = new GLuint[2];
glGenTextures(1, textures);
glGenTextures(2, textures);
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myOtherTexture.bmp");
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
The above line gives an error: left of .data must have class/struct/ union
glDisable(GL_TEXTURE_2D);
Draw Method:
void Draw()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glBindTexture(GL_TEXTURE_2D, textures[0]);
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
//glColor3d(0.5,0.40,0.05);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0.3, 2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-4.5, 0.3, 2.5);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2.5);//top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(4.5, 0.3, 2);//top left
glEnd();
glDisable(GL_TEXTURE_2D);
}
Only problem here is that texture is undefined.
One last thing hopefully!
void loadTexture(GLuint texture, const char* filename)
{
Bitmap image;
Bitmap image[2];
image[0].loadBMP("myTexture.bmp"); <=== error
image[1].loadBMP("myTexture2.bmp"); <=== error
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE,
image.data);
}
When I try and load multiple bitmaps I get 7 errors,
error C2040: image:'Bitmap [2]' differs in level of indirection of 'Bitmap'
error C2088: '[' illegal for class (twice)
error C2228: left of .BMP must have class/struct/union (twice)
no operator "[]" matches these operands (twice)
So. glGenTextures takes two parameters. An int, and a GLuint*. The int tells GL how many textures to generate, and the GLuint* is an array of GLuints (where to generate the textures). The reason you do the following...
GLuint m_TextureID
glGenTextures(1, &m_TextureID)
is because you only have one texture. If you have more than one, you would do this:
// Substitute 'n' for some const number
GLuint *textures = new GLuint[n];
glGenTextures(n, textures);
This way, you are telling GL I want to generate n textures, and here's an array with allocated space for at least that many textures.
Say you want to use both of them in your draw loop, you would implement that like this:
void draw()
{
glBindTexture(GL_TEXTURE_2D, textures[0]); // Tell GL to use the first texture
// Any drawing here will use first texture
glBindTexture(GL_TEXTURE_2D, textures[1]); // Tell GL to use the second textures
// Any drawing here will use second texture
glBindTexture(GL_TEXTURE_2D, 0); // Set the GL texture to NULL, standard cleanup
}
Make sure to delete textures; at the end of your program to properly clean up after allocating that space.
There are also ways to not have to bind separate textures. You can use what's called a "texture atlas". Basically, this is one bitmap that contains multiple sub-images. So you just generate and bind one bitmap, and use separate parts of it.
To deal with multiple bitmaps, do this:
Bitmap image[2];
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myOtherTexture.bmp");
Then follow the process to generate one bitmap for both bitmaps.
Everything below this line is responding to your updated question.
This should pretty much do what you're trying to do.
// Global variable
GLuint textures[2];
// init function
void init()
{
textures = new GLuint[2];
glGenTextures(2, textures);
loadTexture(textures[0], "texture1.bmp");
loadTexture(textures[1], "texture2.bmp");
}
void loadTexture(GLuint texture, const char* filename)
{
Bitmap image;
image.loadBMP(filename);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}
// draw function
void draw()
{
glBegin(GL_QUADS); //TOP
glBindTexture(GL_TEXTURE_2D, textures[0]);
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f); glVertex3f(-4.5, 0.3, 2); //bottom left
glTexCoord2f(1.0f,0.0f); glVertex3f(-4.5, 0.3, 2.5); //bottom right
glTexCoord2f(1.0f,1.0f); glVertex3f(4.5, 0.3, 2.5); //top right
glTexCoord2f(0.0f,1.0f); glVertex3f(4.5, 0.3, 2); //top left
glEnd();
}
// cleanup function
void cleanup()
{
delete textures;
}
Some of the issues you are pointing out aren't exactly OpenGL related, they are more C/C++ related. I know this isn't the answer you're looking for, but it would probably help you out a lot to learn about C functions, pointers, arrays, etc, and spend a solid month working closely with functions/pointers/arrays before moving on to something like OpenGL, which requires a pretty moderate understanding of C.
const GLsizei n = (number of textures here);
GLuint textureIDs = new GLuint[ n ];
glGenTextures( n, textureIDs );
I assume you mean you want to generate multiple textures and store the id's in an array? You can do that like this:
GLsizei num_textures = 5;
GLuint textures[num_textures];
glGenTextures(num_textures, textures);
Once you've done that, just loop through your textures and for each one, bind it, set parameters, load in image data, etc.
You may want to create your texture array on the heap so you can access the texture IDs later:
GLuint* textures = new GLuint[num_textures];
Just make sure to delete the array later:
delete textures;

OpenGL Nvidia Driver 259.12 texture not working

My OpenGL application which was working fine on ATI card stopped working when I put in an NVIDIA Quadro card. Texture simply don't work at all! I've reduced my program to a single display function which doesn't work:
void glutDispCallback()
{
//ALLOCATE TEXTURE
unsigned char * noise = new unsigned char [32 * 32 * 3];
memset(noise, 255, 32*32*3);
glEnable(GL_TEXTURE_2D);
GLuint textureID;
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, noise);
delete [] noise;
//DRAW
glDrawBuffer(GL_BACK);
glViewport(0, 0, 1024, 1024);
setOrthographicProjection();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, textureID);
glColor4f(0,0,1,0);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-0.4,-0.4);
glTexCoord2f(0, 1);
glVertex2f(-0.4, 0.4);
glTexCoord2f(1, 1);
glVertex2f(0.4, 0.4);
glTexCoord2f(1,0);
glVertex2f(0.4,-0.4);
glEnd();
glutSwapBuffers();
//CLEANUP
GL_ERROR();
glDeleteTextures(1, &textureID);
}
The result is a blue quad (or whatever is specified by glColor4f()), and not a white quad which is what the texture is. I have followed the FAQ on OpenGL site. I have disabled blending in case texture was being blended out. I have disabled lighting. I have looked through glGetError() - no errors. I've also set glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); and GL_DECAL. Same result. I've also tried different polygon winding - CW and CCW.
Anyone else encounter this?
Can you try using GL_REPLACE in glTexEnvi? It could be a bug in the NV driver.
Your code is correct and does what it should.
memset(noise, 255, 32*32*3); makes the texture white, but you call glColor4f(0,0,1,0); so the final color will be (1,1,1)*(0,0,1) = (0,0,1) = blue.
What is the behavior you would like to have ?
I found the error. Somewhere else in my code I had initialized a GL_TEXTURE_3D object and had not called glDisable(GL_TEXTURE_3D);
Even though I had called glBindTexture(GL_TEXTURE_2D, textureID); it should have bound a 2D texture as the current texture and used that - as this code always worked on ATI cards. Well apparently the nVidia driver wasn't doing that - it was using that 3D texture for some reason. So adding glDisable(GL_TEXTURE_3D); fixed the problem and everything works as expected.
Thanks all who tried to help.