Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » OpenGL » General » Downloads
 
Add your own
alternative version
Go to top

Generating Outlines in OpenGL

, 7 Oct 2004
Using multi-pass techniques to generate outlines in OpenGL.
#include "gl/gl.h"
#include "CRGBSurface.h"

#ifndef _CGLIMAGE_H_
#define _CGLIMAGE_H_

typedef GLvoid* GL_PTR_TO_VOID;
typedef BYTE*	PTR_TO_BYTE;

/*! \brief An rendering context for GL.

\ingroup OGLToolsGroup
 CGLImage class adds red and blue component swaping 
 to some CRGBSurface::Create() methods.

 A GDI DIB section object created by CGLImage,
 may be used directly as a source image 
 for OpenGL texture object, as OpenGL or GDI rendering surface,
 or for a direct bit manipulations.
  

Examples:
\code
	CGLImage glImage;
	glImage.Create(10, 10, GL_RGBA);  // create empty, black image
	glImage.Create(&CBitmapObject);	  // create from DDB bitmap GDI object
	glImage.Create("D:/TestImg/testimg.bmp", GL_RGBA); // create from file DIB 
	glImage.Render(0, 100);   // render image                               
	GLvoid* ptr = glImage.GetPixels();  // get save pointer for direct bit manipulation
	glTexImage2D(...., glImage); // use GL_PTR_TO_VOID() conversion operator to get save pointer
\endcode
\sa CRGBSurface
\sa CWGL
\author (c) W.Weyna 'Voytec', http://shiptech.tuniv.szczecin.pl/~weyna/OpenGL/
*/
class CGLImage : public CRGBSurface
{
// Attributes
public:
	/*! \name Attributes: */
	//@{
	/*! \brief Get Frame Pixels

	Flushes OpenGL calls and returns a pointer to the frame buffer.
	\la CRGB::GetPixels
	*/
	GLvoid* GetPixels() {glFlush(); return (GLvoid*)CRGBSurface::GetPixels();}
	/*! \brief Pixel format
	
	Returns pixel format :
	<ul>
	<li> GL_RGB 24 bits.
	<li> GL_RGBA 32 bits
	</ul>
	It produces an assert otherwize.
	
	\sa GetImagePixelFormatName
	\sa CWGL::SetPixelFormat
	*/
	int GetImagePixelFormat();
	/*! \brief Pixel format
	
	Returns pixel format :
	<ul>
	<li> "GL_RGB" 24 bits.
	<li> "GL_RGBA" 32 bits
	</ul>
	It produces an assert otherwize.
	\sa GetImagePixelFormatName
	\sa CWGL::SetPixelFormat
	*/
	const char* GetImagePixelFormatName();
	//@}
// Convertions
public:
	/*! \name Conversions: */
	//@{
	//! returns GetPixels()
	operator GL_PTR_TO_VOID() {return GetPixels(); }
	//! returns (BYTE*)GetPixels()
	operator PTR_TO_BYTE() {return (BYTE*)GetPixels(); }
	//@}

// Operations
public:  
	/*! \name Operations:
	*/
	//@{
	//! Create DIB using CRGBSurface::Create(CRGBSurface* const)
	bool Create(CRGBSurface* const pSourceSurface)
		{return CRGBSurface::Create(pSourceSurface); }  // must be explisitly declared because of ambiguity with Create(const HBITMAP hBitmap ...
	/*! \brief  Create cx x cy DIB

	\param cx width of DIB
	\param cy height of DIB
	\param nBitsPerPixel nb of pits per pixels (default is 24). RGBA image need 32.
	\sa CRGBSurface::Create(int, int, int)
	*/
	bool Create(int cx, int cy, int nBitsPerPixel = 24)
		{return CRGBSurface::Create(cx, cy, GLPFtoBPP(nBitsPerPixel));}

	//! Create DIB using CRGBSurface::Create(HBITMAP,int,CDC*) then swap Red and Blue.
	bool Create(const HBITMAP hBitmap, int nBitsPerPixel = 24, CDC* pDC = NULL)
		{bool bRetVal = CRGBSurface::Create(hBitmap, GLPFtoBPP(nBitsPerPixel), pDC); SwapRB(); return bRetVal;}
	//! Create DIB using CRGBSurface::Create(CBitmap*,int,CDC*) then swap Red and Blue.
	bool Create(CBitmap* pBitmap, int nBitsPerPixel = 24, CDC* pDC = NULL) 
		{bool bRetVal = CRGBSurface::Create(pBitmap, GLPFtoBPP(nBitsPerPixel), pDC); SwapRB(); return bRetVal;}	
	//! Create DIB using CRGBSurface::Create(const CBitmap&,int,CDC*) then swap Red and Blue.
	bool Create(const CBitmap& bitmap, int nBitsPerPixel = 24, CDC* pDC = NULL)
		{bool bRetVal = CRGBSurface::Create(bitmap, GLPFtoBPP(nBitsPerPixel), pDC); SwapRB(); return bRetVal;}
	//! Create DIB using CRGBSurface::Create(const char*,int) then swap Red and Blue.	
	bool Create(const CString& strPath, int nBitsPerPixel = 24)
		{bool bRetVal = CRGBSurface::Create(strPath, GLPFtoBPP(nBitsPerPixel)); SwapRB(); return bRetVal;}

	//! Wraper to glDrawPixels().
	void Render(int x, int y, int z = 0);

	//! Sets the pixel storage mode to GL_UNPACK_ALIGNMENT.
	void PixelStore() { glPixelStorei(GL_UNPACK_ALIGNMENT, 4); }

	/*! \brief Swap red and blue bytes in all pixels of DIB section.
	*/
	void SwapRB();
	/*! \brief Convert pixel mode from RGB to RGBA
	
	Convert from OpenGL pixel format name GL_RGB or GL_RGBA
	to a number of bits per pixel.
	If bits per pixel are passed instead of pixel format name,
	check if this number is valid for OpenGL image (24 bits or 32 bits)
	 and return valid value;
	*/
	int GLPFtoBPP(const int nGLPForBPP);
	//@}
public:
	/*! \name Multi-Threading
	*/
	//@{
	//! Locks the object on this thread
	BOOL Lock();
	//! Unlocks the object on this thread
	BOOL Unlock();
	//! returns TRUE if locked
	BOOL IsLocked() const;
	//@}
protected:
	CMutex* m_pThreadSynchro;
	CSingleLock* m_pLock;
};

#endif

////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Dave Handley
Web Developer
United Kingdom United Kingdom
I started programming on 8 bit machines as a teenager, writing my first compiled programming language before I was 16. I went on to study Engineering and Computer Science at Oxford University, getting a first and the University Prize for the best results in Computer Science. Since then I have worked in a variety of roles, involving systems management and development management on a wide variety of platforms. Now I manage a software development company producing CAD software for Windows using C++.
 
My 3 favourite reference books are: Design Patterns, Gamma et al; The C++ Standard Library, Josuttis; and Computer Graphics, Foley et al.
 
Outside computers, I am also the drummer in a band, The Unbelievers and we have just released our first album. I am a pretty good juggler and close up magician, and in my more insane past, I have cycled from Spain to Eastern Turkey, and cycled across the Namib desert.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 7 Oct 2004
Article Copyright 2004 by Dave Handley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid