1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| public class TexturedCubeVA extends JApplet { private Canvas canvas; private Thread gameThread; boolean running = false; private Texture texture; public void startLWJGL(){ gameThread = new Thread (){ public void run(){ running = true; try { Display.setParent(canvas); Display.create(); Display.setVSyncEnabled(true); initGL(); } catch (LWJGLException e) { e.printStackTrace(); } gameLoop(); } }; gameThread.start(); } public void stopLWJGL(){ running = false; try { gameThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } }
public void destroy(){ remove(canvas); super.destroy(); } public void init(){ setLayout(new BorderLayout()); canvas = new Canvas(){ public final void addNotify(){ super.addNotify(); startLWJGL(); } public final void removeNotify(){ stopLWJGL(); super.removeNotify(); } }; canvas.setSize(Conf.W, Conf.H); setSize(Conf.W, Conf.H); canvas.setBackground(Color.BLACK); canvas.setFocusable(true); canvas.requestFocus(); canvas.setIgnoreRepaint(false); add(canvas); } private void initGL() { glViewport(0, 0, Conf.W,Conf.H); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); GL11.glEnable(GL11.GL_TEXTURE_2D); glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); try { texture = TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream("res/caution.png")); } catch (IOException e) { e.printStackTrace(); } } float rquad; public void gameLoop(){ final int amountOfVertices = 24; final int vertexSize = 3; final int textureVertexSize = 2; FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize); vertexData.put(CubeUtil.getCube(1f, 1f, 1f)); vertexData.flip(); FloatBuffer textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureVertexSize); textureData.put(CubeUtil.getCubeTexture(1.0f,1.0f)); textureData.flip(); while(running) { update(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); texture.bind(); glLoadIdentity(); GL11.glTranslatef(-1.0f, 0.0f, -8.0f); GL11.glRotatef(rquad, 1.0f, 1.0f, 1.0f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(vertexSize, 0, vertexData); glTexCoordPointer(textureVertexSize, 0, textureData); glDrawArrays(GL_QUADS, 0, amountOfVertices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); drawGUI(); Display.sync(60); Display.update(); } Display.destroy(); } private void update(){ rquad -= 0.80f; }
private void drawGUI() { enterOrtho(); guiRenderer(); leaveOrtho(); } private void guiRenderer() { GL11.glColor3f(0.5f, 0.5f, 1.0f); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2f(10, 410); GL11.glVertex2f(70, 410); GL11.glVertex2f(70, 470); GL11.glVertex2f(10, 470); GL11.glEnd(); } public void enterOrtho() { GL11.glPushAttrib(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_ENABLE_BIT); GL11.glPushMatrix(); GL11.glLoadIdentity(); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glPushMatrix(); GL11.glLoadIdentity(); GL11.glOrtho(0, Conf.W, Conf.H, 0, 1, -1); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDisable(GL11.GL_LIGHTING); } public void leaveOrtho() { GL11.glPopMatrix(); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glPopMatrix(); GL11.glPopAttrib(); } } |