jeudi 12 mars 2015

Why are my textures showing up as fractal patterns with my openGL renderer?


I'm working on getting textures to render using openGL. I'm part of the way there and stuck.


My goal is to get this picture: http://ift.tt/1C8OFUa


and this is where I'm at: http://ift.tt/1AmfiO8


Has anyone seen this issue before?



if (tObject == 0) // We don't yet have an OpenGL texture target
{
// This code counts the number of images and if there are none simply
// returns without doing anything
int nImages = 0;
while (tName[nImages][0] != '\0' && nImages < MAX_IMAGES)
nImages++;

if (nImages < 1)
return;

// To Do
//
// Generate a texture object and place the object's value in the "tObject"
// member, then bind the object to the 2D texture target

glGenTextures(nImages, &tObject);
glBindTexture(GL_TEXTURE_2D, tObject);

for (int nImage = 0; nImage < nImages; nImage++)
{
// This code loads the texture using the windows library's "BitmapFile" object
BitmapFile texture;
if (!texture.Read(tName[nImage]))
complain("Couldn't read texture %s", tName);

GLuint srcFormat, targFormat;
// To Do
//
// First decide which format the texture is. If the texture has 4 bytes
// per pixel then it should be an RGBA texture, if it is 3 bytes per pixel
// then it is an RGB image. Notice though that the byte order for the BitmatFile
// object is reversed, so you need to take that into account in the "source" format

if( texture.BytesPerPixel() == 3 )
{
srcFormat = GL_BGR;
targFormat = GL_RGB;
}
else
{
srcFormat = GL_BGRA;
targFormat = GL_RGBA;
}

// Then you need to set the unpack alignment to tell OpenGL about the structure
// of the data in the image and send the data to OpenGL. If there are multiple files
// then we are manually creating a mipmap here and you will use the "level" parameter
// of glTexImage2D to tell OpenGL which mipmap level is being set. The levels are
// set in the same order as they are stored in the image list.

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

if( nImages > 1 )
{
glGenerateMipmap(GL_TEXTURE_2D);
}

glTexImage2D(GL_TEXTURE_2D, nImage, targFormat, texture.Width(), texture.Height(), 0, srcFormat, GL_UNSIGNED_BYTE, texture.ImageData());
}
// Finally, if there is only one image, you need to tell OpenGL to generate a mipmap

if( nImages == 1)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
}

// Here you need to bind the texture to the 2D texture target and set the texture parameters
// You need to set the wrap mode, the minification and magnification filters.

glBindTexture(GL_TEXTURE_2D, tObject);

glTexParameteri(tObject, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(tObject, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(tObject, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tObject, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// To Do
//
// For advanced antialiasing set the number of anisotropic samples
GLERR;



Aucun commentaire:

Enregistrer un commentaire