Jump to content
  • Advertisement
Sign in to follow this  
Chad Smith

OpenGL Textures

This topic is 4444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I am pretty new to OpenGL, but not Graphics Programming in general. Anyway, I have been making my way to OpenGL from SDL. I have been following some tutorials on OpenGL and I have been doing great I think. Anyway, I have gotten to the texture mapping tutorial. Well, the problem is my texture will not show up. I can not figure out what is wrong. It should display an error message if it can not load up the texture. It never does, so I am not sure. Here is my code. It uses GLut also.
#include <iostream>
//include glut
#include <gl/glut.h>
#include <il\il.h>
#include <il\ilu.h>	
#include <il\ilut.h>

//render function
void RenderScene();
//resize function
void Resize(int w, int h);
//init function
void Init();
//get normal keys function
void GetNormalKeys(unsigned char key, int x, int y);
//get the special keys now
void GetSpecialKeys(int key, int x, int y);
//load up the textures
int LoadGLTextures();

//Create enough storage space for one texture
GLuint texture[1];	

void main(int argc, char **argv) 
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("GLut");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(Resize);
	glutIdleFunc(RenderScene);
	glutKeyboardFunc(GetNormalKeys);
	glutSpecialFunc(GetSpecialKeys);
	glutMainLoop();
}

void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(-1.f, 0.0f, -6.0f);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		// Bottom Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glEnd();
	glutSwapBuffers();
}

void Resize(int w, int h)
{
	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;

	float ratio = 1.0* w / h;

	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45,ratio,1,1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f);
}


void Init()
{
	//enable depth testing
	glEnable(GL_DEPTH_TEST);
	//smooth for the shade model
	glShadeModel(GL_SMOOTH);
	//clear background
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepth(1.0f);	
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);	
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	if(!LoadGLTextures())
	{
		std::cout<<"could not load the textures"<<std::endl;
	}
	glEnable(GL_TEXTURE_2D);
}


void GetNormalKeys(unsigned char key, int x, int y)
{
	if(key==27)
	{
		exit(0);
	}
}

void GetSpecialKeys(int key, int x, int y)
{
	switch(key)
	{
	case GLUT_KEY_F1: glutFullScreen(); break;
	}
}

int LoadGLTextures()
{
	ILuint ImgId;										// The Image Name

	ilGenImages(1, &ImgId);								// Generate Image Name
	ilBindImage(ImgId);									// Bind Image Name to Be Current
	if (!ilLoadImage((ILstring)"texture.bmp")) {				// Load the Bitmap and Check for Errors
		ilDeleteImages(1, &ImgId);						// If Could Not Load, Delete Image
		return FALSE;									//  and Return False
	}

	texture[0] = ilutGLBindTexImage();					// Send the Image to OpenGL
	ilDeleteImages(1, &ImgId);							// Delete the Image Name, As GL Has a Copy
	
	return true;	
}

So, can someone please help me figure this out? Thanks to all who help! Chad.

Share this post


Link to post
Share on other sites
Advertisement
Are you sure that your texture is power of 2 (64, 128, 256)?

And if it is that you are saving it as rgb, a couple days ago I couldn't get my height map to load, I found out I was saving it as greyscale :b

Share this post


Link to post
Share on other sites
yes, it is a power of 2. The image size is 128X128. I will check on how I am saving it. I did have to save something as a grayscale the other day, although I don't think I am now. Let me open photoshop up and check.


Yep, saving it as a RGB. I also just converted it to a jpeg, to see if that worked and it didn't. Still didn't show up.


Anyone else? This is making no since to me.

Thanks to all who help!!


Share this post


Link to post
Share on other sites
Two things try using this


glFrontFace(GL_CCW);

//or
glFrontFace(GL_CW);

//and try this

gluLookAt(0.0, 0.0, -10.0, 0.0, 0.0, 0.0, 0.0f, 1.0f, 0.0f);





Off the top of my head I am betting its the gluLookAt() try that first. But I am assuming you left the default winding order to CCW so try CW your order for your vertices maybe backwards...

