Hi!! I’m sorry for my English but I don’t speak English very well
I would like to draw a cube by calling the menu, but I don’t know how.
I try to use your code but visual studio said that:
error C2065: ‘draw_square’: undeclared identifier
It said the same thing for sphere and cube…
Do I declare the functions at the beginning of the code?
Does anyone have any suggestions for me?
Thank you a lot anyway for the suggestion
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
GLfloat size = 10.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;
break;
case 2: draw_cube = !draw_cube;
break;
case 3: draw_sphere = !draw_sphere;
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;
}