Opengl如何在窗口大小上使用正交矩阵缩放对象

时间:2015-03-02 08:31:20

标签: java opengl

好吧我有一个正交矩阵(左= 0,右= 800,底= 0,顶= 600,近= -1.0f,远= 1.0f)。如何缩放对象,如半尺寸400宽度和高度为300.当我按比例缩放时,只有0.5f,一半尺寸而不是400或300。

MainGame java

package com.engine.base.test;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.util.glu.GLU;

import com.engine.base.game.Game;
import com.engine.base.game.GameObject;
import com.engine.base.maths.Vector3f;
import com.engine.base.utils.Console;

public class MainGame extends Game{

    public MainGame(int width, int height, String title) {
        super(width, height, title);
    }

    @Override
    protected void updateGame() {
        Console.printLog(GLU.gluErrorString(glGetError()));

    }

    @Override
    public void init() {
        GameObject object = new GameObject();
        object.getTransformation().Scale2f(400, 300); //Here it only scales from 0.0f to 1.0f
        object.getTransformation().Rotate1f(150);
        //object.getTool().setColor(new Vector3f(0.2f, 1f, 0.5f));
        //object.setAmbientLight(new Vector3f(0.2f, 0.2f, 0.2f));
        getLevel().addObject(object);
    }

    @Override
    public void destroy(){

    }

}

Rectangle java(这里是我编写椎体和索引的地方)

package com.engine.base.graphicComponents;

import com.engine.base.graphics.Mesh;
import com.engine.base.graphics.Vertex;
import com.engine.base.maths.Vector3f;

public class Rectangle extends Shape{

public Rectangle(float x, float y, float width, float height, Vector3f color)
{
    super(x, y,color);
}

public Rectangle(float x, float y, float width, float height)
{
    super(x, y, new Vector3f(1, 1, 1));
}

public Mesh getMesh(){
    Vertex[] vertecies = new Vertex[]{
        new Vertex(new Vector3f(800, 600, 0)),
        new Vertex(new Vector3f(0, 600, 0)),
        new Vertex(new Vector3f(800, -0, 0)),
        new Vertex(new Vector3f(0, 0, 0))
    };

    int[] indices = new int[]{
        3, 1, 0,
        3, 2, 0
    };
    return new Mesh(vertecies, indices);
}

}

转换java

package com.engine.base.game;

import org.lwjgl.util.vector.Vector2f;

import com.engine.base.maths.Matrix4f;

public class Transformation {

    private Vector2f pos, scale;
    private float angle = -1;

    public Transformation(){
        pos = new Vector2f(0, 0);
        scale = new Vector2f(1, 1);
        angle = 0;
    }

    public Matrix4f getWorldTransformation(){
        return new Matrix4f().initOrthographic(0, 800, 0, 600, -1.0f, 1.0f).mul(new Matrix4f().initTranslation(pos.getX(), pos.getY(), 0).mul(new Matrix4f().initRotation(angle).mul(new Matrix4f().initScale(scale.getX(), scale.getY(), 0))));
    }

    public void Rotate1f(float angle){
        this.angle = angle;
    }

    public void Scale2f(float x, float y){
        scale = new Vector2f(x, y);
    }

    public void Translate2f(float x, float y){
        pos = new Vector2f(x, y);
    }

    public Vector2f getPos() {
        return pos;
    }

    public void setPos(Vector2f pos) {
        this.pos = pos;
    }

    public Vector2f getScale() {
        return scale;
    }

    public void setScale(Vector2f scale) {
        this.scale = scale;
    }

    public float getAngle() {
        return angle;
    }

    public void setAngle(float angle) {
        this.angle = angle;
    }

}

0 个答案:

没有答案