PDA

View Full Version : Drawing texture in Windows OS using OpenGL



fatalcoder
06-30-2011, 07:12 AM
I'm new in OpenGL.

I want to draw an bitmap image in my Windows application using OpenGL. But image should be draw in its same width and height. For example, let say image size is width:156px & Height:93px.

I don't know how to render this image in my Windows GUI using OpenGL by keeping the same width and height.

See my code below,
Assume that already bitmap image is loaded as texture, I want to load full image in its original size in Windows

void drawImage()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, m_unTextureId);
glBegin(GL_POLYGON);

glTexCoord2f(0.0f, 1.0f); glVertex2f( -0.5, 0.5 ); // Top Left Vertex
glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.5, 0.5 ); // Top Right Vertex
glTexCoord2f(1.0f, 0.0f); glVertex2f( 0.5, -0.5 ); // Bottom Right Vertex
glTexCoord2f(0.0f, 0.0f); glVertex2f( -0.5, -0.5 ); // Bottom Left Vertex

glEnd();
glFlush();
glFinish();
SwapBuffers( m_hDC );
}

Above code will draw the bitmap correctly, but size is not as original image.


Texture loading code as shown below,

bool LoadTexture(CString csTextureFile_i)
{
AUX_RGBImageRec *pTextureImage = auxDIBImageLoad( csTextureFile_i );
if( pTextureImage != NULL )
{
glGenTextures( 1, &m_unTextureId );
glBindTexture( GL_TEXTURE_2D, m_unTextureId );
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, pTextureImage->sizeX, pTextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pTextureImage->data );
}
if( pTextureImage )
{
if( pTextureImage->data )
free( pTextureImage->data );
free( pTextureImage );
}
return true;
}


Please help.
Thanks.

BionicBytes
06-30-2011, 08:13 AM
what projection are you using...and what view port dimension have you set up?
i.e. are you using gluPerspective or gluOrtho to setup the projection matrix. What parameters have you used for glViewPort ?

The polygon you are drawing is only going from -0.5 to 0.5 - so thats a width of 1.0 units. Perhaps you should be drawing a square with width = 156 and height = 93!

MaxH
06-30-2011, 10:07 AM
If you use glDrawPixels you won't have to mess around with texture mapping at all.
It's not as fast as using textures, but it's simpler.

mhagain
06-30-2011, 10:40 AM
If you use glDrawPixels you won't have to mess around with texture mapping at all.
It's not as fast as using textures, but it's simpler.

I'll second this. Your requirement seems to cover non power of two textures too, so the best way that's going to work across as wide a range of hardware as possible is glDrawPixels. Make sure that you use a GL_BGRA format for best performance, and also make sure that you disable texturing, lighting, all shaders, etc before switching to glDrawPixels mode, as otherwise OpenGL will quite happily texture individual pixels.

fatalcoder
06-30-2011, 08:37 PM
what projection are you using...and what view port dimension have you set up?
i.e. are you using gluPerspective or gluOrtho to setup the projection matrix. What parameters have you used for glViewPort ?

The polygon you are drawing is only going from -0.5 to 0.5 - so thats a width of 1.0 units. Perhaps you should be drawing a square with width = 156 and height = 93!

Actually I did not mention all these in my code. I think OpenGL will takes its default. I'm using wiggle (OpenGL for Windows).

My code will be look like below,

m_hDC = ::GetDC( m_hWnd );
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
32, // 32-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int iPixelFormat;

// Get the best available match of pixel format for the device context
iPixelFormat = ChoosePixelFormat( m_hDC, &pfd);

// make that the pixel format of the device context
SetPixelFormat(m_hDC, iPixelFormat, &pfd);
// Create a rendering context
m_hGLRC = wglCreateContext (m_hDC);

// Make it the calling thread's current rendering context
wglMakeCurrent( m_hDC, m_hGLRC );
if( !LoadTexture( ".\\texture.bmp" ))
{
AfxMessageBox( "Texture loading failed." );
}

glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glEnable( GL_TEXTURE_2D );

After executing above code, I directly going to render loaded texture using below code,

void drawImage()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, m_unTextureId);
glBegin(GL_POLYGON);

glTexCoord2f(0.0f, 1.0f); glVertex2f( -0.5, 0.5 ); // Top Left Vertex
glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.5, 0.5 ); // Top Right Vertex
glTexCoord2f(1.0f, 0.0f); glVertex2f( 0.5, -0.5 ); // Bottom Right Vertex
glTexCoord2f(0.0f, 0.0f); glVertex2f( -0.5, -0.5 ); // Bottom Left Vertex

glEnd();
glFlush();
glFinish();
SwapBuffers( m_hDC );
}

Any idea? Please help. I know my questions may be foolish.. I don't know the basics even.. Please try to figure it out.. Thanks...

fatalcoder
07-02-2011, 02:25 AM
Any help please?

V-man
07-02-2011, 04:47 AM
You should disable polygon culling since you are rendering your poly in the wrong direction. Also, why are you calling glflush and glfinish?

http://www.opengl.org/wiki/Common_Mistakes#glFinish_and_glFlush