简体   繁体   中英

OpenGL Texture Mapping Not mapping

I am creating a flat surface and applying a texture for it. But for some reason the texture is not getting applied properly. I am getting something like this.

这就是输出的样子 This is the code that I am using (I have a class for applying textures),

for(int i = 0; i < 512; i++) {
    for(int j = 0; j < 512; j++) {
        int c = ((((i&0x8)==0)^(((j&0x8))==0)))*255;

        checkImage[i][j][0] = (GLubyte) c;
        checkImage[i][j][1] = (GLubyte) c;
        checkImage[i][j][2] = (GLubyte) c;
        checkImage[i][j][3] = (GLubyte)255;
        //cout<<"("<<(int)dataForPixel.rgbtRed<<","<<(int)dataForPixel.rgbtGreen<<","<<(int)dataForPixel.rgbtBlue<<")";
    }
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,       GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,       GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,   GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,   GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
             0,
             GL_RGBA,
             imageX,
             imageY,
             0,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             checkImage
);

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);

glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3d(-100, -100,  0.0);
glTexCoord2d(0.0, 1.0); glVertex3d(-100,  100,  0.0);
glTexCoord2d(1.0, 1.0); glVertex3d( 100,  100,  0.0);
glTexCoord2d(1.0, 0.0); glVertex3d( 100, -100,  0.0);
glEnd();

The image is a 512 x 512 image.

Why is the texture not applying properly.

UPDATE: The c value is just for producing a chess board pattern which consists of squares of 8 pixels width and height of alternating black and white.

Ok I found out the problem. Seems i was trying to allocate the checkImage memory dynamically but not in one go. So there were gaps in the memory which the OpenGL did not bother with.

Once I fixed it to allocate the memory as one big chunk it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM