java - Opengl - 旋转、缩放、平移

标签 java opengl minecraft minecraft-forge

问题:使用 opengl 进行渲染时应用旋转、缩放和平移的顺序是什么?

当前我正在运行代码

  • GL11.glScaled(比例值)
  • GL11.glRotated(x)//间距
  • GL11.glRotated(y)//偏航
  • GL11.glRotated(z)//滚动
  • GL11.glTranslated(x, y, z)

我尝试过更改顺序,但得到了很多不同的结果。某些配置可以改善渲染效果,例如,最后缩放不会干扰翻译。然而,它似乎破坏了旋转,导致俯仰和滚动都使模型滚动。

一些额外信息: 该代码适用于使用 JSON 数据渲染项目的 Minecraft mod。确切的代码可以在这里找到: https://github.com/VoltzEngine-Project/Engine/blob/development/src/main/scala/com/builtbroken/mc/client/json/render/state/ModelState.java#L48

应 BDL 的要求将代码添加到帖子中

package com.builtbroken.mc.client.json.render.state;

import com.builtbroken.jlib.helpers.MathHelper;
import com.builtbroken.mc.client.SharedAssets;
import com.builtbroken.mc.client.json.ClientDataHandler;
import com.builtbroken.mc.client.json.imp.IModelState;
import com.builtbroken.mc.client.json.models.ModelData;
import com.builtbroken.mc.client.json.texture.TextureData;
import com.builtbroken.mc.imp.transform.rotation.EulerAngle;
import com.builtbroken.mc.imp.transform.vector.Pos;
import cpw.mods.fml.client.FMLClientHandler;
import org.lwjgl.opengl.GL11;

/**
 * Render/Texture/Animation states used for rendering models in the game
 *
 * @see <a href="https://github.com/BuiltBrokenModding/VoltzEngine/blob/development/license.md">License</a> for what you can and can't do with the code.
 * Created by Dark(DarkGuardsman, Robert) on 11/22/2016.
 */
public class ModelState extends TextureState implements IModelState
{
    public String modelID;
    public String[] parts;
    public Pos offset;
    public Pos scale;
    public EulerAngle rotation;

    public boolean renderParent = false;
    public boolean renderOnlyParts = true;

    public ModelState(String ID)
    {
        super(ID);
    }

    public ModelState(String ID, String modelID, Pos offset, Pos scale, EulerAngle rotation)
    {
        this(ID);
        this.modelID = modelID;
        this.offset = offset;
        this.scale = scale;
        this.rotation = rotation;
    }

    @Override
    public boolean render(boolean subRender, float yaw, float pitch, float roll)
    {
        TextureData textureData = getTexture(); //Texture reference, texture is .png loaded using property code owned by Mojang so can not be shared
        ModelData data = getModel(); //Model reference, model is .obj rendered using triangles
        if (data != null && data.getModel() != null)
        {
            //Starts rendering by storing previous matrix
            GL11.glPushMatrix();

            if (!subRender)
            {
                //TODO handle parent additions, in which parent and child data are combined
                //Scales object by value
                if (scale != null)
                {
                    GL11.glScaled(scale.x(), scale.y(), scale.z());
                }
                else if (parentState instanceof IModelState && ((IModelState) parentState).getScale() != null)
                {
                    GL11.glScaled(((IModelState) parentState).getScale().x(), ((IModelState) parentState).getScale().y(), ((IModelState) parentState).getScale().z());
                }

                //Rotates object, needs to be handled after scaling
                if (rotation != null)
                {
                    GL11.glRotated(MathHelper.clampAngleTo360(rotation.pitch() + pitch), 1, 0, 0);
                    GL11.glRotated(MathHelper.clampAngleTo360(rotation.yaw() + yaw), 0, 1, 0);
                    GL11.glRotated(MathHelper.clampAngleTo360(rotation.roll() + roll), 0, 0, 1);
                }
                else if (parentState instanceof IModelState && ((IModelState) parentState).getRotation() != null)
                {
                    GL11.glRotated(MathHelper.clampAngleTo360(((IModelState) parentState).getRotation().pitch() + pitch), 1, 0, 0);
                    GL11.glRotated(MathHelper.clampAngleTo360(((IModelState) parentState).getRotation().yaw() + yaw), 0, 1, 0);
                    GL11.glRotated(MathHelper.clampAngleTo360(((IModelState) parentState).getRotation().roll() + roll), 0, 0, 1);
                }

                //Moves the object
                if (offset != null)
                {
                    GL11.glTranslated(offset.x(), offset.y(), offset.z());
                }
                else if (parentState instanceof IModelState && ((IModelState) parentState).getOffset() != null)
                {
                    GL11.glTranslated(((IModelState) parentState).getOffset().x(), ((IModelState) parentState).getOffset().y(), ((IModelState) parentState).getOffset().z());
                }
            }

            //Render parent
            GL11.glPushMatrix();
            if (parentState instanceof IModelState)
            {
                if (renderParent)
                {
                    ((IModelState) parentState).render(true);
                }
                else if (parts == null && parentState instanceof ModelState && ((ModelState) parentState).renderParent)
                {
                    if (((ModelState) parentState).parentState instanceof IModelState)
                    {
                        ((IModelState) ((ModelState) parentState).parentState).render(true);
                    }
                }
            }
            GL11.glPopMatrix();

            //Binds texture
            if (textureData != null)
            {
                FMLClientHandler.instance().getClient().renderEngine.bindTexture(textureData.getLocation());
            }
            else
            {
                //Backup texture bind, if no texture
                FMLClientHandler.instance().getClient().renderEngine.bindTexture(SharedAssets.GREY_TEXTURE);
            }

            //Render model
            data.render(renderOnlyParts, getPartsToRender());

            //Ends render by restoring previous matrix(rotation, position, etc)
            GL11.glPopMatrix();
            return true;
        }
        return false;
    }

