Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL texturing problem

Started by Hacktank Aug 11, 2010 at 3:29 PM 5 replies 900+ views
Original Post
Hacktank
Hacktank
Hello, i am using OpenGL in a win32 project and am trying to get a textured quad to render. But when i enable GL_TEXTURE_2D the quad simply does not render, when it is disabled it just renders an untextured blank square. I have confirmed that my image is indeed loaded correctly. I assume I have made a stupid mistake in my opengl initialization.

Code:

Main OpenGL window init:
	void Engine::CreateViewport() {		glMatrixMode(GL_PROJECTION);		glLoadIdentity();		//glViewport(0,0,screenwidth,screenheight);		glMatrixMode(GL_PROJECTION);		glLoadIdentity();		glOrtho(0.0f,(float)screenwidth,(float)screenheight,0.0f,-99999999.0f,99999999.0f);		//gluPerspective(45.0f,(GLfloat)screenwidth/(GLfloat)screenheight,1.0f,1000.0f);		//glMatrixMode(GL_PROJECTION);		glMatrixMode(GL_MODELVIEW);		glLoadIdentity();		ShowCursor(true);		RECT r;		GetWindowRect(hwnd,&r);		ClipCursor(&r);		rendering = false;	}	void Engine::InitGL() { // Called after CreateViewport()		glLoadIdentity();		glEnable(GL_TEXTURE_2D);		glShadeModel(GL_SMOOTH);		glClearColor(0.2f,0.2f,0.2f,0.0f);		glClearDepth(0.0f);		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);		glDisable(GL_DEPTH_TEST);		glDepthFunc(GL_ALWAYS);		glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);		glDisable(GL_LIGHTING);		//glEnable(GL_LIGHT0);		//GLfloat pos[] = { 2.0f,4.0f,-3.0f,1.0f };		//glLightfv(GL_LIGHT0,GL_POSITION,pos);		glEnable(GL_LINE_SMOOTH);		glEnable(GL_POLYGON_SMOOTH);		glEnable(GL_BLEND);		glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);		glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);	}


Texture OpenGL prepping (after loading):
	unsigned int Texture::GLPrepare() {		if(!loaded) return 0;		if(gltexid >= 0) glDeleteTextures(1,&gltexid);		glGenTextures(1,&gltexid);		glBindTexture(GL_TEXTURE_2D,gltexid);		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);		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,bpp/8,width,height,0,bpp == 24 ? GL_RGB:GL_RGBA,GL_UNSIGNED_BYTE,imagedata);		if(gluBuild2DMipmaps(GL_TEXTURE_2D,bpp/8,width,height,bpp == 24 ? GL_RGB:GL_RGBA,GL_UNSIGNED_BYTE,imagedata)) {			glDeleteTextures(1,&gltexid);			return 0;		}		return gltexid;	}


Attempt at rendering:
	virtual void Draw() {		glPushMatrix();		glEnable(GL_TEXTURE_2D);		glBindTexture(GL_TEXTURE_2D,texid);		glTranslatef(160.0f,80.0f,0.0f);		glBegin(GL_QUADS);			glTexCoord2f(0,0);			glVertex2f(0,0);			glTexCoord2f(0,1);			glVertex2f(0,60);			glTexCoord2f(1,1);			glVertex2f(60,60);			glTexCoord2f(1,0);			glVertex2f(60,0);		glEnd();				glPopMatrix();	}


What am i doing wrong? This is driving me nuts, because when i was using SDL as a base with opengl almost the exact same code worked like a charm.
empirical2
empirical2
Texture size of not power of two maybe?
Hacktank
Hacktank
My texture is 128x128.
Hacktank
Hacktank
Ok, i changed it to be the same as the second format parameter but the problem remains.

GLenum format = bpp == 24 ? GL_RGB:GL_RGBA;if(gluBuild2DMipmaps(GL_TEXTURE_2D,format,width,height,format,GL_UNSIGNED_BYTE,imagedata)) {	glDeleteTextures(1,&gltexid);	return 0;}
szecs
szecs
Where does "imagedata" come from? Show more code.
How do you know the image loading was successful?
How do you know "gluBuild2DMipmaps" was successful?

Untextured black square? That means you set the drawing color to black?

The default texture environment mode (glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)) is "modulate", that means the colors of the texture will be multiplied with the current color, which is supposedly black with your setup. That means the output will be black too.

If that's not the problem, pleas try to explain it better (what happens if texturing is disabled?)
Hacktank
Hacktank
While typing a long reply the problem hit me; my test image was set as rgba but contained only black text. I added some color to it to see if i could pull a result, and it turns out that the loading function is messed up. I was just checking the imagedata for having a substantial amount of non-null data. I spent an hour or so reading information and fixing my loading function. I also implemented (badly) compressed tga loading. My noncompressed loading works perfect if there is no alpha channel. But when there is it loads alpha as light blue, blue as alpha, green as alpha, and as a result, black as alpha. I could use some help fixing it up a bit. The compressed loading works a bit, but it's results are just strange.

uncompressed:
no alpha (works)http://img525.imageshack.us/i/uncomnoa.png/
alpha (doesnt work) http://img101.imageshack.us/i/uncoma.png/

compressed:
no alpha [url]http://img245.imageshack.us/i/compnoa.png/]/url]
alpha http://img51.imageshack.us/i/compat.png/

Here is my loading function, i cant tell whats wrong with the compressed part, but i know im just not swapping the out-of-order colors correctly with non-compressed w/ alpha.

bool FIO_LoadTGA(std::string filename,unsigned char **imagedata,unsigned long &imagesize,unsigned short &width,unsigned short &height,unsigned char &bpp) {		std::fstream in;		in.open(filename.c_str(),std::ios::in | std::ios::binary);		if(!in) return false;		unsigned char header[17];		in.seekg(0,std::ios::beg);		in.read((char*)&header,sizeof(header));		if((header[2] != 2) && (header[2] != 10)) { in.close(); return false; }		width = header[13] * 256 + header[12];		height = header[15] * 256 + header[14];		bpp = header[16]/8;		if((width <= 0) || (height <= 0) || ((bpp!=3) & (bpp!=4))) { in.close(); return false; }		imagesize = width * height * bpp;		*imagedata = new unsigned char[imagesize];		if(header[2] == 2) {			in.read((char*)*imagedata,imagesize);			unsigned char temp = 0;			for(int i = 0; i < (int)imagesize; i += bpp) {				temp = (*imagedata)[i+1];				(*imagedata)[i+1] = (*imagedata)[i+2];				(*imagedata)[i+2] = temp;			}		} else {			unsigned long pixilcount = width * height;			unsigned long currentpixil = 0, currentbyte = 0;			unsigned char *colorbuffer = new unsigned char[bpp];			do {				unsigned char chunkheader = 0;				in.read((char*)&chunkheader,1);				if(chunkheader < 128) {					chunkheader += 1;					for(short i = 0; i < chunkheader; i++) {						in.read((char*)colorbuffer,bpp);						(*imagedata)[currentbyte] = colorbuffer[2];						(*imagedata)[currentbyte+1] = colorbuffer[1];						(*imagedata)[currentbyte+2] = colorbuffer[0];						if(bpp==4) (*imagedata)[currentbyte+3] = colorbuffer[3];						currentbyte += bpp;						currentpixil += 1;					}				} else {					chunkheader -= 127;					in.read((char*)colorbuffer,bpp);					for(short i = 0; i < chunkheader; i++) {						(*imagedata)[currentbyte] = colorbuffer[2];						(*imagedata)[currentbyte+1] = colorbuffer[1];						(*imagedata)[currentbyte+2] = colorbuffer[0];						if(bpp==4) (*imagedata)[currentbyte+3] = colorbuffer[3];						currentbyte += bpp;						currentpixil += 1;					}			}			} while(currentpixil < pixilcount);		}		in.close();		return true;	}



EDIT: After getting some sleep i realized that my header was one too short, causing all my data to be off by one. It works perfectly now. Thanks for all the help guys :).

[Edited by - Hacktank on August 12, 2010 5:04:37 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.