`

小时钟

阅读更多
     用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();
        }   
    }
}

     
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics