opengl实现3D弹球(增强版)

接着上次的弹球问题,增加了几个功能,首先可以改变弹球的速度(按ctrl+up速度变快,当按ctrl+down速度变慢,当按ctrl+left旋转变慢,当按ctrl+right选择变快),其次改变弹球选择速度,在碰壁的时候会发出碰撞的声音。

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;
using System.Media;

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 cangle = 3.0;
double radius = 0.2;
SoundPlayer myplayer = new SoundPlayer();
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();
Random rn = new Random();
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.2, 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 += cangle;
myplayer.SoundLocation=(Application.StartupPath + @"/16.wav");

if (x + radius >= 1 || x - radius <= -1)
{
cx = -cx;
myplayer.Play();
}
if (y + radius >= 1 || y - radius <= -1)
{
cy = -cy;
myplayer.Play();
}
if (z + radius >= 1 || z - radius <= -1)
{
cz = -cz;
myplayer.Play();
}
x += cx;
y +=cy;
z += cz;
this.simpleOpenGlControl1.Refresh();
}

private void simpleOpenGlControl1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:

if(this.timer1 .Interval >2)
this.timer1.Interval -= 2;
break;
case Keys.Down:
this.timer1.Interval += 2;
break;
case Keys.Left:
if(angle+cangle>0)
this.cangle -= 3.0;
if (cangle == 0.0)
angle = 0.0;
break;
case Keys .Right:
this.cangle += 3.0;
break;
}
this.simpleOpenGlControl1.Refresh();
}


}
}

你可能感兴趣的:(OpenGl)