Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Issues with texture mapping/loading

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    4

    Issues with texture mapping/loading

    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/begin...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:

    Code :
    // 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_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,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;
    }

    Code :
    // 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!

    Thanks.

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,170
    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);

  3. #3
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    4
    Thanks for the reply Bionic.

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

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,170
    Actually, that's not what I said
    You have to specify some filtering otherwise the texture is not complete! So use GL_LINEAR for the min filter.

  5. #5
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    Quote Originally Posted by ScorpZ View Post
    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_Mi...ting_a_Texture
    http://www.opengl.org/wiki/Common_Mi...map_generation
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  6. #6
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    4
    Quote Originally Posted by BionicBytes View Post
    Actually, that's not what I said
    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!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •