2

OpenGLのキャンバスに変数とポイントを含むテキスト行を一緒に印刷しようとしています。私のコードは次のとおりです:

 void display()
 {
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    char string[50];
    sprintf(string, "Base Rotation: %d", numVertices); 
    renderMyText(-0.4, 0.35, string);
    glPointSize(20);
    glBegin(GL_POINTS);
        glVertex2f(characterX, characterY);
    dx = vertices[numVertices-1].x-ox;
    dy = vertices[numVertices-1].y-oy;
    dt = glutGet(GLUT_ELAPSED_TIME);
    characterX = ox + dx / sqrt(dx*dx+dy*dy) * Velocity * dt;
    characterY = oy + dy / sqrt(dx*dx+dy*dy) * Velocity * dt;
    printf("%f %f", characterX, characterY); 
    glEnd();
    glFlush();
}

マウスが動いたときにポイントの位置を更新する別の方法を使用しています。そのコードはすべて正常に機能し、正方形はその位置を更新し、テキスト行を追加するまで完全に移動しました。

プログラムを開始すると、正方形とテキストが表示されますが、ウィンドウ内でマウスを動かすと、正方形が消えてテキストのみが残ります。2つをウィンドウに残してください。誰かが何が悪いのかわかりますか?

4

1 に答える 1

1

私はこの問題を解決したので、解決策を追加する必要があると感じています:

私はこの問題を間違った方法で行っていました、私は次のようにアイドルメソッドで座標値を更新する必要がありました:

    void idle()
{
    //dx is last mouse x - last box x
    dx = vertices[numVertices-1].x-ox;
    //dy is last mouse y - last box y
    dy = vertices[numVertices-1].y-oy;
    dt = 50;
    //dt helps to control the chasing charcters speed
    characterX = ox + dx / sqrt(dx*dx+dy*dy) * Velocity * dt;
    characterY = oy + dy / sqrt(dx*dx+dy*dy) * Velocity * dt;
    //equations to move the character after the cursor by moving it along the slope of the line between the two points
    ox = characterX;
    oy = characterY;
    //update object x and y for next calculation
    if((numVertices > 5) && characterX >= vertices[numVertices-1].x - 1 && characterX <= vertices[numVertices-1].x + 1 && characterY >= vertices[numVertices-1].y -1 && characterY <= vertices[numVertices-1].y + 1) { 
        endGame = true;
        //vertices over 5, so that we don't accidentially die when we start, this collision detection code works on a threshold of contact of one
        //between the cursor and object on the X and Y
    }
    glutPostRedisplay();
}

次に、glutPostRedisplayがdisplayメソッドを呼び出します。ここで、ここで計算された座標を使用して、画面上のポイントの位置を変更します。

        void display() { 
            glColor3f(0,255,0); //set the in game text to green
            if(endGame == true) { 
                glColor3f(255,0,0);
                //if the game is over set the text to red
            }
            glClear (GL_COLOR_BUFFER_BIT);
            glPointSize(20);
            glBegin(GL_POINTS);
                glVertex2f(characterX, characterY);
            glEnd();
            glFlush();
            glutSwapBuffers();
}
于 2013-03-13T13:50:09.917 に答える