-2

我正在尝试构建一个游戏“块”,它可以看到匹配的立方体并删除它们。此代码制作两个立方体并沿 y 轴移动它们。然后,它应该消失,并在不同的地方出现 100 个立方体。我的问题是我不知道如何让立方体看起来消失,也不知道如何编写函数“立方体生成”,我正在用 3d 编写。我应该做什么的任何帮助。

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 System.Threading;
using Tao.FreeGlut;
namespace TAOSample
{
     class VertexDania{
        double vx, vy , vz;

        public double  X{

            get{return vx;}
            set{ vx = value;}
        }
        public double  Y{
            get{return vy;}
            set{ vy = value;}
        }
                 public double Z{
            get{return vz;}
            set{ vz = value;}
        }
    } 
    public partial class Form1 : Form
    {
         public Form1()
        {
InitializeComponent();
            simpleOpenGlControl1.InitializeContexts();
            Gl.glClearColor(1, 1, 0, 0);
            Gl.glClearDepth(1.0);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();

            Glu.gluPerspective(45, simpleOpenGlControl1.Height /(double)simpleOpenGlControl1.Width, 0.1, 1000);
            Gl.glViewport(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height);

            Tao.FreeGlut.Glut.glutInit();

        } 
 private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
        {

            Gl.glEnable(Gl.GL_LIGHTING);
            Gl.glEnable(Gl.GL_LIGHT0);
            Gl.glEnable(Gl.GL_COLOR_MATERIAL);
 Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glLoadIdentity();

 VertexDania[] points = new VertexDania[]
            {
                new VertexDania{X=0     , Y=0.73  , Z=-0.75},
                new VertexDania{X=0.1   , Y=0.85  , Z=-0.75},
                new VertexDania{X=-0.01 , Y=0.95   , Z=-0.75},
                new VertexDania{X=-0.1  , Y=0.80  , Z=-0.75},
                new VertexDania{X=0.1   , Y=0.65  , Z=-1},
                new VertexDania{X=0     , Y=0.5   , Z=-0.75},
                new VertexDania{X=-0.1  , Y=0.6  , Z=-1}    
            };
            VertexDania[] point = new VertexDania[]
            {
                new VertexDania{X=0, Y=0.23, Z=-0.75},
                new VertexDania{X=0.1 , Y=0.35 , Z=-0.75},
                new VertexDania{X=-0.01 , Y=0.45 , Z=-0.75},
                new VertexDania{X=-0.1 , Y=0.30 , Z=-0.75},
                new VertexDania{X=0.1 , Y=0.1 , Z=-0.75},
                new VertexDania{X=0 , Y=0.0 , Z=-0.75},
                new VertexDania{X=-0.1, Y=0.1, Z=-0.75}
            };
            double[,] normals = new double[,]
            {
                {0,0,1},
                {0,0,-1},
                {0,0,-1}
            };
            int[,] faces = new int[,]
            {
                {0 , 1 ,4 , 5},
                {0 , 3 , 6 , 5},
                {0, 3 , 2 , 1}
            };

            Gl.glColor3d(1, 0, 0);
            Gl.glTranslated(0, ymove, 0.1);
            for (int i = 0; i < faces.GetLength(0); i++)
            {

                Gl.glNormal3d(normals[i, 0], normals[i, 1], normals[i, 2]);
                Gl.glBegin(Gl.GL_QUADS);
                for (int j = 0; j < 4; j++)
                {
                    Gl.glVertex3d(points[faces[i, j]].X, points[faces[i, j]].Y, points[faces[i, j]].Z);
                }
                Gl.glEnd();
                Gl.glNormal3d(normals[i, 0], normals[i, 1], normals[i, 2]);
                Gl.glBegin(Gl.GL_QUADS);
                for (int j = 0; j < 4; j++)
                {
                    Gl.glVertex3d(point[faces[i, j]].X, point[faces[i, j]].Y, point[faces[i, j]].Z);
                }
                Gl.glEnd();
            }
            if (ymove >= -3)
                ymove -= 0.03;
   }
 void redrawThread()
        {
            while (true)
            {
                Thread.Sleep(50);
                simpleOpenGlControl1.Invoke(new Action(delegate()
                {
                    simpleOpenGlControl1.Draw();
                }));
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(redrawThread);
            t.IsBackground = true;
            t.Start();
        }

        private void simpleOpenGlControl1_Resize(object sender, EventArgs e)
        {
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(45, simpleOpenGlControl1.Width / (double)simpleOpenGlControl1.Height, 0.1, 100);
            Gl.glViewport(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height);
           // Gl.glMatrixMode(Gl.GL_MODELVIEW);
        }
    }
}
4

1 回答 1

0

这与数据结构/组织有关。由于您知道如何绘制立方体,您可以从创建 Cube 类开始。这对于立即模式 OpenGL(您正在使用的)相当简单

既然你知道如何画一个立方体,这个类应该相当容易:

class Cube
{
    // Member variables
    x,y,z position
    r,g,b color
    // Functions
    public Cube(position, color); // Constructor
    public draw(); // draw the cube
}

然后你需要一个结构来保存关于你应该画什么的信息。

class CubeGroup
{
    // Member variables
    private Cube[] cubes; // cube array
    // Member functions
    public AddCube(Cube in_cube); // Adds a cube
    public DeleteCube(int index); // Removes a cube
    public draw(); // Draws this group of cubes
    ....
}

您至少需要一些结构来帮助您实现目标。这使您至少能够管理一组多维数据集。您需要仔细考虑要对数据执行哪些操作,并构建对此有意义的结构。您似乎也希望您的数据随着时间的推移而变化。然后你需要一些其他的结构来处理它。

在这一点上,这只是纯粹的猜测,但希望它可能会引导您朝着正确的方向前进。这里的类只是示例,绝不是“解决方案”。

于 2013-05-24T14:59:15.870 回答