Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Displaying an image in OpenGL

This topic is 2080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all!


I am trying to render an image that I generate after reading it from a .asc file. I perform all the necessary transformations and then display it on the screen. The output is a ppm image. Initially I used the windows API related BitBlt function to render the image and it shows perfectly on the screen. Then I tried using OpenGL to render the same image and it doesn't.


If I use DrawPixels, it shows a black screen and if I use texture mapping it shows a partial white box.


Here are the images and the bits of code. I have worked with targa images before but this is the first time I am creating a ppm image but I do not think that is an issue as I am just trying to render the buffer which is of type char*.

This image is produced using BitBlt.


This one using OpenGL.


And here is the code of my files: Main.cpp and COpenGLRenderer.cpp:

 DrawFrameBuffer(using BitBlt)

void DrawFrameBuffer()
	HBITMAP m_bitmap;
	HDC memDC;
	memDC = CreateCompatibleDC(hDC);
	//display the current image
	char buffer[sizeof(BITMAPINFO)];
	BITMAPINFO* binfo = (BITMAPINFO*)buffer;
	binfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

	//create the bitmap
	BITMAPINFOHEADER* bih = &binfo->bmiHeader;
	bih->biBitCount = 3*8; //3 - channels
	bih->biWidth =  pApp->GetFrameBufferWidth();
	bih->biHeight = pApp->GetFrameBufferHeight();
	bih->biPlanes = 1;
	bih->biCompression = BI_RGB;
	bih->biSizeImage = 0;	//for rgb bitmaps we set it to 0

	m_bitmap = CreateDIBSection(hDC,binfo,DIB_RGB_COLORS,0,0,0);

	binfo->bmiHeader.biBitCount = 0;
	GetDIBits(memDC, m_bitmap, 0, 0, 0, binfo, DIB_RGB_COLORS);
	binfo->bmiHeader.biBitCount = 24;
	binfo->bmiHeader.biHeight = -abs(binfo->bmiHeader.biHeight); //for top-down image
	SetDIBits(memDC,m_bitmap,0, pApp->GetFrameBufferHeight(), pApp->GetFrameBuffer(), binfo, DIB_RGB_COLORS); //replace the 3rd last argument with the framebuffer

	SetStretchBltMode(hDC, COLORONCOLOR);
	RECT client;
	GetClientRect(hwnd, &client);

 DrawFrameBuffer(OpenGL) function

void COpenGLRenderer::setFrameBuffer(const char* buffer)
	m_pFrameBuffer = (char*)buffer;

	glGenTextures(1, &m_pTextureID);
	glBindTexture(GL_TEXTURE_2D, m_pTextureID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_nWindowWidth, m_nWindowHeight, 0 , GL_RGB, GL_UNSIGNED_BYTE, buffer);
	//gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,m_nWindowWidth, m_nWindowHeight,GL_RGB,GL_UNSIGNED_BYTE, buffer);

void COpenGLRenderer::drawFrameBuffer(const char* buffer)
	glBindTexture(GL_TEXTURE_2D, m_pTextureID);
		glTranslatef(0.0f, 0.0f, -10); //z translation is the last value
			glTexCoord2f(0,0); glVertex2f(0,0);
			glTexCoord2f(1,0); glVertex2f(m_nWindowWidth,0);
			glTexCoord2f(1,1); glVertex2f(m_nWindowWidth,m_nWindowHeight);
			glTexCoord2f(0,1); glVertex2f(0,m_nWindowHeight);
	glBindTexture(GL_TEXTURE_2D, 0);


	glRasterPos2i(0, 0);

	if(buffer != NULL)
		glDrawPixels(m_nWindowWidth, m_nWindowHeight, GL_RGB, GL_BYTE, buffer);

I have commented the DrawPixels technique that did not work. Can anyone tell me what might be going on? Its basically trying to render image data stored in a char* variable using different methods. One method works so I know for sure that the buffer does not contain invalid data.

Edited by RJSkywalker

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!