我无法让我的yoyo计划工作;随着yoyo移动和旋转,我有一条不断增长和缩小的弦乐,但是我的琴弦随着yoyo一起移动。当yoyo上下旋转时,我需要让弦保持静止。我必须将琴弦和yoyo的动作分配到一个按钮上。介意帮我处理我的代码? (也请原谅愚蠢的yoyo;我想在设计之前完成编程)
#include "stdafx.h"
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
void firstDisplay();
void secondDisplay();
void movement();
void init();
void drawButton();
void handleButton(int button, int state, int x, int y);
const int Width=720;
const int Height = 480;
static float PI = 3.14159;
static float radius = 25;
static float INC= (PI/30);
static GLfloat spin = 0.0;
static GLfloat deltaz = .001;
static GLfloat deltax = 0.0;
static GLfloat deltay = 0.0;
//######################################################### Main #########################################################
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE | GLUT_DEPTH);
glutInitWindowSize(Width,Height);
glutInitWindowPosition(0,0);
glutCreateWindow("Yoyo");
glutDisplayFunc(firstDisplay);
glutMouseFunc(handleButton);
init();
glutMainLoop();
return;
}
//######################################################### Draw Button #########################################################
void drawButton()
{
glBegin(GL_POLYGON);
float theta;
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,0,1);
glVertex2f(40 + radius * cos(theta), 40 + radius*sin(theta));
}
glEnd();
}
//######################################################### Second Display #########################################################
void secondDisplay()
{
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,1,1);
float radius1 = 75;
glVertex2f(360 + radius1 * cos(theta), 80 + radius1*sin(theta));
}
glEnd();
glBegin(GL_POLYGON); //Here is the yoyo design
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(0,1,1);
float radius1 = 55;
glVertex2f(350 + radius1 * cos(theta), 90 + radius1*sin(theta));
}
glEnd();
glFlush();
return;
}
//######################################################### First Display #########################################################
void firstDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
float theta;
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,1,1);
float radius1 = 75;
glVertex2f(360 + radius1 * cos(theta), 80 + radius1*sin(theta));
}
glEnd();
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(0,1,1);
float radius1 = 55;
glVertex2f(350 + radius1 * cos(theta), 90 + radius1*sin(theta));
}
glEnd();
drawButton();
glutSwapBuffers();
glFlush();
return;
}
//######################################################### Draw String #########################################################
void draw_string()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(363, 8.25 - deltay, 0.0);
glVertex3f(357, 8.25 - deltay, 0);
glVertex3f(357, 6, 0);
glVertex3f(363, 6, 0);
glEnd();
}
//######################################################### Button Controls #########################################################
void handleButton(int button, int state, int x, int y)
{
static int index =-1;
if(button == GLUT_LEFT_BUTTON)
{
if(y>= 10 && y<= 100)
{
if (state == GLUT_DOWN)
{
glutIdleFunc(movement);
}
if (state == GLUT_UP)
{
glutIdleFunc(NULL);
}
}
glutSwapBuffers();
}
}
//######################################################### Yoyo Movement #########################################################
void movement()
{
static int goingup = 0;
glClear(GL_COLOR_BUFFER_BIT);
if(goingup==1)
{
deltay -= 6;
if(deltay <= 0)
{
goingup = 0;
}
spin = spin -5;
if(spin < 360)
{
spin = spin + 360;
}
}
if(goingup == 0)
{
deltay += 6;
if (deltay >= 315)
{
goingup = 1;
}
spin = spin +5;
if(spin < 360)
{
spin = spin + 360;
}
}
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(spin,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
secondDisplay();
draw_string();
glPopMatrix();
drawButton();
glutSwapBuffers();
}
//######################################################### Init #########################################################
void init()
{
glClearColor(0, 0, 0, 0.0);
glColor3f(1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,Width,Height,0);
return;
}