Click here to Skip to main content
14,218,418 members

Writing a Platform and GUI Toolkit Independent OpenGL Class

Rate this:
4.95 (30 votes)
Please Sign up or sign in to vote.
4.95 (30 votes)
1 Nov 2010CPOL
Article showing how to write OS and GUI toolkit portable OpenGL view class
basecode
linux
MultiGL
GLView
GLViewGTKApp
.project
AUTHORS
autom4te.cache
output.0
requests
traces.0
ChangeLog
config.guess
config.h.in
config.sub
configure
configure.in
COPYING
depcomp
glviewgtkapp.gladep
INSTALL
install-sh
Makefile.am
Makefile.in
missing
mkinstalldirs
NEWS
po
ChangeLog
Makefile.in.in
POTFILES.in
README
src
Makefile.am
Makefile.in
stamp-h.in
GLViewMotifApp
.project
autom4te.cache
output.0
requests
traces.0
config.h.in
configure
configure.ac
depcomp
install-sh
Makefile.am
Makefile.in
missing
src
Makefile.am
Makefile.in
GLViewQtApp
.project
GLViewQtApp.ico
GLViewQtApp.pro.user
glviewqtapp.qrc
Makefile
Resources
.project
autom4te.cache
output.0
requests
traces.0
config.guess
config.h.in
config.sub
configure
configure.ac
depcomp
install-sh
m4
Makefile.am
Makefile.in
missing
src
Makefile.am
Makefile.in
win32
MultiGL
GLView
GLViewGTKApp
AUTHORS
ChangeLog
configure.in
devcpp
glviewgtkapp.dev
glviewgtkapp.gladep
Makefile.am
msvc
glviewgtkapp.suo
glviewgtkapp.vcproj
NEWS
po
ChangeLog
POTFILES.in
README
src
Makefile.am
stamp-h.in
GLViewMFCDlgApp
GLViewMFCDlgApp.aps
GLViewMFCDlgApp.vcproj
res
GLViewMFCDlgApp.ico
GLViewMFCMDIApp
GLViewMFCMDIApp.vcproj
res
buttons.bmp
filelarge.bmp
filesmall.bmp
GLViewMFCMDIApp.ico
GLViewMFCMDIAppDoc.ico
main.bmp
Toolbar.bmp
Toolbar256.bmp
writelarge.bmp
writesmall.bmp
GLViewQtApp
GLViewQtApp
GLViewQtApp.ico
glviewqtapp.qrc
GLViewQtApp.vcproj
Makefile
moc_glviewqtapp.o
Resources
GLView.cpp.bak
GLView.suo
GLView.vcproj
MultiGL.suo
// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the GLVIEW_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// GLVIEW_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
// We add the condition WIN32 for UNIX/Linux portability
#ifdef GLVIEW_EXPORTS
	#ifdef WIN32
		#define GLVIEW_API __declspec(dllexport)
	#else
		#define GLVIEW_API
	#endif
#else
	#ifdef WIN32
		#define GLVIEW_API __declspec(dllimport)
	#else
		#define GLVIEW_API
	#endif
#endif


#ifdef WIN32
#include <windows.h> // required by OpenGL and must be included before gl.h
#include <TCHAR.H>  // for unicode support
#endif

#include <GL/gl.h>
#include <GL/glu.h>

#ifndef WIN32
#include <GL/glx.h>
#endif

// This class is exported from the GLView.dll
class GLVIEW_API CGLView 
{
public:
	CGLView(void);

#ifdef WIN32
	void SetWindow(HWND ihWnd);
#else
	void SetWindow(Display* pDisp, const Window& wnd);
#endif
	bool SetupGLContext(bool iSetPixelFormat);
	void Resize(unsigned short iWidth, unsigned short iHeight);
	void RenderScene(void);
	void Refresh( void);

private:
#ifdef WIN32
	int SetPixelFormat(HDC hdc);
#endif
	void Setup3DEnvironment();
	void DrawGradientBackground();
	void InitDisplayLists();
	void DoAntialiasing();

private:

#ifdef WIN32
	HWND m_hWnd;
	HDC  m_hDC;
	HGLRC m_hGLRC;
#else
	Display* m_pXDisplay;
	int m_iXScreen;
	Window m_iXWindow;
	GLXContext m_hGLContext;
	XVisualInfo *m_hVisual;
	Colormap m_ColMap;
#endif

	GLdouble nRange;	
	GLdouble win_xmax,
		win_xmin,
		win_ymax,
		win_ymin,
		winz,
		winH,
		winW;

	bool m_bGradientBg;
	bool m_bAntiAlias;

	GLubyte bgTopR;
	GLubyte bgTopG;
	GLubyte bgTopB;
	GLubyte bgBotR;
	GLubyte bgBotG;
	GLubyte bgBotB;

	GLfloat ambiLight[4];
	GLfloat diffLight[4];
	GLfloat specLight[4];

};



void GLVIEW_API drawTorus(int numMajor, int numMinor, float majorRadius, float minorRadius, bool bShaded = true);


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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sharjith
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Stats

72.6K views
6.9K downloads
88 bookmarked