glTexParameteri problems I think...

Sorry for the lengthy code snippet but I have been stuck on this for about a day now, and the notes I have just don’t seem to be doing justice… I apologize for the school question hopefully its not an issue but I have no idea where else to go, professor won’t necessarily help me.

I know the reading of the image is good because if I use GL_QUADS and use my own vertex points and tex points it shows up. I believe something is going wrong when I try to call the glTexParameteri functions. Could someone look over and make sure they are correct, because I have no idea on how to even go about debugging them and tired of guessing…

Thank you

void textureShader::SetupTextureMap(void)
{
	if (tShape == Box)
		glTarget = GL_TEXTURE_CUBE_MAP;
	else
		glTarget = GL_TEXTURE_2D;

	// Note that we only initialize the OpenGL texture objects if we 
	// don't yet have an OpenGL texture object
	if (tObject <= 0)        
	{
		// Find the number of textures that have been specified
		// Multiple images are used for manually specifiying mipmaps
		// and for box maps
		int nImages = 0;
		while (tName[nImages][0] != '\0' && nImages < MAX_IMAGES)
			nImages++;

		// If there are none, then do nothing and return
		if (nImages < 1)
			return;

		// Generate the texture object and bind it to the texture target defined above
		glGenTextures(1, &tObject);
		glBindTexture(glTarget, tObject);
     
		// Now, loop through the images and send them to OpenGL
		for (int nImage = 0; nImage < nImages; nImage++)
		{
			BitmapFile texture;
			if (!texture.Read(tName[nImage]))
				complain("Couldn't read texture %s", tName);

			// Now we will multiply the image by tFilterColor and add tConstColor.
			// We do these with methods in the BitmapFile class.
			if (tFilterColor[0] < .99999 || tFilterColor[1] < .99999 || tFilterColor[2] < .99999 || tFilterColor[3] < .99999)
				texture.Filter(tFilterColor[0], tFilterColor[1], tFilterColor[2], tFilterColor[3]);
			if (tConstColor[0] > .00001 || tConstColor[1] > .00001 || tConstColor[2] > .00001 || tConstColor[3] > .00001)
				texture.Add(tConstColor[0], tConstColor[1], tConstColor[2], tConstColor[3]);
			
			// To Do
			//
			// Get the width and height from the texture file
			int w, h;
         w = texture.Width();
         h = texture.Height();
			// To Do
			//
			// Set the image data pointer to point do the data in the texture (Hint:
			// is there a method in the texture that 
			GLubyte *imageData;
         imageData = texture.ImageData();
			// To Do
			// 
			// Set the src and target formats for the texture data.  The srcFormat should 
			// match the format of the data in the texture file itself.  Hint: what order
			// are the colors stored in bitmap and targa files?  The targFormat is the 
			// format that we want the data stored inside OpenGL.  Note that these two 
			// may not be the same.
			//
			// We also need to check here for the number of bytes per pixel in the image.
			// For our examples, it will be either 3 or 4.  If it is 4, then there is an
			// alpha channel in the data, otherwise it is three.  Set the srcFormat and 
			// targFormat appropriately, or the data will not be read in correctly!
			GLuint srcFormat = GL_BGRA, targFormat = GL_RGBA;
         if(texture.BytesPerPixel() == 3)
            srcFormat = GL_BGR, targFormat = GL_RGB;

			// Rows in our image are stored contiguously (not aligned on a specific number
			// of bytes, so set the unpack alignment to 1 --> single byte aligned
			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

			// To Do
			//
			// If it is a box map (which doesn't support manual mipmap specification in RGL)
			// or if only one image is supplied turn on automatic mipmap generation.
         //gluBuild2DMipmaps(glTarget, 3, w, h, srcFormat, GL_UNSIGNED_BYTE, imageData);
         if (nImages == 1 || tShape == Box)
            glTexParameteri(glTarget, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);

			// To Do
			//
			// Send the data to OpenGL.  Be very careful when sending all the data.  For a first
			// shot you may assume that the texture target is GL_TEXTURE2D, but you will need to 
			// change this command when we enable box mapping!
          if (tShape != Box)
            glTexImage2D(GL_TEXTURE_2D, nImage, targFormat, (GLsizei)w, (GLsizei) h, (GLint)0, srcFormat, GL_UNSIGNED_BYTE, imageData); 
         else
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + nImage, 0, targFormat, (GLsizei)w, (GLsizei)h, (GLint)0, srcFormat, GL_UNSIGNED_BYTE, imageData);
 		}
	}	

	// To Do
	//
	// Now, bind the texture and set the texture parameters.  You need to set several here.
	// Set the parameters for how the image wraps and what kind of magnification and minification
	// filter you want to use, and also, set the anisotropy level.  Also set the blend mode and 
	// blend color.  Note, if you do not do this correctly, your textxure will not show up at all, 
	// particularly for mipmapping.  If you use a mipmapped filter without enabling mipmap generation 
	// above, you will get nothing!
	//glBindTexture(glTarget, tObject); 
   glTexParameteri(glTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//this->tMinFilter);
   glTexParameteri(glTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//this->tMagFilter);
   glTexParameteri(glTarget, GL_TEXTURE_WRAP_S, GL_REPEAT);//this->tHorzWrap);
   glTexParameteri(glTarget, GL_TEXTURE_WRAP_T, GL_REPEAT);//this->tVertWrap);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->tAnisotropy);
   glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glBlendFunc(this->tBlend, GL_ONE_MINUS_SRC_ALPHA);
   //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glBlendColor(this->tBlendColor[0], this->tBlendColor[1], this->tBlendColor[2], this->tBlendColor[3]);
  // glEnable(GL_TEXTURE_2D);
}

You try to upload multiple images? But you only have one texture object and only bound one object! Also: Your min/mag filter are linear but i guess you try to use mipmaps (not sure on what hardware GL_GENERATE_MIPMAP_SGIS works…). To set GL_TEXTURE_MAX_ANISOTROPY_EXT, you need to call glTexParameterf not glTexParameteri. Add OpenGL error checks with glGetError (at least the last one should show up as an invalid enum.

If that weren’t the bugs you’r looking for, tell us what that code should do!

Hi menzel thanks for the response.

The idea behind the code is just to apply a simple texture to an object. I’m still stuck on the first example which is a test case of getting the texture to actually appear on the object. Later I am supposed to implement box mapping and I believe cylindrical/spherical mapping.

I am starting to think that the previous code should be set up somewhat correctly, and as for glGetError it didn’t seem to do anything. Although whenever I try to add breakpoints to my code it seems to remove them once I start the build.

I attached the entire shader file to pastebin. If that helps already too late to turn this in but this is just something I want to solve now…
http://pastebin.com/ap12WNgy

Thanks again

Hi,
I haven’t used texture maps without VBO’s for a while but I cannot see a call to glTexCoordxx in you subroutine CalculateTexCoords ( I believe it need to be glMultiTexCoordxx for multiple textures).

Also you have commented out glEnable(GL_TEXTURE_2D). I know it is not needed for shaders but I am not sure for glBegin/glEnd.

you might look at this for multi-texturing
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=240851

You have to check the returned error code of glGetError:
http://www.opengl.org/sdk/docs/man/xhtml/glGetError.xml


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->tAnisotropy);
GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) {
cout << "glTexParameteri did not work" << endl;
}

