Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //frag
- #version 330 core
- layout (location = 0) out vec4 color;
- in DATA
- {
- vec2 tc;
- } fs_in;
- uniform sampler2D tex;
- void main()
- {
- color = texture(tex, fs_in.tc);
- if (color.w < 1.0)
- discard;
- }
- //ver
- #version 330 core
- layout ( location = 0 ) in vec4 position;
- layout ( location = 1 ) in vec2 tc;
- uniform mat4 pr_matrix;
- uniform mat4 vw_matrix;
- uniform mat4 ml_matrix = mat4(1.0);
- out DATA
- {
- vec2 tc;
- } vs_out;
- void main()
- {
- gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
- vs_out.tc = tc;
- }
- ////////////////////////////////
- CAMERA
- package ru.syswowgames.system.graphics.camera;
- import org.joml.Matrix4f;
- import org.joml.Vector3f;
- import ru.syswowgames.system.Syswow;
- import ru.syswowgames.system.shader.Shader;
- import ru.syswowgames.system.shader.ShaderManager;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL13.GL_TEXTURE1;
- import static org.lwjgl.opengl.GL13.glActiveTexture;
- public class Camera {
- private Vector3f position;
- private Vector3f rotation;
- private Vector3f view;
- private Matrix4f viewMatrix;
- private Matrix4f positionMatrix;
- private Vector3f positionNew = new Vector3f();
- private final Vector3f xAxis = new Vector3f( 1, 0, 0 );
- private final Vector3f yAxis = new Vector3f( 0, 1, 0 );
- private final Vector3f up = new Vector3f( 0, 1, 0 );
- private final Vector3f zAxis = new Vector3f( 0, 0, 1 );
- private FloatBuffer uniformMatrix4Buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
- private boolean FPSRotation = false;
- public enum FPS_MOVE_TYPE { FORWARD, BACK, LEFT, RIGHT, UP, DOWN }
- Shader shader;
- public Camera(Vector3f position) {
- this.position = position;
- this.rotation = new Vector3f();
- this.viewMatrix = new Matrix4f();
- this.positionMatrix = new Matrix4f();
- build();
- }
- public void build() {
- glActiveTexture(GL_TEXTURE1);
- glEnable(GL_DEPTH_TEST);
- shader = ShaderManager.getDefaultShader();
- shader.enable();
- Matrix4f perspectiveMatrix = new Matrix4f();
- perspectiveMatrix.perspective(1, Syswow.graphics.getAspectRatio(), 0.01f, 1000f);
- positionMatrix.identity();
- positionMatrix.translate(position);
- shader.setUniformMat4f("pr_matrix", perspectiveMatrix, uniformMatrix4Buffer);
- shader.setUniformMat4f("vw_matrix", positionMatrix, uniformMatrix4Buffer);
- shader.disable();
- }
- public void update (float deltaTime) {
- if(FPSRotation) {
- updateFPSRotation(Syswow.input.getMouseInput().getX(), Syswow.input.getMouseInput().getY());
- }
- }
- private float oldX, oldY;
- public void updateFPSRotation(float mouseX, float mouseY) {
- float deltaX = oldX - mouseX;
- float deltaY = oldY - mouseY;
- this.oldX = mouseX;
- this.oldY = mouseY;
- if(deltaX != 0 || deltaY != 0) {
- Syswow.debug.log(deltaX + " " + deltaY);
- this.rotation.x = rotation.x + deltaX;
- this.rotation.y = rotation.y + deltaY;
- if(rotation.x >= 360.0f || rotation.x <= -360.0f)
- rotation.x = rotation.x % 360.0f;
- if(rotation.y >= 360.0f || rotation.y <= -360.0f)
- rotation.y = rotation.y % 360.0f;
- if(rotation.z >= 360.0f || rotation.z <= -360.0f)
- rotation.z = rotation.z % 360.0f;
- }
- }
- public void addFPSMove(FPS_MOVE_TYPE FPSMoveType, Vector3f speed) {
- positionNew.set(position);
- if(FPSMoveType == FPS_MOVE_TYPE.FORWARD) {
- positionNew.x += Math.cos(Math.toRadians(rotation.y)) * speed.x;
- positionNew.z += Math.sin(Math.toRadians(rotation.y)) * speed.x;
- //positionNew.z += Math.cos(Math.toRadians(rotation.x)) * -Math.cos(Math.toRadians(rotation.y)) * speed.z;
- }
- if(FPSMoveType == FPS_MOVE_TYPE.RIGHT) {
- positionNew.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed.x;
- positionNew.z += Math.sin(Math.toRadians(-rotation.y)) * speed.z;
- } else if(FPSMoveType == FPS_MOVE_TYPE.LEFT) {
- // Only move in the XZ-directions
- positionNew.x -= Math.sin(Math.toRadians(rotation.y + 90)) * speed.x;
- positionNew.z -= Math.sin(Math.toRadians(-rotation.y)) * speed.z;
- } else if(FPSMoveType == FPS_MOVE_TYPE.UP) {
- positionNew.y = positionNew.y + speed.y;
- }
- position.set(positionNew);
- }
- public void render(float deltaTime) {
- positionMatrix.identity();
- //Rotate
- positionMatrix.rotateY((float)Math.toRadians(-rotation.x));
- positionMatrix.rotateZ((float)Math.toRadians(-rotation.z));
- //Set position
- positionMatrix.translate(-position.x, -position.y, -position.z);
- shader.setUniformMat4f("vw_matrix", positionMatrix, uniformMatrix4Buffer);
- }
- public Vector3f getPosition() {
- return position;
- }
- public void setPosition(Vector3f position) {
- this.position = position;
- }
- public void setFPSRotation(boolean FPSRotation) {
- this.FPSRotation = FPSRotation;
- }
- }
- ///
- CUBE
- public class ActorRender {
- private int VAO, VBO, IBO, TCBO;
- int idx = 0;
- private int count;
- private int verticesLength;
- private Matrix4f positionMatrix;
- private FloatBuffer uniformMatrix4Buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
- private Texture texture;
- public ActorRender() {
- this.positionMatrix = new Matrix4f();
- this.texture = new Texture("assets/crate.jpg");
- }
- private float[] texturesCoordinates = new float[]{
- 0, 1,
- 0, 0,
- 1, 0,
- 1, 1
- };
- private byte[] indices = new byte[]{
- 0,1,2,
- 2,3,0
- };
- public void generateMesh(float[] vertices) {
- this.verticesLength = vertices.length;
- count = indices.length;
- VAO = glGenVertexArrays();
- glBindVertexArray(VAO);
- VBO = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, BufferMaker.createFloatBuffer(vertices), GL_STATIC_DRAW);
- glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0);
- glEnableVertexAttribArray(0);
- TCBO = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, TCBO);
- glBufferData(GL_ARRAY_BUFFER, BufferMaker.createFloatBuffer(texturesCoordinates), GL_STATIC_DRAW);
- glVertexAttribPointer(Shader.TEXTURE_COORDS_ATTRIB, 2, GL_FLOAT, false, 0, 0);
- glEnableVertexAttribArray(Shader.TEXTURE_COORDS_ATTRIB);
- IBO = glGenBuffers();
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferMaker.createByteBuffer(indices), GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- }
- public void render(Vector3f position) {
- texture.bind();
- positionMatrix.identity();
- positionMatrix.translate(position);
- ShaderManager.getDefaultShader().setUniformMat4f("ml_matrix", positionMatrix, uniformMatrix4Buffer);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
- glDrawArrays(GL_TRIANGLES, 0, verticesLength);
- glBindVertexArray(0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- texture.unbind();
- }
- public void bind(){
- glBindVertexArray(VAO);
- //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
- }
- public void unbind(){
- //glBindVertexArray(0);
- //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- }
- public void draw(){
- //glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_BYTE, 0);
- glDrawArrays(GL_TRIANGLES, 0, verticesLength);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement