PDA

View Full Version : openGL texturing



nolfolk
03-13-2009, 09:36 PM
Ok, I don't know if I'm doing things right... But I'm trying to put some textures in my project. I made it with some GLubyte bitmap:

GLubyte bitmap[32] = {
// Hexadecimals here
};

And then putted with glBitmap();

Ok. But now I'm trying to get from a BMP file ( test.bmp ) ( after that I'll try with JPG and PNG. ).

But I'm gettin some trouble, and I don't know if its really supouse to be this hard: I'm writing a program that opens the BMP image, make sure its BMP ( with the "Magic Words" BM ), get the sizes of it and get the Image-Array.
I've done that ! Here's the code ( kinda lame, but whatever ):

DEV-C++ 4.9.9.2


#include <stdio.h>
#include <iostream>

int main(){
FILE *fp;
int i, j;

char* arquivo;
int MAGIC_NUMBER[2];
int tmp[4];
int H;
int W;

arquivo = "teset.bmp";

fp = fopen(arquivo, "r");

fseek(fp, 0, SEEK_SET);
for(i=0; i<2; i++)
MAGIC_NUMBER[i] = getc(fp);

fseek(fp, 18, SEEK_SET);
for(i=0; i<4; i++)
tmp[i] = getc(fp);
W = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);

fseek(fp, 22, SEEK_SET);
for(i=0; i<4; i++)
tmp[i] = getc(fp);
H = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);

printf("MAGIC_WORD: %c%c\n", MAGIC_NUMBER[0], MAGIC_NUMBER[1]);
printf("WIDTH : %d\n", W);
printf("HEIGHT : %d\n", H);

fseek(fp, 54, SEEK_SET);
for(i=0; i<H; i++){
for(j=0; j<W; j++){
printf("%2X . ", getc(fp));
}
printf("\n");
getc(fp);getc(fp);
}

fclose(fp);

system("pause");
return 0;
}


I've learned BMP stuff from wikipedia, then I made that by myself.

Ok, but now what ? What do I do with these information? I mean, how do I put the Array that I get with this code ( actually in here I'm printing on the screen, but you get my point ) and make openGL read it ?

I think I'm in the wrong path :( I'ts really necessary to do all the things I'm doing ? ... openGL starts to be complicated to me here :(

If someone can help, I appreciate :)
AND NO, I DON'T WANT TO USE SDL, ALLEGRO, OR WHATEVER. Only if is libPNG, libJPG and ( I don't know if exists ) libBMP. I don't know how to use them, anyway :(

ZbuffeR
03-14-2009, 02:22 AM
OpenGL feels complicated to you, whereas there is not a single line of GL code in your snippet above ?

You don't want to use a GL helper library ? Whereas you need help with GL ?

... do you belong to these people like to get themselves in trouble, then complain loudly about their situation :p ? ...

glut glfw sdl or go straight to win/X programming

(glfw even does the image file loading to GL texture for you)

martinsm
03-14-2009, 04:34 AM
Use stb_image.c (http://www.nothings.org/stb_image.c) for loading common image formats (tga, bmp, png, jpg). It has no dependencies on other libraries.

Ilian Dinev
03-14-2009, 04:56 AM
//---[ stuff you provide ]-----[
int TexWid = 256;
int TexHei = 256;
char* TexData = ...;
//-----------------------------/

// Stuff OpenGL gives you ---[
unsigned int TexHandle;
//---------------------------/


glEnable(GL_TEXTURE_2D);
glGenTextures( 1, &amp;TexHandle);
glBindTexture(GL_TEXTURE_2D,TexHandle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, TexWid, TexHei, 0, GL_BGRA, GL_UNSIGNED_BYTE,TexData);


http://lmgtfy.com/?q=opengl+texture+tutorial