Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GL3 {
- // Entry point for the application
- public static void main(String[] args) {
- new GL3();
- }
- // Setup variables
- private final String WINDOW_TITLE = "The Quad: Update VBO";
- private final int WIDTH = 320;
- private final int HEIGHT = 320;
- Quad rect, square;
- public GL3() {
- setupOpenGL();
- rect = new Quad(true, 0);
- square = new Quad(false, 1);
- while (!Display.isCloseRequested()) {
- // Do a single loop (logic/render)
- loopCycle();
- // Force a maximum FPS of about 60
- Display.sync(60);
- // Let the CPU synchronize with the GPU if GPU is tagging behind
- Display.update();
- }
- // Destroy OpenGL (Display)
- this.destroyOpenGL();
- }
- private void setupOpenGL() {
- // Setup an OpenGL context with API version 3.2
- try {
- PixelFormat pixelFormat = new PixelFormat();
- ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
- .withForwardCompatible(true)
- .withProfileCore(true);
- Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
- Display.setTitle(WINDOW_TITLE);
- Display.create(pixelFormat, contextAtrributes);
- GL11.glViewport(0, 0, WIDTH, HEIGHT);
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(-1);
- }
- // Setup an XNA like background color
- GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
- // Map the internal OpenGL coordinate system to the entire screen
- GL11.glViewport(0, 0, WIDTH, HEIGHT);
- this.exitOnGLError("setupOpenGL");
- }
- private void loopCycle() {
- rect.update();
- square.update();
- rect.render();
- square.render();
- this.exitOnGLError("loopCycle");
- }
- private void destroyOpenGL() {
- this.exitOnGLError("destroyOpenGL");
- rect.destroy();
- square.destroy();
- Display.destroy();
- }
- private int loadShader(String filename, int type) {
- StringBuilder shaderSource = new StringBuilder();
- int shaderID = 0;
- try {
- BufferedReader reader = new BufferedReader(new FileReader(filename));
- String line;
- while ((line = reader.readLine()) != null) {
- shaderSource.append(line).append("\n");
- }
- reader.close();
- } catch (IOException e) {
- System.err.println("Could not read file.");
- e.printStackTrace();
- System.exit(-1);
- }
- shaderID = GL20.glCreateShader(type);
- GL20.glShaderSource(shaderID, shaderSource);
- GL20.glCompileShader(shaderID);
- if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
- System.err.println("Could not compile shader.");
- System.exit(-1);
- }
- this.exitOnGLError("loadShader");
- return shaderID;
- }
- private int loadPNGTexture(String filename, int textureUnit) {
- ByteBuffer buf = null;
- int tWidth = 0;
- int tHeight = 0;
- try {
- // Open the PNG file as an InputStream
- InputStream in = new FileInputStream(filename);
- // Link the PNG decoder to this stream
- PNGDecoder decoder = new PNGDecoder(in);
- // Get the width and height of the texture
- tWidth = decoder.getWidth();
- tHeight = decoder.getHeight();
- // Decode the PNG file in a ByteBuffer
- buf = ByteBuffer.allocateDirect(
- 4 * decoder.getWidth() * decoder.getHeight());
- decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
- buf.flip();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(-1);
- }
- // Create a new texture object in memory and bind it
- int texId = GL11.glGenTextures();
- GL13.glActiveTexture(textureUnit);
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
- // All RGB bytes are aligned to each other and each component is 1 byte
- GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
- // Upload the texture data and generate mip maps (for scaling)
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0,
- GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
- GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
- // Setup the ST coordinate system
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
- // Setup what to do when the texture has to be scaled
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
- GL11.GL_LINEAR);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
- GL11.GL_LINEAR_MIPMAP_LINEAR);
- this.exitOnGLError("loadPNGTexture");
- return texId;
- }
- private void exitOnGLError(String errorMessage) {
- int errorValue = GL11.glGetError();
- if (errorValue != GL11.GL_NO_ERROR) {
- String errorString = GLU.gluErrorString(errorValue);
- System.err.println("ERROR - " + errorMessage + ": " + errorString);
- if (Display.isCreated()) Display.destroy();
- System.exit(-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement