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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| package chapter3;
import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import static org.lwjgl.opengl.GL11.*;
public class Stencil { private float x1; private float y1; private float rsize; private float xstep; private float ystep; private float clipWidth; private float clipHeight; public Stencil() { x1 = 0.0f; y1 = 0.0f; rsize = 25; xstep = 1.0f; ystep = 1.0f; try { Display.setDisplayMode(new DisplayMode(800, 600)); Display.setTitle("Stencil Animation"); Display.create(); Display.setResizable(true); Display.setVSyncEnabled(true); } catch (LWJGLException e) { e.printStackTrace(); } } public void run() { while (!Display.isCloseRequested()) { setupRC(); changeSize(Display.getWidth(), Display.getHeight()); animate(Display.getWidth(), Display.getHeight()); renderScene(); Display.update(); } cleanUp(); } public void renderScene() { double dRadius = 0.1; double dAngle; glClearStencil(0); glEnable(GL_STENCIL_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_NEVER, 0x0, 0x0); glStencilOp(GL_INCR, GL_INCR, GL_INCR); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_LINE_STRIP); for (dAngle = 0; dAngle < 400; dAngle += 0.1) { glVertex2d(dRadius * Math.cos(dAngle), dRadius * Math.sin(dAngle)); dRadius *= 1.002; } glEnd(); glStencilFunc(GL_NOTEQUAL, 0x1, 0x1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glColor3f(1.0f, 0.0f, 0.0f); glRectf(x1, y1, x1 + rsize, y1 - rsize); } public void setupRC() { glClearColor(0.0f, 0.0f, 1.0f, 0.0f); } public void changeSize(int w, int h) { float nRange = 100.0f; if (h == 0) { h = 1; } glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) { glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange); clipWidth = nRange; clipHeight = nRange * h / w; } else { glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange); clipWidth = nRange * w / h; clipHeight = nRange; } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } public void animate(float windowWidth, float windowHeight) { if (x1 > clipWidth - rsize || x1 < -clipWidth) { xstep = -xstep; } if (y1 > clipHeight || y1 < -clipHeight + rsize) { ystep = -ystep; } animate(); if (x1 > (windowWidth - rsize + xstep)) { x1 = windowWidth - rsize - 1; } else if (x1 < -(windowWidth + xstep)) { x1 = -windowWidth - 1; } if (y1 > (windowHeight + ystep)) { y1 = windowHeight - 1; } else if (y1 < -(windowHeight - rsize + ystep)) { y1 = -windowHeight + rsize - 1; } } public void animate() { x1 += xstep; y1 += ystep; } public void cleanUp() { Display.destroy(); } public static void main(String[] args) { new Stencil().run(); } }
|