PDA

View Full Version : Texture loading problem



gui24a
08-18-2008, 07:34 PM
Hi masters!

I'm experiencing problems while loading a texture from a .raw file generated by Photoshop.

the function to load is quite simple:


void CarregaTextura(const char* caminho, GLuint TextIndex)
{
int largura = 256;
int altura = 256;
GLubyte *dados;
FILE *arquivo;

dados = (GLubyte*) malloc( largura * altura * 4 );

//abre e lê como bytes
arquivo = fopen(caminho, "rb");

//faz a leitura do arquivo para dentro da variável dados
fread(dados, altura * largura * 4, 1, arquivo);
fclose(arquivo);

glBindTexture(GL_TEXTURE_2D, TextIndex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256,256, 0, GL_RGBA, GL_UNSIGNED_BYTE,dados);

}

The problem is the texture is not loading correctly (see image below). I've tried paintbrush bitmaps but the problem is the same.

http://img229.imageshack.us/img229/8766/textproblemcy9.th.jpg (http://img229.imageshack.us/my.php?image=textproblemcy9.jpg)
click to enlarge

The original texture is as below:

http://i76.photobucket.com/albums/j4/Dabigsamoan/2007-03-23_020601.jpg

Also, when I put the text coordinates inside a display list, nothing happens.

Can someone help me again?

Regards!

************************************************** ***********

EDIT: I got it! the problem was GL_RGBA. It's GL_RGB in that case. But now another problem:

http://img522.imageshack.us/img522/8193/textproblemsm1.th.jpg (http://img522.imageshack.us/my.php?image=textproblemsm1.jpg)

How to remove this borders? Why this happens?

and also, the problem with display list continues.. :(

dletozeun
08-19-2008, 12:59 AM
You "border problem" is due to your texture. OpenGL don't access border elements in repeat wrap mode. I have checked your texture with gimp and with a big zoom I can see a 1 pixel grey border all around the texture. Remove it.

For your display list problem, I don't know, put some code.

gui24a
08-19-2008, 02:19 PM
I got it.

It was my mistake, sorry and another big thank you!

It's impressive how cool all you are, you really help us with all this stuff! This is really awesome, hope I can start helping people here soon! :D

gui24a
08-19-2008, 07:13 PM
By the way, How can I set texture to glu or glut polygon functions?

for ex., how to set texture coordinates to glutSolidCube?

dletozeun
08-20-2008, 03:33 AM
You can look at automatic texture coordinates generation here (http://www.opengl.org/sdk/docs/man/xhtml/glTexGen.xml)

I have never used it for generating texture coordinates on a cube, but something like this may work:

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

gui24a
08-20-2008, 04:24 PM
Ok, i'll try, thanks!

www_iptry_cn
08-21-2008, 06:28 AM
hello my is chinase
English not good
the is code
void LoadBitmapOpenGL(GLuint *uTexture, char *BmpName)
{
DWORD dwBmpWidth = 256;
DWORD dwBmpHeight = 256;
DWORD dwBmpSize = dwBmpWidth*dwBmpHeight*3;

HANDLE hFile = CreateFile(BmpName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Open Bitmap Error!",BmpName,MB_OK|MB_ICONERROR);
return FALSE;
}
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
ZeroMemory(&bf, sizeof(bf));
ZeroMemory(&bi, sizeof(bi));
DWORD dwReadByte = 0;

ReadFile(hFile, &bf, sizeof(bf), &dwReadByte, NULL);
SetFilePointer(hFile, bf.bfOffBits, NULL, FILE_BEGIN);
LPBYTE pBmp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBmpSize);
ReadFile(hFile, pBmp, dwBmpSize, &dwReadByte, NULL);

unsigned int i = 0;
BYTE tmp = 0;
for (i = 0; i < dwBmpSize; i += 3)
{
tmp = *(pBmp+i);
*(pBmp+i) = *(pBmp+i+2);
*(pBmp+i) = tmp;
}
glBindTexture(GL_TEXTURE_2D,*uTexture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, dwBmpWidth, dwBmpHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pBmp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, pBmp);

CloseHandle(hFile);
}

gui24a
08-21-2008, 08:38 AM
Thanks man!