finlaybob

Cube Not Texturing

Jun 11th, 2012
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package com.fbsp.tt.main.game;
  2.  
  3.  
  4.  
  5. import com.badlogic.gdx.ApplicationListener;
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.files.FileHandle;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.Mesh;
  10. import com.badlogic.gdx.graphics.Texture;
  11. import com.badlogic.gdx.graphics.VertexAttribute;
  12. import com.badlogic.gdx.graphics.VertexAttributes.Usage;
  13. import com.badlogic.gdx.graphics.g3d.loaders.obj.ObjLoader;
  14. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  15. import com.badlogic.gdx.math.Matrix4;
  16. import com.badlogic.gdx.math.Vector3;
  17.  
  18. public class Game implements ApplicationListener {
  19.  
  20.    
  21.     Matrix4 model,view,project,mvp,mvm;
  22.     float angle;
  23.     Mesh mesh;
  24.     Mesh tmesh;
  25.     ShaderProgram s;
  26.     Texture t;
  27.    
  28.     public void create() {
  29.         angle = 0;
  30.         model = new Matrix4();
  31.         view = new Matrix4();
  32.         project = new Matrix4();
  33.         mvp = new Matrix4();
  34.         mvm = new Matrix4();
  35.        
  36.        
  37.        
  38.        
  39.        
  40.        
  41.         mesh = ObjLoader.loadObj(Gdx.files.internal("Mesh/cube2.obj").read());
  42.         tmesh = new Mesh(true, 4, 6, /*...*/
  43.                 new VertexAttribute(Usage.Position, 3, "a_position"),
  44.                 new VertexAttribute(Usage.Normal, 3, "a_normal"),
  45.                 new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
  46.        
  47.         tmesh.setVertices(new float[] {
  48.                 -1,-1,0,0,0,1,0,0,
  49.                 1,-1,0,0,0,1,1,0,
  50.                 -1,1,0,0,0,1,0,1,
  51.                 1,1,0,0,0,1,1,1
  52.         });
  53.         tmesh.setIndices(new short[] {
  54.                 0,1,2,1,2,3
  55.         });
  56.         FileHandle vs = Gdx.files.internal("Shader/std.vs");
  57.         FileHandle fs = Gdx.files.internal("Shader/std.fs");
  58.        
  59.         t = new Texture(Gdx.files.internal("Image/r2.png"));
  60.  
  61.        
  62.         s = new ShaderProgram(vs, fs);
  63.         if(!s.isCompiled()){
  64.  
  65.             Gdx.app.log("Shader Probs:", s.getLog());
  66.         }
  67.                        
  68.         mesh.getVertexAttribute(Usage.Position).alias = "a_position";
  69.         mesh.getVertexAttribute(Usage.Normal).alias = "a_normal";
  70.         mesh.getVertexAttribute(Usage.TextureCoordinates).alias = "a_texCoord";
  71.     }
  72.  
  73.    
  74.     public void dispose() {
  75.        
  76.     }
  77.  
  78.     public void pause() {
  79.        
  80.     }
  81.  
  82.     public void render() {
  83.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  84.         angle++;
  85.         model.setToRotation(0, 0, 1, angle);
  86.        
  87.         mvm.set(view);
  88.         mvm.mul(model);
  89.        
  90.        
  91.         mvp.set(project.getValues());
  92.         mvp.mul(view);
  93.         mvp.mul(model);
  94.        
  95.         t.bind();
  96.        
  97.         s.begin();
  98.         s.setUniformMatrix("mvp", mvp, false);
  99.         s.setUniformi("tex", 0);
  100.            
  101.         tmesh.render(s, GL20.GL_TRIANGLES);
  102.         //mesh.render(s, GL20.GL_TRIANGLES);
  103.        
  104.     }
  105.  
  106.     public void resize(int width, int height) {
  107.         Gdx.gl.glViewport(0, 0, width, height);
  108.         Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
  109.         float ratio = (float)width/(float)height;
  110.         project.setToProjection(0.1f, 20, 65, ratio);
  111.        
  112.         view.setToLookAt(new Vector3(0,0,-5), new Vector3(0,0,0), new Vector3(0,1,0));
  113.     }
  114.  
  115.     public void resume() {
  116.         // TODO Auto-generated method stub
  117.        
  118.     }
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment