Draw a cube or other 3d Objects glut. using a menu

This topic is 2839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi!! I'm sorry for my English but I don't speak English very well

To explain myself better:
I have to create a program an application that allows you to add cubes in the scene, balls..using
3D objects that GLUT provides
so that:
each object is placed in front of the room,
the color of the object is selected from the menu.
For starters I'm trying to draw an arbitrary number of solid figures by using the menu, I try with this code..but It doesn't work.. If you try the code draw only the square
Is there anyone who can give me a suggestion or help?

 #include <stdio.h> #include <stdlib.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut.h> #endif GLfloat size = 10.0; int draw_square=0; int draw_cube=0; int draw_sphere=0; void myInit() { glClearColor(0.0,0.0,0.0,1.0); glColor3f(1.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,500.0,0.0,500.0); glMatrixMode(GL_MODELVIEW); } void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); if (draw_square) { glBegin(GL_POLYGON); glVertex2f(250.-size,250.-size); glVertex2f(250.+size,250.-size); glVertex2f(250.+size,250.+size); glVertex2f(250.-size,250.+size); glEnd(); } if (draw_cube) { glutSolidCube (1); } if (draw_sphere) { glutSolidSphere (100, 24, 24); } glFlush(); } void demo_menu(int id) { switch (id) { case 1: draw_square = !draw_square; int draw_square=1; int draw_cube=0; int draw_sphere=0; break; case 2: draw_cube = !draw_cube; int draw_square=0; int draw_cube=1; int draw_sphere=0; break; case 3: draw_sphere = !draw_sphere; int draw_square=0; int draw_cube=0; int draw_sphere=1; break; default: break; } int main (int argc, const char * argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("Menu"); glutCreateMenu(demo_menu); glutAddMenuEntry("quadrato",1); glutAddMenuEntry("cubo",2); glutAddMenuEntry("sfera",3); glutAttachMenu(GLUT_RIGHT_BUTTON); myInit(); glutDisplayFunc(mydisplay); glutMainLoop(); return 0; } 

Share on other sites
Um..... It seems that you have to learn a lot in programming too.
You declare variables as globals, then you declare them again in the switch statement. Local variables with the same name will be used in the function where they are declared. Not the globals. So the globals won't be affected. (meaning: remove those 'int':s from the 'case' statements)
draw_square = !draw_square;[font="Arial"]
int draw_square=1;[/font]

you assign something, then you assign something else. The first assignment is pretty much useless. I guess you want to toggle the display of the square (show/hide). They why are you trying to assign 1 to it just after that?

But the code should work anyway (because of those useless local variables i mentioned earlier).
Try to add GlutPostRedisplay() at the end of demo_menu. That will force the screen to be redrawn.

If you want to draw arbitrary number of things, that means more than one thing. Is that what you mean? If so.... I don't even know where to start...

So I'd say the whole thing is a bit more advanced compared to your current skill.

I don't even know how your code can be compiled, a '}' is missing.

EDIT: what the hell happened to my avatar???

Share on other sites
I tried and I managed to solve! Thank you!

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• Forum Statistics

• Total Topics
633768
• Total Posts
3013753
×