Ok menzel I posted those errors and it seems to be breaking at the first parameter function… how could that be wrong? I tried with linear and what tMinFilter should be but it errors out. Do you have any idea what might be happening?

EDIT: After looking into it I was receiving error code 1280 which apparently is an invalid enum… hmmm


glBindTexture(glTarget, tObject); 
   glTexParameteri(glTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//this->tMinFilter);
   GLenum errorCode = glGetError();
   if (errorCode != GL_NO_ERROR) {
   cout << "GL_TEXTURE_MIN_FILTER did not work" << endl;
   }
   glTexParameteri(glTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//this->tMagFilter);
   errorCode = glGetError();
   if (errorCode != GL_NO_ERROR) {
   cout << "GL_TEXTURE_MAG_FILTER did not work" << endl;
   }
   glTexParameteri(glTarget, GL_TEXTURE_WRAP_S, GL_REPEAT);//this->tHorzWrap);
   errorCode = glGetError();
   if (errorCode != GL_NO_ERROR) {
   cout << "GL_TEXTURE_WRAP_S did not work" << endl;
   }
   glTexParameteri(glTarget, GL_TEXTURE_WRAP_T, GL_REPEAT);//this->tVertWrap);
   errorCode = glGetError();
   if (errorCode != GL_NO_ERROR) {
   cout << "GL_TEXTURE_WRAP_T did not work" << endl;
   }
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->tAnisotropy);
   errorCode = glGetError();
   if (errorCode != GL_NO_ERROR) {
   cout << "GL_TEXTURE_MAX_ANISOTROPY_EXT did not work" << endl;
   }
   glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   errorCode = glGetError();
   if (errorCode != GL_NO_ERROR) {
   cout << "GL_TEXTURE_ENV_MODE did not work" << endl;
   }
   glBlendFunc(this->tBlend, GL_ONE_MINUS_SRC_ALPHA);
   //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glBlendColor(this->tBlendColor[0], this->tBlendColor[1], this->tBlendColor[2], this->tBlendColor[3]);

Hi tonyo_au, I wasn’t even aware of that but I tried throwing in a glTexCoord2d(u,v) after calculating the coordinates but still didnt work. I think its my parameter functions breaking.

First, make sure the error was generated by the first glTexParameteri call. If another GL call prior to that generated an error, OpenGL will ‘save’ that and present it lates when you first call glGetError (makes debugging harder, sadly). So test for errors also before that call.

Second: glTexParameteri will give you an INVALID_ENUM (1280 is indeed an invalid enum) in case glTarget is not GL_TEXTURE_1D, GL_TEXTURE_2D or GL_TEXTURE_3D (see http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml).

Ok so it seems to be happening prior to all of this. Guess its happening during the begin shading or something. I’ll dig around some more later and hopefully figure this out.

Thanks a bunch menzel.