Jump to content
  • Advertisement
Sign in to follow this  
neo87

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

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!