PDA

View Full Version : I need help drawing some cubes



Areinn
08-08-2013, 05:46 AM
I know C++ for some time, and I am a beginner at 3D drawing and OpenGL.

I have a 3D matrix (a[x][y][z]) and I want to draw this matrix as a rubik cube.

I need a function which will place a custom color cube at a desired location, something like:
void DrawCube(x , y , z , red_amount , green_amount , blue_amount);

Right now I have a function from stackoverflow which draws a cube, but I am unable to adapt it to my needs, and I am not
sure it is a fresh function because I see many posts about old OpenGL code which is now slow, and new
OpenGL methods which are way better.

The code:
#include <glut.h>
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}

void DrawCube(void)
{
glLoadIdentity();
gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
glBegin(GL_QUADS);

//face in xy plane
glColor3f(0.82, 0.41, 0.12);//this the color with which complete cube is drawn.
glVertex3f(0,0 ,0 );
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);

//face in yz plane
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glVertex3f(0, 5, 0);
glVertex3f(0, 5, 5);

//face in zx plance
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0 );
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 0, 0);

//|| to xy plane.
glColor3f(0, 0, 1);
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);

//|| to yz plane
glColor3f(0.73, 0.58, 0.58);
glVertex3f(0,0 ,5 );
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);

//|| to zx plane
glVertex3f(0.58, 0, 0.82);
glVertex3f(0, 5, 0 );
glVertex3f(0, 5, 5);
glVertex3f(5, 5, 5);
glVertex3f(5, 5, 0);
glEnd();
glFlush();
}


void reshape(int w,int h){

glViewport(0, 0, (GLsizei)w, (GLsizei)h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv){

glutInit(&argc, argv);//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Please help me with a good function to draw a cube and with a 2 placed cubes working sample

Carmine
08-08-2013, 01:23 PM
...I have a function from stackoverflow which draws a cube, but I am unable to adapt it to my needs, and I am not sure it is a fresh function because I see many posts about old OpenGL code which is now slow, and new OpenGL methods which are way better. Not sure what you mean by 'from stackoverflow'? If you are talking about an error message, something's fishy. You should not be having stack problems trying to draw one cube. I wouldn't worry about Modern GL vs. old GL for your application. If you were trying to draw 100,000 cubes it might be better to go with Modern GL. In your case it doesn't matter. I would suggest reorganizing your code just a little bit. Instead of making DrawCube your 'DisplayFunc', put in something like 'Draw_Scene'. Then inside of Draw_Scene you would call your DrawCube routine many times. Put the glLoadIdentity and the glulookat in the Draw_Scene function. Pass x,y,z into the Draw_Cube routine and position the cube with a glTranslatef (x, y, z). You'll need a Push and PopMatrix at the beginning and end of the Draw_Cube routine.

Areinn
08-08-2013, 01:44 PM
I figured out how to do it, however I need to draw around 30.000 cubes.
I will post the code soon (~15 min), it is not ready yet.

I mentioned that is from stackoverflow, because the code was not written by me.

Thank you for the response.

Areinn
08-08-2013, 02:11 PM
Now it works. The program(180 lines, it call the function 3 times, with different coordonates): pastebin. com/JZ0f8aid
I am using this function:

void placeCube(double s,double x, double y, double z){

//Multi-colored side - FRONT
glBegin(GL_QUADS);
glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( s+x, -s+y, -s+z ); // P1 is red
glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( s+x, s+y, -s+z ); // P2 is green
glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -s+x, s+y, -s+z ); // P3 is blue
glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( -s+x, -s+y, -s+z ); // P4 is purple
glEnd();

// White side - BACK
glBegin(GL_QUADS);
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( s+x, -s+y, s+z );
glVertex3f( s+x, s+y, s+z );
glVertex3f( -s+x, s+y, s+z );
glVertex3f( -s+x, -s+y, s+z );
glEnd();

// Purple side - RIGHT
glBegin(GL_QUADS);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( s+x, -s+y, -s+z );
glVertex3f( s+x, s+y, -s+z );
glVertex3f( s+x, s+y, s+z );
glVertex3f( s+x, -s+y, s+z );
glEnd();

// Green side - LEFT
glBegin(GL_QUADS);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -s+x, -s+y, s+z );
glVertex3f( -s+x, s+y, s+z );
glVertex3f( -s+x, s+y, -s+z );
glVertex3f( -s+x, -s+y, -s+z );
glEnd();

// Blue side - TOP
glBegin(GL_QUADS);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( s+x, s+y, s+z );
glVertex3f( s+x, s+y, -s+z );
glVertex3f( -s+x, s+y, -s+z );
glVertex3f( -s+x, s+y, s+z );
glEnd();

// Red side - BOTTOM
glBegin(GL_QUADS);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( s+x, -s+y, -s+z );
glVertex3f( s+x, -s+y, s+z );
glVertex3f( -s+x, -s+y, s+z );
glVertex3f( -s+x, -s+y, -s+z );
glEnd();
}

is the size, x,y,z are coordinates. I will have to try to draw a matrix a[50][20][20], and I will have to update the picture at least 3 times per second. (20.000 cubes)


I should try to use a different function?

GClements
08-08-2013, 08:50 PM
I will have to try to draw a matrix a[50][20][20], and I will have to update the picture at least 3 times per second. (20.000 cubes)

I should try to use a different function?
Yes. For large amounts of data, you should use vertex arrays (glDrawArrays, glDrawElements, etc), preferably from buffer objects (glBindBuffer etc). If the data includes a lot of repetition, it may be preferable to use instanced rendering.