i am making a program which will have menu and submenus.
EDITED------------------------------------------------
The "shape" submenu don't work when i call it.
When i call the shape menu ,it must draw for example,a square or a triangle or a circle.
I am putting a sample of my code here.
int WXSIZE=400,WYSIZE=400;
float xmin=-5, xmax=5, ymin=-5, ymax=5;
int CLEARFLAG=1;
float xpos=0,ypos=0;
float x=1.0,y=1.0;
float col1=1.0,col2=0,col3=0,col4=0,col5=1.0,col6=0,col7=1.0,col8=1.0,col9=0,col10=0.6,col11=0.4,col12=0.7;
int sub_menu1,sub_menu2,sub_menu3;
int draw=1;
float k=0,l=0.5;
void setupmywindow()
{
glClearColor(1,1,1,0);
gluOrtho2D(xmin, xmax, ymin, ymax);
}
void setXYpos(int px, int py)
{
xpos=xmin+(xmax-xmin)*px/WXSIZE;
ypos=ymax-(ymax-ymin)*py/WYSIZE;
printf(">>%d %d %f %f\n",px,py,xpos,ypos);
}
....
void triangle(float x,float y){
draw==3;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(col1,col2,col3);
glBegin(GL_TRIANGLES);
glVertex2f(x-l,y+k);
glVertex2f(x-k,y+l);
glVertex2f(x+k,y+l);
glEnd();
}
....
void mydraw(float x,float y)
{
draw==1;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glShadeModel(GL_FLAT);
glBegin(GL_QUAD_STRIP);
glVertex2f(x-l,y+k);
glVertex2f(x-l,y+l);
glColor3f(col1,col2,col3);glVertex2f(x+k,y+k);
glVertex2f(x+k,y+l);
glVertex2f(x+l,y+k);
glColor3f(col4,col5,col6);glVertex2f(x+l,y+l);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex2f(x-l,y-l);
glVertex2f(x-l,y+k);
glVertex2f(x+k,y-l);
glColor3f(col7,col8,col9);glVertex2f(x+k,y+k);
glVertex2f(x+l,y-l);
glColor3f(col10,col11,col12);glVertex2f(x+l,y+k);
glEnd();
}
void mymenu(int n){
switch (n) {
case 1: shape_menu();break;
case 2: size_menu();break;
case 3: color_menu();break;
case 4: clear_menu();break;
case 5: exit_menu();break;
}
glutPostRedisplay();
}
void shape_menu (int n){
switch (n) {
case 2: draw==2;square(xpos,ypos);break;
case 3开发者_如何学C: draw==3;triangle(xpos,ypos);break;
case 4: draw==4;circle(xpos,ypos);break;
}
glutPostRedisplay();
}
...
void myDisplay()
{
if(CLEARFLAG) glClear(GL_COLOR_BUFFER_BIT);
else if (draw==1) {mydraw(xpos,ypos);}
else if (draw==2) {triangle(xpos,ypos);}
else if (draw==3) {square(xpos,ypos);}
else if (draw==4) {circle(xpos,ypos);}
glutSwapBuffers();
}
The problem is that when i chose to change shape,it doesn't anything.As you can see , i use "draw" to call the relevant shape.I either have sth wrong in the myDisplay function or the shape_menu function. Thank you
EDITED---------------------------------------------------------------------------
I finally managed to it except one thing.When it draws a circle i can't move it.It doesn't respond to mouse click in the window. My corrections are :
...
void shape_menu (int n){
switch (n) {
case 2: draw=2;break;
case 3: draw=3;break;
case 4: draw=4;break;
}
glutPostRedisplay();
}
...
void myDisplay()
{
if(CLEARFLAG) glClear(GL_COLOR_BUFFER_BIT);
else if (draw==1) {mydraw(xpos,ypos);}
else if (draw==2) {square(xpos,ypos);}
else if (draw==3) {triangle(xpos,ypos);}
else if (draw==4) {circle(xpos,ypos);}
glutSwapBuffers();
}
....
and i also removed the "draw==1" from my functions. The function which gives the circle is :
void circle(float x,float y )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int numpoints=84;
glColor3f(col1,col2,col3);
glBegin( GL_LINE_STRIP );
int i;
for(i=0; i<numpoints; i++ )
{
float angle = i * (2.0*PI/numpoints);
x = cos( angle )*l;
y = sin( angle )*l;
glVertex2f(x,y);
}
glEnd();
}
How should i manage to fix my problem?
OpenGL is not a scene graph. It's a drawing API.
What this means is, that you don't "add" objects into some kind of hierachy. What you have to do is store which objects to draw where with which size and then upon drawing iterate through this list and draw things according what's stored therein.
精彩评论