PDA

View Full Version : My code in OpenGL with texture just did not work



archy_yu
10-25-2009, 07:16 AM
What i can confirm is that my code is no problem,The program work well in others' computer,but not in my.
where is the problem.
I appreciate any help!

Ilian Dinev
10-25-2009, 08:06 AM
Your gpu/system is?

archy_yu
10-25-2009, 11:50 PM
unsigned int ID; /** < &amp;#29983;&amp;#25104;&amp;#32441;&amp;#29702;&amp;#30340;ID&amp;#214 95; */
int imageWidth; /** < &amp;#22270;&amp;#20687;&amp;#23485;&amp;#24230; */
int imageHeight; /** < &amp;#22270;&amp;#20687;&amp;#39640;&amp;#24230; */
unsigned char *image; /** < &amp;#25351;&amp;#21521;&amp;#22270;&amp;#20687;&amp;#25968;&amp;#25454;&amp;# 30340;&amp;#25351;&amp;#38024; */
FILE *pFile = 0;


BITMAPINFOHEADER bitmapInfoHeader;
BITMAPFILEHEADER header;

unsigned char textureColors = 0;

pFile = fopen("&amp;#26700;&amp;#38754;.bmp", "rb");
if(pFile == 0) return false;

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

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

fread(&amp;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, &amp;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_RE PLACE);

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 the main code which work well in others' computer but failed in my.

Ilian Dinev
10-26-2009, 05:53 AM
So, again:
- what gpu/system ?
- what is the size of the bitmap (width x height) ?

archy_yu
10-26-2009, 11:09 PM
sorry,
my gpu : nViDIA GeForce g21o
system :windows xp
width : 64.