PDA

View Full Version : Bmp image in a OpenGl window



RafalTypiak
05-26-2009, 06:15 AM
This is the best I could come up with while trying to display a bmp image in a OpenGL window. It's not working, so if someone could take a look at it and tell me what's wrong, I'd be gratefull:



glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(560,800);
glutInitWindowPosition(100,100);
glutCreateWindow("Instrukcja");
init();
glutDisplayFunc(display);
glutMainLoop();



init();



void init(void)
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);

//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

}


display();



void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);

HBITMAP hBmp = (HBITMAP) ::LoadImage (NULL,
(LPCTSTR) "Q0.bmp", IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE |LR_CREATEDIBSECTION);
if (hBmp == NULL) {
printf("Nie znaleziono bitmapy\n");
}
else printf("Znaleziono bitmape!\n");

BITMAP BM;
::GetObject (hBmp, sizeof (BM), &BM);

if (BM.bmBitsPixel != 24){
printf("Zla paleta kolorow!\n");
}
else printf("Dobra paleta kolorow!\n");


glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glDrawPixels( BM.bmHeight, BM.bmWidth, GL_RGB,
GL_UNSIGNED_BYTE, BM.bmBits );
glEnd();

glFlush();
}


the resulting windo looks like a 'system halt' back on the old Win95s. Random configuration of pixels /but the pallete is taken from the desktop/. But at least it's not black as it was some time ago. So i'm guessing it's the parameter problem. Can someone help we with finding the right configuration?

Regards, Rafal

vivek vyas
05-26-2009, 06:22 AM
i am beginner if mistake please apologies me
you need to create object like Square by glVertex2f
which also have Texture coordinate
seems like
glBegin(GL_QUADS);
glTexCoord2f(x,y);glVertex2f(-x,y);
glTexCoord2f(x,y);glVertex2f(x,-y);
glTexCoord2f(x,y);glVertex2f(-x,y);
glTexCoord2f(x,y);glVertex2f(x,-y);
glEnd();
I suggest you start from beggining
like create object ,transformation etc bla bla bla
caus your init function have mistake
i think you need refrance bas

vivek vyas
05-26-2009, 06:26 AM
i tell you in programming language please understand
if(you_care){

cause_you_have_something_like_half_knowladge_and_i t_is dangerous_then_an_idiot_so_start_from_beginning
and_come_at_point_of_texture+and_do_this;
}
else
{
damage_your_system_and_reinstall;
}

vivek vyas
05-26-2009, 08:33 AM
i want to say that one time i was crazy in programming games but problem is i don't know how to use opengl(today i also don't know (beginner)) but that time i wrote source with many mistake which some time take windows xp to blue screen of death
so i just want to give

WARNING YOU: don't make code which halt start from beginning
so that you don't face the same problem as i faced

vivek vyas
05-26-2009, 09:21 AM
Try DevIl openil.sourceforge.net to load bmp

RafalTypiak
05-27-2009, 04:31 AM
I finally managed to run the whole thing. If someone is/will be interested, this is my source code for opening a 24 bit bmp from hdd and dosplaying it in a 560x800 window:




void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

HBITMAP hBmp = (HBITMAP) ::LoadImage (NULL,(LPCTSTR) "Q0.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |LR_CREATEDIBSECTION);
if (hBmp == NULL) {
printf("Nie znaleziono bitmapy\n");
}
else printf("Znaleziono bitmape!\n");

BITMAP BM;
::GetObject (hBmp, sizeof (BM), &BM);
if (BM.bmBitsPixel != 24){
printf("Zla paleta kolorow!\n");
}
else
printf("Dobra paleta kolorow!\n");
printf("Wysokosc:%d\n",BM.bmHeight);
printf("Szerokosc:%d\n",BM.bmWidth);

GLwidth=BM.bmWidth;
GLheight=BM.bmHeight;

glDrawPixels( GLwidth, GLheight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BM.bmBits );
glEnd();
glFlush();
}

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}


main ()
{

int argc = 1;
char *argv[1] = {{""}};
int g=(int)pvParam;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(560,800);
glutInitWindowPosition(1400,100);
glutCreateWindow("Instrukcja sterowania Q0");
init();
glutDisplayFunc(display);
glutMainLoop();


return 0;
}


the 'main' name was added just for the show. I was running the opengl window as a new thread so there was no 'main' in my code. Originaly /in my code/ there was "FunkcjaWatku(PVOID pvParam)" not "main()"

anyway...

This code creates a opengl window sized 560x 800 and than fills it with a 24bit bmp. The file which one wants to load is to be placed in the same dir as the exe file.

I know I could make a dynamically changing window depending on the image size, but that would require me to get the image data during the init() function, not display. And that would require me to somehow pass the whole image array from the init() function to the display().
If anyone knows how to do it fast and clean, please don't let me stop you. I'm just not that keen on it. It seemed to me like taking to much from the processor time, but I may be wrong.

If someone does know how to do it, please. Post your response. I'll be happy to learn something from you.

Regards,
Rafal Typiak

tionxtion
05-27-2009, 10:38 AM
nice!

hmm, i dunno if this is useful, but doesn't hurt to check:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06