Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.exoterragame.client;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.GL13;
- import org.lwjgl.opengl.GL15;
- import org.lwjgl.opengl.GL20;
- import org.lwjgl.opengl.GL21;
- import org.newdawn.slick.opengl.Texture;
- import org.newdawn.slick.opengl.TextureLoader;
- import org.newdawn.slick.util.ResourceLoader;
- import com.exoterragame.client.util.FileUtility;
- public class Main {
- public void start() {
- try {
- Display.setDisplayMode(new DisplayMode(800, 600));
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- FloatBuffer vert_b = BufferUtils.createFloatBuffer(8);
- IntBuffer idx_b = BufferUtils.createIntBuffer(4);
- float[] verts = {
- -1.0f, -1.0f,
- 1.0f, -1.0f,
- -1.0f, 1.0f,
- 1.0f, 1.0f
- };
- int[] idx = {
- 0, 1, 2, 3
- };
- vert_b.put(verts).rewind();
- idx_b.put(idx).rewind();
- int buffer_id = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer_id);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vert_b, GL15.GL_STATIC_DRAW);
- int idx_buffer_id = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_buffer_id);
- GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_b, GL15.GL_STATIC_DRAW);
- int vert_shader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
- String vert_source = "";
- try {
- vert_source = FileUtility.loadTextFile("res/shaders/default.v.glsl");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.exit(0);
- }
- GL20.glShaderSource(vert_shader, vert_source);
- GL20.glCompileShader(vert_shader);
- if (GL20.glGetShader(vert_shader, GL20.GL_COMPILE_STATUS) == 0) {
- System.exit(0);
- }
- int frag_shader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
- String frag_source = "";
- try {
- frag_source = FileUtility.loadTextFile("res/shaders/default.f.glsl");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.exit(0);
- }
- GL20.glShaderSource(frag_shader, frag_source);
- GL20.glCompileShader(frag_shader);
- if (GL20.glGetShader(frag_shader, GL20.GL_COMPILE_STATUS) == 0) {
- System.exit(0);
- }
- int program = GL20.glCreateProgram();
- GL20.glAttachShader(program, vert_shader);
- GL20.glAttachShader(program, frag_shader);
- GL20.glLinkProgram(program);
- if (GL20.glGetProgram(program, GL20.GL_LINK_STATUS) == 0) {
- System.exit(0);
- }
- int tex_uniform = GL20.glGetUniformLocation(program, "texture");
- int pos_attribute = GL20.glGetAttribLocation(program, "position");
- Texture texture = null;
- try {
- texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/tex0.png"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- while (!Display.isCloseRequested()) {
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- GL20.glUseProgram(program);
- if (texture != null) {
- GL13.glActiveTexture(GL13.GL_TEXTURE0);
- texture.bind();
- GL20.glUniform1i(tex_uniform, 0);
- }
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer_id);
- GL20.glEnableVertexAttribArray(pos_attribute);
- GL20.glVertexAttribPointer(pos_attribute, 2, false, Float.SIZE * 2, vert_b);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_buffer_id);
- GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, idx_b);
- GL20.glDisableVertexAttribArray(pos_attribute);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
- Display.update();
- }
- GL15.glDeleteBuffers(buffer_id);
- GL15.glDeleteBuffers(idx_buffer_id);
- }
- public static void main(String[] args) {
- Main main = new Main();
- main.start();
- }
- }
Add Comment
Please, Sign In to add comment