From 145438d5f40e2fe8f7bfc8b9103c74feef6203dd Mon Sep 17 00:00:00 2001 From: "Florian Richer (MrDev023)" Date: Thu, 24 Dec 2015 13:15:02 +0100 Subject: [PATCH] Update --- res/shaders/font.frag | 12 +++ res/shaders/font.vert | 15 ++++ res/textures/font.png | Bin 0 -> 1379 bytes src/mrdev023/audio/Audio.java | 74 +++++++++++++++--- src/mrdev023/gameengine/GameEngine.java | 9 ++- src/mrdev023/gamestate/MainMenu.java | 4 +- src/mrdev023/math/Color4f.java | 8 ++ src/mrdev023/opengl/Display.java | 15 ++++ src/mrdev023/opengl/DisplayManager.java | 4 + src/mrdev023/rendering/Shader.java | 5 +- src/mrdev023/rendering/TextFont.java | 96 ++++++++++++++++++++++++ src/mrdev023/rendering/Texture.java | 27 ++++--- 12 files changed, 243 insertions(+), 26 deletions(-) create mode 100644 res/shaders/font.frag create mode 100644 res/shaders/font.vert create mode 100644 res/textures/font.png create mode 100644 src/mrdev023/rendering/TextFont.java diff --git a/res/shaders/font.frag b/res/shaders/font.frag new file mode 100644 index 0000000..bb415e0 --- /dev/null +++ b/res/shaders/font.frag @@ -0,0 +1,12 @@ +#version 330 + +in vec4 color; +in vec2 out_coord_texture; +uniform sampler2D myTexture; + + +out vec4 frag_color; + +void main(){ + frag_color = color * texture2D(myTexture,out_coord_texture); +} \ No newline at end of file diff --git a/res/shaders/font.vert b/res/shaders/font.vert new file mode 100644 index 0000000..09d8634 --- /dev/null +++ b/res/shaders/font.vert @@ -0,0 +1,15 @@ +#version 330 +layout (location = 0) in vec3 in_position; +layout (location = 1) in vec4 in_color; +layout (location = 2) in vec2 in_coord_texture; + +uniform mat4 projection; + +out vec4 color; +out vec2 out_coord_texture; + +void main(){ + color = in_color; + out_coord_texture = in_coord_texture; + gl_Position = projection * vec4(in_position,1.0f); +} diff --git a/res/textures/font.png b/res/textures/font.png new file mode 100644 index 0000000000000000000000000000000000000000..f614b318ec9595461ab3528c247894fedad2a2b2 GIT binary patch literal 1379 zcmV-p1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zvpq)I!J}KUu8jA lights; + public TextFont text; public void update() { time+=Timer.getDeltaTime(); @@ -69,10 +70,11 @@ public class MainMenu extends Game implements IGameState{ lights.add(new AmbientLight(new Vector3f(100,100,100),Color4f.WHITE,Color4f.mul(Color4f.WHITE,1.0f))); vao = MeshBuilder.createFloor(400, Color4f.WHITE,Texture.FLOOR); cube = MeshBuilder.createCube(100, Color4f.WHITE,Texture.WOOD); + text = new TextFont("Test", 10, 10, 16, Color4f.WHITE); } public void render2D() { - + text.drawText(); } public void render3D() { diff --git a/src/mrdev023/math/Color4f.java b/src/mrdev023/math/Color4f.java index 68545a9..401fa47 100644 --- a/src/mrdev023/math/Color4f.java +++ b/src/mrdev023/math/Color4f.java @@ -1,5 +1,7 @@ package mrdev023.math; +import static org.lwjgl.opengl.GL11.*; + public class Color4f { public static final Color4f @@ -64,6 +66,12 @@ public class Color4f { this.a = a; } + public void bind(){ + glColor4f(r,g,b,a); + } + public void unbind(){ + BLACK.bind(); + } } diff --git a/src/mrdev023/opengl/Display.java b/src/mrdev023/opengl/Display.java index e0acc11..789d294 100644 --- a/src/mrdev023/opengl/Display.java +++ b/src/mrdev023/opengl/Display.java @@ -8,6 +8,7 @@ import java.awt.*; import java.nio.*; import org.lwjgl.*; +import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; import mrdev023.exception.*; @@ -103,6 +104,20 @@ public class Display { } } + public static void printMonitorsInfo(){ + PointerBuffer monitors = glfwGetMonitors(); + GLFWVidMode m; + if(monitors == null){ + System.out.println("No monitor detected !"); + return; + } + for(int i = 0;i < monitors.capacity();i++){ + m = glfwGetVideoMode(monitors.get(i)); + System.out.println(glfwGetMonitorName(monitors.get(i)) + "(" + i + ") : " + m.width() + "x" + m.height() + ":" + m.refreshRate() + "Hz"); + } + + } + public static boolean isCloseRequested(){ return glfwWindowShouldClose(window) == GL_TRUE; } diff --git a/src/mrdev023/opengl/DisplayManager.java b/src/mrdev023/opengl/DisplayManager.java index a59ad61..133f70f 100644 --- a/src/mrdev023/opengl/DisplayManager.java +++ b/src/mrdev023/opengl/DisplayManager.java @@ -23,6 +23,10 @@ public class DisplayManager { projection.loadIdentity(); projection.Ortho2D(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), 1, -1); Shader.MAIN.uniform("projection", projection); + Shader.FONT.uniform("projection", projection); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_2D); } public static void preRender3D(){ diff --git a/src/mrdev023/rendering/Shader.java b/src/mrdev023/rendering/Shader.java index 7896be3..b5dc124 100644 --- a/src/mrdev023/rendering/Shader.java +++ b/src/mrdev023/rendering/Shader.java @@ -12,12 +12,15 @@ public class Shader { public int program; public int includeV = 0,includeF = 0; - public static Shader MAIN,LIGHT,LIGHT_AMBIENT; + public static Shader MAIN,LIGHT,LIGHT_AMBIENT,FONT; public static void init() { + System.out.println("---------------------------- Load Shader ---------------------------------------------"); MAIN = new Shader("res/shaders/main"); + FONT = new Shader("res/shaders/font"); LIGHT = new Shader("res/shaders/light"); LIGHT_AMBIENT = new Shader("res/shaders/light_ambient"); + System.out.println("--------------------------------------------------------------------------------------"); } public Shader(String file){ diff --git a/src/mrdev023/rendering/TextFont.java b/src/mrdev023/rendering/TextFont.java new file mode 100644 index 0000000..6cb2106 --- /dev/null +++ b/src/mrdev023/rendering/TextFont.java @@ -0,0 +1,96 @@ +package mrdev023.rendering; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL15.*; +import static org.lwjgl.opengl.GL20.*; +import static org.lwjgl.opengl.GL30.*; + +import java.nio.*; + +import org.lwjgl.*; + +import mrdev023.math.*; + +public class TextFont { + + private int x,y,vbo,size,sizeVBO; + private String text; + private Color4f color; + + private static final String chars = "" + // + "ABCDEFGHIJKLMNOPQRSTUVWXYZ " + // + "0123456789.,!?'\"-+=/\\%()<>:; " + // + ""; + + private static final int SIZE_OF_FLOAT = 4; + + public TextFont(String text,int x,int y,int size,Color4f color){ + this.size = size; + this.x = x; + this.y = y; + this.text = text; + this.color = color; + String msg = text.toUpperCase(); + FloatBuffer buffer = BufferUtils.createFloatBuffer(8*4*msg.length()); + for (int i = 0; i < msg.length(); i++) { + int ix = chars.indexOf(msg.charAt(i)); + int iy = 0; + if (ix >= 32) iy = 1; + if (iy >= 0) { + if (ix >= 0) { + float xx = x + i * size; + float yy = y; + + float yo = iy; + float xo = ix % 32; + float texSize = 32.0f; + + buffer.put(new float[]{ + xx + size, yy, color.r,color.g,color.b,color.a, (1 + xo) / texSize, (0 + yo) / 2.0f, + xx , yy, color.r,color.g,color.b,color.a, (0 + xo) / texSize, (0 + yo) / 2.0f, + xx, yy + size, color.r,color.g,color.b,color.a, (0 + xo) / texSize, (1 + yo) / 2.0f, + xx + size, yy + size, color.r,color.g,color.b,color.a, (1 + xo) / texSize, (1 + yo) / 2.0f + }); + } + } + } + buffer.flip(); + sizeVBO = buffer.capacity(); + + vbo = glGenBuffers(); + + glBindBuffer(GL_ARRAY_BUFFER,vbo); + glBufferData(GL_ARRAY_BUFFER,buffer,GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER,0); + + } + + + public void drawText() { + Shader.FONT.bind(); + glBindTexture(GL_TEXTURE_2D, Texture.FONT.getID()); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + + glVertexAttribPointer(0, 2, GL_FLOAT, false, (2 + 4 + 2) * SIZE_OF_FLOAT, 0); + glVertexAttribPointer(1, 4, GL_FLOAT, false, (2 + 4 + 2) * SIZE_OF_FLOAT, (2) * SIZE_OF_FLOAT); + glVertexAttribPointer(2, 2, GL_FLOAT, false, (2 + 4 + 2) * SIZE_OF_FLOAT, (2 + 4) * SIZE_OF_FLOAT); + + glDrawArrays(GL_QUADS, 0, sizeVBO/(2 + 4 + 2)); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + Shader.MAIN.bind(); + } + + public void destroy(){ + glDeleteBuffers(vbo); + } +} diff --git a/src/mrdev023/rendering/Texture.java b/src/mrdev023/rendering/Texture.java index ce6f6b4..138bea4 100644 --- a/src/mrdev023/rendering/Texture.java +++ b/src/mrdev023/rendering/Texture.java @@ -13,14 +13,17 @@ import org.lwjgl.*; public class Texture { - public static Texture FLOOR,WOOD; + public static Texture FLOOR,WOOD,FONT; int width, height; int id; public static void init() { + System.out.println("---------------------------- Load Texture --------------------------------------------"); FLOOR = Texture.loadTexture("res/textures/floor.jpg"); WOOD = Texture.loadTexture("res/textures/wood.jpg"); + FONT = Texture.loadTexture("res/textures/font.png"); + System.out.println("--------------------------------------------------------------------------------------"); } public Texture(int width,int height,int id){ @@ -38,16 +41,18 @@ public class Texture { image.getRGB(0, 0, width, height, pixels, 0,width); - ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4); - for(int y = 0; y < width; y++){ - for(int x = 0; x < height; x++){ - int i = pixels[x + y * width];//0xAARRGGBB - buffer.put((byte) ((i >> 16) & 0xFF));//r - buffer.put((byte) ((i >> 8) & 0xFF));//g - buffer.put((byte) ((i) & 0xFF));//b - buffer.put((byte) ((i >> 24) & 0xFF));//a - } + int[] data = new int[pixels.length]; + for (int i = 0; i < data.length; i++) { + int a = (pixels[i] & 0xff000000) >> 24; + int r = (pixels[i] & 0xff0000) >> 16; + int g = (pixels[i] & 0xff00) >> 8; + int b = (pixels[i] & 0xff); + + data[i] = a << 24 | b << 16 | g << 8 | r; } + + IntBuffer buffer = BufferUtils.createIntBuffer(data.length); + buffer.put(data); buffer.flip(); int id = glGenTextures(); @@ -59,7 +64,7 @@ public class Texture { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); glBindTexture(GL_TEXTURE_2D, 0);