Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
          }
        }
      }
}


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.....
Posted
Updated 19-May-11 1:51am
v4
Comments
Slacker007 18-May-11 6:28am    
Edited for spelling, grammar, and readability.
Legor 18-May-11 7:35am    
It's not clear which problems you're encountering.

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

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.
 
Share this answer
 
Comments
Niklas L 18-May-11 12:13pm    
I was thinking the same, but it's actually 0.66 that will overflow by a fraction. Nevertheless, it's always good to avoid floating point arithmetics in loops, when all you want is to run the loop a specific number of times.
Stefan_Lang 18-May-11 12:19pm    
Ok, then this is not currently the problem, or at least not within the part of the code that we see. ;)
Sergey Alexandrovich Kryukov 18-May-11 15:14pm    
I'm quite pessimistic here. Whatever you advice, it cannot help OP much -- look at the "techniques" and style. OP needs to get back to basics.

Please see my answer.
--SA
Sergey Alexandrovich Kryukov 18-May-11 15:13pm    
5 for this solution.
--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
 
Share this answer
 
Comments
Stefan_Lang 19-May-11 3:10am    
You are correct of course, but since the code posted has nothing to do with the actual problem I chose not to comment on it. The problem at hand may or may not have to do with bad code style, but until we see the relevant pieces of code we cannot tell.

P.S.: I've once seen a function for the purpose of updating a clients database from the host. It hardcoded each field of every table with its type and parameters into SQL statements, resulting in 11000 lines of code. Needless to say, when some of the table or field definitions changed, it was a PITA to fix. But it did work! So, even with bad code style like that, you may be able to get something to work.
Sergey Alexandrovich Kryukov 19-May-11 10:14am    
Thank you, Stefan.

You see, bad code with works is in real life much worse than that which does not. You're right, except "but". It's not "but", it's "even worse".

--SA
Niklas L 19-May-11 9:09am    
Good general advice. Fived once more.
Sergey Alexandrovich Kryukov 19-May-11 10:14am    
[misplaced...]
Sergey Alexandrovich Kryukov 20-May-11 15:21pm    
Thank you, Niklas.
If you mean you fixed someone's bad vote (I don't remember exactly), I can see that this person re-voted to 5...
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.
 
Share this answer
 
Comments
Niklas L 19-May-11 9:07am    
Good link indeed! 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900