2013-05-24 3 views
-2

일치하는 큐브를보고 삭제하는 '블록'게임을 만들려고합니다. 이 코드는 두 개의 큐브를 만들고이를 y 축을 따라 이동합니다. 그런 다음 사라지기로되어 있고 다른 곳에 100 큐브가 나타납니다. 내 문제는 큐브를 사라지게 만드는 방법이나 '큐브 생성'기능을 작성하는 방법을 모르겠다. 나는 3D로 글을 쓰고있다. 내가 어떻게해야하는지에 대한 도움.OpenGL에서 임의로 큐브를 생성하는 방법/C#

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); 
     } 
    } 
} 
+0

여기서 달성하고자하는 것을 설명하기 위해 더 많은 노력을 기울여야합니다. – Grimmy

+0

내 주요 문제 빨간색, 녹색 및 파란색의 세 가지 기본 색상으로 임의로 큐브를 생성하는 함수가 필요합니다. –

답변

0

이 데이터 구조/무엇보다도 조직에 관한 것입니다. 큐브 그리기 방법을 알고 있으므로 큐브 클래스를 만들어 시작할 수 있습니다.

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

그런 다음 당신이 정보를 유지하는 구조가 필요

: 이것은 당신이 큐브를 그리는 방법을 알고 있기 때문에,이 클래스는 매우 쉽게해야

(당신이 사용하고있는) 즉각적인 모드는 OpenGL과 매우 간단하다 무엇을 그려야하는지.

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 
    .... 
} 

목표를 달성하는 데 필요한 구조가 최소한 있어야합니다. 이렇게하면 최소한 한 세트의 큐브를 관리 할 수 ​​있습니다. 데이터에 대해 수행 할 작업을 신중히 생각하고 이에 적합한 구조를 만들어야합니다. 시간이 지남에 따라 데이터가 변경되기를 원하는 것 같습니다. 그럼 그걸 처리 할 다른 구조가 필요해.

이것은 현재로서는 추측이지만, 올바른 방향으로 인도 할 수 있기를 바랍니다. 여기에있는 수업은 단지 예일 뿐이며 결코 "해결책"이 될 수 없습니다.

관련 문제