Unit1.cpp
****************************
//----------------
#include "stdio.h"
#include <vcl.h>
#include "Unit1.h"
#include "math.h"
//----------------
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//-------------------------------------
GLfloat Throttle=5;
GLfloat glow=.4;
GLfloat glowp=0;
//------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Sender)
: TForm(Sender)
{
hdc = GetDC(Handle);
Control_Zone->Top =0 ;
Control_Zone->Left =FormMain->Width;
Control_Zone->Width = 0;
Control_Zone->Height = FormMain->Height;
float nRange =75.0;
w = ClientWidth ;
h = ClientHeight;
if( h==0 ) h = 1;
if ( w<=h ) glOrtho( -nRange, nRange, -nRange*h/w,nRange*h/w,-nRange,nRange );
else glOrtho( -nRange*w/h,nRange*w/h,-nRange, nRange, -nRange,nRange );
Application->OnIdle = IdleLoop;
Left_Button_Down = FALSE;
key_and_mouse_down = FALSE;
inclined = TRUE;//FALSE;
zoom = 0.99;
}
//-------------------
void __fastcall TFormMain::FormResize(TObject *Sender)
{
Control_Zone->Top = 0;
Control_Zone->Left = Control_Zone->ClientWidth;
Control_Zone->Width = 0;
Control_Zone->Height=ClientHeight;
float nRange =75.0;
w = ClientWidth ;
h = ClientHeight;
if( h==0 ) h = 1;
glViewport(0, 0, w, h);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
if ( w<=h ) glOrtho( -nRange, nRange, -nRange*h/w,nRange*h/w,-nRange,nRange );
else glOrtho( -nRange*w/h,nRange*w/h,-nRange, nRange, -nRange,nRange );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
inclined = TRUE;
key_and_mouse_down = FALSE;
}
void __fastcall TFormMain::IdleLoop(TObject*, bool& done)
{
done = false;
if( inclined==FALSE )
{
inclined = TRUE;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
Draw_Out_All();
SwapBuffers(hdc);
}
//-------------------
void __fastcall TFormMain::SetPixelFormatDescriptor()
{
PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,24,
0,0,0,0,0,0,
0,0,
0,0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
PixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat( hdc, PixelFormat, &pfd );
}
void __fastcall TFormMain::SetupLighting()
{
float MaterialAmbient[] = { 0.6, 0.6, 0.6, 1.0 };
float MaterialDiffuse[] = { 0.8, 0.8, 0.8, 1.0 };
float MaterialSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
float MaterialShininess[] = { 80.0 };
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, MaterialAmbient );
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialDiffuse );
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, MaterialSpecular );
glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, MaterialShininess);
float AmbientLightPosition[] = { 20, -20, 30.0, 0.0 };
float LightAmbient[] = { 0.5, 0.5, 0.5, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, AmbientLightPosition);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, LightAmbient);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
glShadeModel( GL_SMOOTH );
glEnable(GL_POLYGON_SMOOTH);
}
void __fastcall TFormMain::FormPaint( TObject *Sender )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glFlush();
}
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
hdc = GetDC( Handle );
SetPixelFormatDescriptor();
hrc = wglCreateContext( hdc );
if( hrc == NULL ) ShowMessage("对不起,GL设备描述表是空的。");
if( wglMakeCurrent(hdc, hrc) == false ) ShowMessage("对不起 MakeCurrent() 执行不了。");
w = ClientWidth;
h = ClientHeight;
glEnable( GL_DEPTH_TEST );
glDisable( GL_CULL_FACE );
glClearColor(0.0f,0.0f, 0.4f, 0.0f);
SetupLighting();
N=1000;
Prepare_All();
}
//---------------------------
void __fastcall TFormMain::FormMouseDown(TObject *Sender,TMouseButton Button, TShiftState Shift, int X, int Y)
{
if( Button==mbLeft )
{
Left_Button_Down = TRUE;
key_and_mouse_down = TRUE;
x = X;
y = Y;
}
else if( Button==mbRight )
{
glLoadIdentity();
inclined = TRUE;
Left_Button_Down = FALSE;
key_and_mouse_down = FALSE;
}
else
Left_Button_Down = FALSE;
}
void __fastcall TFormMain::FormMouseUp(TObject *Sender,TMouseButton Button, TShiftState Shift, int X, int Y)
{
Left_Button_Down = FALSE;
}
void __fastcall TFormMain::FormMouseMove(TObject *Sender,TShiftState Shift, int X, int Y)
{
if( Left_Button_Down==TRUE )
{
if( x != X )
glRotatef( (X-x)/4, 0.0, 1.0, 0.0);
if( y != Y )
glRotatef( (Y-y)/4, 1.0, 0.0, 0.0);
x = X;
y = Y;
}
}
void __fastcall TFormMain::FormMouseWheelDown(TObject *Sender,TShiftState Shift, TPoint &MousePos, bool &Handled)
{
glScalef( zoom,zoom,zoom );
}
void __fastcall TFormMain::FormMouseWheelUp(TObject *Sender,TShiftState Shift, TPoint &MousePos, bool &Handled)
{
glScalef( 1/zoom,1/zoom,1/zoom );
}
void __fastcall TFormMain::Normal( float*p1,float*p2,float*p3,float*n )
{
float A[3], B[3], l;
A[0] = p1[0] - p2[0];
A[1] = p1[1] - p2[1];
A[2] = p1[2] - p2[2];
B[0] = p3[0] - p2[0];
B[1] = p3[1] - p2[1];
B[2] = p3[2] - p2[2];
n[0] = B[1]*A[2] - B[2]*A[1];
n[1] = B[2]*A[0] - B[0]*A[2];
n[2] = B[0]*A[1] - B[1]*A[0];
l = sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
if(l!=0)
{
n[0]=n[0]/l;
n[1]=n[1]/l;
n[2]=n[2]/l;
}
}
//----
//-----------------
void __fastcall TFormMain::Prepare_All()
{
list_of_objects = glGenLists(4);
Prepare_miao();
Prepare_fen();
Prepare_shi();
Prepare_pan();
}
//
void __fastcall TFormMain::Prepare_miao()
{
GLUquadricObj *quadObj;
glNewList(list_of_objects,GL_COMPILE);
GLfloat p1[]={0,30,0};
GLfloat p2[]={1,-5.0,0} ;
GLfloat p3[]={0,-5.0,0.5};
GLfloat m[3];GLfloat n[3];
Normal(p1,p2,p3,m);
Normal(p3,p2,p1,n);
glBegin(GL_POLYGON);
glColor3f(0.8,0.0,0.0);
glNormal3f(m[0],m[1],m[2]);
glVertex3f(p1[0],p1[1],p1[2]);
glVertex3f(p2[0],p2[1],p2[2]);
glVertex3f(p3[0],p3[1],p3[2]);
glEnd();
GLfloat p4[]={-1,-5.0,0};
GLfloat j[3];GLfloat k[3];
Normal(p1,p3,p4,j);
Normal(p4,p3,p1,k);
glBegin(GL_POLYGON);
glColor3f(0.8,0.0,0.0);
glNormal3f(j[0],j[1],j[2]);
glVertex3f(p1[0],p1[1],p1[2]);
glVertex3f(p3[0],p3[1],p3[2]);
glVertex3f(p4[0],p4[1],p4[2]);
glEnd();
glEndList();
}
void __fastcall TFormMain::Prepare_fen()
{
GLUquadricObj *quadObj;
glNewList(list_of_objects+1,GL_COMPILE);
GLfloat p1[]={0,20,0};
GLfloat p2[]={1.5,-5.0,0} ;
GLfloat p3[]={0,-5.0,0.5};
GLfloat m[3];GLfloat n[3];
Normal(p1,p2,p3,m);
Normal(p3,p2,p1,n);
glBegin(GL_POLYGON);
glColor3f(0.3,0.3,0.3);
glNormal3f(m[0],m[1],m[2]);
glVertex3f(p1[0],p1[1],p1[2]);
glVertex3f(p2[0],p2[1],p2[2]);
glVertex3f(p3[0],p3[1],p3[2]);
glEnd();
GLfloat p4[]={-1.5,-5.0,0};
GLfloat j[3];GLfloat k[3];
Normal(p1,p3,p4,j);
Normal(p4,p3,p1,k);
glBegin(GL_POLYGON);
glColor3f(0.3,0.3,0.3);
glNormal3f(j[0],j[1],j[2]);
glVertex3f(p1[0],p1[1],p1[2]);
glVertex3f(p3[0],p3[1],p3[2]);
glVertex3f(p4[0],p4[1],p4[2]);
glEnd();
glEndList();
}
void __fastcall TFormMain::Prepare_shi()
{
GLUquadricObj *quadObj;
glNewList(list_of_objects+2,GL_COMPILE);
GLfloat p1[]={0,15,0};
GLfloat p2[]={2.5,0,0} ;
GLfloat p3[]={0,0,0.9};
GLfloat m[3];GLfloat n[3];
Normal(p1,p2,p3,m);
Normal(p3,p2,p1,n);
glBegin(GL_POLYGON);
glColor3f(0.1,0.1,0.1);
glNormal3f(m[0],m[1],m[2]);
glVertex3f(p1[0],p1[1],p1[2]);
glVertex3f(p2[0],p2[1],p2[2]);
glVertex3f(p3[0],p3[1],p3[2]);
glEnd();
GLfloat p4[]={-2.5,0,0};
GLfloat j[3];GLfloat k[3];
Normal(p1,p3,p4,j);
Normal(p4,p3,p1,k);
glBegin(GL_POLYGON);
glColor3f(0.1,0.1,0.1);
glNormal3f(j[0],j[1],j[2]);
glVertex3f(p1[0],p1[1],p1[2]);
glVertex3f(p3[0],p3[1],p3[2]);
glVertex3f(p4[0],p4[1],p4[2]);
glEnd();
glPushMatrix();
glColor3f(0.1,0.1,0.1);
quadObj=gluNewQuadric();
gluQuadricNormals(quadObj,GLU_SMOOTH);
gluCylinder(quadObj,4,0,0.9,36,2);
glPopMatrix();
glEndList();
}
void __fastcall TFormMain::Prepare_pan()
{
GLUquadricObj *quadObj;
glNewList(list_of_objects+3,GL_COMPILE);
glPushMatrix();
glColor3f(0.5,0.5,0.5);
quadObj=gluNewQuadric();
gluQuadricNormals(quadObj,GLU_SMOOTH);
glTranslatef(0,0,2);
gluCylinder(quadObj,2,0,1,36,2);
glTranslatef(0,0,-2.5);
gluCylinder(quadObj,2,2,2.5,36,2);
glPopMatrix();
glPushMatrix();
glColor3f(0.5,0.5,0.5);
quadObj=gluNewQuadric();
gluQuadricNormals(quadObj,GLU_SMOOTH);
gluCylinder(quadObj,32,0,0,360,2);
gluCylinder(quadObj,32,34,4,360,2);
glTranslatef(0,0,-1);
gluCylinder(quadObj,38,0,0,360,2);
gluCylinder(quadObj,38,37,5,360,2);
glTranslatef(0,0,5);
gluCylinder(quadObj,34,37,0,360,2);
glPopMatrix();
glPushMatrix();
glColor4f(0.4,0.4,0.4,0.7);
glTranslatef(0,0,3);
gluCylinder(quadObj,34,0,0,360,2);
glPopMatrix();
glEndList();
}
//------------------------------------------------------------------------------
void __fastcall TFormMain::Draw_Out_All()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Draw_Out_miao();
glFlush();
SwapBuffers(hdc);
}
void __fastcall TFormMain::Draw_Out_miao()
{
glPushMatrix();
glTranslatef(0,0,1.5);
glRotatef(-n,0,0,1 );
glCallList(list_of_objects);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,1);
glRotatef(-J,0,0,1 );
glCallList(list_of_objects+1);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,0.1);
glRotatef(-K,0,0,1 );
glCallList(list_of_objects+2);
glPopMatrix();
glPushMatrix();
glCallList(list_of_objects+3);
glPopMatrix();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Timer1Timer(TObject *Sender)
{
Timer1->Interval=N;
n+=6;J+=0.1;
if(n==360){n=0;K+=0.5 ;}
if(J==360)J=0;
if(K==360)K=0;
Yield();
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Exit1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TFormMain::N10001Click(TObject *Sender)
{
N=1;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N1Click(TObject *Sender)
{
N=1000;
}
//---------------------------------------------------------------------------
************************
下面是效果图
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[此贴子已经被作者于2006-5-28 14:09:10编辑过]