Advertisement
Guest User

Untitled

a guest
Dec 27th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. //frag
  2.  
  3. #version 330 core
  4.  
  5. layout (location = 0) out vec4 color;
  6.  
  7.  
  8. in DATA
  9. {
  10.     vec2 tc;
  11. } fs_in;
  12.  
  13. uniform sampler2D tex;
  14.  
  15. void main()
  16. {
  17.     color = texture(tex, fs_in.tc);
  18.     if (color.w < 1.0)
  19.         discard;
  20. }
  21.  
  22. //ver
  23.  
  24. #version 330 core
  25.  
  26. layout ( location = 0 ) in vec4 position;
  27. layout ( location = 1 ) in vec2 tc;
  28.  
  29. uniform mat4 pr_matrix;
  30. uniform mat4 vw_matrix;
  31. uniform mat4 ml_matrix = mat4(1.0);
  32.  
  33. out DATA
  34. {
  35.     vec2 tc;
  36. } vs_out;
  37.  
  38. void main()
  39. {
  40.     gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
  41.     vs_out.tc = tc;
  42. }
  43.  
  44.  
  45. ////////////////////////////////
  46.  
  47.  
  48. CAMERA
  49.  
  50. package ru.syswowgames.system.graphics.camera;
  51.  
  52. import org.joml.Matrix4f;
  53. import org.joml.Vector3f;
  54. import ru.syswowgames.system.Syswow;
  55. import ru.syswowgames.system.shader.Shader;
  56. import ru.syswowgames.system.shader.ShaderManager;
  57.  
  58. import java.nio.ByteBuffer;
  59. import java.nio.ByteOrder;
  60. import java.nio.FloatBuffer;
  61.  
  62. import static org.lwjgl.opengl.GL11.*;
  63. import static org.lwjgl.opengl.GL13.GL_TEXTURE1;
  64. import static org.lwjgl.opengl.GL13.glActiveTexture;
  65.  
  66. public class Camera {
  67.  
  68.     private Vector3f position;
  69.     private Vector3f rotation;
  70.     private Vector3f view;
  71.  
  72.     private Matrix4f viewMatrix;
  73.     private Matrix4f positionMatrix;
  74.  
  75.  
  76.     private Vector3f positionNew = new Vector3f();
  77.     private final Vector3f xAxis =  new Vector3f( 1, 0, 0 );
  78.     private final Vector3f yAxis = new Vector3f( 0, 1, 0 );
  79.     private final Vector3f up     = new Vector3f( 0, 1, 0 );
  80.     private final Vector3f zAxis = new Vector3f( 0, 0, 1 );
  81.  
  82.     private FloatBuffer uniformMatrix4Buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
  83.  
  84.     private boolean FPSRotation = false;
  85.  
  86.  
  87.     public enum FPS_MOVE_TYPE { FORWARD, BACK, LEFT, RIGHT, UP, DOWN }
  88.  
  89.     Shader shader;
  90.  
  91.     public Camera(Vector3f position) {
  92.         this.position = position;
  93.         this.rotation = new Vector3f();
  94.         this.viewMatrix = new Matrix4f();
  95.         this.positionMatrix = new Matrix4f();
  96.         build();
  97.     }
  98.  
  99.     public void build() {
  100.         glActiveTexture(GL_TEXTURE1);
  101.         glEnable(GL_DEPTH_TEST);
  102.  
  103.         shader = ShaderManager.getDefaultShader();
  104.         shader.enable();
  105.         Matrix4f perspectiveMatrix = new Matrix4f();
  106.         perspectiveMatrix.perspective(1, Syswow.graphics.getAspectRatio(), 0.01f, 1000f);
  107.         positionMatrix.identity();
  108.         positionMatrix.translate(position);
  109.         shader.setUniformMat4f("pr_matrix",  perspectiveMatrix, uniformMatrix4Buffer);
  110.         shader.setUniformMat4f("vw_matrix", positionMatrix, uniformMatrix4Buffer);
  111.         shader.disable();
  112.     }
  113.  
  114.     public void update (float deltaTime) {
  115.  
  116.         if(FPSRotation) {
  117.             updateFPSRotation(Syswow.input.getMouseInput().getX(), Syswow.input.getMouseInput().getY());
  118.         }
  119.     }
  120.  
  121.  
  122.     private float oldX, oldY;
  123.  
  124.     public void updateFPSRotation(float mouseX, float mouseY) {
  125.         float deltaX = oldX - mouseX;
  126.         float deltaY = oldY - mouseY;
  127.  
  128.         this.oldX = mouseX;
  129.         this.oldY = mouseY;
  130.  
  131.         if(deltaX != 0 || deltaY != 0) {
  132.             Syswow.debug.log(deltaX + " " + deltaY);
  133.  
  134.             this.rotation.x = rotation.x + deltaX;
  135.             this.rotation.y = rotation.y + deltaY;
  136.  
  137.  
  138.             if(rotation.x >= 360.0f || rotation.x <= -360.0f)
  139.                 rotation.x = rotation.x % 360.0f;
  140.  
  141.             if(rotation.y >= 360.0f || rotation.y <= -360.0f)
  142.                 rotation.y = rotation.y % 360.0f;
  143.  
  144.             if(rotation.z >= 360.0f || rotation.z <= -360.0f)
  145.                 rotation.z = rotation.z % 360.0f;
  146.  
  147.         }
  148.     }
  149.  
  150.     public void addFPSMove(FPS_MOVE_TYPE FPSMoveType, Vector3f speed) {
  151.         positionNew.set(position);
  152.  
  153.         if(FPSMoveType == FPS_MOVE_TYPE.FORWARD) {
  154.             positionNew.x += Math.cos(Math.toRadians(rotation.y)) * speed.x;
  155.             positionNew.z += Math.sin(Math.toRadians(rotation.y)) * speed.x;
  156.             //positionNew.z += Math.cos(Math.toRadians(rotation.x)) * -Math.cos(Math.toRadians(rotation.y)) * speed.z;
  157.         }
  158.  
  159.         if(FPSMoveType == FPS_MOVE_TYPE.RIGHT) {
  160.             positionNew.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed.x;
  161.             positionNew.z += Math.sin(Math.toRadians(-rotation.y)) * speed.z;
  162.         } else if(FPSMoveType == FPS_MOVE_TYPE.LEFT) {
  163.             // Only move in the XZ-directions
  164.             positionNew.x -= Math.sin(Math.toRadians(rotation.y + 90)) * speed.x;
  165.             positionNew.z -= Math.sin(Math.toRadians(-rotation.y)) * speed.z;
  166.         } else if(FPSMoveType == FPS_MOVE_TYPE.UP) {
  167.             positionNew.y = positionNew.y + speed.y;
  168.         }
  169.  
  170.  
  171.         position.set(positionNew);
  172.     }
  173.  
  174.     public void render(float deltaTime) {
  175.  
  176.         positionMatrix.identity();
  177.  
  178.         //Rotate
  179.         positionMatrix.rotateY((float)Math.toRadians(-rotation.x));
  180.         positionMatrix.rotateZ((float)Math.toRadians(-rotation.z));
  181.  
  182.         //Set position
  183.         positionMatrix.translate(-position.x, -position.y, -position.z);
  184.  
  185.         shader.setUniformMat4f("vw_matrix", positionMatrix, uniformMatrix4Buffer);
  186.     }
  187.  
  188.     public Vector3f getPosition() {
  189.         return position;
  190.     }
  191.  
  192.     public void setPosition(Vector3f position) {
  193.         this.position = position;
  194.     }
  195.  
  196.     public void setFPSRotation(boolean FPSRotation) {
  197.         this.FPSRotation = FPSRotation;
  198.  
  199.     }
  200.  
  201. }
  202.  
  203.  
  204. ///
  205.  
  206. CUBE
  207.  
  208. public class ActorRender {
  209.  
  210.     private int VAO, VBO, IBO, TCBO;
  211.     int idx = 0;
  212.     private int count;
  213.     private int verticesLength;
  214.     private Matrix4f positionMatrix;
  215.     private FloatBuffer uniformMatrix4Buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
  216.     private Texture texture;
  217.  
  218.     public ActorRender() {
  219.         this.positionMatrix = new Matrix4f();
  220.         this.texture = new Texture("assets/crate.jpg");
  221.     }
  222.  
  223.     private float[] texturesCoordinates = new float[]{
  224.             0, 1,
  225.             0, 0,
  226.             1, 0,
  227.             1, 1
  228.     };
  229.  
  230.     private byte[] indices = new byte[]{
  231.             0,1,2,
  232.             2,3,0
  233.     };
  234.  
  235.     public void generateMesh(float[] vertices) {
  236.         this.verticesLength = vertices.length;
  237.         count = indices.length;
  238.         VAO = glGenVertexArrays();
  239.         glBindVertexArray(VAO);
  240.  
  241.         VBO = glGenBuffers();
  242.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  243.         glBufferData(GL_ARRAY_BUFFER, BufferMaker.createFloatBuffer(vertices),  GL_STATIC_DRAW);
  244.         glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0);
  245.         glEnableVertexAttribArray(0);
  246.  
  247.  
  248.         TCBO = glGenBuffers();
  249.         glBindBuffer(GL_ARRAY_BUFFER, TCBO);
  250.         glBufferData(GL_ARRAY_BUFFER, BufferMaker.createFloatBuffer(texturesCoordinates), GL_STATIC_DRAW);
  251.         glVertexAttribPointer(Shader.TEXTURE_COORDS_ATTRIB, 2, GL_FLOAT, false, 0, 0);
  252.         glEnableVertexAttribArray(Shader.TEXTURE_COORDS_ATTRIB);
  253.  
  254.         IBO = glGenBuffers();
  255.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
  256.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferMaker.createByteBuffer(indices), GL_STATIC_DRAW);
  257.  
  258.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  259.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  260.         glBindVertexArray(0);
  261.     }
  262.  
  263.  
  264.     public void render(Vector3f position) {
  265.         texture.bind();
  266.  
  267.         positionMatrix.identity();
  268.         positionMatrix.translate(position);
  269.  
  270.         ShaderManager.getDefaultShader().setUniformMat4f("ml_matrix", positionMatrix, uniformMatrix4Buffer);
  271.  
  272.         glBindVertexArray(VAO);
  273.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
  274.  
  275.         glDrawArrays(GL_TRIANGLES, 0, verticesLength);
  276.  
  277.  
  278.         glBindVertexArray(0);
  279.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  280.         glBindVertexArray(0);
  281.  
  282.         texture.unbind();
  283.     }
  284.  
  285.     public void bind(){
  286.         glBindVertexArray(VAO);
  287.         //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
  288.     }
  289.  
  290.     public void unbind(){
  291.         //glBindVertexArray(0);
  292.         //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  293.         glBindVertexArray(0);
  294.     }
  295.  
  296.     public void draw(){
  297.         //glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_BYTE, 0);
  298.         glDrawArrays(GL_TRIANGLES, 0, verticesLength);
  299.     }
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement