簡體   English   中英

在OpenGL中移動3d形狀

[英]Moving 3d Shape in OpenGL

我正在用OpenGL / C ++編寫一個程序,該程序創建多個Sphere,然后將它們移到屏幕附近,直到看不見它們為止。 我了解如何創建球體,但是似乎無法弄清楚它們創建后如何移動它們。 有誰知道我如何有效地更改move()函數,以便每次調用z時將z遞增1? 或者,如果有更好的方法,請告訴我。 謝謝!

 class Sphere {

public:
    double x, y, z;
    void create(double newx, double newy, double r, double g, double b) {
        x = newx;
        y = newy;
        z = -175; // start at back of projection matrix
        glPushMatrix();
        glTranslatef(x, y, z);
        glColor3f(r, g, b);
        glScalef(1.0, 1.0, 1.0);
        glutSolidSphere(1, 50, 50);
        glPopMatrix();
    }
    void move() {
        glPushMatrix();
        //if z is at front, move to start over in the back
        if (z >= 0) {
            z = -176;
            x = rand() % 100;
            y = rand() % 100;
            x = x - 50;
            y = y - 50;
        }
        x = x;
        y = y;
        glPushMatrix();
        z++;
        glTranslatef(x, y, z);
        glPopMatrix();
    }
};

為了更改球體的位置,沒有必要在move()函數中調用glPushMatrix()和glPopMatrix()函數。 您可以將move()函數簡化為:

    void Move() {
        //if z is at front, move to start over in the back
        if (z >= 0) {
            z = -176;
        }
        z++;
    }

此外,就像用戶datenwolf所說的那樣,您無需創建,而是``繪制''或``渲染''一個球體(或您希望OpenGL顯示的任何3D對象)。舉一個例子,讓我們來看一下Render。

您想要創建一個Render()函數並將代碼從create()函數移至該函數。 因為稍后要移動球體的位置,所以將x,y和z坐標移到Sphere的構造函數中並對其進行初始化。

例:

 class Sphere {
  public:
    double x,y,z;

    // We also set the color values of the sphere inside the sphere's
    // constructor for simplicity sake.
    double r,g,b;

   Sphere(double _x, double _y, double _z) {
        x = _x;
        y = _y;
        z = _z;

        r = 1.0;
        g = 0.0;
        b = 0.0;

    }
    void Render() {
        glPushMatrix();
        glTranslatef(x, y, z);
        glColor3f(r, g, b);
        glScalef(1.0, 1.0, 1.0);
        glutSolidSphere(1, 50, 50);
        glPopMatrix();
    }

};

OpenGL使用稱為glutDisplayFunc()的回調函數。 您將一個函數傳遞給glutDisplayFunc(),以便呈現您想要在屏幕上顯示的所有內容。 我將要傳遞的函數稱為“ void Render3DEnvironment()”。 您需要找到glutDisplayFunc()和傳遞給它的函數,然后在其中調用球體的Render()函數。

這是一個例子:

#include "Sphere.h"

Sphere* sphere;

void Render3DEnvironment()
{
    sphere->Render();
    sphere->Move();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    sphere = new Sphere(50,50,50);

    glutDisplayFunc(Render3DEnvironment);

    glutMainLoop();

    return 0;

}

glutDisplayFunc()函數將不斷調用已傳遞給它的函數,在本例中為Render3DEnvironment()函數。 如您所見,Render3DEnviroment()函數中也調用了Move()函數。 這意味着您的球面也在z軸上(快速)移動,並且當z達到等於0或大於0時返回到-176。

提示 :由於OpenGL函數使用GLfloat變量,因此請使用GLfloat而不是double作為x,y,z和r,g,b變量的數據類型。 為您節省一些編譯器警告。

在OpenGL中,沒有創建任何“模型”。實際上,在OpenGL中甚至沒有“模型”。只有一個數字畫布(稱為幀緩沖區)和OpenGL提供的繪制工具來繪制點,線和三角形,其中一個位於在您使用OpenGL繪制某些內容的那一刻,它已經忘記了。

因此,將函數命名為“ create”的整個概念是錯誤的。 相反,您擁有的是“繪制”功能。

因此,您所要做的很簡單:重繪場景,但將幾何圖形放置在其他位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM