draw a cube using a menu

Hi!! I’m sorry for my English but I don’t speak English very well :stuck_out_tongue:
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;
}


the variables dont exist in the global scope thus the linker cant find them add teh following declarations to the top along side the size variable.

bool draw_square=true, draw_cube=false, draw_sphere=false;

I tried to put the code you told me at first but it does not work … the program It always respond by saying that the identifier is not declared


#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

GLfloat size = 10.0;
bool draw_square=true, draw_cube=false,draw_sphere=false; // I put It here

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);
	
}




the program says:
error C2061: syntax error: identifier ‘draw_square’
error C2059: syntax error: ‘;’
error C2513: '/ * global * / ': no variable declared before ‘=’ :sorrow:

ok can u declare each on a different line. By the way what IDE/platform are u on?



GLfloat size = 10.0;
bool draw_square=true;
bool draw_cube=false;
bool draw_sphere=false; // I put It here

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);
	
}

the problem is the same…
error C2061: syntax error: identifier 'draw_square
error C2059: syntax error: ‘;’
error C2513: '/ * global * / ': no variable declared before ‘=’
error C2065: ‘true’: identificatore non dichiarato
I use visual studio 2008

I think your are saving the main file as a .c file and not .cpp file. C files have no bool type so there are two solutions

  1. Change the type to int and assign it 0 or 1 for true or false.
  2. Rename the main.c file to main.cpp and replace in the project.

See if this helps.

Thak you!! One of the many error was that…
I use the solution with .c because I tried to use cpp but there was one new error:
error C2664: ‘glutInit_ATEXIT_HACK’: can not convert parameter 2 from ‘const char * []’ to 'char ** ’
If I use .c the program compile.
BUT there is another problem :sorrow: if I try to draw a figure the program doesn’t work…If I call the menu, and push for example cube or sphere, the program desn’t draw anything.

I try also in a different way:

#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;

and I change here:

void demo_menu(int id)
{
	switch (id) {
	case 1:
		draw_square = !draw_square;
		draw_square=1;
		break;
	case 2: 
		draw_cube = !draw_cube;
		draw_cube=1;
		break;
	case 3: 
		draw_sphere = !draw_sphere;
		draw_sphere=1;
		break;
	default:
		break;
	}
}

or I try


and I change here:

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;
}



But it doesn't work anyway...  :mad:
 
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.

Thanks for the help you're giving me .. you have any suggestions on what should I change?

Thats because the argv pointer is non const. Change the declaration of your main function to this

void main(int argc, char* argv[])

See if this helps.


#include <stdio.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

GLfloat size = 1.0;
int draw_square=1;
int draw_cube=0;
int draw_sphere=0;

void myInit()
{
	glClearColor(0.0,0.0,0.0,1.0);	
}


void mydisplay()
{
   glClear(GL_COLOR_BUFFER_BIT);
   glLoadIdentity(); 
   
   //use this with gluPerspective
   gluLookAt(5,5,5,0,0,0,0,1,0); 

   //use this with gluOrtho
   //gluLookAt(0,0,-size,0,0,0,0,1,0);  

   if (draw_square)
   {
	glBegin(GL_POLYGON);
	    glColor3f(1.0,0.0,0.0);	
		glVertex2f(-size,-size);
		glVertex2f(size,-size);
		glVertex2f(size,size);
		glVertex2f(-size,size);
	glEnd();
   }
   if (draw_cube)
   {
      glutSolidCube (size);
   }
   if (draw_sphere)
   {
      glutSolidSphere (size, 10, 10);
   }

   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;
	}
	glutPostRedisplay();
}

void resize(int nw, int nh) {
	glViewport(0,0,nw,nh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(60,(float)nw/nh,0.1,10);
	
	//gluOrtho2D(-2.0,2.0,-2.0,2.0);

	glMatrixMode(GL_MODELVIEW);	
}
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);
	glutReshapeFunc(resize);
	glutMainLoop();

    return 0;
}



I don’t really know how to thank you… thank you very much!!!
Now it works!!
If it’s possible I’d like to ask you another little thing :stuck_out_tongue:
I tried to insert another menu with the opportunity to decide the colors and obviously does not work.
Do you have any advice on how I should do to change color to the figures?
I had thought about creating a struct and enhance value for each shape and then inserted through that change color … the problem is that I don’t know how to do it because I use different shapes and functions for draw…

This is the menu that I’ve tried to use but obviously It doesn’t work


void demo_menu2(int id)
{
	switch (id) {
	case 1:
		glColor3f(1.0,0.0,0.0)	;
		break;
	case 2: 
		glColor3f(0.0,1.0,0.0);
		break;
	case 3: 
		glColor3f(0.0,0.0,1.0);	
		break;
	default:
		break;
	}
	glutPostRedisplay();
} 

The way you are doing it now this is not correct. Usually this sort of thing should be done in the render function. You can do something like this. Create a GLfloat* pCurrentColor and
make three constants colors like this in the global scope


const GLfloat RED[3]={1,0,0};
const GLfloat GREEN[3]={0,1,0};
const GLfloat BLUE[3]={0,0,1};
GLfloat* pCurrentColor=RED;

Your menu function will be this,


void demo_menu2(int id)
{
	switch (id) {
	case 1:
		pCurrentColor=RED;
		break;
	case 2: 
		pCurrentColor=GREEN;
		break;
	case 3: 
		pCurrentColor=BLUE; 	
		break;
	default:
		break;
	}
	glutPostRedisplay();
} 

Your render code will simply set the current color as the glColor like this,


void Render() {
   glClear(..);
   //matrix handling 
   
   glColor3fv(pCurrentColor);

   //object drawing

   //other stuff
}

I hope u get the idea. Try this and see if this helps.
I have not compiled this part just giving u a hint on how it should be done.

Hello!
Again thank you very much for your time!!
I quote what you wrote to me and now you change the color but there is a problem. The color changes to all objects that were drawn, and if I try to disgnare a new object, the progam drawn the new object with the last color I had changed.
If i want to change the color of only one object, What should I do?
Should I use the technique of picking the item is selected?

Well store per object color but then when u change the color which object’s color would be changed?

Use color picking http://www.lighthouse3d.com/opengl/picking/index.php3?color1

Other methods include selection buffer (though deprecated in OpenGL 3.0 and above), ray based approach using glUnProject. Details here http://www.opengl.org/resources/faq/technical/selection.htm