For my Java OpenGL project I am trying to make a Rubik's Cube. I've got all rotations calculated and working, but there's one thing I don't know how to do, namely give each side of the cube it's own color. I used glRotatef and glTranslatef to position each of the 27 blocks, and glutSolidCube to draw each block. How can I give each side of glutSolidCube a different color? I've looked at textured cubes, but that seems hard as I don't know the (x, y, z) coordinates of each block, I only have the transformation matrix (rotation and translation). What is the easiest way to do this?

有帮助吗?

解决方案

This may not be possible directly: glutSolidCube does not generate color attributes (see the fghCube function in freeglut source code).

The simplest way would be to generate the geometry of the cube yourself. Generate 6 (faces) * 4 = 24 vertices total, with the expected positions, normals and an additional color attribute per vertex. Just like for the normal attribute, for each of the 8 distinct vertex positions on a cube, you should have 3 different colors (since the same vertex is shared by 3 faces but you need a different color per face).

Another way if you really insist on using glutSolidCube would be to assign the vertex color based on the vertex normal in the vertex shader. But maybe you are not using vertex shaders...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top