    @Override
    public Pos getScale()
    {
        if (scale == null)
        {
            return parentState instanceof IModelState ? ((IModelState) parentState).getScale() : null;
        }
        else if (parentState instanceof IModelState)
        {
            //TODO add to parent rotation, or null out rotation
            //TODO setup logic via configs to allow users to decide how rotation is used
        }
        return scale;
    }

    @Override
    public Pos getOffset()
    {
        if (offset == null)
        {
            return parentState instanceof IModelState ? ((IModelState) parentState).getOffset() : null;
        }
        else if (parentState instanceof IModelState)
        {
            //TODO add to parent rotation, or null out rotation
            //TODO setup logic via configs to allow users to decide how rotation is used
        }
        return offset;
    }

    @Override
    public EulerAngle getRotation()
    {
        if (rotation == null)
        {
            return parentState instanceof IModelState ? ((IModelState) parentState).getRotation() : null;
        }
        else if (parentState instanceof IModelState)
        {
            //TODO add to parent rotation, or null out rotation
            //TODO setup logic via configs to allow users to decide how rotation is used
        }
        return rotation;
    }

    @Override
    public ModelData getModel()
    {
        if (parentState instanceof IModelState)
        {
            return ((IModelState) parentState).getModel();
        }
        return ClientDataHandler.INSTANCE.getModel(modelID);
    }

    @Override
    public String[] getPartsToRender()
    {
        if (parentState instanceof IModelState && (parts == null || parts.length == 0))
        {
            return ((IModelState) parentState).getPartsToRender();
        }
        return parts;
    }

    @Override
    public TextureData getTexture()
    {
        TextureData textureData = ClientDataHandler.INSTANCE.getTexture(textureID);
        if (textureData == null && parentState instanceof IModelState)
        {
            return ((IModelState) parentState).getTexture();
        }
        return textureData;
    }
}

最佳答案

对于通常的用例,请遵循以下操作顺序:缩放 -> 旋转 -> 平移

对于旋转本身,直观的惯例是首先滚动,然后俯仰,最后偏航。滚动沿着对象的轴发生。向上或向下倾斜滚动的物体。然后水平放置模型。

缩放发生在对象模型坐标中。 缩放发生后旋转,因此您可以相对于场景框架旋转缩放后的模型。最终,您最后使用翻译将生成的转换对象放置到位。

您有时需要进行前一轮缩放和旋转,以考虑工具/引擎在右手习惯、坐标(Z 向上或 Y 向上)和缩放方面的差异

关于java - Opengl - 旋转、缩放、平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609120/

相关文章:

java - 为什么我的recpie 显示此错误?

java - 未定义的反射和注释

java - 取消读操作

opengl - 在分割期间如何计算 gl_TessCoord?

c++ - glUnmapBuffer(GL_ARRAY_BUFFER) 与 glBindBuffer(GL_ARRAY_BUFFER,0)

C++ Opengl 渲染图像的一部分

Java Minecraft 彩色玻璃

java - 在运行时创建和覆盖 Java 枚举对象 [GregTech/minecraft]

java - Spring boot - 返回 403 Forbidden 而不是重定向到登录页面

java - 如何将可执行 jar 与可移植 JRE 打包在一起?