[Solved] glTexImage2D issues

Started by
7 comments, last by DesignerX 14 years, 10 months ago
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]
There is nothing that can't be solved. Just people that can't solve it. :)
Advertisement
The value 3 (third parameter) is invalid, it should be something like GL_RGB.
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);
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
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.
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).
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...
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
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)
There is nothing that can't be solved. Just people that can't solve it. :)

This topic is closed to new replies.

Advertisement