Jump to content

  • Log In with Google      Sign In   
  • Create Account

Displaying an image in OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 RJSkywalker   Members   -  Reputation: 156

Like
0Likes
Like

Posted 09 November 2013 - 08:18 PM

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.

screenShot1.jpg

This one using OpenGL.

screenShot2.jpg

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;
	memset(binfo,0,sizeof(BITMAPINFO));
	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);

	SelectObject(memDC,m_bitmap);
	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);
	BitBlt(hDC,0,0,pApp->GetFrameBufferWidth(),pApp->GetFrameBufferHeight(),memDC,0,0,SRCCOPY);
	
	DeleteDC(memDC);
	DeleteObject(m_bitmap);
	
}

 DrawFrameBuffer(OpenGL) function


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

	glGenTextures(1, &m_pTextureID);
	glBindTexture(GL_TEXTURE_2D, m_pTextureID);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	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)
{
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, m_pTextureID);
	
	glPushMatrix();
		glLoadIdentity();
		glTranslatef(0.0f, 0.0f, -10); //z translation is the last value
		glBegin(GL_QUADS);
			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);
		glEnd();
	glPopMatrix();
		
	glDisable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 0);
	

	/*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);

	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, 11 November 2013 - 10:26 PM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS