C ++ OpenGL:Rubik多维数据集的代码帮助 [英] C++ OpenGL: Help with code for Rubik's cube

查看:91
本文介绍了C ++ OpenGL:Rubik多维数据集的代码帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关Rubik立方体的OpenGL C ++代码的帮助.我创建了一个魔方,但是旋转每个切片时遇到困难.

I need some help with OpenGL C++ code for Rubik''s cube. I created a Rubik''s cube but I am having difficulty rotating each slice.

void colorcube()
{
for(i=0;i<=.66;i=i+.22)
  {
    for(j=0;j<=.66;j=j+.22)
      {
        for(k=0;k<=.66;k=k+.22)
          {
           glColor3fv(colors[1]);
           glBegin(GL_POLYGON);
           glVertex3f(i-.3,j-.3,k-.3);
           glVertex3f(i-.3,j-.1,k-.3);
           glVertex3f(i-.1,j-.1,k-.3);
           glVertex3f(i-.1,j-.3,k-.3);
           glEnd();	

           glColor3fv(colors[2]);
           glBegin(GL_POLYGON);
           glVertex3f(i-.3,j-.3,k-.3);
           glVertex3f(i-.3,j-.1,k-.3);
           glVertex3f(i-.3,j-.1,k-.1);
           glVertex3f(i-.3,j-.3,k-.1);
           glEnd();
	
           glColor3fv(colors[3]);
           glBegin(GL_POLYGON);
           glVertex3f(i-.3,j-.3,k-.3);
           glVertex3f(i-.3,j-.3,k-.1);
           glVertex3f(i-.1,j-.3,k-.1);
           glVertex3f(i-.1,j-.3,k-.3);
           glEnd();	

           glColor3fv(colors[4]);
           glBegin(GL_POLYGON);
           glVertex3f(i-.1,j-.1,k-.3);
           glVertex3f(i-.1,j-.3,k-.3);
           glVertex3f(i-.1,j-.3,k-.1);
           glVertex3f(i-.1,j-.1,k-.1);
           glEnd();	

           glBegin(GL_POLYGON);
           glColor3fv(colors[5]);
           glVertex3f(i-.1,j-.1,k-.1);
           glVertex3f(i-.3,j-.1,k-.1);
           glVertex3f(i-.3,j-.3,k-.1);
           glVertex3f(i-.1,j-.3,k-.1);
           glEnd();

           glBegin(GL_POLYGON);
           glColor3fv(colors[6]);
           glVertex3f(i-.1,j-.1,k-.1);
           glVertex3f(i-.3,j-.1,k-.1);
           glVertex3f(i-.3,j-.1,k-.3);
           glVertex3f(i-.1,j-.1,k-.3);
           glEnd();
          }
        }
      }
}



任何帮助将不胜感激.


编码样式不是我想要讨论的..我想拥有每个模块都应该能够旋转的rubiks立方体..我想用opengl c ++进行此操作,而不是用c Sharp ....



Any help is greatly appreciated.


Coding style is not what i want to discuss.. i want to have rubiks cube where each slice should be able to rotate.. i want to do this opengl c++, not in c sharp.....

推荐答案

for(i=0;i<=.66;i=i+.22)


这可能会导致意想不到的问题,因为0.22不是一个可以使用浮点型或双精度类型(i的类型是什么?)精确表示的值,该值在内部可能等同于0.2200000003-因此,在递增后我第三次,您的循环将比预期的更早退出!

当然,内部表示形式也可能类似于0.219999997,在这种情况下,循环可能仍然有效.

在任何情况下,使用整数变量进行循环迭代并计算循环内的小数,而不是将小数用作迭代计数器,将是更安全的.


This could cause unexpected problems, as 0.22 is not a value that can be represented exactly using a float or double type (what is the type of i?) This value may internally be equivalent to something like 0.2200000003 - and as a result, after incrementing i for the third time, your loop will exit earlier than expected!

Of course, it''s also possible the internal representation is something like 0.219999997, in which case the loop might still work.

In any case, it would be safer to use integer variables for iterating through the loops, and calculate the fractionals inside the loop, rather than use the fractionals as iteration counters.


编码代码未重用;重复类似的代码块.这不是编程的全部内容.在您从根本上改善编码技术和样式之前,请不要期望成功.
也许您仍然需要针对初学者的更简单的练习.

-SA
Everything is hard-coded; code is not re-used; similar blocks of code are repeated. This is not what programming is all about. Don''t expect success until you radically improve your coding techniques and style.
Perhaps you still need much more simple exercises for beginners.

—SA


可能是此示例将对您有所帮助你更好.它是用Visual C ++/MFC和OpenGL中的3D视图编写的.它会编译(经过一些努力),至少会旋转并随机播放图层.
祝你好运,
谢尔盖·吉普林(Sergey Chepurin).
May be this sample will help you better. It is written in Visual C++/MFC with 3D view in OpenGL. It compiles (after some effort) and at least it rotates and shuffles layers.
Good luck,
Sergey Chepurin.


这篇关于C ++ OpenGL:Rubik多维数据集的代码帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