Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenGL
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 18-May-11 1:02am
Edited 19-May-11 2:51am
v4
Comments
Slacker007 at 18-May-11 6:28am
   
Edited for spelling, grammar, and readability.
Legor at 18-May-11 7:35am
   
It's not clear which problems you're encountering.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Niklas Lindquist at 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.
Stefan63 at 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. ;)
SAKryukov at 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
SAKryukov at 18-May-11 15:13pm
   
5 for this solution.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Stefan63 at 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.
SAKryukov at 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 Lindquist at 19-May-11 9:09am
   
Good general advice. Fived once more.
SAKryukov at 19-May-11 10:14am
   
[misplaced...]
SAKryukov at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Niklas Lindquist at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 19 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100