Jump to content
  • Advertisement
Sign in to follow this  
archyyu

OpenGL My program In OpenGL with texture just do not work!!!

This topic is 3332 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 read data from a bmp file,and use it as texture to map,but it didnot work. What i can confirm is my code is no problem,which work well in others' computer. But in my computer ,it failed. where is the Crux!! I appreciate any hlep!

Share this post


Link to post
Share on other sites
Advertisement
Is your texture square and power of 2?
That got me a few times. I guess a few video cards don't like other sizes.

Share this post


Link to post
Share on other sites
Quote:
Original post by apefish
Is your texture square and power of 2?
That got me a few times. I guess a few video cards don't like other sizes.


Did you try calling glGetError()? It will tell you something is wrong with your glTexImage call.

Share this post


Link to post
Share on other sites
unsigned int ID; /** < 生成纹理的ID号 */
int imageWidth; /** < 图像宽度 */
int imageHeight; /** < 图像高度 */
unsigned char *image; /** < 指向图像数据的指针 */
FILE *pFile = 0;


BITMAPINFOHEADER bitmapInfoHeader;
BITMAPFILEHEADER header;

unsigned char textureColors = 0;

pFile = fopen("桌面.bmp", "rb");
if(pFile == 0) return false;

fread(&header, sizeof(BITMAPFILEHEADER), 1, pFile);

if(header.bfType != BITMAP_ID)
{
fclose(pFile);
return false;
}

fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);

imageWidth = bitmapInfoHeader.biWidth;
imageHeight = bitmapInfoHeader.biHeight;


if(bitmapInfoHeader.biSizeImage == 0)
bitmapInfoHeader.biSizeImage = bitmapInfoHeader.biWidth *
bitmapInfoHeader.biHeight * 3;


fseek(pFile, header.bfOffBits, SEEK_SET);


image = new unsigned char[bitmapInfoHeader.biSizeImage];


fread(image, 1, bitmapInfoHeader.biSizeImage, pFile);


for(int index = 0; index < (int)bitmapInfoHeader.biSizeImage; index+=3)
{
textureColors = image[index];
image[index] = image[index + 2];
image[index + 2] = textureColors;
}

fclose(pFile);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);

glGenTextures(1, &ID);

glBindTexture(GL_TEXTURE_2D, ID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imageWidth,
imageHeight, GL_RGB, GL_UNSIGNED_BYTE,
image);

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);

glPushMatrix();
glScaled(45.0,30.0,0);
glBindTexture(GL_TEXTURE_2D, ID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-2.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, -1.0f, 0.0f);

glEnd();
glPopMatrix();


GLvoid ReShapeGLScene( GLsizei width,GLsizei height )
{
glViewport(0,0,(GLsizei)width,(GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((GLdouble)-450.0,(GLdouble)450.0,(GLdouble)-300.0,(GLdouble)300.0,50.0,-10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};




The above is my code which work well in others' compuer but failed in my.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!