Share this post


Link to post
Share on other sites
I tried everything you said, and still nothing. I am beginning to think that my error checking is wrong, and it's not loading it up period. Ever since I posted my source, I have even added DevIL error checking and still no errors have been outputted.

While I still look at some things, can someone maybe compile my source, to see if it is the source or if I am just being stupid with the texture? Any help is appreciated!


Thanks,
Chad!

Share this post


Link to post
Share on other sites
First of all, sorry for the double post. I have done more thinking on this, and reading more and more things, and I have finally gotten it closer now. First of all...now the application gets a runtime error. Yes, I said I was closer. I am closer because the application breaks at the ilLoadImage function. When it breaks, I get this as the run time error (not sure if it's runtime or not, but it does it at runtime, so yeah...):


First-chance exception at 0x00000000 in OpenGL.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x00000000 in OpenGL.exe: 0xC0000005: Access violation reading location 0x00000000.



It is giving me that, and it is breaking in this function:


void LoadGLTexture()
{
ILuint ImgId; // The Image Name

ilGenImages(1, &ImgId); // Generate Image Name
ilBindImage(ImgId); // Bind Image Name to Be Current
ilLoadImage("texture.jpg");
std::cout<<Error<<std::endl;
texture= ilutGLBindTexImage(); // Send the Image to OpenGL
ilDeleteImages(1, &ImgId); // Delete the Image Name, As GL Has a Copy
}




Anyone see anything wrong?

Here is my new code. I have changed some stuff from the first one. So, here is the new code:


#include <iostream>
//include glut
#include <gl/glut.h>
#include <il\il.h>
#include <il\ilu.h>
#include <il\ilut.h>

//render function
void RenderScene();
//resize function
void Resize(int w, int h);
//init function
void Init();
//get normal keys function
void GetNormalKeys(unsigned char key, int x, int y);
//get the special keys now
void GetSpecialKeys(int key, int x, int y);
//load up the textures
void LoadGLTexture();

//Create enough storage space for one texture
GLuint texture;

ILenum Error;

//rotation variables
float xR=.12;
float yR=.12;
float zR=.12;

void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
Init();
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("GLut");
glutDisplayFunc(RenderScene);
glutReshapeFunc(Resize);
glutIdleFunc(RenderScene);
glutKeyboardFunc(GetNormalKeys);
glutSpecialFunc(GetSpecialKeys);
glutMainLoop();
}

void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, -10.0, 0.0, 0.0, 0.0, 0.0f, 1.0f, 0.0f);
glFrontFace(GL_CW);
glRotatef(xR,1.0f,0.0f,0.0f);
glRotatef(yR,0.0f,1.0f,0.0f);
glRotatef(zR,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
xR+=0.1f;
yR+=0.1f;
zR+=0.1f;
glutSwapBuffers();
}

void Resize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f);
}


void Init()
{
glEnable(GL_TEXTURE_2D);
ilutRenderer(ILUT_OPENGL);
Error = ilGetError();
LoadGLTexture();
//enable depth testing
glEnable(GL_DEPTH_TEST);
//smooth for the shade model
glShadeModel(GL_SMOOTH);
//clear background
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}


void GetNormalKeys(unsigned char key, int x, int y)
{
if(key==27)
{
exit(0);
}
}

void GetSpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_F1: glutFullScreen(); break;
}
}

void LoadGLTexture()
{
ILuint ImgId; // The Image Name

ilGenImages(1, &ImgId); // Generate Image Name
ilBindImage(ImgId); // Bind Image Name to Be Current
ilLoadImage("texture.jpg");
std::cout<<Error<<std::endl;
texture= ilutGLBindTexImage(); // Send the Image to OpenGL
ilDeleteImages(1, &ImgId); // Delete the Image Name, As GL Has a Copy
}




Anyone have any ideas? This is really confusing me.

Thanks to all who help!

Chad.

Share this post


Link to post
Share on other sites
You have a null pointer somewhere. See the error message.

You should find the exact line with a debugger.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!