Click here to Skip to main content
13,900,110 members
Click here to Skip to main content
Articles » Multimedia » OpenGL » General » Downloads


51 bookmarked
Posted 7 Oct 2004

Generating Outlines in OpenGL

, 7 Oct 2004
Using multi-pass techniques to generate outlines in OpenGL.

#ifndef _CGLTEXTURE_H_
#define _CGLTEXTURE_H_

#include "cglimage.h"

/*! \brief A texture wrapper for OpenGL

\ingroup OGLToolsGroup
CGLTexture is a simple wrapper to OpenGL texture object.
It enables easy changing of texture images, 
reusing existing texture object when possible.
Call ChangeImage(&CGLImage) to create/recreate/reuse
a texture and copy CGLImage image to the texture object.
Use Bind() to bind a texture. 
Use Invalidate() before switching to a new RC.
If Invalidate() is called and then Bind() is called in a new RC,
the texture will be automaticaly recreated using a backup copy
of the image. If you are not going to use the same texture 
in multiple RC's, call ChangeImage() with second parameter 
set to 'false' so it won't make unnecessary 
backup copy of the image.

      // Handle a request to use a different image 
      // for texture.

	bChangeImage = false;
	CGLImage glImage;
	glImage.Create(m_strNewImageFilePath, GL_RGBA);
	m_tex1.ChangeImage(&glImage, false); 


Use Coordn() function to generate texture coordinates 
which point to the four corners of current subimage
inside a possibly larger texture.

		m_tex1.Coord0(); glVertex3f(-5.0, -5.0, 0.0);
		m_tex1.Coord1(); glVertex3f(-5.0, 5.0, 0.0);
		m_tex1.Coord2(); glVertex3f(5.0, 5.0, 0.0);
		m_tex1.Coord3(); glVertex3f(5.0, -5.0, 0.0);

\sa CGLImage
\sa CWGL
\author (c) W.Weyna,
class CGLTexture
	//! Default constructor
	//! Destroy textures (see glDeleteTextures)

// Attributes
	/*! \name Attributes: */
	//! returns identifier of texture
	UINT GetTexName() {ASSERT(m_bTextureObjectValid); return m_nTexName; }
	//! returns width (in pixels)	
	int	GetWidth() {ASSERT(m_bTextureObjectValid); return m_nTexWidth; }
	//! returns height (in pixels)
	int GetHeight() {ASSERT(m_bTextureObjectValid); return m_nTexHeight; }
	//! returns width (in pixels) of base image	
	int	GetImageWidth()	{ASSERT(m_bTextureObjectValid); return m_nImageWidth; }
	//! returns height (in pixels) of base image	
	int	GetImageHeight()	{ASSERT(m_bTextureObjectValid); return m_nImageHeight; }
	//! TRUE if texture is valid
	bool IsValid() {return m_bTextureObjectValid; }
	//! TRUE if texture is reused
	bool IsReused() {return m_bSubImage; }

	//! returns length in X direction (in texture coordinates)
	GLfloat GetCoordX() {return m_fCoordX; }
	//! returns length in Y direction (in texture coordinates)
	GLfloat GetCoordY() {return m_fCoordY; }

	UINT	m_nTexName;
	int		m_nTexWidth;
	int		m_nTexHeight;

	int		m_nImageWidth;
	int		m_nImageHeight;

	bool	m_bTextureObjectValid;
	bool	m_bReadyToSubimage;
	bool	m_bSubImage;

	GLfloat m_fCoordX;
	GLfloat m_fCoordY;

	CGLImage m_texSurface;      // temp surface, needed for texture object creation only
	CGLImage m_backupTexImage;  // optional backup copy of image, used to restore texture if it is invalid.
	bool m_bBackupCopyValid;

// Operations
	/*! \name Operations: */
	/*! \brief Change texture to pImage
	Use this function to supply a source image for a texture.
	See inside of the code for details.

	If bCopyImage is true (default),
	a backup copy of the image is created and if later
	CGLTexture::Invalidate() is called and then CGLTexture::Bind()
	the texture will be automaticaly recreated using a backup copy
	of the image. This is usefull for recreating the same textures 
	when you switch to a new RC. If you don't intend to do this,
	set this parameter to false to avoid alocating extra memory.
	bool ChangeImage(CGLImage* pImage, bool bCopyImage = true);
	/*! \brief Create an OpenGL texture object.
	The image for the texture is copied by OpenGL
	from m_texSurface CGLImage object. 
	If m_texSurface wasn't created
	prior to the call to CreateTextureObject() or
	the size of previously created m_texSurface differs, 
	CreateTextureObject() will create an empty, black, 
	texture object.
	After a call to CreateTextureObject() the m_texSurface
	surface is lost.
	bool CreateTextureObject(int nWidth, int nHeight, GLenum pixelFormat = GL_RGBA);
	/*! \brief Bind texture.
	  Bind OpenGL texture object.
	If texture object is invalid, recreate it
	and initialize with a backup copy of the image,
	if one is available.
	bool Bind();
	//! Invalidate this CGLTexture object and delete the attached OpenGL texture object.
	void Invalidate();

	//! Sets (0,0) tex coordinate.
	void Coord0() {glTexCoord2f(0.0, 0.0); }
	//! Sets (0,m_fCoordY) tex coordinate.
	void Coord1() {glTexCoord2f(0.0, m_fCoordY); }
	//! Sets (m_fCoordX,m_fCoordY) tex coordinate.
	void Coord2() {glTexCoord2f(m_fCoordX, m_fCoordY); }
	//! Sets (m_fCoordX,0) tex coordinate.
	void Coord3() {glTexCoord2f(m_fCoordX, 0.0); }
// Helpers
	/*! \name Helpers: */
	DWORD AlignToPow2(DWORD dwValue);



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.


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


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.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 7 Oct 2004
Article Copyright 2004 by Dave Handley
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid