Why is there like no Libgdx 3D tutorials?
Can anyone give me advice on where to learn this?
Why is there like no Libgdx 3D tutorials?
Can anyone give me advice on where to learn this?
I have been though all of them. I have been using LWJGL 3+ recently and i want to use my knowledge to make a game using libgdx but Libgdx has no tutorials on how to do anything other then animation and rendering a cube
Then use the same knowledge you have. Ive been following general Opengl/ES tutorials and apliying them to libgdx.
Its true that there arent tutorials for making a game, but there are for the diferent pars of it. Just google the right words, like Shader Libgdx, Mesh Libgdx, or whatever you need Libgdx. YouTube have great ones.
Here is the main of one of my projects to draw a room:
http://pastebin.java-gaming.org/0945f96712115
The room render code calls a reder function for each plane:
http://pastebin.java-gaming.org/945f601812519
Sorry dude but i don’t understand your code
I want to make something like this but i cant even get a quad/plane to render
Previously I used LWJGL 3+
private Vector3f position;
private int vaoID;
private int count;
private int[][][] blockData = new int[LENGTH][LENGTH][LENGTH];
private int verticesVBOID;
private int indicesVBOID;
private int texturesVBOID;
private int normalsVBOID;
public Chunk(float xPos, float yPos, float zPos) {
position = new Vector3f(xPos,yPos,zPos);
createBlockData();
createChunk(false);
}
private void createBlockData() {
for (int x = 0; x < LENGTH; x++) {
for (int y = 0; y < LENGTH; y++) {
for (int z = 0; z < LENGTH; z++) {
if(y == 1){
blockData[x][y][z] = 1;
}else if(z == 15){
blockData[x][y][z] = 1;
}else{
blockData[x][y][z] = 0;
}
}
}
}
}
public void updateChunk(){
createChunk(true);
}
private void createMesh(float[] vertices, int[] indices, float[] textureCoords, float[] normals) {
count = indices.length;
vaoID = createVAO();
indicesVBOID = bindIndices(indices);
verticesVBOID = storeAttributesInVAO(0, 3, vertices);
if (textureCoords != null)
texturesVBOID = storeAttributesInVAO(1, 2, textureCoords);
if (normals != null) {
normalsVBOID = storeAttributesInVAO(2, 3, normals);
}
unbindVAO();
}
private void updateMesh(float[] vertices, int[] indices, float[] textureCoords, float[] normals) {
count = indices.length;
vaoID = createVAO();
updateIndices(indicesVBOID,indices);
updateAttributesInVAO(verticesVBOID,0, 3, vertices);
if (textureCoords != null)
updateAttributesInVAO(texturesVBOID,1, 2, textureCoords);
if (normals != null) {
updateAttributesInVAO(normalsVBOID,2, 3, normals);
}
unbindVAO();
}
private boolean checkCullBlockLeft(int x, int y, int z) {
if (getBlockData(x - 1, y, z) != BlockType.AIR) {
return true;
}
return false;
}
private boolean checkCullBlockRight(int x, int y, int z) {
if (getBlockData(x + 1, y, z) != BlockType.AIR) {
return true;
}
return false;
}
private boolean checkCullBlockFace(int x, int y, int z) {
if (getBlockData(x, y, z + 1) != BlockType.AIR) {
return true;
}
return false;
}
private boolean checkCullBlockBack(int x, int y, int z) {
if (getBlockData(x, y, z - 1) != BlockType.AIR) {
return true;
}
return false;
}
private boolean checkCullBlockTop(int x, int y, int z) {
if (getBlockData(x, y + 1, z) != BlockType.AIR) {
return true;
}
return false;
}
private boolean checkCullBlockBottom(int x, int y, int z) {
if (getBlockData(x, y - 1, z) != BlockType.AIR) {
return true;
}
return false;
}
private void bindToTexture(List<Vector2f> textureArray, int tileX, int tileY) {
textureArray.add(new Vector2f((1f + tileX) / TEXTURES_SPLIT, (1f + tileY) / TEXTURES_SPLIT));
textureArray.add(new Vector2f((1f + tileX) / TEXTURES_SPLIT, (0f + tileY) / TEXTURES_SPLIT));
textureArray.add(new Vector2f((0f + tileX) / TEXTURES_SPLIT, (0f + tileY) / TEXTURES_SPLIT));
textureArray.add(new Vector2f((0f + tileX) / TEXTURES_SPLIT, (1f + tileY) / TEXTURES_SPLIT));
}
public int getBlockData(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0 || x >= LENGTH || y >= LENGTH || z >= LENGTH) {
return BlockType.AIR;
}
return blockData[x][y][z];
}
public void setBlock(int x,int y,int z,int value){
blockData[x][y][z] = value;
}
private void createChunk(boolean update) {
List<Vertex> verticesArray = new ArrayList<Vertex>();
List<Integer> indicesArray = new ArrayList<Integer>();
List<Vector2f> textureArray = new ArrayList<Vector2f>();
List<Vector3f> normalsArray = new ArrayList<Vector3f>();
for (int x = 0; x < LENGTH; x++) {
for (int y = 0; y < LENGTH; y++) {
for (int z = 0; z < LENGTH; z++) {
if (blockData[x][y][z] != BlockType.AIR) {
createCube(verticesArray, indicesArray, textureArray, normalsArray, x, y, z);
}
}
}
}
float[] vertices = new float[verticesArray.size() * 3];
int[] indices = new int[indicesArray.size()];
float[] textures = new float[textureArray.size() * 2];
float[] normals = new float[normalsArray.size() * 3];
int pointer = 0;
for (int i = 0; i < verticesArray.size(); i++) {
Vertex v = verticesArray.get(i);
vertices[pointer++] = v.getX();
vertices[pointer++] = v.getY();
vertices[pointer++] = v.getZ();
}
for (int i = 0; i < indices.length; i++) {
indices[i] = indicesArray.get(i);
}
pointer = 0;
for (int i = 0; i < textureArray.size(); i++) {
Vector2f texCoord = textureArray.get(i);
textures[pointer++] = texCoord.getX();
textures[pointer++] = texCoord.getY();
}
pointer = 0;
for (int i = 0; i < normalsArray.size(); i++) {
Vector3f n = normalsArray.get(i);
normals[pointer++] = n.getX();
normals[pointer++] = n.getY();
normals[pointer++] = n.getZ();
}
if(!update){
createMesh(vertices, indices, textures, normals);
}else{
updateMesh(vertices, indices, textures, normals);
}
}
private void createCube(List<Vertex> verticesArray, List<Integer> indicesArray, List<Vector2f> textureArray, List<Vector3f> normalsArray, int xPos, int yPos, int zPos) {
// Face
if (checkCullBlockFace(xPos, yPos, zPos) != true) {
indicesArray.add(verticesArray.size() + 0);
indicesArray.add(verticesArray.size() + 1);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 3);
indicesArray.add(verticesArray.size() + 0);
if(getBlockData(xPos, yPos, zPos) != BlockType.SAND ){
bindToTexture(textureArray, 2, 0);
}else{
bindToTexture(textureArray, 0, 1);
}
normalsArray.add(new Vector3f(0, 0, 1));
normalsArray.add(new Vector3f(0, 0, 1));
normalsArray.add(new Vector3f(0, 0, 1));
normalsArray.add(new Vector3f(0, 0, 1));
verticesArray.add(new Vertex(xPos, yPos, zPos));
verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
}
// Back
if (checkCullBlockBack(xPos, yPos, zPos) != true) {
indicesArray.add(verticesArray.size() + 0);
indicesArray.add(verticesArray.size() + 3);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 1);
indicesArray.add(verticesArray.size() + 0);
if(getBlockData(xPos, yPos, zPos) != BlockType.SAND ){
bindToTexture(textureArray, 2, 0);
}else{
bindToTexture(textureArray, 0, 1);
}
normalsArray.add(new Vector3f(0, 0, -1));
normalsArray.add(new Vector3f(0, 0, -1));
normalsArray.add(new Vector3f(0, 0, -1));
normalsArray.add(new Vector3f(0, 0, -1));
verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
}
// Left
if (checkCullBlockLeft(xPos, yPos, zPos) != true) {
indicesArray.add(verticesArray.size() + 0);
indicesArray.add(verticesArray.size() + 1);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 3);
indicesArray.add(verticesArray.size() + 0);
if(getBlockData(xPos, yPos, zPos) != BlockType.SAND ){
bindToTexture(textureArray, 2, 0);
}else{
bindToTexture(textureArray, 0, 1);
}
normalsArray.add(new Vector3f(-1, 0, 0));
normalsArray.add(new Vector3f(-1, 0, 0));
normalsArray.add(new Vector3f(-1, 0, 0));
normalsArray.add(new Vector3f(-1, 0, 0));
verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
verticesArray.add(new Vertex(xPos, yPos, zPos));
}
// Right
if (checkCullBlockRight(xPos, yPos, zPos) != true) {
indicesArray.add(verticesArray.size() + 0);
indicesArray.add(verticesArray.size() + 3);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 1);
indicesArray.add(verticesArray.size() + 0);
if(getBlockData(xPos, yPos, zPos) != BlockType.SAND ){
bindToTexture(textureArray, 2, 0);
}else{
bindToTexture(textureArray, 0, 1);
}
normalsArray.add(new Vector3f(1, 0, 0));
normalsArray.add(new Vector3f(1, 0, 0));
normalsArray.add(new Vector3f(1, 0, 0));
normalsArray.add(new Vector3f(1, 0, 0));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
}
// Bottom
if (checkCullBlockBottom(xPos, yPos, zPos) != true) {
indicesArray.add(verticesArray.size() + 0);
indicesArray.add(verticesArray.size() + 3);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 1);
indicesArray.add(verticesArray.size() + 0);
if(getBlockData(xPos, yPos, zPos) != BlockType.SAND ){
bindToTexture(textureArray, 1, 0);
}else{
bindToTexture(textureArray, 0, 1);
}
normalsArray.add(new Vector3f(0, -1, 0));
normalsArray.add(new Vector3f(0, -1, 0));
normalsArray.add(new Vector3f(0, -1, 0));
normalsArray.add(new Vector3f(0, -1, 0));
verticesArray.add(new Vertex(xPos, yPos, zPos));
verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
}
if (checkCullBlockTop(xPos, yPos, zPos) != true) {
// Top
indicesArray.add(verticesArray.size() + 0);
indicesArray.add(verticesArray.size() + 1);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 2);
indicesArray.add(verticesArray.size() + 3);
indicesArray.add(verticesArray.size() + 0);
if(getBlockData(xPos, yPos, zPos) != BlockType.SAND ){
bindToTexture(textureArray, 0, 0);
}else{
bindToTexture(textureArray, 0, 1);
}
normalsArray.add(new Vector3f(0, 1, 0));
normalsArray.add(new Vector3f(0, 1, 0));
normalsArray.add(new Vector3f(0, 1, 0));
normalsArray.add(new Vector3f(0, 1, 0));
verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
}
}
private Vector3f tempPos = new Vector3f();
public void render(StaticShader shader) {
shader.start();
{
GL30.glBindVertexArray(getVaoID());
{
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
{
tempPos.set((position.x * LENGTH), (position.y * LENGTH), (position.z * LENGTH)+(LENGTH*BLOCK_LENGTH));
shader.loadTransformationMatrix(Utils.createTransformationMatrix3D(tempPos, new Vector3f(), new Vector3f(1, 1, 1)));
GL11.glDrawElements(GL11.GL_TRIANGLES, getCount(), GL11.GL_UNSIGNED_INT, 0);
}
GL20.glDisableVertexAttribArray(2);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(0);
}
GL30.glBindVertexArray(0);
}
shader.stop();
}
private int bindIndices(int[] data) {
int iboID = glGenBuffers();
DisposeManager.addVBO(iboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
IntBuffer buffer = Utils.createIntBuffer(data);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW);
return iboID;
}
private int updateIndices(int iboID,int[] data) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
IntBuffer buffer = Utils.createIntBuffer(data);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW);
return iboID;
}
private int storeAttributesInVAO(int index, int size, float[] data) {
int vboID = glGenBuffers();
DisposeManager.addVBO(vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
{
FloatBuffer buffer = Utils.createFloatBuffer(data);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW);
GL20.glVertexAttribPointer(index, size, GL11.GL_FLOAT, false, 0, 0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
return vboID;
}
private void updateAttributesInVAO(int vboID,int index, int size, float[] data) {
glBindBuffer(GL_ARRAY_BUFFER, vboID);
{
FloatBuffer buffer = Utils.createFloatBuffer(data);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW);
GL20.glVertexAttribPointer(index, size, GL11.GL_FLOAT, false, 0, 0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
private int createVAO() {
int vao = glGenVertexArrays();
DisposeManager.addVAO(vao);
glBindVertexArray(vao);
return vao;
}
private void unbindVAO() {
glBindVertexArray(0);
}
public int getVaoID() {
return vaoID;
}
public void setVaoID(int vaoID) {
this.vaoID = vaoID;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Vector3f getPosition() {
return position;
}
Here is my attempt on Libgdx
public static final int FRAME_WIDTH = 1280;
public static final int FRAME_HEGIHT = 720;
public static final int FPS_CAP = 60;
public static final boolean VSYNCENABLED = false;
// Camera Settings
public static final int FOV = 67;
public static final float NEAR = 0.1f;
public static final float FAR = 300f;
public static PerspectiveCamera camera;
public Model model;
public ModelBatch modelBatch;
public ModelInstance instance;
private Mesh mesh;
@Override
public void create () {
modelBatch = new ModelBatch();
init();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
blockInit();
}
private void init(){
camera = new PerspectiveCamera(FOV,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.far = FAR;
camera.near = NEAR;
camera.position.set(0, 0, 50);
camera.update();
}
private void blockInit(){
float[] vertices = {
-1,-1,0,
-1,1,0,
1,1,0,
1,-1,0,
};
short[] indices = {
0,1,2,
2,3,0
};
VertexAttributes attribs = new VertexAttributes(
new VertexAttribute(Usage.Position,3,ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.Position,2,ShaderProgram.TEXCOORD_ATTRIBUTE)
);
mesh = new Mesh(true, vertices.length, indices.length, attribs);
mesh.setVertices(vertices);
mesh.setIndices(indices);
Material m = new Material(ColorAttribute.createDiffuse(Color.GREEN));
model = ModelBuilder.createFromMesh(mesh, GL20.GL_TRIANGLES, m);
instance = new ModelInstance(model);
}
@Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(camera);
modelBatch.render(instance);
modelBatch.end();
}
@Override
public void dispose() {
modelBatch.dispose();
model.dispose();
}
It looks like you didn’t set your camera to look at your cube.
camera.lookAt(0, 0, 0);
You also overwrite your instance. You first create a Model then call blockInit to create a new instance, overwriting the first model.
If your set of vertices is going to work you need to set the Z vertex to 1 or something. Right now your max Z depth is 0.1 but your vertices are at 0 so they won’t be seen.
I looked at your posts earlier , you have mentioned having troubles with 3d very frequently. The issue I have here is that when you started out less than a year ago you did’nt know where to start with a camera class , perfectly fine we were all beginners. But we didn’t begin making magical games over night ,especially not 3d ones. Currently you should stick with 2D , the maths is simpler , your brain wont hurt as much and well it’s just a lot faster to get a usable result from. When you have made a few 2D games , then start 3d because from what ive seen you cannot really understand it fully which is perfectly fine , but it also means that you need to be able to comprehend all the things you are doing in 2D first.
Wow i have done a lot of posts
Yeah i started just over a year ago but I have the grips with most of the programming knowledge but i have been moving all over to place from Java 2d api, lwjgl to libgdx.
What i usually do is learn all of the fundamentals of a certain subject using LWJGL or Java 2D Api and then try and create a game using Libgdx for android my problem is that Libgdx makes things all complicated and there is hardly any tutorials that actually explain in enough detail how it works and how to use it. So i post on the Java Game Forum for other peoples knowledge on this stuff and as far as i know not many people know how to do 3D in libgdx
I program 24/7 or atleast try to
^^ Yeah sorry didnt make it clear
I was only messing around so instead of creating two objects i just override the other one
I did get a cube to render though but very glitchy
public static final int FRAME_WIDTH = 1280;
public static final int FRAME_HEGIHT = 720;
public static final int FPS_CAP = 60;
public static final boolean VSYNCENABLED = false;
// Camera Settings
public static final int FOV = 67;
public static final float NEAR = 0.1f;
public static final float FAR = 300f;
public static PerspectiveCamera camera;
public Model model;
public ModelBatch modelBatch;
public ModelInstance instance;
private Mesh mesh;
private ShaderProgram shader;
@Override
public void create () {
String vertexShader = "attribute vec4 a_position; \n" +
"attribute vec4 a_color;\n" +
"attribute vec2 a_texCoord0;\n" +
"uniform mat4 u_worldView;\n" +
"varying vec4 v_color;" +
"varying vec2 v_texCoords;" +
"void main() \n" +
"{ \n" +
" v_color = vec4(1,0,0,1); \n" +
" v_texCoords = a_texCoord0; \n" +
" gl_Position = u_worldView * a_position; \n" +
"} \n" ;
String fragmentShader = "#ifdef GL_ES\n" +
"precision mediump float;\n" +
"#endif\n" +
"varying vec4 v_color;\n" +
"varying vec2 v_texCoords;\n" +
"uniform sampler2D u_texture;\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = v_color;\n" +
"}";
shader = new ShaderProgram(vertexShader, fragmentShader);
modelBatch = new ModelBatch();
init();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
blockInit();
}
private void init(){
camera = new PerspectiveCamera(FOV,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.far = FAR;
camera.near = NEAR;
camera.position.set(0, 0, 50);
camera.update();
}
private void blockInit(){
float[] vertices = {
-1f, -1f, 0, // bottom left
1f, -1f, 0, // bottom right
1f, 1f, 0, // top right
-1f, 1f, 0,
};
short[] indices = {
0,1,2,
2,3,0
};
VertexAttributes attribs = new VertexAttributes(
new VertexAttribute(Usage.Position,3,ShaderProgram.POSITION_ATTRIBUTE)
);
mesh = new Mesh(true, vertices.length, indices.length, attribs);
mesh.setVertices(vertices);
mesh.setIndices(indices);
instance = new ModelInstance(model);
}
@Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
shader.begin();
shader.enableVertexAttribute(0);
shader.enableVertexAttribute(1);
shader.enableVertexAttribute(2);
shader.setUniformMatrix("u_worldView", camera.combined);
shader.disableVertexAttribute(2);
shader.disableVertexAttribute(1);
shader.disableVertexAttribute(0);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
}
@Override
public void dispose() {
modelBatch.dispose();
model.dispose();
}
One problem is that you think you are rendering a cube when you are only rendering a face. A cube has 6 faces with a total of 8 vertices.
I ran your code an there were no glitches for me. Define “glitchy”.
sorry again my mistake I meant face.
If you look at that photo above i am trying to do that same thing how would i do such a thing?
Example code would be helpful
If you willing to help via skype then my skype it: tommohawkaction
I don’t do much 3D myself, I just know the concepts.
That image looks like he is using billboarding. Search on that term to see if it gives you what you want.
No its quite simple as he doesnt rotate the camera so no billboarding is needed.
I am just stuck trying to get a plane to render with colour/texture ahh
Is there a way i get can hold of the default shader instead of having to create a new one for the meshes?
String vertexShader = "attribute vec4 a_position; \n" +
"attribute vec4 a_color;\n" +
"attribute vec2 a_texCoord0;\n" +
"uniform mat4 u_worldView;\n" +
"varying vec4 v_color;" +
"varying vec2 v_texCoords;" +
"void main() \n" +
"{ \n" +
" v_color = vec4(1,0,0,1); \n" +
" v_texCoords = a_texCoord0; \n" +
" gl_Position = u_worldView * a_position; \n" +
"} \n" ;
String fragmentShader = "#ifdef GL_ES\n" +
"precision mediump float;\n" +
"#endif\n" +
"varying vec4 v_color;\n" +
"varying vec2 v_texCoords;\n" +
"uniform sampler2D u_texture;\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = v_color;\n" +
"}";
shader = new ShaderProgram(vertexShader, fragmentShader);
modelBatch
Okay been working on this all day now and i have got no where other then a plane (with colour) on the ground however for me to get this to work i had to create my own shader and make a transformation matrix using a method i made in LWJGL… I thought libgdx had all the basic stuff done?
I am now struggling with textureing as the Texture class doesnt allow you to access its ID but you can bind it but that doesnt work as i need to tell the shader which texture to draw using its ID… ahhhhhhhhh >:(
The default shader can be obtained by calling
SpriteBatch.createDefaultShader().
You can then get the source if you wanted by calling
shader.getVertexShaderSource()
shader.getFragmentShaderSource()
Thanks
Okay been working on this all day now and i have got no where other then a plane (with colour) on the ground however for me to get this to work i had to create my own shader and make a transformation matrix using a method i made in LWJGL… I thought libgdx had all the basic stuff done?
I am now struggling with textureing as the Texture class doesnt allow you to access its ID but you can bind it but that doesnt work as i need to tell the shader which texture to draw using its ID… ahhhhhhhhh >:(
JUST IN CASE YOU DIDN’T SEE IT LOL
public static final int FRAME_WIDTH = 1280;
public static final int FRAME_HEGIHT = 720;
public static final int FPS_CAP = 60;
public static final boolean VSYNCENABLED = false;
// Camera Settings
public static final int FOV = 67;
public static final float NEAR = 0.1f;
public static final float FAR = 300f;
public static PerspectiveCamera camera;
public static FirstPersonCameraController input;
public Model model;
public ModelBatch modelBatch;
public ModelInstance instance;
private Mesh mesh;
private ShaderProgram shader;
private Matrix4 transformation;
private Texture texture;
@Override
public void create () {
String vertexShader = "attribute vec4 a_position; \n" +
"attribute vec4 a_color;\n" +
"attribute vec2 a_texCoord0;\n" +
"uniform mat4 u_worldView;\n" +
"uniform mat4 trans;\n" +
"varying vec4 v_color;" +
"varying vec2 v_texCoords;" +
"void main() \n" +
"{ \n" +
" v_color = a_color; \n" +
" v_texCoords = a_texCoord0; \n" +
" gl_Position = u_worldView * (trans * a_position); \n" +
"} \n" ;
String fragmentShader = "#ifdef GL_ES\n" +
"precision mediump float;\n" +
"#endif\n" +
"varying vec4 v_color;\n" +
"varying vec2 v_texCoords;\n" +
"uniform sampler2D u_texture;\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = texture2D(u_texture,v_texCoords);\n" +
"}";
shader = new ShaderProgram(vertexShader, fragmentShader);
modelBatch = new ModelBatch();
init();
blockInit();
input = new FirstPersonCameraController(camera);
texture = new Texture(Gdx.files.internal("tileable_grass_00.png"));
texture.bind();
Gdx.input.setInputProcessor(input);
}
private void init(){
camera = new PerspectiveCamera(FOV,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.far = FAR;
camera.near = NEAR;
camera.position.set(0, 0, 0);
camera.update();
}
private void blockInit(){
float[] vertices = {
-1f, 0, -1f,1,0,0, // bottom left
1f, 0, -1f,1,1,0, // bottom right
1f, 0, 1f,1,1,1, // top right
-1f, 0, 1f,0,1,
};
short[] indices = {
0,1,2,
2,3,0
};
VertexAttributes attribs = new VertexAttributes(
new VertexAttribute(Usage.Position,3,ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates,2,ShaderProgram.TEXCOORD_ATTRIBUTE)
);
mesh = new Mesh(true, vertices.length, indices.length, attribs);
mesh.setVertices(vertices);
mesh.setIndices(indices);
}
@Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
input.update();
camera.update();
transformation = Utils.createTransformationMatrix(new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(10,1,10));
shader.begin();
shader.setUniformMatrix("trans", transformation);
shader.setUniformMatrix("u_worldView", camera.combined);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
}
@Override
public void dispose() {
modelBatch.dispose();
model.dispose();
}