| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7399 人关注过本帖
标题:[原创]基于OpenGL的时钟
只看楼主 加入收藏
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
 问题点数:0 回复次数:31 
[原创]基于OpenGL的时钟

为庆祝C图形编辑专区开放,专门做的一个小东东
因为只用了3个小时,所以图形不是很精细,功能也不是很完善,无法调整时间,在这个程序中, 你可以明显的看到钟面上有块玻璃, 支持鼠标拉动,点右键复原. 为了方便观看时针效果,特设了1000倍速的时间选项.
本程序是用BC++6.0编译的.以下是原代码

1nTiNaCb.rar (205.64 KB) [原创]基于OpenGL的时钟



Unit1.h
**********************
//------------------------------------------
#ifndef Unit1H
#define Unit1H
//-------------------------------------
//--------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
#include <gl\gl.h>
#include <gl\glu.h>
#include <math.h>
//---------------

//------------
class TFormMain : public TForm
{
__published: // IDE-managed Components
TPanel *Control_Zone;

TTimer *Timer1;
TMainMenu *MainMenu1;
TMenuItem *File1;
TMenuItem *Exit1;
TMenuItem *N10001;
TMenuItem *N1;

void __fastcall FormResize( TObject *Sender);
void __fastcall FormCreate( TObject *Sender);
void __fastcall FormMouseMove( TObject *Sender, TShiftState Shift, int X,int Y);
void __fastcall FormMouseDown( TObject *Sender, TMouseButton Button,TShiftState Shift, int X, int Y );
void __fastcall FormMouseUp( TObject *Sender, TMouseButton Button,TShiftState Shift, int X, int Y );
void __fastcall FormMouseWheelDown( TObject *Sender, TShiftState Shift, TPoint &MousePos, bool &Handled );
void __fastcall FormMouseWheelUp( TObject *Sender, TShiftState Shift, TPoint &MousePos, bool &Handled );

void __fastcall Timer1Timer(TObject *Sender);
void __fastcall Exit1Click(TObject *Sender);
void __fastcall N10001Click(TObject *Sender);
void __fastcall N1Click(TObject *Sender);


private: // User declarations
HDC hdc;
float w, h;
int PixelFormat,x,y;
int I;
float zoom,M,J,K,n,N;
HGLRC hrc;
GLuint list_of_objects,waike;
Graphics::TBitmap* bitmap;
BOOL Left_Button_Down,inclined,key_and_mouse_down;

int loop;

public: // User declarations
__fastcall TFormMain(TComponent* Sender);
void __fastcall IdleLoop(TObject*, bool&);
void __fastcall SetPixelFormatDescriptor();
void __fastcall SetupLighting();
void __fastcall SetupTextures();
void __fastcall FormPaint( TObject *Sender );
void __fastcall Prepare_All();
void __fastcall Prepare_miao();
void __fastcall Prepare_fen();
void __fastcall Prepare_shi();
void __fastcall Prepare_pan();

void __fastcall Normal(float*p1,float*p2,float*p3,float*n);
void __fastcall Draw_Out_All();
void __fastcall Draw_Out_miao();

};
//---------------------------------------------------------------------------
extern PACKAGE TFormMain *FormMain;
//-------------
#endif

******************************





[此贴子已经被作者于2006-5-28 14:16:26编辑过]



KYezuIWc.rar (205.48 KB) [原创]基于OpenGL的时钟

搜索更多相关主题的帖子: 时钟 OpenGL 
2006-05-27 11:22
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 
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编辑过]


我的征途是星辰大海
2006-05-27 11:24
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
传个EXE上来看看

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-05-27 11:27
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 

顶楼里面不是有吗?


我的征途是星辰大海
2006-05-27 11:29
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 

和原代码放一起压缩了


我的征途是星辰大海
2006-05-27 11:29
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 
单独的EXE文件

csx4w1V0.rar (198.71 KB)

[此贴子已经被作者于2006-5-28 14:18:34编辑过]



ukCER2IJ.rar (198.56 KB)

我的征途是星辰大海
2006-05-27 11:31
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
程序有问题 运行后死机
系统 windows 2003

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-05-27 11:32
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 
,不知老K你的显卡如何?
有些老显卡跑起来会出问题

我的征途是星辰大海
2006-05-27 11:36
starrysky
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 
我到别人的电脑上去试试

我的征途是星辰大海
2006-05-27 11:38
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
以下是引用starrysky在2006-5-27 11:36:00的发言:
,不知老K你的显卡如何?
有些老显卡跑起来会出问题

可能吧,我是老掉牙显卡了


九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-05-27 11:41
快速回复:[原创]基于OpenGL的时钟
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031723 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved