Jump to content
  • Advertisement
Sign in to follow this  
DesignerX

OpenGL [Solved] glTexImage2D issues

This topic is 3316 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

I am trying desperately to texture a simple quad using known examples but I always get a white quad with no texture. I tried creating the texture by myself and still nothing ! My initialization is :

	// Initialize display mode to be double buffered, with RGBA color model and with a depth buffer
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE  | GLUT_DEPTH);

	// Initialize window position and size
	glutInitWindowPosition(-1, -1); // OS dependent
	glutInitWindowSize(500, 400);

	// Initialize the GLUT library
	glutInit(&argc, args);

	// Initialize opengl
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glShadeModel(GL_SMOOTH);
        glEnable(GL_TEXTURE_2D);

	// Bind and generate the texture
	glGenTextures(1, &g_uiTextureId);
	glBindTexture(GL_TEXTURE_2D, g_uiTextureId);

	unsigned char *data;
	data = (unsigned char *)malloc(256 * 256 * 3);

        // Should create a red texture
	for (int i = 0;  i < 256 * 256 * 3; i += 3) {
		data = 255;
		data[i+1] = 0;
		data[i+2] = 0;
	}

	glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 	if (glGetError() != GL_NO_ERROR)
		printf("ERROR!"); // Never reached here


Rendering :
	// Clear drawing buffer and depth buffer
	glClearColor(0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);

	// Reset world matrix and set world position for a triangle
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -6.0f);
	glBindTexture(GL_TEXTURE_2D, g_uiTextureId);

	// Render a textured cube
	glBegin(GL_QUADS);

	// Front face
	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();
	glFlush();
	// Display frame
	glutSwapBuffers();


thx. [Edited by - DesignerX on June 19, 2009 12:40:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
try using GL_LINEAR instead of GL_NEAREST for GL_TEXTURE_MIN_FILTER, IIRC GL_NEAREST requires you to specify all mipmap levels.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Share this post


Link to post
Share on other sites
Quote:
Original post by Riraito
try using GL_LINEAR instead of GL_NEAREST for GL_TEXTURE_MIN_FILTER, IIRC GL_NEAREST requires you to specify all mipmap levels.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


False

Share this post


Link to post
Share on other sites
Quote:
Original post by Riraito
try using GL_LINEAR instead of GL_NEAREST for GL_TEXTURE_MIN_FILTER, IIRC GL_NEAREST requires you to specify all mipmap levels.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


No, GL_NEAREST doesn't require mipmaps, GL_NEAREST_MIPMAP_* and GL_LINEAR_MIPMAP_* do.

Share this post


Link to post
Share on other sites
Quote:
The value 3 (third parameter) is invalid, it should be something like GL_RGB.
Are you sure about that? I believe 1, 2, 3, and 4 are valid values for the 'internal format' argument (unless that's changed in a more recent OpenGL version).

Share this post


Link to post
Share on other sites
Quote:
Original post by HuntsMan
Quote:
Original post by Riraito
try using GL_LINEAR instead of GL_NEAREST for GL_TEXTURE_MIN_FILTER, IIRC GL_NEAREST requires you to specify all mipmap levels.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


No, GL_NEAREST doesn't require mipmaps, GL_NEAREST_MIPMAP_* and GL_LINEAR_MIPMAP_* do.


Oops o.0 that makes more sense, guess my memory doesn't serve me as well as I thought it did...

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
The value 3 (third parameter) is invalid, it should be something like GL_RGB.
Are you sure about that? I believe 1, 2, 3, and 4 are valid values for the 'internal format' argument (unless that's changed in a more recent OpenGL version).


http://www.opengl.org/wiki/Common_Mistakes#Texture_Precision

Share this post


Link to post
Share on other sites
Thx for the replies but nothing worked ! this is unbelievable, how can this be so complicated ????? do u have any other suggestions ?

thx.

[Edit]
didnt notice the line that says that i should disable mipmapping in HuntsMan link (a very resourceful page I might add, thx alot)

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!