PDA

View Full Version : Problem to create a triangle or square with menu



neo87
03-08-2011, 03:44 PM
Hi!! I'm sorry for my English..but I'm not English :whistle:

I have a problem to create a geometric figure using a menu..like a square or something similar..
I've tried with this code but it doesn't work:



#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 drawSquare()
{
glBegin(GL_POLYGON);
glVertex2f(250.-size,250.-size);
glVertex2f(250.+size,250.-size);
glVertex2f(250.+size,250.+size);
glVertex2f(250.-size,250.+size);
glEnd();
glFlush();
}

void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(250.-size,250.-size);
glVertex2f(250.+size,250.-size);
glVertex2f(250.+size,250.+size);
glVertex2f(250.-size,250.+size);
glEnd();
glFlush();
}

void demo_menu(int id)
{
switch (id) {
case 1:
void drawsquare();
break;
case 2: size = size*2;
break;
case 3: size = size/2;
break;
default:
break;
}
glutPostRedisplay();
}

int main (int argc, const char * argv[])
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Menu");

glutCreateMenu(demo_menu);
glutAddMenuEntry("quit",1);
glutAddMenuEntry("increase square size",2);
glutAddMenuEntry("decrease square size",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);

myInit();

glutDisplayFunc(mydisplay);
glutMainLoop();

return 0;
}



Help me please! :)

elgawin
03-09-2011, 06:32 AM
hi !

you can draw only in the mydisplay function.

neo87
03-09-2011, 06:55 AM
I do this excercise:

create an application that allows you to add cubes in the scene, balls, etc ... (used
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.

So I try to use an example..in this code if I delete the


void drawSquare()
{
glBegin(GL_POLYGON);
glVertex2f(250.-size,250.-size);
glVertex2f(250.+size,250.-size);
glVertex2f(250.+size,250.+size);
glVertex2f(250.-size,250.+size);
glEnd();
glFlush();
}

and the case 1 in switch
the computer draw two squares. So I tried to enter the dunction drawecc..but It's not correct..
Do you have any suggestions? to draw cube and sphere I use glutWireCube etc..But I don't know how to ensure that the user can draw them using menu.
Thank you very much for the suggestion :)

elgawin
03-09-2011, 07:10 AM
I do like this:



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

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

neo87
03-09-2011, 08:20 AM
#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(&amp;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;
}


I try to use your code but visual studio said that:
error C2065: 'draw_square': undeclared identifier and the same for sphere and cube..
Do I declare the functions at the beginning of the code?
I would to ask one more thing because I don't get it (I'm sorry but I am a beginner). Why did you put the "!" in the switch?
Thank you a lot anyway for the suggestion :)

neo87
03-09-2011, 02:04 PM
:sorrow: :sorrow: anybody has any advice for me?