我如何使用顶点在opengles中移动对象



我想知道是否有一种使用顶点在OpenGL ES中更新和移动三角形的方法。这些是我三角的顶点:

// in counterclockwise order :
static float triangleCoords[] = {
        0.0f,  0.622008459f, 0.0f,  // top
        -0.5f, -0.311004243f, 0.0f, // bottom left
        0.5f, -0.311004243f, 0.0f   // bottom right
}; 

我想知道是否可以在没有矩阵的情况下移动三角形。

谢谢!

是的,这可能是毫无意义的。转换顶点的最快方法是使用矩阵,并且恰好在GPU上有专门的电路,以使其非常快。因此,如果您想自己转换和更新这些顶点,只需在提交之前修改顶点值。但是,这要比通过矩阵在顶点着色器中转换它们要慢得多。

好吧,我会向您展示我的一些想法(我使用ECMA6(。用返回的方法更改每个数字。然后在课堂(模型(中定义您想要的东西。

首先使用基于类的编码避免程序性。WebGL2项目 - 面向对象

      class Scale {
    
        constructor() {
    
            this.x = 1;
            this.y = 1;
            this.z = 1;
    
        }
    
        LinearScale(scale_) {
    
            this.x = scale_;
            this.y = scale_;
            this.z = scale_;
    
        }
    
    }
    
        class Point {
    
        constructor(x, y, z) {
    
            if (typeof z == 'undefined') {
                z = 0;
            }
    
            this.x = x;
            this.y = y;
            this.z = z;
            this.scale = new Scale();
    
        }
    
        get X() {
            return parseFloat(this.x * this.scale.x);
        }
        get Y() {
            return parseFloat(this.y * this.scale.y);
        }
        get Z() {
            return parseFloat(this.z * this.scale.z);
        }
    
    }
class TriangleVertex {
    constructor(root) {
        this.root = root;
        this.size = root.size;
        this.dynamicBuffer = App.dynamicBuffer;
        this.pointA = new Point(0.0, 1, 0.0);
        this.pointB = new Point(-1, -1, 0);
        this.pointC = new Point(1, -1, 0);
    }
    // GETTER
    get vertices() {
        return new Float32Array([
                this.pointA.X, this.pointA.Y * this.root.size, this.pointA.Z,
                this.pointB.X * this.root.size, this.pointB.Y * this.root.size, this.pointB.Z,
                this.pointC.X * this.root.size, this.pointC.Y * this.root.size, this.pointC.Z
            ]);
    }
    setScale(scale) {
        this.size = scale;
        if (this.dynamicBuffer == true)
            return;
        App.operation.triangle_buffer_procedure(this.root)
        return 'dynamicBuffer is false but i will update vertex array prototypical.';
    }
}
class Position {
    constructor(x, y, z) {
        //super()
        if (typeof x == 'undefined') {
            x = 0;
        }
        if (typeof y == 'undefined') {
            y = 0;
        }
        if (typeof z == 'undefined') {
            z = 0;
        }
        this.x = x;
        this.y = y;
        this.z = z;
        return this;
    }
    get worldLocation() {
        return [this.x, this.y, this.z];
    }
    SetX(newx) {
        this.x = newx;
    }
    SetY(newy) {
        this.y = newy;
    }
    SetZ(newz) {
        this.z = newz;
    }
    setPosition(newx, newy, newz) {
        this.x = newx;
        this.y = newy;
        this.z = newz;
    }
}

最新更新