Add Audio and Shadow Class

This commit is contained in:
Florian Richer (MrDev023) 2016-03-12 17:59:49 +01:00
parent 63023152fe
commit 0f4d6c37e8
10 changed files with 479 additions and 2 deletions

View file

@ -1 +1,2 @@
/fr/
/mrdev023/

Binary file not shown.

View file

@ -1,4 +1,7 @@
package fr.technicalgames;
//http://www.tomdalling.com/blog/modern-opengl/08-even-more-lighting-directional-lights-spotlights-multiple-lights/
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
@ -6,6 +9,7 @@ import static org.lwjgl.system.MemoryUtil.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import fr.technicalgames.audio.*;
import fr.technicalgames.game.*;
import fr.technicalgames.input.*;
import fr.technicalgames.math.*;
@ -56,6 +60,11 @@ public class Main {
System.out.println("GLSL Shader Version :" + glGetString(GL20.GL_SHADING_LANGUAGE_VERSION));
//------------------------------------------------------------------------------------
//Creation du device audio
//------------------------------------------------------------------------------------
Audio.create();
//------------------------------------------------------------------------------------
//initialisation
//------------------------------------------------------------------------------------
Input.init();
@ -96,6 +105,7 @@ public class Main {
}
}
Audio.destroy();
glfwDestroyWindow(windowID);
glfwTerminate();
}

View file

