这个系列教程 是[https://github.com/mattdesl/lwjgl-basics/wiki][1]的系列翻译。
为了减小一些不重要的细节 咱们这个系列教程是利用LWJGL(Lightweight Java Game Library)实现的。可是对LWJGL的了解不是必须的。每一个Shaders 教程咱们都有一个libgdx的实现。
这一节咱们介绍一个简单的渲染周期,之后咱们会扩展它去学习。这是一段用来展现LWJGL生命周期的代码,代码很简单,很容易看懂。你也能够在 LWJGL wiki找到更多的例子。代码放在文章的最后。
opengl的启动
为了让opengl正常的工做,咱们须要按顺序执行一坨东西。
首先咱们要让opengGL的视区和显示的视区一致。咱们也要在显示的视区变化大小的时候去执行它:php
glViewport(0, 0, Display.getWidth(), Display.getHeight());
下一步 咱们要关闭掉大部分2d游戏用不到的深度测试(depth testing 是测试位置远近的,以达到渲染顺序的正确的做用)。在2d游戏中哪一个sprite在上边 是有由draw的顺序决定的。如今咱们先不考虑有深度测试的状况。git
glDisable(GL_DEPTH_TEST);
下一步咱们要让blending(混合)功能启做用。咱们在其余节去解释它的细节。若是blending不生效,一些透明的sprites不能像咱们想要的那样渲染。github
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
在初始化中咱们要设置清除颜色 。就是每次opengl清除屏幕要用的颜色。记住RGBA 4个浮点数的范围都是0.0f ~1.0f。这里咱们指定为透明黑。缓存
glClearColor(0f, 0f, 0f, 0f);
注意opengl 是一个 静态的 基于状态的API。简单来讲就是 咱们设置了一个状态,若是咱们不去设置另外一个状态,那么这个状态是不改变的。因此咱们没有必要每一帧都去调用设置状态的函数。除非一些第三方库去自动调用。
游戏主循环
下边的游戏循环是很简单的。在这里咱们设置了每秒60贞。
因为 LWJGL 用了双缓冲技术(Double Buffering)因此每次调用update的时候咱们都要清除缓存。app
glClear(GL_COLOR_BUFFER_BIT);
这样屏幕就会变为咱们以前设置的黑色。
好了剩下的代码你们应该能够本身看懂了。
所有代码函数
package mdesl.test; import static org.lwjgl.opengl.GL11.GL_BLEND; import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA; import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA; import static org.lwjgl.opengl.GL11.glBlendFunc; import static org.lwjgl.opengl.GL11.glClear; import static org.lwjgl.opengl.GL11.glClearColor; import static org.lwjgl.opengl.GL11.glDisable; import static org.lwjgl.opengl.GL11.glEnable; import static org.lwjgl.opengl.GL11.glViewport; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; /** * A bare-bones implementation of a LWJGL application. * @author davedes */ public class Game { // Whether to enable VSync in hardware. public static final boolean VSYNC = true; // Width and height of our window public static final int WIDTH = 800; public static final int HEIGHT = 600; // Whether to use fullscreen mode public static final boolean FULLSCREEN = false; // Whether our game loop is running protected boolean running = false; public static void main(String[] args) throws LWJGLException { new Game().start(); } // Start our game public void start() throws LWJGLException { // Set up our display Display.setTitle("Display example"); //title of our window Display.setResizable(true); //whether our window is resizable Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); //resolution of our display Display.setVSyncEnabled(VSYNC); //whether hardware VSync is enabled Display.setFullscreen(FULLSCREEN); //whether fullscreen is enabled //create and show our display Display.create(); // Create our OpenGL context and initialize any resources create(); // Call this before running to set up our initial size resize(); running = true; // While we're still running and the user hasn't closed the window... while (running && !Display.isCloseRequested()) { // If the game was resized, we need to update our projection if (Display.wasResized()) resize(); // Render the game render(); // Flip the buffers and sync to 60 FPS Display.update(); Display.sync(60); } // Dispose any resources and destroy our window dispose(); Display.destroy(); } // Exit our game loop and close the window public void exit() { running = false; } // Called to setup our game and context protected void create() { // 2D games generally won't require depth testing glDisable(GL_DEPTH_TEST); // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set clear to transparent black glClearColor(0f, 0f, 0f, 0f); // ... initialize resources here ... } // Called to render our game protected void render() { // Clear the screen glClear(GL_COLOR_BUFFER_BIT); // ... render our game here ... } // Called to resize our game protected void resize() { glViewport(0, 0, Display.getWidth(), Display.getHeight()); // ... update our projection matrices here ... } // Called to destroy our game upon exiting protected void dispose() { // ... dispose of any textures, etc ... }