Update
This commit is contained in:
parent
f298038298
commit
145438d5f4
12 changed files with 243 additions and 26 deletions
12
res/shaders/font.frag
Normal file
12
res/shaders/font.frag
Normal file
|
@ -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);
|
||||
}
|
15
res/shaders/font.vert
Normal file
15
res/shaders/font.vert
Normal file
|
@ -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);
|
||||
}
|
BIN
res/textures/font.png
Normal file
BIN
res/textures/font.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
|
@ -11,13 +11,12 @@ import java.io.*;
|
|||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.openal.*;
|
||||
|
||||
import static org.lwjgl.BufferUtils.*;
|
||||
|
||||
import org.lwjgl.openal.ALCCapabilities;
|
||||
import org.lwjgl.openal.ALContext;
|
||||
import org.lwjgl.openal.ALDevice;
|
||||
import org.lwjgl.stb.STBVorbisInfo;
|
||||
|
||||
public class Audio {
|
||||
|
@ -44,6 +43,7 @@ public class Audio {
|
|||
if ( device == null )
|
||||
throw new IllegalStateException("Failed to open the default device.");
|
||||
caps = device.getCapabilities();
|
||||
System.out.println("---------------------------- Create Audio Device -------------------------------------");
|
||||
System.out.println("OpenALC10: " + caps.OpenALC10);
|
||||
System.out.println("OpenALC11: " + caps.OpenALC11);
|
||||
System.out.println("caps.ALC_EXT_EFX = " + caps.ALC_EXT_EFX);
|
||||
|
@ -58,6 +58,7 @@ public class Audio {
|
|||
System.out.println("ALC_SYNC: " + (alcGetInteger(device.address(), ALC_SYNC) == ALC_TRUE));
|
||||
System.out.println("ALC_MONO_SOURCES: " + alcGetInteger(device.address(), ALC_MONO_SOURCES));
|
||||
System.out.println("ALC_STEREO_SOURCES: " + alcGetInteger(device.address(), ALC_STEREO_SOURCES));
|
||||
System.out.println("---------------------------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
public static void destroy(){
|
||||
|
@ -93,12 +94,64 @@ public class Audio {
|
|||
System.out.println(fileName + " loaded !" + " | TIME : " + (size/channels/(bits/8)/freq) + "s | BITS : " + bits + " | CHANNELS : " + channels + " | FREQUENCE : " + freq + " FORMAT : " + format);
|
||||
}
|
||||
|
||||
public void loadWavFormat() throws FileNotFoundException{
|
||||
WaveData soundData = WaveData.create(new BufferedInputStream(new FileInputStream(fileName)));
|
||||
buffer = alGenBuffers();
|
||||
source = alGenSources();
|
||||
alBufferData(buffer, soundData.format, soundData.data, soundData.samplerate);
|
||||
soundData.dispose();
|
||||
public void loadWavFormat() throws Exception{
|
||||
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(fileName)));
|
||||
AudioFormat audioformat = ais.getFormat();
|
||||
|
||||
// get channels
|
||||
int channels = 0;
|
||||
if (audioformat.getChannels() == 1) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_MONO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_MONO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else if (audioformat.getChannels() == 2) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_STEREO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_STEREO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else {
|
||||
assert false : "Only mono or stereo is supported";
|
||||
}
|
||||
|
||||
int available = ais.available();
|
||||
if(available <= 0) {
|
||||
available = ais.getFormat().getChannels() * (int) ais.getFrameLength() * ais.getFormat().getSampleSizeInBits() / 8;
|
||||
}
|
||||
byte[] buf = new byte[ais.available()];
|
||||
int read = 0, total = 0;
|
||||
while ((read = ais.read(buf, total, buf.length - total)) != -1
|
||||
&& total < buf.length) {
|
||||
total += read;
|
||||
}
|
||||
byte[] audio_bytes = buf;
|
||||
boolean two_bytes_data = audioformat.getSampleSizeInBits() == 16;
|
||||
ByteOrder order = audioformat.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
|
||||
ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
|
||||
dest.order(ByteOrder.nativeOrder());
|
||||
ByteBuffer src = ByteBuffer.wrap(audio_bytes);
|
||||
src.order(order);
|
||||
if (two_bytes_data) {
|
||||
ShortBuffer dest_short = dest.asShortBuffer();
|
||||
ShortBuffer src_short = src.asShortBuffer();
|
||||
while (src_short.hasRemaining())
|
||||
dest_short.put(src_short.get());
|
||||
} else {
|
||||
while (src.hasRemaining())
|
||||
dest.put(src.get());
|
||||
}
|
||||
dest.rewind();
|
||||
|
||||
this.buffer = alGenBuffers();
|
||||
this.source = alGenSources();
|
||||
alBufferData(this.buffer, channels, dest, (int)audioformat.getSampleRate());
|
||||
dest.clear();
|
||||
}
|
||||
|
||||
public void loadOGGFormat(){
|
||||
|
@ -143,7 +196,6 @@ public class Audio {
|
|||
ByteBuffer pcm = BufferUtils.createByteBuffer(lengthSamples * 2 * channels);
|
||||
|
||||
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm, lengthSamples);
|
||||
float duration = stb_vorbis_stream_length_in_seconds(decoder);
|
||||
stb_vorbis_close(decoder);
|
||||
|
||||
buffer = alGenBuffers();
|
||||
|
|
|
@ -26,10 +26,13 @@ public class GameEngine {
|
|||
|
||||
public static void start(String title,int width,int height){
|
||||
TITLE = title;
|
||||
System.out.println("---------------------------- Create OpenGL Context -----------------------------------");
|
||||
Display.create(title, width, height);
|
||||
Display.createContext();
|
||||
Display.printMonitorsInfo();
|
||||
System.out.println("Window : " + width + "x" + height);
|
||||
System.out.println("OpenGL " + GL11.glGetString(GL11.GL_VERSION));
|
||||
|
||||
System.out.println("--------------------------------------------------------------------------------------");
|
||||
init();
|
||||
loop();
|
||||
}
|
||||
|
@ -46,13 +49,15 @@ public class GameEngine {
|
|||
Timer.addTimer("ticks");
|
||||
Timer.addTimer("fps");
|
||||
Display.setMouseGrabbed(true);
|
||||
System.out.println("---------------------------- Load Audio File -----------------------------------------");
|
||||
try {
|
||||
a = new Audio("res/audio/test.ogg");
|
||||
a = new Audio("res/audio/test.wav");
|
||||
a.playSound();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("--------------------------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
public static void loop(){
|
||||
|
|
|
@ -21,6 +21,7 @@ public class MainMenu extends Game implements IGameState{
|
|||
public int time = 0;
|
||||
public static final float speed = 1.0f;
|
||||
public ArrayList<Light> 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() {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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(){
|
||||
|
|
|
@ -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){
|
||||
|
|
96
src/mrdev023/rendering/TextFont.java
Normal file
96
src/mrdev023/rendering/TextFont.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
Reference in a new issue