Texture Mapping

Started by
8 comments, last by Myopic Rhino 18 years ago
Hello, I'm a new OpenGL programmer as well as a new member to this messageboard, and let me stress I'm a VERY new OpenGL programmer :) I'm just going through NeHe's (http://nehe.gamedev.net, which is an *excellent* starting point for OpenGL) tutorials, and I've decided to test my knowledge by creating a scene with a random starfield, with some of the stars being "large" (a twinkle BMP texture of size 128x128). The problem I'm having is getting the texture to show up on the quad. This is the relevant code(or what I think is relevant):


//...

int LoadGLTextures() {
	int Status = FALSE;
	AUX_RGBImageRec *TextureImage[1];

	memset(TextureImage, 0, sizeof(void *)*1);

	//load streaks
	TextureImage[0] = LoadBMP("streaks.bmp");
	Status = TRUE;

	glGenTextures(1, &texture[0]);
		
	//GL_NICEST
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

	if (TextureImage[0]) {
		if (TextureImage[0]->data) {
			free(TextureImage[0]->data);
		}
		free(TextureImage[0]);
	}

	return TRUE;
}

//...

int InitGL(GLvoid) {

	if (!LoadGLTextures()) { return FALSE; }

	glEnable(GL_TEXTURE_2D);

	glShadeModel(GL_SMOOTH);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	
	glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient); //Add Ambient Light
	glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse); //Add Diffuse Light
	glLightfv(GL_LIGHT1, GL_POSITION, lightPosition); //Set Position

	//glEnable(GL_LIGHT1);

	return TRUE;
}

int DrawGLScene(GLvoid) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glBegin(GL_QUADS);
		glBindTexture(GL_TEXTURE_2D, texture[0]);
				
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -5.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -5.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -5.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -5.0f);
	glEnd();

	glLoadIdentity();

	return TRUE;
}


I'm pretty sure the problem lays in here somewhere because I previously had the starfield drawing fine. The resulting code is from me pulling the starfield out to isolate the problem. Any suggestions are greatly appreciated. Thanks in advance, Visser
Advertisement
Sorry I forgot to describe what the display was.. it's all black. Nothing displays but a completely black window. I tried replacing "streaks.bmp" with a couple textures and they didn't work.
Ensure you have lighting disabled. If you have it enabled, but with no lights enabled (or black lights), you'll end up with black geometry. Also, try setting your clear colour to blue or something, just to make sure you're drawing something in the view of the camera.
I double-checked that I don't have any duplicate glEnable calls for lights anywhere, and I changed the clear-colour to blue. Now the screen is plain blue, showing no geometry in the field of view. I don't have any translation calls anywhere else in the code so I don't know why that polygon is not being drawn. I must be missing something really simple here. CCW vertex order means the "front" of the polygon is facing the camera, am I right?

I changed the way I was drawing the polygon to make sure I was drawing it in the right spot. This is my new DrawGLScene code:

int DrawGLScene(GLvoid) {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glBegin(GL_QUADS);		glBindTexture(GL_TEXTURE_2D, texture[0]);		glTranslatef(0.0f, 0.0f, -5.0f);						glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);	glEnd();	glLoadIdentity();	return TRUE;}


I also tried ditching the texture and drawing like this:

int DrawGLScene(GLvoid) {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glBegin(GL_QUADS);		/*glBindTexture(GL_TEXTURE_2D, texture[0]);		glTranslatef(0.0f, 0.0f, -5.0f);						glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);*/		glTranslatef(0.0f, 0.0f, -5.0f);		glColor4ub(255, 0, 0, 255);		glVertex3f(-1.0f, 1.0f, 0.0f);		glVertex3f(-1.0f, -1.0f, 0.0f);		glVertex3f(1.0f, -1.0f, 0.0f);		glVertex3f(1.0f, 1.0f, 0.0f);	glEnd();	glLoadIdentity();	return TRUE;}


D:
More than likely I'm asking a silly question -- but I've done it before myself :P -- are you swapping the buffers after drawing? I don't see it there, but likely you already have it outside the given functions. But dangit, I just have to ask. :)
haha I understand.. if it weren't for the goold 'ol NeHe template I wouldn't be :) but in this case, I am..

I figured out what the problem was, and it's much simpler than swapping buffers..


Don't put your glTranslatef and glBindTexture calls inside the glBegin() and glEnd() structs....

[bawling]

Thanks for help guys.. I'll be back hopefully with either more challenging problems, or solutions to some of yours :)

- Visser

Quote:Original post by Visser
Don't put your glTranslatef and glBindTexture calls inside the glBegin() and glEnd() structs....
Even better solution: don't use glBegin()/glEnd().
Quote:Original post by Myopic Rhino
Even better solution: Don't use glBegin()/glEnd().


Oh? Speaking as a beginning OpenGL programmer, can I hijack the thread and ask why?

Thanks
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
I think what rhino's referring to is display lists or other grouped methods (vertex arrays/buffers, etc) which are essentially arrays of vertices that are drawn with a single call to glDrawElements. These are much faster (both development- and processing speed- wise) than glBegin()/glEnd.

Nehe Lesson 12 (Display Lists)

Hope that helped,
Aviosity
VBOs moreso than display lists, but yeah. Not only is their mechanism much more efficient than begin/end, but using them - especially from the beginning - will help you to think about how your data is managed in the "right" way.

This topic is closed to new replies.

Advertisement