PDA

View Full Version : texture problems



mcbastian
11-16-2000, 11:05 PM
Hi again http://www.opengl.org/discussion_boards/ubb/smile.gif

i have a little problem adding textures to my scene.
I create my textures using the photoshop RAW-format. size 256*256 * 3 colors (RGB)(filesize: 196608 bytes)
loaded with

char *loadtech(char *fname)
{
file *x;
char *pic;
pic=(char*)malloc(196608);
x=fopen(fname,"rb");
fgets(pic,196608,x);
return pic;
}

GLuint *text;
char *tec;
void init()
{
text=(GLuint*)malloc(3*sizeof(GLuint));
glGenTextures(2,text);
glBindTexture(GL_TEXTURE_2D,*(text));
tec=loadtech("texture.rgb");
glTexImage2d(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL _UNSIGNED_BYTE,tec);
free(tec);

// then, texture-parameters, lightning parameters and so... are set....

}

and now the problem. 2 files, same size. one is loaded and displayed, the other not. fileattributes are set correct, filename is also no problem, filesize is equal........even access rights...
one is loaded and displayed, if i create another texture (for example with the same filename) it won't be displayed.....


hope, you can help.

bastian

Kilam Malik
11-17-2000, 01:44 AM
You should check, if the fopen succeded:

char *loadtech(char *fname)
{
file *x;
char *pic;
pic=(char*)malloc(196608);
x=fopen(fname,"rb");
if (x)
fgets(pic,196608,x);
else
throw() or something else...
return pic;
}

Next, stop with the debugger at
return pic;
and look if fgets has loaded something.
To check it better, fill the memory "pic" you allocated before with 0's.
Next, you never close the file! fclose(x)
is needed, otherwise you wont get another
file handle in w9x.

Kilam.

mcbastian
11-17-2000, 02:27 AM
I close the files. this code wasn't copied out of my original source, cause i'm not at the machine where i wrote it http://www.opengl.org/discussion_boards/ubb/smile.gif
The files are opened. but I'll try to check this out a little bit better. even if fgets will succeed.
but i think this is not the error. caus eat some textures you'll se some parts of them on the poly. ?!?

hope, someone can help.

CU!
bastian

Kilam Malik
11-17-2000, 03:30 AM
Another thing:
Instead of

glTexImage2d(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL _UNSIGNED_BYTE,tec);


You should use:

glTexImage2d(GL_TEXTURE_2D,0,GL_RGB8,256,256,0,GL_ RGB,GL_UNSIGNED_BYTE,tec);

Otherwise the texture is (e.g. on my machine) converted to 16 colors :-( But
I think that depends on the driver.

Kilam.

mcbastian
11-18-2000, 06:47 AM
finally I could solve it.
I changed char* to void* and fgets to fread.
but i still don't know why it works with the one file and not with the other (now it works with all)(filesize & access rithts are the same).
i think there some probs with strings and so....

thanx a lot.

Bastian

BK
11-18-2000, 02:55 PM
The difference it probably a '0' byte. fgets is designed to read zero terminated strings, and so it will stop the read when it gets to a '0'.

Janne
11-19-2000, 11:37 AM
Like the previous poster said, fgets is not the function to use for loading binary data.

The short description for the function is:
The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

Instead of fgets you should use fread, like this:
char *loadtech(char *fname)
{
int bytesRead = 0;
char *pic = null;
FILE *x = null;
pic = (char *) malloc(196608);
if (pic == NULL)
return null;
x = fopen(fname, "rb");
if (x == NULL)
return null;
bytesRead = fread(pic, sizeof(char), 196608, x);
if (bytesRead != 196608)
{
free(pic);
fclose(x);
return null;
}
fclose(x);
return pic;
}

This should give you the basic error checking that you should implement always when you are trying to get some system resources. It will save you alot of trouble later if you do so.

- Janne / Endymio - TDT Productions

[This message has been edited by Janne (edited 11-19-2000).]

monson
11-19-2000, 05:18 PM
If you are displaying both textures at the same time then you need 2 texture variables.
for example:
use GLuint texture[2];

mcbastian
11-19-2000, 08:59 PM
I use a pointer. like


GLuint *textures;
#define NUMTEX 2
textures=(GLuint*)malloc(NUMTEX*sizeof(GLuint));
glGenTextures(2,textures);
glBind....(GL_TEXTURE_2D,*(textures))
// here code to load tex 1

glBind....(GL_TEXTURE_2D,*(textures+1))
// and now load tex 2

sorry for spelling errors http://www.opengl.org/discussion_boards/ubb/smile.gif

it's not very different from using an array
:-)

greets
bastian