Java tutorial
/* * Copyright (C) 2012 Robert 'Bobby' Zenz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.bonsaimind.badgersvoyage.tools.planetstudio; import org.bonsaimind.badgersvoyage.opengl.Sphere; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.ContextAttribs; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.PixelFormat; import org.lwjgl.util.vector.Vector3f; /** * * @author Robert 'Bobby' Zenz */ public class Studio { private Camera camera; private boolean closeRequested = false; private int programId; private Sphere sphere; public Studio(String title, int width, int height) throws LWJGLException { PixelFormat pixelFormat = new PixelFormat(); ContextAttribs contextAttribs = new ContextAttribs(3, 2); contextAttribs.withForwardCompatible(true); contextAttribs.withProfileCore(true); Display.setDisplayMode(new DisplayMode(width, height)); Display.setTitle(title); Display.create(pixelFormat, contextAttribs); ErrorChecker.exitOnOpenGlError("Display creation."); programId = GL20.glCreateProgram(); ErrorChecker.exitOnOpenGlError("Program creation."); int shaderId = GL20.glCreateShader(GL20.GL_VERTEX_SHADER); // GL20.glShaderSource(shaderId, "void main {\n" // + " gl_Normal = gl_NormalMatrix * gl_Normal; //Note that you can perform operations on matrices and vectors as if they were\n" // + " //primitive types. This is useful for simple, readable code like this.\n" // + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; //The order in which you times matrices and vertices is IMPORTANT.\n" // + " gl_FrontColor = gl_Color; //These lines just pass on the colour value to the fragment shader.\n" // + " gl_BackColor = gl_Color;\n" // + "}"); GL20.glShaderSource(shaderId, "void main(void){gl_Position = ftransform();}"); // GL20.glShaderSource(shaderId, "#version 140\n" // + "\n" // + "uniform Transformation {\n" // + " mat4 projectionMatrix;\n" // + " mat4 viewMatrix;\n" // + " mat4 modelMatrix;\n" // + "};\n" // + "in vec4 in_Position;\n" // + "\n" // + "in vec3 vertex;\n" // + "\n" // + "void main() {\n" // + " gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;\n" // + "}"); // GL20.glShaderSource(shaderId, "#version 150 core\n" // + "\n" // + "uniform mat4 projectionMatrix;\n" // + "uniform mat4 viewMatrix;\n" // + "uniform mat4 modelMatrix;\n" // + "\n" // + "in vec4 in_Position;\n" // + "in vec4 in_Color;\n" // + "in vec2 in_TextureCoord;\n" // + "\n" // + "out vec4 pass_Color;\n" // + "out vec2 pass_TextureCoord;\n" // + "\n" // + "void main(void) {\n" // + " gl_Position = in_Position;\n" // + " gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;\n" // + "\n" // + " pass_Color = in_Color;\n" // + " pass_TextureCoord = in_TextureCoord;\n" // + "}"); GL20.glCompileShader(shaderId); GL20.glAttachShader(programId, shaderId); // int shaderId2 = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER); // GL20.glShaderSource(shaderId2, "#version 150 core\n" // + "\n" // + "uniform sampler2D texture_diffuse;\n" // + "\n" // + "in vec4 pass_Color;\n" // + "in vec2 pass_TextureCoord;\n" // + "\n" // + "out vec4 out_Color;\n" // + "\n" // + "void main(void) {\n" // + " out_Color = pass_Color;\n" // + " // Override out_Color with our texture pixel\n" // + " out_Color = vec4(0.5, 0.5, 0.5, 1); //texture2D(texture_diffuse, pass_TextureCoord);\n" // + "}"); // GL20.glCompileShader(shaderId2); // GL20.glAttachShader(programId, shaderId2); GL20.glLinkProgram(programId); ErrorChecker.exitOnOpenGlError("Program linking."); System.err.println(GL20.glGetProgramInfoLog(programId, 255)); System.err.println(); GL20.glValidateProgram(programId); ErrorChecker.exitOnOpenGlError("Program validation."); camera = new Camera(width / (float) height, new Vector3f(0, 0, 2.5f), 100f, 60f, 0.1f, programId); ErrorChecker.exitOnOpenGlError("Camera creation."); sphere = new Sphere(60, 0.5f); sphere.create(programId); ErrorChecker.exitOnOpenGlError("Sphere creation."); } public void run() { GL11.glClearColor(0.2f, 0.5f, 1f, 0f); GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight()); //GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); while (!Display.isCloseRequested() && !closeRequested) { processKeyboard(); ErrorChecker.exitOnOpenGlError("After processKeyboard."); camera.startTranslate(); sphere.translate(); camera.endTranslate(); ErrorChecker.exitOnOpenGlError("End of Translate."); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL20.glUseProgram(programId); sphere.render(); GL20.glDisableVertexAttribArray(0); GL30.glDeleteVertexArrays(0); GL20.glUseProgram(0); Display.sync(60); Display.update(); ErrorChecker.exitOnOpenGlError("End of main loop."); } Display.destroy(); } public void processKeyboard() { while (Keyboard.next()) { if (Keyboard.getEventKeyState()) { switch (Keyboard.getEventKey()) { case Keyboard.KEY_ADD: sphere.getRotation().setY(sphere.getRotation().getY() + 1); break; case Keyboard.KEY_MINUS: sphere.getRotation().setY(sphere.getRotation().getY() - 1); break; } } } } }