@ -0,0 +1,341 @@
package fr.technicalgames.audio;
import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*;
import static org.lwjgl.openal.ALC11.*;
import static org.lwjgl.openal.ALUtil.*;
import static org.lwjgl.stb.STBVorbis.*;
import static org.lwjgl.system.MemoryUtil.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import javax.sound.sampled.*;
import org.lwjgl.*;
import org.lwjgl.openal.*;
import org.lwjgl.stb.STBVorbisInfo;
public abstract class Audio {
//Variables global
//------------------------------------------------------
public static ALDevice device;
public static ALCCapabilities caps;
public static ALContext context;
public static final int INITIAL_STATE = 4113,PAUSED_STATE = 4115,STOPPED_STATE = 4116,PLAYING_STATE = 4114;
//------------------------------------------------------
//Variable de l'objet audio ou du son a lire
//------------------------------------------------------
private int buffer,source;
private String fileName;
private String format;
//------------------------------------------------------
//Fonction global
//------------------------------------------------------
public static void create(){
device = ALDevice.create(null);
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);
String defaultDeviceSpecifier = alcGetString(0L, ALC_DEFAULT_DEVICE_SPECIFIER);
System.out.println("Default device: " + defaultDeviceSpecifier);
context = ALContext.create(device);
System.out.println("ALC_FREQUENCY: " + alcGetInteger(device.address(), ALC_FREQUENCY) + "Hz");
System.out.println("ALC_REFRESH: " + alcGetInteger(device.address(), ALC_REFRESH) + "Hz");
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(){
context.destroy();
device.destroy();
}
//------------------------------------------------------
//Fonction de l'objet audio ou du son a lire
//------------------------------------------------------
public Audio(String fileName) throws Exception{
this.fileName = fileName;
setSound();
}
private void setSound() throws Exception{
if(fileName.endsWith(".ogg")){
loadOGGFormat();
format = "OGG";
}else if(fileName.endsWith(".wav")){
loadWavFormat();
format = "WAV";
}else{
throw new Exception("Format not supported !");
}
alSourcei(source, AL_BUFFER, buffer);
checkALError();
int size = alGetBufferi(buffer,AL_SIZE);
int bits = alGetBufferi(buffer, AL_BITS);
int channels = alGetBufferi(buffer, AL_CHANNELS);
int freq = alGetBufferi(buffer, AL_FREQUENCY);
System.out.println(fileName + " loaded !" + " | TIME : " + (size/channels/(bits/8)/freq) + "s | BITS : " + bits + " | CHANNELS : " + channels + " | FREQUENCE : " + freq + " FORMAT : " + format);
}
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(){
STBVorbisInfo info = STBVorbisInfo.malloc();
ByteBuffer buff = BufferUtils.createByteBuffer(0);
//lecture du fichier
//----------------------------------------------------------------------------------------------------------------
try {
File file = new File(fileName);
if ( file.isFile() ) {
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
buff = BufferUtils.createByteBuffer((int)fc.size() + 1);
while ( fc.read(buff) != -1 ) ;
fis.close();
fc.close();
} else {
System.err.println("File not found !");
return;
}
buff.flip();
} catch (IOException e) {
throw new RuntimeException(e);
}
//----------------------------------------------------------------------------------------------------------------
IntBuffer error = BufferUtils.createIntBuffer(1);
long decoder = stb_vorbis_open_memory(buff, error, null);
if ( decoder == NULL )
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
stb_vorbis_seek_start(decoder);
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
ByteBuffer pcm = BufferUtils.createByteBuffer(lengthSamples * 2 * channels);
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm, lengthSamples);
stb_vorbis_close(decoder);
buffer = alGenBuffers();
checkALError();
source = alGenSources();
checkALError();
if(channels == 1)alBufferData(buffer, AL_FORMAT_MONO16, pcm, info.sample_rate());
else alBufferData(buffer, AL_FORMAT_STEREO16, pcm, info.sample_rate());
checkALError();
}
public void playSound(){
if(source == 0 || buffer == 0) return;
alSourcePlay(source);
}
public int getPosition(){
return alGetSourcei(source, AL_POSITION);
}
public int getDurationInSeconds(){
if(source == 0 || buffer == 0) return 0;
int size = alGetBufferi(buffer,AL_SIZE);
int bits = alGetBufferi(buffer, AL_BITS);
int channels = alGetBufferi(buffer, AL_CHANNELS);
int freq = alGetBufferi(buffer, AL_FREQUENCY);
return size/channels/(bits/8)/freq;
}
public int getStateSound(){
if(source == 0 || buffer == 0) return 0;
return alGetSourcei(source, AL_SOURCE_STATE);
}
public boolean isStopped(){
if(source == 0 || buffer == 0) return false;
if(alGetSourcei(source, AL_SOURCE_STATE) == STOPPED_STATE)return true;
else return false;
}
public boolean isPaused(){
if(source == 0 || buffer == 0) return false;
if(alGetSourcei(source, AL_SOURCE_STATE) == PAUSED_STATE)return true;
else return false;
}
public boolean isPlaying(){
if(source == 0 || buffer == 0) return false;
if(alGetSourcei(source, AL_SOURCE_STATE) == PLAYING_STATE)return true;
else return false;
}
public boolean isInitial(){
if(source == 0 || buffer == 0) return false;
if(alGetSourcei(source, AL_SOURCE_STATE) == INITIAL_STATE)return true;
else return false;
}
public void stopSound(){
if(source == 0 || buffer == 0) return;
alSourceStop(source);
}
public void pauseSound(){
if(source == 0 || buffer == 0) return;
alSourcePause(source);
}
public void rewindSound(){
if(source == 0 || buffer == 0) return;
alSourceRewind(source);
}
public void setGain(float gain){
if(source == 0 || buffer == 0) return;
if(gain > 1.0f)gain = 1.0f;
if(gain < 0.0f)gain = 0.0f;
alSourcef(source, AL_GAIN, gain);
}
public void setPitch(float pitch){
if(source == 0 || buffer == 0) return;
if(pitch < 0.0f)pitch = 0.0f;
alSourcef(source, AL_PITCH, pitch);
}
public float getGain(){
if(source == 0 || buffer == 0) return 0;
return alGetSourcef(source, AL_GAIN);
}
public float getPitch(){
if(source == 0 || buffer == 0) return 0;
return alGetSourcef(source, AL_PITCH);
}
public void setLooping(boolean looping){
if(source == 0 || buffer == 0) return;
if(looping){
alSourcef(source, AL_LOOPING, AL_TRUE);
}else{
alSourcef(source, AL_LOOPING, AL_FALSE);
}
}
public void destroySound(){
alDeleteSources(source);
alDeleteBuffers(buffer);
source = 0;
buffer = 0;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) throws Exception {
this.fileName = fileName;
destroySound();
setSound();
}
public int getBuffer() {
return buffer;
}
public void setBuffer(int buffer) {
this.buffer = buffer;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
//------------------------------------------------------
}

View file

