Java Opengl如何在窗口大小上使用正交矩阵缩放对象
好的,我有一个正交矩阵,左=0,右=800,下=0,上=600,近=1.0f,远=1.0f。如何缩放对象,比如400宽和300高的一半。当我缩放它时,它只缩放0.5f,而不是400或300 主游戏javaJava Opengl如何在窗口大小上使用正交矩阵缩放对象,java,opengl,Java,Opengl,好的,我有一个正交矩阵,左=0,右=800,下=0,上=600,近=1.0f,远=1.0f。如何缩放对象,比如400宽和300高的一半。当我缩放它时,它只缩放0.5f,而不是400或300 主游戏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
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(){
}
}
这里是我写顶点和索引的地方
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.5f,0.5f传递给你的比例向量,而不知道你如何使用你的比例向量。很难说。我算出了。缩放时,我应该将新大小与旧大小分开,然后我将得到比例因子。