opengl实现3D弹球

     实现在3D空间小球的运动,碰到墙壁后反弹,小球会自己旋转和移动,运用了opengl,开发语言运用的是c#,框架运用的是taoframework.代码如下:
    
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 homework2
{
    public partial class Form1 : Form
    {
        double cx = 0.01, cy = 0.02, cz = 0.03;
        double x = 0.0, y = 0.0, z = 0.0;
        double angle = 2.0;
        double radius = 0.1;
       
        public Form1()
        {
            InitializeComponent();
            this.simpleOpenGlControl1.InitializeContexts();
            Glut.glutInit();
        }
        private void MyInit()
        {
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        }
        private void SetViewVolume()
        {
           
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            double aspect = (double)this.simpleOpenGlControl1.Width / (double)this.simpleOpenGlControl1.Height;
            Glu.gluPerspective(45, aspect, 0.1, 100.0);
            Gl.glViewport(0, 0, this.simpleOpenGlControl1.Width, this.simpleOpenGlControl1.Height);
        }
        private void simpleOpenGlControl1_Load(object sender, EventArgs e)
        {
            MyInit();
            SetViewVolume();
        }

        private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            drawWall();
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Glu.gluLookAt(0.0, 0.0, 3.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
            Gl.glPushMatrix();
            Gl.glColor3f(1.0f, 1.0f, 1.0f);
             Gl.glTranslated(x, y, z); 
          Gl.glRotated(angle, 0.0, 1.0, 0.0);
            Glut.glutWireSphere(0.1, 20, 20);
            Gl.glPopMatrix();
            
        }

        private void drawWall()
        {
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
           
            Gl.glPushMatrix();
            Glu.gluLookAt(0.0, 0.0, 3.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
            Gl.glBegin(Gl.GL_QUADS);
         

            Gl.glColor3f(0.0f, 0.0f, 1.0f);			
            Gl.glVertex3f(1.0f, 1.0f, 1.0f);			
            Gl.glVertex3f(-1.0f, 1.0f, 1.0f);			
            Gl.glVertex3f(-1.0f, -1.0f, 1.0f);			
            Gl.glVertex3f(1.0f, -1.0f, 1.0f);
            Gl.glEnd();

            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3f(1.0f, 0.0f, 0.0f);			
            Gl.glVertex3f(1.0f, -1.0f, -1.0f);			
            Gl.glVertex3f(-1.0f, -1.0f, -1.0f);			
            Gl.glVertex3f(-1.0f, 1.0f, -1.0f);			
            Gl.glVertex3f(1.0f, 1.0f, -1.0f);
            Gl.glEnd();

            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3f(0.0f, 1.0f, 0.0f);			
            Gl.glVertex3f(-1.0f, 1.0f, 1.0f);			
            Gl.glVertex3f(-1.0f, 1.0f, -1.0f);			
            Gl.glVertex3f(-1.0f, -1.0f, -1.0f);			
            Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
            Gl.glEnd();

            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3f(0.0f, 1.0f, 0.0f);			
            Gl.glVertex3f(1.0f, 1.0f, -1.0f);			
            Gl.glVertex3f(1.0f, 1.0f, 1.0f);			
            Gl.glVertex3f(1.0f, -1.0f, 1.0f);			
            Gl.glVertex3f(1.0f, -1.0f, -1.0f);		

            Gl.glEnd();
            Gl.glPopMatrix();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            angle += 3.0;
            if (x+radius >= 1||x-radius<=-1)
                cx = -cx;
            if (y+radius >= 1 || y-radius <= -1)
                cy = -cy;
            if (z+radius >=1 || z-radius <= -1)
                cz = -cz;
            x += cx;
            y +=cy;
            z += cz;
            this.simpleOpenGlControl1.Refresh();
        }
        

    }
}

    

你可能感兴趣的:(C++,c,框架,windows,LINQ)