<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>



<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document"><title>Glut Object</title></head><body>



<p align="left"><font color="#ff0000" size="6"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></span>
<b>&nbsp;<span lang="en-ca">&nbsp;&nbsp; </span></b></font><span lang="en-ca">
<font color="#ff0000" size="6"><b>A Half-finished OpenGL Object Model</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <p>
      <span lang="en-ca">I spent almost three days including Thanks-giving Day 
		to try to establish an object-oriented model for those primitives and 
		commonly-used model in OpenGL. However, the result is very 
		disappointing. At some point, I have to stop this futile exploration as 
		deadline of assignment is approaching. Then I realized one of the major 
		reason why this way is not so popular. It is efficiency. When talking 
		about efficiency, there are two aspect regarding it. One is the 
		performance efficiency and the other is the coding efficiency. Students 
		from Engineering department can enumerate various reasons against OOP 
		when performance is highly required. Even students want to argue that 
		OOP helps improving coding efficiency, they have to realize that OpenGL 
		transformation must be done at a proper order and sometimes lack of 
		flexibility in OOP will eliminate all those benefits brought by class 
		model. </span>
	</p><p>
      <span lang="en-ca">The idea of my small project is straight forward and it 
		is different from Dr. Grogono who tries to wrap those basic primitives 
		in OpenGL which is handy and sound. My design concentrate on all higher 
		level object developed by programmer and try to find out all the common 
		property and method of them. Then define a base class which will be 
		inherited by all models and also define most commonly operations of 
		them. i.e. Every model object has a property of coordinate, color, 
		direction which is the axis of the object, occupying dimension which is 
		the enclosing cube created by scaling on three dimensional direction. 
		And all objects have to implement an abstract method display which will 
		be called by a global callback. However, within this function programmer 
		doesn't have to think about rotation, translate, scaling operations 
		because base class already take care of that with the property of 
		derived classes such as coordinate, direction, dimension volume etc. 
		Only the most basic simple standard drawing function need to be defined 
		by programmer. And an object manager class handles all creation, 
		storage, viewing jobs of all objects. This sounds quite nice to me and 
		one immediate benefit to this system is that I can used it in my project 
		design by creating a script system that art user can create their 3D 
		world by script file which will be read by my glut Object manager class. 
		However, some viewing problem puzzled me for quite some time and I 
		stopped there. Just in case I need them again, I store a half-finished 
		version here. </span></p><p>
      　</p></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<p align="left"><span lang="en-ca">In openGL, those error-prone transformation 
is tedious and boring. Why should I write those similar codes a thousand times?</span></p>
<div align="left">
  <p align="left">　</p></div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font color="#ff0000" size="5"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
	<pre><span lang="en-ca">I think I already explained very clearly about the purpose of this simple program.</span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><a name="opengl"></a>Just in case you are not familiar with OpenGL and want a test-drive, you need to install an OpenGL programming environment in your VC++ by </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">copying "glut.h" file in your "/include" directory and a compiling library "glut32.lib" must be copied in your "lib" directory. The run-time</span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">"dll", "glut32.dll" must be copied to your "system" or "system32" director in your "windows" directory. Once created with a "win console" </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">project in VC++, go to the "setting" of "project" and "linking" tab to add "glut32.lib". </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">(This sounds trivial for programmers, but it is worthwhile for beginners.:) And of course I can use a VC++ key word: </span></pre>
	<pre><code>#pragma comment( lib, "<span lang="en-ca">glut32</span>")<span lang="en-ca"> to ask linker to search glut32.lib automatically, still those .h .lib and .dll file needs to be copied by </span></code></pre>
	<pre><code><span lang="en-ca">programmer.</span>
</code></pre>
	<pre><span lang="en-ca">
</span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font color="#ff0000" size="5"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">The next step?</span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font color="#ff0000" size="5"><b>File listing</b></font></span></pre>
</div><span style="font-family: monospace;"><span style="font-weight: bold;"><br>
<br>
<br>
1. glutObject.h<br>
<br>
2. glutObject.cpp<br>
<br>
3. glutObjects.h<br>
<br>
4. glutObjects.cpp<br>
<br>
5. main.cpp<br>
<br>
file name: glutObject.h<br>
<br>
#ifndef&nbsp; GLUTOBJECT_H<br>
#define GLUTOBJECT_H<br>
<br>
#include &lt;GL/glut.h&gt;<br>
//#include "glutobjects.h"<br>
#include &lt;cstdlib&gt;<br>
//#define NICKSPACE_BEGIN namespace NICKSPACE {<br>
//#define NICKSPACE_END }<br>
<br>
//NICKSPACE_BEGIN<br>
const GLfloat PI=3.14159;<br>
const GLint MaximumObjectNumber=50;<br>
<br>
<br>
//typedef GLfloat[3] GlutVector;<br>
<br>
class GlutObjectList;<br>
<br>
extern GlutObjectList* gObjectListPtr;<br>
extern enum AdjustViewState;<br>
//extern enum PerspetiveStyle;<br>
const GLfloat DefaultPosition[3]={0,0,0};<br>
const GLfloat DefaultDirection[3]={0,0,0};<br>
const GLfloat DefaultColor[3]={1,0,0};<br>
const GLfloat DefaultVolume[3]={1,1,1};<br>
const GLfloat DefaultBackColor[3]={1,1,1};<br>
const bool DefaultDrawingStyle=false;//this means solid instead of mesh<br>
const GLfloat DefaultPerspectAngle=70;<br>
const GLfloat DefaultViewVolume[3]={800, 600, 800};<br>
const GLfloat DefaultNearPlane=0;<br>
const GLfloat DefaultWindowWidth=800;<br>
const GLfloat DefaultWindowHeight=600;<br>
const GLint DefaultWindowPosition_x=0;<br>
const GLint DefaultWindowPosition_y=0;<br>
//void display();<br>
<br>
<br>
<br>
class GlutObject<br>
{<br>
&nbsp;&nbsp;&nbsp; friend class GlutObjectList;<br>
&nbsp;&nbsp;&nbsp; friend &nbsp;&nbsp;&nbsp; void arrayCopy(GLfloat tgt[], const GLfloat src[]);<br>
public:<br>
&nbsp;&nbsp;&nbsp; void setPosition(GLfloat x, GLfloat y, GLfloat z);<br>
&nbsp;&nbsp;&nbsp; void setPosition(GLfloat* array);<br>
&nbsp;&nbsp;&nbsp; void setDirection(GLfloat x, GLfloat y, GLfloat z);<br>
&nbsp;&nbsp;&nbsp; void setDirection(GLfloat* array);<br>
&nbsp;&nbsp;&nbsp; void setVolume(GLfloat w, GLfloat h, GLfloat l);<br>
&nbsp;&nbsp;&nbsp; void setColor(GLfloat r, GLfloat g, GLfloat b);<br>
&nbsp;&nbsp;&nbsp; void doRotation();<br>
&nbsp;&nbsp;&nbsp; GlutObject();<br>
&nbsp;&nbsp;&nbsp; //virtual void keyboardFunction(unsigned char key, GLint x,&nbsp; GLint y);<br>
&nbsp;&nbsp;&nbsp; void draw();<br>
&nbsp;&nbsp;&nbsp; virtual void display()=0;<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; void setClipRatio(GLfloat ratio);<br>
&nbsp;&nbsp;&nbsp; bool bDrawingMesh;<br>
protected:<br>
&nbsp;&nbsp;&nbsp; GLfloat position[3];<br>
&nbsp;&nbsp;&nbsp; GLfloat direction[3];<br>
&nbsp;&nbsp;&nbsp; GLfloat color[3];<br>
&nbsp;&nbsp;&nbsp; GLfloat volume[3];<br>
&nbsp;&nbsp;&nbsp; GLdouble clipPlane[4];<br>
&nbsp;&nbsp;&nbsp; GLfloat clipRatio;<br>
&nbsp;&nbsp;&nbsp; bool bDisableClip;<br>
&nbsp;&nbsp;&nbsp; bool bVisible;<br>
&nbsp;&nbsp;&nbsp; void setClipPlane();<br>
&nbsp;&nbsp;&nbsp; void normalizeDirection();<br>
};<br>
<br>
<br>
class GlutObjectList<br>
{<br>
&nbsp;&nbsp;&nbsp; friend class GlutObject;<br>
&nbsp;&nbsp;&nbsp; friend void displayCallback();<br>
&nbsp;&nbsp;&nbsp; friend void reshapeCallback(int w, int h);<br>
&nbsp;&nbsp;&nbsp; friend void keyboardCallback(unsigned char key, int x, int y);<br>
&nbsp;&nbsp;&nbsp; friend void arrayCopy(GLfloat tgt[], const GLfloat src[]);<br>
&nbsp;&nbsp;&nbsp; friend void run();<br>
&nbsp;&nbsp;&nbsp; friend void init();<br>
public:<br>
&nbsp;&nbsp;&nbsp; GlutObjectList();<br>
//&nbsp;&nbsp;&nbsp; void keyboardFunction(unsigned char key, GLint x, GLint y);<br>
&nbsp;&nbsp;&nbsp; <br>
<br>
protected:<br>
&nbsp;&nbsp;&nbsp; GLint objectCount;<br>
&nbsp;&nbsp;&nbsp; GLfloat perspectAngle;<br>
&nbsp;&nbsp;&nbsp; //PerspetiveStyle ePerspective;<br>
&nbsp;&nbsp;&nbsp; bool bPerspective;<br>
&nbsp;&nbsp;&nbsp; bool bDrawAxis;<br>
<br>
&nbsp;&nbsp;&nbsp; AdjustViewState eAdjustView;<br>
&nbsp;&nbsp;&nbsp; GLfloat viewVolume[3];<br>
&nbsp;&nbsp;&nbsp; GLfloat windowWidth, windowHeight;<br>
&nbsp;&nbsp;&nbsp; GLfloat viewPortWidth, viewPortHeight;<br>
&nbsp;&nbsp;&nbsp; int windowPos_x, windowPos_y;<br>
&nbsp;&nbsp;&nbsp; GLfloat nearPlane;<br>
&nbsp;&nbsp;&nbsp; GLfloat backColor[3];<br>
&nbsp;&nbsp;&nbsp; GLfloat foreColor[3];<br>
&nbsp;&nbsp;&nbsp; GlutObject*&nbsp; objectList[MaximumObjectNumber];<br>
&nbsp;&nbsp;&nbsp; void reshapeCallback(int w, int h);<br>
&nbsp;&nbsp;&nbsp; void displayCallback();<br>
&nbsp;&nbsp;&nbsp; void keyboardCallback(unsigned char key, int x, int y);<br>
&nbsp;&nbsp;&nbsp; void addNewObject(GlutObject* ptr);<br>
&nbsp;&nbsp;&nbsp; //void draw3DAxis(GLfloat width, GLfloat height);<br>
&nbsp;&nbsp;&nbsp; void setDisplayMode();<br>
&nbsp;&nbsp;&nbsp; void adjustView();<br>
&nbsp;&nbsp;&nbsp; void init();<br>
&nbsp;&nbsp;&nbsp; void run();<br>
&nbsp;&nbsp;&nbsp; void drawAll();<br>
};<br>
//NICKSPACE_END<br>
<br>
#endif<br>
<br>
　<br>
<br>
file name: glutObject.cpp<br>
<br>
#include &lt;GL/glut.h&gt;<br>
#include &lt;cstdlib&gt;<br>
#include &lt;cmath&gt;<br>
#include "glutObject.h"<br>
<br>
<br>
//NICKSPACE_BEGIN<br>
const GLint AdjustScale=2;<br>
GlutObjectList* gObjectListPtr=NULL;<br>
//enum PerspetiveStyle{PERSPECTIVE, ORTHOGONAL};<br>
enum AdjustViewState{ADV_KEEPSHAPE=0, ADV_ENLARGE=AdjustScale, ADV_REDUCE=-AdjustScale};<br>
<br>
GLdouble STDPlane[3][3]=<br>
{<br>
&nbsp;&nbsp;&nbsp; {1,0,0},<br>
&nbsp;&nbsp;&nbsp; {0,1,0},<br>
&nbsp;&nbsp;&nbsp; {0,0,1}<br>
};<br>
<br>
const bool DefaultPerspectiveStyle=true;<br>
<br>
<br>
void GlutObject::setColor(GLfloat r, GLfloat g, GLfloat b)<br>
{<br>
&nbsp;&nbsp;&nbsp; color[0]=r;<br>
&nbsp;&nbsp;&nbsp; color[1]=g;<br>
&nbsp;&nbsp;&nbsp; color[2]=b;<br>
}<br>
<br>
<br>
void GlutObject::setPosition(GLfloat x, GLfloat y, GLfloat z)<br>
{<br>
&nbsp;&nbsp;&nbsp; position[0]=x;<br>
&nbsp;&nbsp;&nbsp; position[1]=y;<br>
&nbsp;&nbsp;&nbsp; position[2]=z;<br>
&nbsp;&nbsp;&nbsp; //setClipPlane();<br>
}<br>
<br>
void GlutObject::setPosition(GLfloat* array)<br>
{<br>
&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;3; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; position[i]=array[i];<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; //setClipPlane();<br>
}<br>
<br>
void GlutObject::setDirection(GLfloat x, GLfloat y, GLfloat z)<br>
{<br>
&nbsp;&nbsp;&nbsp; direction[0]=x;<br>
&nbsp;&nbsp;&nbsp; direction[1]=y;<br>
&nbsp;&nbsp;&nbsp; direction[2]=z;<br>
}<br>
<br>
void GlutObject::setDirection(GLfloat* array)<br>
{<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;3; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; direction[i]=array[i];<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
void GlutObject::setVolume(GLfloat w, GLfloat h, GLfloat l)<br>
{<br>
&nbsp;&nbsp;&nbsp; volume[0]=w;<br>
&nbsp;&nbsp;&nbsp; volume[1]=h;<br>
&nbsp;&nbsp;&nbsp; volume[2]=l;<br>
}<br>
<br>
<br>
GlutObject::GlutObject()<br>
{<br>
&nbsp;&nbsp;&nbsp; if (gObjectListPtr==NULL)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gObjectListPtr=new GlutObjectList;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; gObjectListPtr-&gt;addNewObject(this);<br>
&nbsp;&nbsp;&nbsp; arrayCopy(direction, DefaultDirection);<br>
&nbsp;&nbsp;&nbsp; arrayCopy(volume, DefaultVolume);<br>
&nbsp;&nbsp;&nbsp; arrayCopy(color, DefaultColor);<br>
&nbsp;&nbsp;&nbsp; arrayCopy(position, DefaultPosition);<br>
&nbsp;&nbsp;&nbsp; bDrawingMesh=DefaultDrawingStyle;<br>
&nbsp;&nbsp;&nbsp; clipRatio=0.0;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; setClipPlane();<br>
&nbsp;&nbsp;&nbsp; bDisableClip=true;<br>
&nbsp;&nbsp;&nbsp; bVisible=true;<br>
}<br>
<br>
void arrayCopy(GLfloat tgt[], const GLfloat src[])<br>
{<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;3; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tgt[i]=src[i];<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void GlutObject::normalizeDirection()<br>
{<br>
&nbsp;&nbsp;&nbsp; GLfloat temp;<br>
&nbsp;&nbsp;&nbsp; temp=sqrt(direction[0]*direction[0]+direction[1]*direction[1]+<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; direction[2]*direction[2]);<br>
&nbsp;&nbsp;&nbsp; direction[0]/=temp;<br>
&nbsp;&nbsp;&nbsp; direction[1]/=temp;<br>
&nbsp;&nbsp;&nbsp; direction[2]/=temp;<br>
}<br>
<br>
<br>
void GlutObject::doRotation()<br>
{<br>
/*<br>
&nbsp;&nbsp;&nbsp; GLfloat anglex, angley;<br>
&nbsp;&nbsp;&nbsp; //GLfloat d;<br>
&nbsp;&nbsp;&nbsp; //normalizeDirection();<br>
&nbsp;&nbsp;&nbsp; anglex=atan2(direction[1], direction[2])*360/2/PI;<br>
&nbsp;&nbsp;&nbsp; //d=sqrt(direction[1]*direction[1]+direction[0]*direction[0]);<br>
&nbsp;&nbsp;&nbsp; angley=atan2(direction[0], direction[2])*360/2/PI;<br>
&nbsp;&nbsp;&nbsp; glRotatef(anglex, 1,0,0);&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; glRotatef(angley, 0,1,0);<br>
&nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;3; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (direction[i]!=0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glRotatef(direction[i], STDPlane[i][0], STDPlane[i][1],STDPlane[i][2]);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
<br>
void GlutObject::setClipRatio(GLfloat ratio)<br>
{<br>
&nbsp;&nbsp;&nbsp; clipPlane[3]=-ratio/2;<br>
}<br>
<br>
void GlutObject::setClipPlane()<br>
{<br>
&nbsp;&nbsp;&nbsp; //this is a naive clip plane setting, which is xz plane <br>
&nbsp;&nbsp;&nbsp; //at position<br>
&nbsp;&nbsp;&nbsp; clipPlane[0]=clipPlane[2]=0;<br>
&nbsp;&nbsp;&nbsp; clipPlane[1]=1;<br>
&nbsp;&nbsp;&nbsp; clipPlane[3]=0;<br>
}<br>
<br>
//no rotatations!!!<br>
void GlutObject::draw()<br>
{<br>
&nbsp;&nbsp;&nbsp; glPushMatrix();<br>
&nbsp;&nbsp;&nbsp; glLoadIdentity();<br>
&nbsp;&nbsp;&nbsp; glColor3f(color[0], color[1], color[2]);<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; glTranslatef(position[0], position[1], position[2]);<br>
&nbsp;&nbsp;&nbsp; glScalef(volume[0], volume[1], volume[2]);<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(-position[0], -position[1], -position[2]);<br>
&nbsp;&nbsp;&nbsp; doRotation();<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(-position[0], -position[1], -position[2]);<br>
<br>
&nbsp;&nbsp;&nbsp; if (!bDisableClip)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glClipPlane (GL_CLIP_PLANE0, clipPlane);&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glEnable (GL_CLIP_PLANE0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; display();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glDisable(GL_CLIP_PLANE0);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; display();<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(-position[0], -position[1], -position[2]);<br>
&nbsp;&nbsp;&nbsp; glPopMatrix();<br>
}<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
<br>
<br>
//GlutObjectList<br>
<br>
void reshapeCallback(int w, int h)<br>
{<br>
&nbsp;&nbsp;&nbsp; gObjectListPtr-&gt;reshapeCallback(w, h);<br>
}<br>
<br>
void displayCallback()<br>
{<br>
&nbsp;&nbsp;&nbsp; gObjectListPtr-&gt;displayCallback();<br>
}<br>
<br>
void keyboardCallback(unsigned char key, int x, int y)<br>
{<br>
&nbsp;&nbsp;&nbsp; gObjectListPtr-&gt;keyboardCallback(key, x, y);<br>
}<br>
<br>
void init()<br>
{<br>
&nbsp;&nbsp;&nbsp; gObjectListPtr-&gt;init();<br>
}<br>
<br>
void run()<br>
{<br>
&nbsp;&nbsp;&nbsp; gObjectListPtr-&gt;run();<br>
}<br>
<br>
void GlutObjectList::addNewObject(GlutObject* ptr)<br>
{<br>
&nbsp;&nbsp;&nbsp; objectList[objectCount++]=ptr;<br>
}<br>
<br>
GlutObjectList::GlutObjectList()<br>
{<br>
&nbsp;&nbsp;&nbsp; objectCount=0;<br>
&nbsp;&nbsp;&nbsp; arrayCopy(backColor, DefaultBackColor);<br>
&nbsp;&nbsp;&nbsp; arrayCopy(viewVolume, DefaultViewVolume);<br>
&nbsp;&nbsp;&nbsp; nearPlane=DefaultNearPlane;<br>
&nbsp;&nbsp;&nbsp; perspectAngle=DefaultPerspectAngle;<br>
&nbsp;&nbsp;&nbsp; windowWidth=DefaultWindowWidth;<br>
&nbsp;&nbsp;&nbsp; windowHeight=DefaultWindowHeight;<br>
&nbsp;&nbsp;&nbsp; windowPos_x=DefaultWindowPosition_x;<br>
&nbsp;&nbsp;&nbsp; windowPos_y=DefaultWindowPosition_y;<br>
&nbsp;&nbsp;&nbsp; viewPortWidth=windowWidth;<br>
&nbsp;&nbsp;&nbsp; viewPortHeight=windowHeight;<br>
&nbsp;&nbsp;&nbsp; //ePerspective=DefaultPerspectiveStyle;<br>
&nbsp;&nbsp;&nbsp; bPerspective=DefaultPerspectiveStyle;<br>
&nbsp;&nbsp;&nbsp; eAdjustView=ADV_KEEPSHAPE;<br>
&nbsp;&nbsp;&nbsp; setDisplayMode();<br>
&nbsp;&nbsp;&nbsp; bDrawAxis=true;<br>
}<br>
<br>
void GlutObjectList::init()<br>
{<br>
<br>
}<br>
<br>
<br>
<br>
<br>
void GlutObjectList::keyboardCallback(unsigned char key, int x, int y)<br>
{<br>
&nbsp;&nbsp;&nbsp; int i;<br>
&nbsp;&nbsp;&nbsp; switch(key)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; case 'm':<br>
&nbsp;&nbsp;&nbsp; case 'M':<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i=0; i&lt;objectCount; i++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objectList[i]-&gt;bDrawingMesh=!objectList[i]-&gt;bDrawingMesh;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; case 'a':<br>
&nbsp;&nbsp;&nbsp; case 'A':<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; bDrawAxis=!bDrawAxis;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; case 'p':<br>
&nbsp;&nbsp;&nbsp; case 'P':<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; bPerspective=!bPerspective;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //glClear(GL_COLOR_BUFFER_BIT);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; setDisplayMode();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //glFlush();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; case 's':<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eAdjustView=ADV_REDUCE;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; adjustView();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; case 'S':<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eAdjustView=ADV_ENLARGE;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; adjustView();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //THIS is only useful for debugging<br>
&nbsp;&nbsp;&nbsp; case 'c':<br>
&nbsp;&nbsp;&nbsp; case 'C':<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i=0; i&lt;objectCount; i++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objectList[i]-&gt;clipRatio+=0.1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objectList[i]-&gt;setClipRatio(objectList[i]-&gt;clipRatio);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; case 27:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; default:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; glutPostRedisplay();<br>
<br>
}<br>
<br>
<br>
void GlutObjectList::displayCallback()<br>
{<br>
&nbsp;&nbsp;&nbsp; glClear(GL_COLOR_BUFFER_BIT);<br>
<br>
&nbsp;&nbsp;&nbsp; drawAll();<br>
&nbsp;&nbsp;&nbsp; glutPostRedisplay();<br>
}<br>
<br>
void GlutObjectList::setDisplayMode()<br>
{<br>
&nbsp;&nbsp;&nbsp; glMatrixMode(GL_PROJECTION);<br>
&nbsp;&nbsp;&nbsp; glLoadIdentity();<br>
&nbsp;&nbsp;&nbsp; if(bPerspective)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gluPerspective(perspectAngle, viewVolume[0]/viewVolume[1], nearPlane, <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nearPlane+viewVolume[2]);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glOrtho(-viewVolume[0]/2, viewVolume[0]/2, -viewVolume[1]/2, viewVolume[1]/2, <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nearPlane, nearPlane+viewVolume[2]);<br>
&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Change the
view volume to maintain the aspect ratio wrt to viewport.<br>
<br>
&nbsp;&nbsp;&nbsp; glMatrixMode(GL_MODELVIEW);<br>
}<br>
<br>
void GlutObjectList::adjustView()<br>
{<br>
&nbsp;&nbsp;&nbsp; GLfloat ratio;<br>
&nbsp;&nbsp;&nbsp; ratio=viewVolume[0]/viewVolume[1];<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; if (eAdjustView==ADV_KEEPSHAPE)<br>
&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (windowWidth/windowHeight&gt;ratio)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //too wider<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; viewPortWidth= viewPortHeight*ratio;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //become thinner<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; viewPortHeight=viewPortWidth/ratio;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (bPerspective)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; perspectAngle-=eAdjustView;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; setDisplayMode();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; viewPortHeight+=eAdjustView;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; viewPortWidth+=eAdjustView*ratio;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
glViewport((windowWidth-viewPortWidth)/2,
(windowHeight-viewPortHeight)/2,<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; (windowWidth+viewPortWidth)/2,
(windowHeight+viewPortHeight)/2);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //glViewport(0, 0, viewPortWidth, viewPortHeight);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eAdjustView=ADV_KEEPSHAPE;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
<br>
void GlutObjectList::reshapeCallback(int w, int h)<br>
{<br>
&nbsp;&nbsp;&nbsp; windowWidth=w;<br>
&nbsp;&nbsp;&nbsp; windowHeight=h;<br>
&nbsp;&nbsp;&nbsp; adjustView();<br>
<br>
&nbsp;&nbsp;&nbsp; glViewport((windowWidth-viewPortWidth)/2, (windowHeight-viewPortHeight)/2,<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; (windowWidth+viewPortWidth)/2,
(windowHeight+viewPortHeight)/2);<br>
<br>
&nbsp;&nbsp;&nbsp; //glViewport(0, 0, viewPortWidth, viewPortHeight);<br>
<br>
&nbsp;&nbsp;&nbsp; adjustView();<br>
&nbsp;&nbsp;&nbsp; setDisplayMode();<br>
<br>
&nbsp;&nbsp;&nbsp; glMatrixMode(GL_MODELVIEW);<br>
&nbsp;&nbsp;&nbsp; glutPostRedisplay();<br>
}<br>
<br>
void GlutObjectList::drawAll()<br>
{<br>
&nbsp;&nbsp;&nbsp; glMatrixMode(GL_MODELVIEW);<br>
&nbsp;&nbsp;&nbsp; glLoadIdentity();<br>
&nbsp;&nbsp;&nbsp; glTranslatef(0, 0, -50);<br>
&nbsp;&nbsp;&nbsp; /*<br>
&nbsp;&nbsp;&nbsp; if (bDrawAxis)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; axis.draw();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp; glTranslatef(0, 0, 50);<br>
<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;objectCount; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objectList[i]-&gt;draw();<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; glFlush();<br>
}<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
void GlutObjectList::run()<br>
{<br>
&nbsp;&nbsp;&nbsp; glutInitWindowSize(windowWidth, windowHeight);<br>
&nbsp;&nbsp;&nbsp; glutInitWindowPosition(windowPos_x, windowPos_y);<br>
&nbsp;&nbsp;&nbsp; glutCreateWindow("A primitive model of a bike ! ~RG");<br>
<br>
&nbsp;&nbsp;&nbsp; glutDisplayFunc(::displayCallback);<br>
&nbsp;&nbsp;&nbsp; glutReshapeFunc(::reshapeCallback);<br>
&nbsp;&nbsp;&nbsp; glutKeyboardFunc(::keyboardCallback);<br>
&nbsp;&nbsp;&nbsp; glutMainLoop();<br>
}<br>
<br>
//NICKSPACE_END<br>
<br>
file name: glutObjects.h<br>
<br>
#ifndef&nbsp; GLUTOBJECTS_H<br>
#define GLUTOBJECTS_H<br>
<br>
#include "glutObject.h"<br>
<br>
//NICKSPACE_BEGIN<br>
const GLfloat DefaultTopDownRatio=0.5;<br>
<br>
class GlutObjectSphere : public GlutObject<br>
{<br>
public:<br>
&nbsp;&nbsp;&nbsp; virtual void display();<br>
};<br>
<br>
class GlutObjectCube: public GlutObject<br>
{<br>
&nbsp;&nbsp;&nbsp; <br>
public:<br>
&nbsp;&nbsp;&nbsp; virtual void display();<br>
};<br>
<br>
class GlutObjectHalfSphere: public GlutObject<br>
{<br>
public:<br>
&nbsp;&nbsp;&nbsp; GlutObjectHalfSphere();<br>
&nbsp;&nbsp;&nbsp; virtual void display();<br>
protected:<br>
&nbsp;&nbsp;&nbsp; //GLdouble clipPlane[4];<br>
};<br>
<br>
class GlutObject3DAxis: public GlutObject<br>
{<br>
public:<br>
&nbsp;&nbsp;&nbsp; GlutObject3DAxis();<br>
&nbsp;&nbsp;&nbsp; virtual void display();<br>
};<br>
<br>
<br>
class GlutObjectCylinder: public GlutObject<br>
{<br>
public:<br>
&nbsp;&nbsp;&nbsp; GlutObjectCylinder();<br>
&nbsp;&nbsp;&nbsp; virtual void display();<br>
&nbsp;&nbsp;&nbsp; void setRatio(GLfloat theRatio){ratio=theRatio;}<br>
protected:<br>
&nbsp;&nbsp;&nbsp; GLfloat ratio;<br>
};<br>
<br>
class GlutObjectCone: public GlutObject<br>
{<br>
public:<br>
&nbsp;&nbsp;&nbsp; GlutObjectCone();<br>
&nbsp;&nbsp;&nbsp; virtual void display();<br>
protected:<br>
&nbsp;&nbsp;&nbsp; GLfloat ratio;<br>
};<br>
<br>
//NICKSPACE_END<br>
<br>
#endif<br>
<br>
<br>
file name: glutObjects.cpp<br>
<br>
#include "glutObjects.h"<br>
#include &lt;GL/glut.h&gt;<br>
<br>
const GLfloat DefaultAxisLength=DefaultWindowWidth/2;<br>
const GLfloat ArrowLength=4;<br>
GlutObject3DAxis axis;<br>
<br>
//NICKSPACE_BEGIN<br>
GLdouble xy[4] = {0.0, 0.0, 1.0, 0.0};<br>
GLdouble yz[4] = {1.0, 0.0, 0.0, 0.0};<br>
GLdouble zx[4] = {0.0, 1.0, 0.0, 0.0};<br>
<br>
void GlutObjectCube::display()<br>
{<br>
&nbsp;&nbsp;&nbsp; if (bDrawingMesh)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutWireCube(1);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutSolidCube(1);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
<br>
<br>
void GlutObjectSphere::display()<br>
{<br>
&nbsp;&nbsp;&nbsp; if (bDrawingMesh)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutWireSphere(1, (int)volume[0]/2, (int)volume[1]/2);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutSolidSphere(1, (int)volume[0]/2, (int)volume[1]/2);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
<br>
GlutObjectHalfSphere::GlutObjectHalfSphere()<br>
{<br>
&nbsp;&nbsp;&nbsp; bDisableClip=false;<br>
}<br>
<br>
void GlutObjectHalfSphere::display()<br>
{<br>
&nbsp;&nbsp;&nbsp; //assume clipplane is xy parallel<br>
&nbsp;&nbsp;&nbsp; /*<br>
&nbsp;&nbsp;&nbsp; clipPlane[0]=clipPlane[2]=0;<br>
&nbsp;&nbsp;&nbsp; clipPlane[1]=1;<br>
&nbsp;&nbsp;&nbsp; clipPlane[3]=-position[1]+20.3;<br>
&nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp; //glClipPlane (GL_CLIP_PLANE0, clipPlane);&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; //glEnable (GL_CLIP_PLANE0);<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(position[0], position[1]-volume[1]/2.0, position[2]);<br>
&nbsp;&nbsp;&nbsp; if (bDrawingMesh)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutWireSphere(1, volume[0]/2, volume[1]/2);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutSolidSphere(1, volume[0]/2, volume[1]/2);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; //glDisable(GL_CLIP_PLANE0);<br>
}<br>
<br>
<br>
GlutObject3DAxis::GlutObject3DAxis()<br>
{<br>
&nbsp;&nbsp;&nbsp; position[0]=position[1]=0;<br>
&nbsp;&nbsp;&nbsp; position[2]=-50;<br>
&nbsp;&nbsp;&nbsp; color[0]=color[1]=0; <br>
&nbsp;&nbsp;&nbsp; color[2]=1;<br>
}<br>
<br>
void GlutObject3DAxis::display()<br>
{<br>
&nbsp;&nbsp;&nbsp; glPushMatrix();<br>
&nbsp;&nbsp;&nbsp; glColor3f(color[0], color[1], color[2]);<br>
&nbsp;&nbsp;&nbsp; //glMatrixMode(GL_MODELVIEW);<br>
&nbsp;&nbsp;&nbsp; //glLoadIdentity();<br>
<br>
&nbsp;&nbsp;&nbsp; //glRotatef(30, 1,0, 0);<br>
&nbsp;&nbsp;&nbsp; //glRotatef(-45, 0,1, 0);<br>
&nbsp;&nbsp;&nbsp; //glRotatef(60, 0, 0, 1);<br>
&nbsp;&nbsp;&nbsp; glBegin(GL_LINES);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //X axis<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glVertex3fv(position);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glVertex3f(position[0]+DefaultAxisLength, position[1], position[2]);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Y axis<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glVertex3fv(position);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glVertex3f(position[0], position[1]+DefaultAxisLength, position[2]);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Z axis<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glVertex3fv(position);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glVertex3f(position[0], position[1], position[2]+DefaultAxisLength);<br>
<br>
&nbsp;&nbsp;&nbsp; glEnd();<br>
<br>
&nbsp;&nbsp;&nbsp; //draw three solid cone as arrows of axis<br>
&nbsp;&nbsp;&nbsp; //glLoadIdentity();<br>
&nbsp;&nbsp;&nbsp; //save current TM matrix<br>
&nbsp;&nbsp;&nbsp; //draw cone for X axis<br>
&nbsp;&nbsp;&nbsp; glPushMatrix();<br>
&nbsp;&nbsp;&nbsp; glTranslatef(position[0]+DefaultAxisLength-ArrowLength, position[1], position[2]);<br>
&nbsp;&nbsp;&nbsp; glRotatef(90, 0, 1, 0);<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(x/2-ArrowLength, 0.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(100, 0.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; glutSolidCone(ArrowLength, ArrowLength*4, 10, 10);<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(x/2+ArrowLength, ArrowLength, 0);<br>
&nbsp;&nbsp;&nbsp; //glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'X');<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; //glLoadIdentity();<br>
&nbsp;&nbsp;&nbsp; glPopMatrix();<br>
&nbsp;&nbsp;&nbsp; glPushMatrix();<br>
&nbsp;&nbsp;&nbsp; //glRotatef(-90, 0, 1, 0);<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(-(x/2-ArrowLength), 0.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; glTranslatef(position[0], position[1]+DefaultAxisLength-ArrowLength, position[2]);<br>
&nbsp;&nbsp;&nbsp; glRotatef(-90, 1, 0, 0);<br>
&nbsp;&nbsp;&nbsp; glutSolidCone(ArrowLength, ArrowLength*4, 10, 10);<br>
&nbsp;&nbsp;&nbsp; glPopMatrix();<br>
&nbsp;&nbsp;&nbsp; //glLoadIdentity();<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; glTranslatef(position[0], position[1], position[2]+DefaultAxisLength-ArrowLength);<br>
&nbsp;&nbsp;&nbsp; //glRotatef(-90, 1, 0, 0);<br>
&nbsp;&nbsp;&nbsp; glutSolidCone(ArrowLength, ArrowLength*4, 10, 10);<br>
&nbsp;&nbsp;&nbsp; glPopMatrix();<br>
}<br>
<br>
GlutObjectCylinder::GlutObjectCylinder()<br>
{<br>
&nbsp;&nbsp;&nbsp; ratio=DefaultTopDownRatio;<br>
}<br>
<br>
void GlutObjectCylinder::display()<br>
{<br>
&nbsp;&nbsp;&nbsp; GLUquadricObj* pObj;<br>
&nbsp;&nbsp;&nbsp; pObj = gluNewQuadric();<br>
&nbsp;&nbsp;&nbsp; // Creates a new quadrics object and returns a pointer to it.<br>
&nbsp;&nbsp;&nbsp; glTranslatef(position[0], position[1], -position[2]/2);<br>
&nbsp;&nbsp;&nbsp; if (bDrawingMesh)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gluQuadricDrawStyle(pObj, GLU_LINE);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gluQuadricDrawStyle(pObj, GLU_FILL);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; //gluCylinder(pObj, volume[0]*ratio, volume[0], volume[2], volume[2]/2, volume[0]/2);<br>
&nbsp;&nbsp;&nbsp; //gluCylinder(pObj, (GLdouble)volume[0], (GLdouble)volume[0], (GLdouble)volume[2], <br>
&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp; (int)volume[2], (int)volume[0]);<br>
&nbsp;&nbsp;&nbsp; gluCylinder(pObj, 0.8, 0.8, 0.5, 20, 20);<br>
&nbsp;&nbsp;&nbsp; //gluSphere(pObj, volume[0], volume[0]/2, volume[0]/2);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; // Draw the cylinder with a radius : fRadius.<br>
&nbsp;&nbsp;&nbsp; gluDeleteQuadric(pObj);<br>
}<br>
<br>
GlutObjectCone::GlutObjectCone()<br>
{<br>
&nbsp;&nbsp;&nbsp; ratio=DefaultTopDownRatio;<br>
}<br>
<br>
void GlutObjectCone::display()<br>
{<br>
&nbsp;&nbsp;&nbsp; //GLUquadricObj* pObj;<br>
&nbsp;&nbsp;&nbsp; //pObj = gluNewQuadric();<br>
&nbsp;&nbsp;&nbsp; // Creates a new quadrics object and returns a pointer to it.<br>
&nbsp;&nbsp;&nbsp; //glTranslatef(position[0], position[1], -position[2]/2);<br>
&nbsp;&nbsp;&nbsp; if (bDrawingMesh)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //gluQuadricDrawStyle(pObj, GLU_LINE);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutWireCone(volume[0], volume[2], volume[0], volume[2]);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //gluQuadricDrawStyle(pObj, GLU_FILL);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glutSolidCone(volume[0], volume[2], volume[0], volume[2]);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; //gluCylinder(pObj, volume[0]*ratio, volume[0], volume[2], volume[2]/2, volume[0]/2);<br>
&nbsp;&nbsp;&nbsp; //gluCylinder(pObj, (GLdouble)volume[0], (GLdouble)volume[0], (GLdouble)volume[2], <br>
&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp; (int)volume[2], (int)volume[0]);<br>
&nbsp;&nbsp;&nbsp; //gluCylinder(pObj, 0.8, 0.8, 0.5, 20, 20);<br>
&nbsp;&nbsp;&nbsp; //gluPartialDisk(pObj, volume[0]*ratio, volume[0], volume[0], volume[1], 0, 90);<br>
&nbsp;&nbsp;&nbsp; //gluSphere(pObj, volume[0], volume[0]/2, volume[0]/2);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; // Draw the cylinder with a radius : fRadius.<br>
&nbsp;&nbsp;&nbsp; //gluDeleteQuadric(pObj);<br>
}<br>
<br>
<br>
//NICKSPACE_END<br>
&nbsp;&nbsp;&nbsp; <br>
<br>
　<br>
<br>
file name: main.cpp<br>
<br>
<br>
<br>
<br>
</span></span><pre>#include &lt;GL/glut.h&gt;<br>#include "glutObject.h"<br>#include "glutobjects.h"<br>#include &lt;stdio.h&gt;<br>//#include "glutObject.h"<br><br>//using namespace NICKSPACE;<br>extern void displayCallback();<br><br>int main(int argc, char** argv)<br>{<br>	glutInit(&amp;argc, argv);	<br>	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);<br>	GlutObjectSphere sphere;<br>	GlutObjectCube cube;<br>	sphere.setPosition(100, 0, -240);<br>	sphere.setVolume(50, 50, 50);<br>	cube.setPosition(100, 0, -280);<br>	cube.setColor(0,1,0);<br>	cube.setVolume(50, 50, 50);<br>	cube.setDirection(45, 45,0);<br><br>	GlutObject3DAxis axis;<br><br>	GlutObjectHalfSphere halfSphere;<br>	halfSphere.setPosition(0, 20, -240);<br>	halfSphere.setVolume(50, 50, 50);<br>	halfSphere.setDirection(0,0,30);<br>	halfSphere.setClipRatio(0.7);<br>/*<br>	GlutObjectCylinder cylinder;<br>	cylinder.setColor(0.5, 0.5, 0.0);<br>	//cylinder.setDirection(0, 60, 0);<br>	cylinder.setVolume(9, 9, 14);<br>	cylinder.setPosition(0, 0, -60);<br>*/<br>	GlutObjectCone cone;<br>	cone.setColor(0, 1, 0);<br>	cone.setPosition(0, 10, -60);<br>	cone.setVolume(2, 2, 1);<br>	run();<br>	<br>	return 0;<br>}</pre>
<pre></pre>
<pre><b><font color="#0000ff"><span lang="en-ca"><a name="result"></a></span></font></b></pre>

<pre><span lang="en-ca">				</span> <a href="game24.htm"><img src="picture/back.gif" style="border: medium none ;" alt="back.gif (341 bytes)" height="35" width="32"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="index.htm"><img src="picture/up.gif" style="border: medium none ;" alt="up.gif (335 bytes)" height="32" width="35"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none ;" alt="next.gif (337 bytes)" height="35" width="32"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;</p>

</body></html>