Java - 通过设置绘制多维数据集的面的点的位置来设置旋转

时间:2016-08-12 21:49:51

标签: java opengl rotation minecraft cube

我使用tesselator在我的世界中这样做......但这并不重要。

Bacisally我正在使用顶点绘制一个立方体。 每张脸都有4个位置。

首先看世界坐标​​。我按照这个顺序添加顶点/面的位置:DCBA ..你以后会得到它......(希望如此)

world coords in MC

基于此,这是我绘制立方体的方式:

public void renderParticles(World w, float x, float y, float z, Tesselator t)
{//**THIS IS AN EXAMPLE METHOD**
int scale = 1;
Random r;

int rotX = r.NextInt(10);
int rotY = r.NextInt(10);
int rotZ = r.NextInt(10);

//front face
     t.addVertex(x,        y,        z);
     t.addVertex(x,        y + scale,z);
     t.addVertex(x + scale,y + scale,z);
     t.addVertex(x + scale,y,        z);

//left face (I might have the Z coords wrong but that doesnt matter right now - basically the other direction)
     t.addVertex(x,          y,        z + scale);
     t.addVertex(x,          y + scale,z + scale);
     t.addVertex(x,          y + scale,z);
     t.addVertex(x,          y,        z);

//back face
     t.addVertex(x + scale,y,        z + scale);
     t.addVertex(x + scale,y + scale,z + scale);
     t.addVertex(x,        y + scale,z + scale);
     t.addVertex(x,        y,        z + scale);

//right face
     t.addVertex(x + scale,y,        z);
     t.addVertex(x + scale,y + scale,z);
     t.addVertex(x + scale,y + scale,z + scale);
     t.addVertex(x + scale,y,        z + scale);

//top face
     t.addVertex(x,        y + scale,z);
     t.addVertex(x,        y + scale,z + scale);
     t.addVertex(x + scale,y + scale,z + scale);
     t.addVertex(x + scale,y + scale,z);

//bottom face
     t.addVertex(x,        y,        z + scale);
     t.addVertex(x,        y,        z);
     t.addVertex(x + scale,y,        z);
     t.addVertex(x + scale,y,        z + scale);
}

当这个立方体产生/绘制时,我希望它已经随机旋转。

我不知道这样的核心数学......

为什么我不在minecraftforge论坛上发帖?

- 此代码所在的mod是coremod(他们不支持它们)

- 实际上已经抓过了他们,他们告诉我先把它变成普通模式。

任何想法我怎么可能旋转这个立方体然后(我希望我正确地写了所有立方体面:D)?

修改

这是我现在正在使用的代码,因为我将其更新为mc版本1.10:

/**
 * Renders the particle
 */
