PDA

View Full Version : Image loaded from texture is dirty



ramy1989
12-11-2012, 10:18 AM
I have a TGA file with contains this image:

917

The texture is 256x256.
So I'm trying to load it and map it to a cube:



#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#import <stdlib.h>
#import <stdio.h>
#import <assert.h>


GLuint width=640, height=480;
GLuint texture;
const char* const filename= "/Users/ramy/Documents/C/OpenGL/Test/Test/texture.tga";




void init()
{

// Initialization

glEnable(GL_DEPTH_TEST);
glViewport(-500, -500, 1000, 1000);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/(float)height, 1, 1000);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

// Texture

char bitmap[256][256][3];
FILE* fp=fopen(filename, "r");
assert(fp);
assert(fread(bitmap, 3*sizeof(char), 256*256, fp) == 256*256);
fclose(fp);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmap);

}


void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glColor3ub(255, 255, 255);

glBegin(GL_QUADS);
glVertex3f(0, 0, 0);
glTexCoord2f(0.0, 0.0);

glVertex3f(40, 0, 0);
glTexCoord2f(0.0, 1.0);

glVertex3f(40, 40, 0);
glTexCoord2f(1.0, 1.0);

glVertex3f(0, 40, 0);
glTexCoord2f(1.0, 0.0);

glEnd();

glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}


But this is what I get when the window loads:

918

So just half of the image is correctly displayed, and also with different colors.Then if I resize the window I get this:

919

Magically the image seems to fix itself, even if the colors are wrong.

tonyo_au
12-11-2012, 10:27 PM
I am not sure what is causing it but your second image is almost certainly caused by an incorrect texture coordinate.
You could try changing to rendering 2 triangles instead of a quad.

ramy1989
12-12-2012, 04:55 AM
Nothing to do, I tried this:



glBegin(GL_TRIANGLES);

glVertex2f(0, 0);
glTexCoord2d(0, 0);

glVertex2f(40, 0);
glTexCoord2d(1, 0);

glVertex2f(40, 40);
glTexCoord2d(1, 1);

glVertex2f(0, 0);
glTexCoord2d(0, 0);

glVertex2f(40, 40);
glTexCoord2d(1, 1);

glVertex2f(0, 40);
glTexCoord2d(0, 1);

glEnd();


But I get this:

922

I also changed the look at this way:



gluLookAt(0, 0, 100, 0, 0, 0, 0, 1, 0);


But nothing to do.Even if I try not changing the gluLookAt I get a wrong image.Even with GL_QUADS.

Claude
12-12-2012, 11:15 AM
First thing,I would check:
TGA file has a header (18 bytes)
http://paulbourke.net/dataformats/tga/
Hope it helps.

Claude

tonyo_au
12-12-2012, 08:32 PM
ooh - I missed an error - all the values that effect a vertex must come before the glVertex3f call - that means



glTexCoord2d(0, 0);
glVertex2f(0, 0);