PDA

View Full Version : Issues with texture mapping/loading



ScorpZ
05-16-2012, 11:54 PM
Hello,

I'm currently having issues with mapping my texture (256x256 BMP) onto a quad for example. I was following this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/ on how to load a BMP texture file. I think my texture is being read but the texture is not mapping properly for some reason: http://i.imgur.com/EQo1Z.png.

So far, this is what I have:



// Texture.cpp

#include "GL/glut.h"
#include "GL/glext.h"
#include "Texture.h"
#include <stdio.h>

Texture::Texture(){}

bool Texture::loadTexture(const char *filename, GLuint texture[], int id){

unsigned char header[54];
unsigned int dataPos;
unsigned int width;
unsigned int height;
unsigned int imageSize;
unsigned char *data;

FILE *file = fopen(filename, "rb");
if(!file){
return false;
}

if(fread(header, 1, 54, file) != 54){
return false;
}else if (header[0] != 'B' || header[1] != 'M'){
return false;
}

dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);

if(imageSize == 0){
imageSize = width * height * 3;
}
if(dataPos == 0){
dataPos = 54;
}

// Create buffer
data = new unsigned char[imageSize];

fread(data, 1, imageSize, file);
fclose(file);

glGenTextures(1, &texture[id]);
glBindTexture(GL_TEXTURE_2D, texture[id]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

free(data);

return true;
}





// main.cpp

#include "GL/glut.h"
#include "Camera.h"
#include "Texture.h"

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

Camera camera;
Texture textureLoader;

GLuint texture[1]; // Test texture

void init(){

textureLoader.loadTexture("test.bmp", texture, 0);

glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 1.0, 1000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

void drawQuad(){
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-10, -10, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-10, 10, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(10, 10, 0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(10, -10, 0);
glEnd();
}

void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

cameraControl();

glLoadIdentity();
gluLookAt(camera.getPos().x, camera.getPos().y, camera.getPos().z,
camera.getLook().x + camera.getPos().x, camera.getLook().y,
camera.getLook().z + camera.getPos().z,
camera.getUp().x, camera.getUp().y, camera.getUp().z);


glEnable(GL_TEXTURE_2D);
drawQuad();
glDisable(GL_TEXTURE_2D);

glutSwapBuffers();
}

// PLUS other functions, ie. main, keyboard, reshape etc.



Any help is appreciated! :D

Thanks.

BionicBytes
05-17-2012, 12:41 AM
It could be as a result of your texture being incomplete in OpenGL. By that I mean there may be missing mip maps because you asked for mip mapping in the filtering:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
Either use glGenerateMipMap (Texture[0]); or disable mip maps in the filtering:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);

ScorpZ
05-17-2012, 02:00 AM
Thanks for the reply Bionic.

I've tried just commenting those filtering lines you mentioned but to no avail. :(

BionicBytes
05-17-2012, 03:41 AM
Actually, that's not what I said:whistle:
You have to specify some filtering otherwise the texture is not complete! So use GL_LINEAR for the min filter.

V-man
05-17-2012, 05:06 AM
Thanks for the reply Bionic.

I've tried just commenting those filtering lines you mentioned but to no avail. :(

http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture
http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation

ScorpZ
05-17-2012, 05:27 AM
Actually, that's not what I said:whistle:
You have to specify some filtering otherwise the texture is not complete! So use GL_LINEAR for the min filter.

Ahh yes, I see now. Thank you very much! :D