public void renderParticle(VertexBuffer worldRendererIn, Entity entityIn, float partialTicks, float rotationX,
        float rotationZ, float rotationYZ, float rotationXY, float rotationXZ) {
    float f = ((float) this.particleTextureIndexX + this.particleTextureJitterX / 4.0F) / 16.0F;
    float f1 = f + 0.015609375F;
    float f2 = ((float) this.particleTextureIndexY + this.particleTextureJitterY / 4.0F) / 16.0F;
    float f3 = f2 + 0.015609375F;
    float f4 = 0.1F * this.particleScale;

    if (this.particleTexture != null) {
        f = this.particleTexture.getInterpolatedU((double) (this.particleTextureJitterX / 4.0F * 16.0F));
        f1 = this.particleTexture.getInterpolatedU((double) ((this.particleTextureJitterX + 1.0F) / 4.0F * 16.0F));
        f2 = this.particleTexture.getInterpolatedV((double) (this.particleTextureJitterY / 4.0F * 16.0F));
        f3 = this.particleTexture.getInterpolatedV((double) ((this.particleTextureJitterY + 1.0F) / 4.0F * 16.0F));
    }

    float f5 = (float) (this.prevPosX + (this.posX - this.prevPosX) * (double) partialTicks - interpPosX);
    float f6 = (float) (this.prevPosY + (this.posY - this.prevPosY) * (double) partialTicks - interpPosY);
    float f7 = (float) (this.prevPosZ + (this.posZ - this.prevPosZ) * (double) partialTicks - interpPosZ);

    int i = this.getBrightnessForRender(partialTicks);
    int j = i >> 16 & 65535;
    int k = i & 65535;

    if (Minecraft.isFancyGraphicsEnabled() == true) {
        if (spawned == false) {
            this.particleRed *= 1.35F;
            this.particleGreen *= 1.35F;
            this.particleBlue *= 1.35F;

            spawned = true;
        }

        // front
        worldRendererIn.pos(f5 + f4, f6, f7 + f4).tex((double) f, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6 + f4, f7 + f4).tex((double) f, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6 + f4, f7 + f4).tex((double) f1, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6, f7 + f4).tex((double) f1, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        // back
        worldRendererIn.pos(f5, f6, f7).tex((double) f1, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        worldRendererIn.pos(f5, f6 + f4, f7).tex((double) f1, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        // done
        worldRendererIn.pos(f5 + f4, f6 + f4, f7).tex((double) f, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        worldRendererIn.pos(f5 + f4, f6, f7).tex((double) f, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        // left
        worldRendererIn.pos(f5, f6, f7 + f4).tex((double) f1, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6 + f4, f7 + f4).tex((double) f, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6 + f4, f7).tex((double) f, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6, f7).tex((double) f1, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        // right
        worldRendererIn.pos(f5 + f4, f6, f7).tex((double) f, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6 + f4, f7).tex((double) f1, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6 + f4, f7 + f4).tex((double) f1, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6, f7 + f4).tex((double) f, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        // top
        worldRendererIn.pos(f5, f6 + f4, f7).tex((double) f1, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6 + f4, f7 + f4).tex((double) f1, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6 + f4, f7 + f4).tex((double) f, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6 + f4, f7).tex((double) f, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();

        // bottom
        worldRendererIn.pos(f5, f6, f7 + f4).tex((double) f, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5, f6, f7).tex((double) f, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6, f7).tex((double) f1, (double) f3)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
        worldRendererIn.pos(f5 + f4, f6, f7 + f4).tex((double) f1, (double) f2)
                .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k)
                .endVertex();
    } else {
        worldRendererIn
                .pos((f5 - rotationX * f4 - rotationXY * f4), (double) (f6 - rotationZ * f4),
                        (double) (f7 - rotationYZ * f4 - rotationXZ * f4))
                .tex((double) f, (double) f3).color(this.particleRed, this.particleGreen, this.particleBlue, 1.0F)
                .lightmap(j, k).endVertex();
        worldRendererIn
                .pos((double) (f5 - rotationX * f4 + rotationXY * f4), (double) (f6 + rotationZ * f4),
                        (double) (f7 - rotationYZ * f4 + rotationXZ * f4))
                .tex((double) f, (double) f2).color(this.particleRed, this.particleGreen, this.particleBlue, 1.0F)
                .lightmap(j, k).endVertex();
        worldRendererIn
                .pos((double) (f5 + rotationX * f4 + rotationXY * f4), (double) (f6 + rotationZ * f4),
                        (double) (f7 + rotationYZ * f4 + rotationXZ * f4))
                .tex((double) f1, (double) f2).color(this.particleRed, this.particleGreen, this.particleBlue, 1.0F)
                .lightmap(j, k).endVertex();
        worldRendererIn
                .pos((double) (f5 + rotationX * f4 - rotationXY * f4), (double) (f6 - rotationZ * f4),
                        (double) (f7 + rotationYZ * f4 - rotationXZ * f4))
                .tex((double) f1, (double) f3).color(this.particleRed, this.particleGreen, this.particleBlue, 1.0F)
                .lightmap(j, k).endVertex();
    }
}

1 个答案:

答案 0 :(得分:1)

tessellator.addVertexWithUV(vertexX, vertexY, vertexY, U,V)

并且不要tessellator.draw()而是 // front worldRendererIn.pos(f5 + f4, f6, f7 + f4).tex((double) f, (double) f2) .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k) .endVertex(); worldRendererIn.pos(f5 + f4, f6 + f4, f7 + f4).tex((double) f, (double) f3) .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k) .endVertex(); worldRendererIn.pos(f5, f6 + f4, f7 + f4).tex((double) f1, (double) f3) .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k) .endVertex(); worldRendererIn.pos(f5, f6, f7 + f4).tex((double) f1, (double) f2) .color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j, k) .endVertex(); ,所以你有纹理顶点。

并且不要忘记最后致电 //OpenGL enable/blend functions/etc... here // for example GL11.glScalef(scale, scale, scale); worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR_NORMAL); // your vertex defining code // your vertex defining code // your vertex defining code worldRendererIn.normal(0.0f, 0.0f, 1.0f); tessellator.draw(); // OPENGL disable code here

有助于了解偏航,俯仰和俯仰http://howthingsfly.si.edu/flight-dynamics/roll-pitch-and-yaw

修改

好的,你的代码是这样的,这是一个完整的四边形。

{{1}}

要绘制四边形,您需要设置世界渲染器模式

{{1}}

最后一行,tesselator.draw()将确保它被绘制到屏幕上,顶点缓冲区被刷新为一个单独的显示列表。