小时钟

用opengl做了个小时钟,明天开始做进阶功能给它打光。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tao.OpenGl;
using Tao.FreeGlut;
namespace homework3
{
public partial class Form1 : Form
{
double second_rot = 0.0;
double minit_rot = 0.0;
double hour_rot = 0.0;
DateTime mydatetime = new DateTime();
public Form1()
{
InitializeComponent();
this.simpleOpenGlControl1.InitializeContexts();
Glut.glutInit();
}
private void setVolume()
{
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
double aspect = (double)this.simpleOpenGlControl1.Size.Width / (double)this.simpleOpenGlControl1.Size.Height;
Glu.gluPerspective(45.0, aspect, 0.1, 100.0);
Gl.glViewport(0, 0, this.simpleOpenGlControl1.Width, this.simpleOpenGlControl1.Height);

}
private void myInit()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

private void simpleOpenGlControl1_Load(object sender, EventArgs e)
{
myInit();
setVolume();
mydatetime = DateTime.Now;
second_rot = mydatetime.Second * 6.0;
minit_rot = mydatetime.Minute * 6.0;
hour_rot = mydatetime.Hour * 30+mydatetime.Minute*0.5;

}

private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();

Glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
Gl.glColor3ub(0, 255, 0);
Glut.glutSolidTorus(0.15, 1.5, 40, 40);

Gl.glColor3ub(255, 255, 0);
Glut.glutSolidSphere(1.35, 40, 40);
Gl.glScaled(1.0, 1.0, 0.1);

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(0.0, 1.25, 0.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25*Math.Sin(30*Math.PI/180.0), 1.25*Math.Cos(30*Math.PI/180.0 ),0.0);
Gl.glRotated(30.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(60 * Math.PI / 180.0), 1.25 * Math.Cos(60 * Math.PI / 180.0), 0.0);
Gl.glRotated(60.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25, 0.0, 0.0);
Gl.glRotated(90.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(120 * Math.PI / 180.0), 1.25 * Math.Cos(120 * Math.PI / 180.0), 0.0);
Gl.glRotated(120.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(150 * Math.PI / 180.0), 1.25 * Math.Cos(150 * Math.PI / 180.0), 0.0);
Gl.glRotated(150.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(0, -1.25 , 0.0);
Gl.glRotated(180.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(210 * Math.PI / 180.0), 1.25 * Math.Cos(210 * Math.PI / 180.0), 0.0);
Gl.glRotated(210.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(240 * Math.PI / 180.0), 1.25 * Math.Cos(240 * Math.PI / 180.0), 0.0);
Gl.glRotated(240.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(270 * Math.PI / 180.0), 1.25 * Math.Cos(270 * Math.PI / 180.0), 0.0);
Gl.glRotated(270.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(300 * Math.PI / 180.0), 1.25 * Math.Cos(300 * Math.PI / 180.0), 0.0);
Gl.glRotated(300.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 0);
Gl.glTranslated(-1.25 * Math.Sin(330 * Math.PI / 180.0), 1.25 * Math.Cos(330 * Math.PI / 180.0), 0.0);
Gl.glRotated(330.0, 0.0, 0.0, 1.0);
Gl.glScaled(0.1, 1.0, 0.01);
Glut.glutSolidCube(0.3);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(0, 0, 255);
Gl.glRotated(-hour_rot, 0.0, 0.0, 1.0);
Gl.glTranslated(0.0, 0.3, 0.0);

Gl.glScaled(0.1, 1.0, 0.05);
Glut.glutSolidCube(1.0);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(0, 0, 255);
Gl.glRotated(-minit_rot, 0.0, 0.0, 1.0);
Gl.glTranslated(0.0,0.45 , 0.0);
Gl.glScaled(0.1, 1.8, 0.05);
Glut.glutSolidCube(0.8);
Gl.glPopMatrix();

Gl.glPushMatrix();
Gl.glColor3ub(255, 0, 255);
Gl.glRotated(-second_rot, 0.0, 0.0, 1.0);
Gl.glTranslated(0.0,0.5, 0.0);
Gl.glScaled(0.05, 2.4, 0.05);
Glut.glutSolidCube(0.6);
Gl.glPopMatrix();
}

private void timer1_Tick(object sender, EventArgs e)
{
second_rot += 6.0;
if (second_rot % 360 == 0)
{
minit_rot += 6.0;
hour_rot += 0.5;

}
this.simpleOpenGlControl1.Refresh();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值