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

glTexImage2D = no texture

Started by hotpixel Apr 8, 2010 at 6:16 AM 1 replies 1.3k views
Original Post
hotpixel
hotpixel
There are a quite a few posts on glTexImage2D but the only one that was my problem remained unanswered so I will dare to ask it again... gluBuild2DMipmaps(...) works okay but glTexImage2D(...) doesn't render a texture onto my model at all. No errors and I've set GL_TEXTURE_MIN_FILTER correctly (I think).

void CRenderingDevice::Initialise(CPlatform *pPlatform)
{
	m_pPlatform = pPlatform;

	m_pPlatform->InitialiseOpenGL();

	// Ensure correct display of polygons
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);	
	glDepthMask(GL_TRUE);

	glFrontFace(GL_CW);

	
	{//test code...
		GLfloat light_ambient[]= { 0.1f, 0.1f, 0.1f, 0.1f };
		GLfloat light_diffuse[]= { 1.0f, 1.0f, 1.0f, 0.0f };
		GLfloat light_specular[]= { 1.0f, 1.0f, 1.0f, 0.0f };

		GLfloat light_position[]= { 10.0f, 10.0f, 10.0f, 1.0f };

		glLightfv (GL_LIGHT1, GL_AMBIENT, light_ambient);
		glLightfv (GL_LIGHT1, GL_DIFFUSE, light_diffuse);
		glLightfv (GL_LIGHT1, GL_SPECULAR, light_specular);
		glLightfv (GL_LIGHT1, GL_POSITION, light_position);

		glEnable (GL_LIGHT1);
		glEnable (GL_LIGHTING);
	}

	// select modulate to mix texture with color for shading
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

	// the texture wraps over at the edges (repeat)
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

...

void CRenderingDevice::DrawMesh(CMesh* pMesh)
{
	static GLuint texture = 0;
	GLenum error = GL_NO_ERROR;

	if (!texture)
	{
		glGenTextures(1, &texture);
		glBindTexture(GL_TEXTURE_2D, texture);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pMesh->m_pImage->m_iWidth, pMesh->m_pImage->m_iHeight, GL_RGB, GL_UNSIGNED_BYTE, pMesh->m_pImage->m_pData);
		//glTexImage2D(GL_TEXTURE_2D, 0, 3, pMesh->m_pImage->m_iWidth, pMesh->m_pImage->m_iHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pMesh->m_pImage->m_pData);
		error=glGetError();
		if (error!=GL_NO_ERROR)
		{
			int parp = 1;
		}
	}
	glBindTexture(GL_TEXTURE_2D, texture);

	// draw the triangle
	glBegin(GL_TRIANGLES);

	for (unsigned int loop = 0; loop < pMesh->m_iNumIndicies; loop+=3)
	{
		glNormal3f(pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_vNormal.m_fX, pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_vNormal.m_fY, pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_vNormal.m_fZ);
		glTexCoord2d(pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_fU,pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_fV);
		glVertex3f(pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_vPosition.m_fX, pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_vPosition.m_fY, pMesh->m_pVertices[pMesh->m_wIndicies[loop]].m_vPosition.m_fZ);

		glNormal3f(pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_vNormal.m_fX, pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_vNormal.m_fY, pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_vNormal.m_fZ);
		glTexCoord2d(pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_fU,pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_fV);
		glVertex3f(pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_vPosition.m_fX, pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_vPosition.m_fY, pMesh->m_pVertices[pMesh->m_wIndicies[loop+1]].m_vPosition.m_fZ);

		glNormal3f(pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_vNormal.m_fX, pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_vNormal.m_fY, pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_vNormal.m_fZ);
		glTexCoord2d(pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_fU,pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_fV);
		glVertex3f(pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_vPosition.m_fX, pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_vPosition.m_fY, pMesh->m_pVertices[pMesh->m_wIndicies[loop+2]].m_vPosition.m_fZ);
	}

	glEnd();

	// restore the previous transformation
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

	glEnd();
}

Thanks, Andy
HuntsMan
HuntsMan
Set the MIN/MAG filters to GL_LINEAR AFTER you've bound the texture, not before. When you do this, your texture will be complete.

This happens because the default filter is mipmapped, and you're uploading only the base level with glTexImage2D, but glBuild2DMipmaps also generates all mipmaps, making your texture complete.
hotpixel
hotpixel
Nice one, thanks ;) !

Topic Locked

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

Sign in to reply to this topic.