libgdx 学习笔记(二)相机和观察点

Libgdx的Stage类中就默认包含了一个Camera。

Camera类按照功能而言也有很多种,最常用的是OrthographicCamera(正投影相机),Stage中默认Camera的实现类为该类。

OrthographicCamera实现以下功能:

1.移动和旋转镜头

2.放大和缩小

3.改变观察点(视角)

4.窗体和世界的点的转化


相机的使用一般配合着mesh。mesh绘制一个矩形区域


import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;


public class FirstGame implements ApplicationListener {
private OrthographicCamera camera;
private Mesh squareMesh;// 那些点坐标,颜色,纹理,材质,光照等等的一个集合
private Mesh nearSquare;


@Override
public void create() {
if (squareMesh == null) {
squareMesh = new Mesh(true, 4, 4, new VertexAttribute(
// 表示:储存顶点的数据,每个顶点的数据由三个坐标数组成(x, y ,z ),
Usage.Position, 3, "a_position"),
// 表示:如果想添加颜色
new VertexAttribute(Usage.ColorPacked, 4, "a_color"));
squareMesh.setVertices(new float[] { -0.5f, -0.5f, 0,
Color.toFloatBits(128, 0, 0, 255),  0,-0.5f, 0,
Color.toFloatBits(192, 0, 0, 255), -0.5f, 0.5f, 0,
Color.toFloatBits(192, 0, 0, 255), 0, 0.5f, 0,
Color.toFloatBits(255, 0, 0, 255) });
squareMesh.setIndices(new short[] { 0, 1, 2, 3 });
}
if (nearSquare == null) {
nearSquare = new Mesh(true, 4, 4, new VertexAttribute(
Usage.Position, 3, "a_position"), new VertexAttribute(
Usage.ColorPacked, 4, "a_color"));


nearSquare.setVertices(new float[] {  0, -0.5f, 0,
Color.toFloatBits(0, 0, 128, 255),0.5f, -0.5f, 0,
Color.toFloatBits(0, 0, 192, 255), 0, 0.5f, 0,
Color.toFloatBits(0, 0, 192, 255),0.5f, 0.5f, 0,
Color.toFloatBits(0, 0, 255, 255) });
nearSquare.setIndices(new short[] { 0, 1, 2, 3 });
}
}


@Override
public void dispose() {


}


@Override
public void pause() {


}


@Override
public void render() {
camera.update();
camera.apply(Gdx.gl10);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);// 清空屏幕
squareMesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4);// 绘制矩形
nearSquare.render(GL10.GL_TRIANGLE_STRIP, 0, 4);// 绘制矩形


}


@Override
public void resize(int width, int height) {
float aspectRatio = (float) width / (float) height;
camera = new OrthographicCamera(2f * aspectRatio, 2f);
}


@Override
public void resume() {


}


}


你可能感兴趣的:(libgdx,float,import,null,buffer,class)