@ -0,0 +1,11 @@
package fr.technicalgames.audio;
import fr.technicalgames.math.*;
public class Sound3D extends Audio{
public Sound3D(String fileName,Vector3f position) throws Exception {
super(fileName);
}
}

View file

@ -13,7 +13,7 @@ public class MainGame extends Game{
public void init() {
lights.add(new SpotLight(new Vector3f(-4,0,10),new Vector3f(2,2,2),0.1f,0.0f,15.0f,new Vector3f(0,0,-1)));
lights.add(new DirectionalLight(new Vector3f(4,0,-10), new Vector3f(0.4f,0.3f,0.1f), 0.06f));
lights.add(new DirectionalLight(new Vector3f(4,5,-10), new Vector3f(0.4f,0.3f,0.1f), 0.06f));
Asset as = new Asset();
@ -31,6 +31,9 @@ public class MainGame extends Game{
as = new Asset();
as.transform = (new Matrix4f()).tranlate(-6,0,0).scale(2,1,0.8f);
assets.add(as);
as = new Asset();
as.transform = (new Matrix4f()).tranlate(-9, -10, -1).scale(20, .5f, 20);
assets.add(as);
as = null;
}
@ -66,6 +69,9 @@ public class MainGame extends Game{
for(Asset a : assets){
a.destroy();
}
for(Light l : lights){
l.destroy();
}
}
}

View file

@ -1,7 +1,8 @@
package fr.technicalgames.light;
import fr.technicalgames.math.*;
import fr.technicalgames.shadow.*;
public abstract class Light {
public abstract class Light extends Shadow{
public Vector4f position;//w == 0 si c une directional light
public Vector3f intensities;
@ -11,6 +12,7 @@ public abstract class Light {
public Vector3f coneDirection;
public Light(Vector4f position,Vector3f intensities,float attenuation,float ambientCoefficient,float coneAngle,Vector3f coneDirection){
super();
this.position = position;
this.intensities = intensities;
this.attenuation = attenuation;

View file

@ -0,0 +1,24 @@
package fr.technicalgames.render.gui;
import java.awt.*;
import java.awt.image.*;
import java.nio.*;
public class GLFont {
private Font font;
private int textureFont;
public GLFont(String name,int style,int size){
this.font = new Font(name, style, size);
}
public GLFont(){
this("Verdana",Font.BOLD,24);
}
}

View file

@ -0,0 +1,9 @@
package fr.technicalgames.render.gui;
import fr.technicalgames.math.*;
public abstract class GUIComponent {
private Vector2f pos;
}

View file

@ -0,0 +1,73 @@
package fr.technicalgames.shadow;
import org.lwjgl.opengl.*;
public class Shadow {
public static final int SIZE_OF_SHADOW_MAPPING = 1024;
private int frameBuffer;
private int depthTexture;
private boolean success = false;
public Shadow(){
//Creer et selectionne le buffer
// this.frameBuffer = GL30.glGenFramebuffers();
// GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, this.frameBuffer);
//
// //Creer la texture pour les ombres
// this.depthTexture = GL11.glGenTextures();
// GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.depthTexture);
// GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT16,SIZE_OF_SHADOW_MAPPING,SIZE_OF_SHADOW_MAPPING,0,GL11.GL_DEPTH_COMPONENT,GL11.GL_FLOAT,0);
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
//
// GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, depthTexture, 0);
// GL11.glDrawBuffer(GL11.GL_NONE);
// GL11.glReadBuffer(GL11.GL_NONE);
// if(GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) == GL30.GL_FRAMEBUFFER_COMPLETE){
// success = true;
// }else{
// success = false;
// System.err.println("Shadow not adding to light");
// }
}
public void render(){
}
public void destroy(){
if(this.frameBuffer != 0)GL30.glDeleteFramebuffers(this.frameBuffer);
if(this.depthTexture != 0)GL11.glDeleteTextures(this.depthTexture);
}
public int getFrameBuffer() {
return frameBuffer;
}
public void setFrameBuffer(int frameBuffer) {
this.frameBuffer = frameBuffer;
}
public int getDepthTexture() {
return depthTexture;
}
public void setDepthTexture(int depthTexture) {
this.depthTexture = depthTexture;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}