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.*;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.*;
|
||||||
|
|
||||||
|
import javax.sound.sampled.*;
|
||||||
|
|
||||||
import org.lwjgl.*;
|
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;
|
import org.lwjgl.stb.STBVorbisInfo;
|
||||||
|
|
||||||
public class Audio {
|
public class Audio {
|
||||||
|
@ -44,6 +43,7 @@ public class Audio {
|
||||||
if ( device == null )
|
if ( device == null )
|
||||||
throw new IllegalStateException("Failed to open the default device.");
|
throw new IllegalStateException("Failed to open the default device.");
|
||||||
caps = device.getCapabilities();
|
caps = device.getCapabilities();
|
||||||
|
System.out.println("---------------------------- Create Audio Device -------------------------------------");
|
||||||
System.out.println("OpenALC10: " + caps.OpenALC10);
|
System.out.println("OpenALC10: " + caps.OpenALC10);
|
||||||
System.out.println("OpenALC11: " + caps.OpenALC11);
|
System.out.println("OpenALC11: " + caps.OpenALC11);
|
||||||
System.out.println("caps.ALC_EXT_EFX = " + caps.ALC_EXT_EFX);
|
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_SYNC: " + (alcGetInteger(device.address(), ALC_SYNC) == ALC_TRUE));
|
||||||
System.out.println("ALC_MONO_SOURCES: " + alcGetInteger(device.address(), ALC_MONO_SOURCES));
|
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("ALC_STEREO_SOURCES: " + alcGetInteger(device.address(), ALC_STEREO_SOURCES));
|
||||||
|
System.out.println("---------------------------------------------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void destroy(){
|
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);
|
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{
|
public void loadWavFormat() throws Exception{
|
||||||
WaveData soundData = WaveData.create(new BufferedInputStream(new FileInputStream(fileName)));
|
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(fileName)));
|
||||||
buffer = alGenBuffers();
|
AudioFormat audioformat = ais.getFormat();
|
||||||
source = alGenSources();
|
|
||||||
alBufferData(buffer, soundData.format, soundData.data, soundData.samplerate);
|
// get channels
|
||||||
soundData.dispose();
|
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(){
|
public void loadOGGFormat(){
|
||||||
|
@ -143,7 +196,6 @@ public class Audio {
|
||||||
ByteBuffer pcm = BufferUtils.createByteBuffer(lengthSamples * 2 * channels);
|
ByteBuffer pcm = BufferUtils.createByteBuffer(lengthSamples * 2 * channels);
|
||||||
|
|
||||||
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm, lengthSamples);
|
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm, lengthSamples);
|
||||||
float duration = stb_vorbis_stream_length_in_seconds(decoder);
|
|
||||||
stb_vorbis_close(decoder);
|
stb_vorbis_close(decoder);
|
||||||
|
|
||||||
buffer = alGenBuffers();
|
buffer = alGenBuffers();
|
||||||
|
|
|
@ -26,10 +26,13 @@ public class GameEngine {
|
||||||
|
|
||||||
public static void start(String title,int width,int height){
|
public static void start(String title,int width,int height){
|
||||||
TITLE = title;
|
TITLE = title;
|
||||||
|
System.out.println("---------------------------- Create OpenGL Context -----------------------------------");
|
||||||
Display.create(title, width, height);
|
Display.create(title, width, height);
|
||||||
Display.createContext();
|
Display.createContext();
|
||||||
|
Display.printMonitorsInfo();
|
||||||
|
System.out.println("Window : " + width + "x" + height);
|
||||||
System.out.println("OpenGL " + GL11.glGetString(GL11.GL_VERSION));
|
System.out.println("OpenGL " + GL11.glGetString(GL11.GL_VERSION));
|
||||||
|
System.out.println("--------------------------------------------------------------------------------------");
|
||||||
init();
|
init();
|
||||||
loop();
|
loop();
|
||||||
}
|
}
|
||||||
|
@ -46,13 +49,15 @@ public class GameEngine {
|
||||||
Timer.addTimer("ticks");
|
Timer.addTimer("ticks");
|
||||||
Timer.addTimer("fps");
|
Timer.addTimer("fps");
|
||||||
Display.setMouseGrabbed(true);
|
Display.setMouseGrabbed(true);
|
||||||
|
System.out.println("---------------------------- Load Audio File -----------------------------------------");
|
||||||
try {
|
try {
|
||||||
a = new Audio("res/audio/test.ogg");
|
a = new Audio("res/audio/test.wav");
|
||||||
a.playSound();
|
a.playSound();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
System.out.println("--------------------------------------------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loop(){
|
public static void loop(){
|
||||||
|
|
|
@ -21,6 +21,7 @@ public class MainMenu extends Game implements IGameState{
|
||||||
public int time = 0;
|
public int time = 0;
|
||||||
public static final float speed = 1.0f;
|
public static final float speed = 1.0f;
|
||||||
public ArrayList<Light> lights;
|
public ArrayList<Light> lights;
|
||||||
|
public TextFont text;
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
time+=Timer.getDeltaTime();
|
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)));
|
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);
|
vao = MeshBuilder.createFloor(400, Color4f.WHITE,Texture.FLOOR);
|
||||||
cube = MeshBuilder.createCube(100, Color4f.WHITE,Texture.WOOD);
|
cube = MeshBuilder.createCube(100, Color4f.WHITE,Texture.WOOD);
|
||||||
|
text = new TextFont("Test", 10, 10, 16, Color4f.WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render2D() {
|
public void render2D() {
|
||||||
|
text.drawText();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render3D() {
|
public void render3D() {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package mrdev023.math;
|
package mrdev023.math;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
|
|
||||||
public class Color4f {
|
public class Color4f {
|
||||||
|
|
||||||
public static final Color4f
|
public static final Color4f
|
||||||
|
@ -64,6 +66,12 @@ public class Color4f {
|
||||||
this.a = a;
|
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 java.nio.*;
|
||||||
|
|
||||||
import org.lwjgl.*;
|
import org.lwjgl.*;
|
||||||
|
import org.lwjgl.glfw.*;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
|
|
||||||
import mrdev023.exception.*;
|
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(){
|
public static boolean isCloseRequested(){
|
||||||
return glfwWindowShouldClose(window) == GL_TRUE;
|
return glfwWindowShouldClose(window) == GL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,10 @@ public class DisplayManager {
|
||||||
projection.loadIdentity();
|
projection.loadIdentity();
|
||||||
projection.Ortho2D(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), 1, -1);
|
projection.Ortho2D(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), 1, -1);
|
||||||
Shader.MAIN.uniform("projection", projection);
|
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(){
|
public static void preRender3D(){
|
||||||
|
|
|
@ -12,12 +12,15 @@ public class Shader {
|
||||||
|
|
||||||
public int program;
|
public int program;
|
||||||
public int includeV = 0,includeF = 0;
|
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() {
|
public static void init() {
|
||||||
|
System.out.println("---------------------------- Load Shader ---------------------------------------------");
|
||||||
MAIN = new Shader("res/shaders/main");
|
MAIN = new Shader("res/shaders/main");
|
||||||
|
FONT = new Shader("res/shaders/font");
|
||||||
LIGHT = new Shader("res/shaders/light");
|
LIGHT = new Shader("res/shaders/light");
|
||||||
LIGHT_AMBIENT = new Shader("res/shaders/light_ambient");
|
LIGHT_AMBIENT = new Shader("res/shaders/light_ambient");
|
||||||
|
System.out.println("--------------------------------------------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Shader(String file){
|
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 class Texture {
|
||||||
|
|
||||||
public static Texture FLOOR,WOOD;
|
public static Texture FLOOR,WOOD,FONT;
|
||||||
|
|
||||||
int width, height;
|
int width, height;
|
||||||
int id;
|
int id;
|
||||||
|
|
||||||
public static void init() {
|
public static void init() {
|
||||||
|
System.out.println("---------------------------- Load Texture --------------------------------------------");
|
||||||
FLOOR = Texture.loadTexture("res/textures/floor.jpg");
|
FLOOR = Texture.loadTexture("res/textures/floor.jpg");
|
||||||
WOOD = Texture.loadTexture("res/textures/wood.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){
|
public Texture(int width,int height,int id){
|
||||||
|
@ -38,16 +41,18 @@ public class Texture {
|
||||||
|
|
||||||
image.getRGB(0, 0, width, height, pixels, 0,width);
|
image.getRGB(0, 0, width, height, pixels, 0,width);
|
||||||
|
|
||||||
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
|
int[] data = new int[pixels.length];
|
||||||
for(int y = 0; y < width; y++){
|
for (int i = 0; i < data.length; i++) {
|
||||||
for(int x = 0; x < height; x++){
|
int a = (pixels[i] & 0xff000000) >> 24;
|
||||||
int i = pixels[x + y * width];//0xAARRGGBB
|
int r = (pixels[i] & 0xff0000) >> 16;
|
||||||
buffer.put((byte) ((i >> 16) & 0xFF));//r
|
int g = (pixels[i] & 0xff00) >> 8;
|
||||||
buffer.put((byte) ((i >> 8) & 0xFF));//g
|
int b = (pixels[i] & 0xff);
|
||||||
buffer.put((byte) ((i) & 0xFF));//b
|
|
||||||
buffer.put((byte) ((i >> 24) & 0xFF));//a
|
data[i] = a << 24 | b << 16 | g << 8 | r;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
|
||||||
|
buffer.put(data);
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
|
|
||||||
int id = glGenTextures();
|
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_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
|
Reference in a new issue