Show Posts
|
|
Pages: [1]
|
|
2
|
Java Game APIs & Engines / JOGL Development / JOGL texture flickering
|
on: 2011-02-25 19:23:40
|
I am playing around with the JOGL double-buffering example at http://www.java-tips.org/other-api-tips/jogl/how-to-implement-a-simple-double-buffered-animation-with-mouse-e.html, and modified it to draw a rotating square moving across the page. This worked, but when I tried to bind a texture to it, the image flickers as it goes across the screen. I have tried also setting the swap interval to use vsync, but there was no improvement. What could cause the flickering, and what can I do to get rid of it? Here is where I altered the code from Kiet Le's original example: 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
| Texture elf; float x = -100f; public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.setSwapInterval(1); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.getGL2().glShadeModel(GLLightingFunc.GL_FLAT); gl.glEnable(GL.GL_BLEND); try { elf = loadImage("jogl/ch8/0_2.png"); } catch (Exception ex) { ex.printStackTrace(); } } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); elf.enable(); elf.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP); elf.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP); elf.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); elf.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); elf.bind(); gl.getGL2().glPushMatrix(); gl.getGL2().glTranslatef(x, -50, 0f); gl.getGL2().glRotatef(spin, 0.0f, 0.0f, 1.0f); gl.getGL2().glColor3f(1.0f, 1.0f, 1.0f); gl.getGL2().glBegin(GL2.GL_QUADS); gl.getGL2().glTexCoord2f(0, 1); gl.getGL2().glVertex2f(0, 0); gl.getGL2().glTexCoord2f(1, 1); gl.getGL2().glVertex2f(25, 0); gl.getGL2().glTexCoord2f(1, 0); gl.getGL2().glVertex2f(25, 25); gl.getGL2().glTexCoord2f(0, 0); gl.getGL2().glVertex2f(0, 25); gl.getGL2().glEnd(); elf.disable(); gl.getGL2().glPopMatrix();
gl.glFlush(); x++; if (x > 50f) { x = -100f; } spinDisplay(); } |
|
|
|
|
|
4
|
Java Game APIs & Engines / JOGL Development / using GLJPanel on JOGL 2.0
|
on: 2009-12-31 15:34:38
|
I recently upgraded to JOGL 2.0, and am having trouble rendering to GLJPanel. I am trying to do something simple, draw three colored rectangles to a 400x200 frame, but I cannot see anything on the last third of the screen. If I change to use GLCanvas, or make the frame 400x300, my code works fine, and it worked under JOGL 1.1.1 as well. Is 2.0 stricter on something I missed? My code is below. 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
| import java.awt.Dimension;
import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.awt.GLJPanel; import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.fixedfunc.GLMatrixFunc; import javax.swing.JFrame;
class Ex1_2 { public static GLJPanel getCanvas() { GLCapabilities capabilities = new GLCapabilities(null); capabilities.setDoubleBuffered(true); GLJPanel canvas = new GLJPanel(capabilities); canvas.addGLEventListener(new Ex1Listener()); return canvas; } private Ex1_2() { } }
class Ex1Listener implements GLEventListener { private GL2 gl; public void display(final GLAutoDrawable drawable) { if (gl == null) { gl = drawable.getGL().getGL2(); } gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(1.0f, 1.0f, 1.0f); gl.glBegin(GL2.GL_POLYGON); { gl.glVertex2i(5, 5); gl.glVertex2i(195, 5); gl.glVertex2i(195, 45); gl.glVertex2i(5, 45); } gl.glEnd(); gl.glColor3f(0.75f, 1.0f, 0.375f); gl.glBegin(GL2.GL_POLYGON); { gl.glVertex2i(195, 45); gl.glVertex2i(267, 45); gl.glVertex2i(267, 95); gl.glVertex2i(195, 95); } gl.glEnd(); gl.glColor3f(0.95f, 0.15f, 0.25f); gl.glBegin(GL2.GL_POLYGON); { gl.glVertex2i(267, 95); gl.glVertex2i(395, 95); gl.glVertex2i(395, 145); gl.glVertex2i(267, 145); } gl.glEnd(); gl.glFlush(); } public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) { } public void dispose(final GLAutoDrawable drawable) { } public void init(final GLAutoDrawable drawable) { if (gl == null) { gl = drawable.getGL().getGL2(); } gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(0.0, 400.0, 0.0, 200.0, -1.0, 1.0); } public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { } }
public final class RedBookMain { public static void main(final String[] args) {
JFrame frame = new JFrame(); frame.addWindowListener(new WindowHandler(frame)); frame.setPreferredSize(new Dimension(400, 200)); frame.getContentPane().add(Ex1_2.getCanvas());
frame.pack(); frame.setVisible(true); } private RedBookMain() { } } |
|
|
|
|
|
5
|
Java Game APIs & Engines / JOGL Development / Newbie question about 2D texture drawing
|
on: 2008-11-24 20:37:27
|
Hi, I'm new to JOGL, and I'm looking for some guidance on drawing 2d textures. I've built an app to display a background image that shifts as you approach the edges. My code's working, but the shifting isn't so smooth; occasionally you see jumps. Am I following the right approach in drawing my image? The texture rendering: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| gl.glEnable(GL.GL_TEXTURE_2D); backgroundImage.bind(); gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); gl.glBegin(GL.GL_QUADS); { gl.glTexCoord2f(newLft, newTop); gl.glVertex2f(verLft, verTop); gl.glTexCoord2f(newRit, newTop); gl.glVertex2f(verRit, verTop); gl.glTexCoord2f(newRit, newBtm); gl.glVertex2f(verRit, verBtm); gl.glTexCoord2f(newLft, newBtm); gl.glVertex2f(verLft, verBtm); } gl.glEnd(); gl.glDisable(GL.GL_TEXTURE_2D); gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE); |
And my test application: 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
| import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.net.*; import javax.media.opengl.*; import com.sun.opengl.util.*; import com.sun.opengl.util.texture.*;
public class JOGLTest { public static final Dimension BK_SIZE = new Dimension(1459, 1100); public static final Dimension VIEW_SIZE = new Dimension(300, 300); public static final Dimension FRAME_SIZE = new Dimension(600, 600); public static void main(String[] args) { final Frame f = new Frame(); f.setSize(FRAME_SIZE); f.setMinimumSize(FRAME_SIZE); f.setLayout(null); GLCapabilities glCaps = new GLCapabilities(); glCaps.setDoubleBuffered(true); glCaps.setHardwareAccelerated(true); glCaps.setRedBits(8); glCaps.setBlueBits(8); glCaps.setGreenBits(8); glCaps.setAlphaBits(8); MapCanvas panel = new MapCanvas(glCaps, VIEW_SIZE); panel.setSize(VIEW_SIZE); panel.setMinimumSize(VIEW_SIZE); panel.setMaximumSize(VIEW_SIZE); panel.setBounds(new Rectangle(new Point(150,150), VIEW_SIZE)); final Animator animator = new Animator(panel); f.add(panel); f.addWindowListener(new WindowListener() { public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { animator.stop(); f.dispose(); System.exit(0); } public void windowClosing(WindowEvent e) { animator.stop(); f.dispose(); System.exit(0); } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } }); f.setVisible(true); animator.start(); } } class MapCanvas extends GLCanvas { private Dimension viewSize; public MapCanvas(final GLCapabilities c, final Dimension s) { super(c); viewSize = s; super.addGLEventListener(new MapCanvasListener(this)); } public Dimension getViewSize() { return viewSize; } } class MapCanvasListener implements GLEventListener { private MapCanvas canvas; private float dx; private float dy; int backgroundImageWidth = 1200; int backgroundImageHeight = 824; private Point2D.Float topLeft; private GL gl; private GLDrawable glDrawable; private static final int EDGE = 64; private static float scrollSpeed = 0.25f; private boolean shiftPossible; private int updates; int updatesBeforeShift = 10; private Texture backgroundImage; public MapCanvasListener(final MapCanvas map) { canvas = map; topLeft = new Point2D.Float(0.0f, 0.0f); } public final void stopShifting() { shiftPossible = false; updates = 0; } private void calculateShift() { dx = 0; dy = 0; int bWidth = backgroundImageWidth; int bHeight = backgroundImageHeight; int vWidth = JOGLTest.VIEW_SIZE.width; int vHeight = JOGLTest.VIEW_SIZE.height; Point mouseLoc = canvas.getMousePosition(); if (mouseLoc == null) { stopShifting(); } else { if (mouseLoc.getX() < EDGE) { if (mouseLoc.getY() < EDGE) { if (topLeft.x <= 0 && topLeft.y <= 0) { stopShifting(); } else { dx = -scrollSpeed; dy = -scrollSpeed; shiftPossible = true; } } else if (mouseLoc.getY() > vHeight - EDGE) { if (topLeft.x <= 0 && topLeft.y >= bHeight - vHeight) { stopShifting(); } else { dx = -scrollSpeed; dy = scrollSpeed; shiftPossible = true; } } else { if (topLeft.x <= 0) { stopShifting(); } else { dx = -scrollSpeed; shiftPossible = true; } } } else if (mouseLoc.getX() > vWidth - EDGE) { if (mouseLoc.getY() < EDGE) { if (topLeft.x >= bWidth - vWidth && topLeft.y <= 0) { stopShifting(); } else { dx = scrollSpeed; dy = -scrollSpeed; shiftPossible = true; } } else if (mouseLoc.getY() > vHeight - EDGE) { if (topLeft.x >= bWidth - vWidth && topLeft.y >= bHeight - vHeight) { stopShifting(); } else { dx = scrollSpeed; dy = scrollSpeed; shiftPossible = true; } } else { if (topLeft.x >= bWidth - vWidth) { stopShifting(); } else { dx = scrollSpeed; shiftPossible = true; } } } else if (mouseLoc.getY() < EDGE) { if (topLeft.y <= 0) { stopShifting(); } else { dy = -scrollSpeed; shiftPossible = true; } } else if (mouseLoc.getY() > vHeight - EDGE) { if (topLeft.y >= bHeight - vHeight) { stopShifting(); } else { dy = scrollSpeed; shiftPossible = true; } } else { stopShifting(); } } } public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) { } public void init(GLAutoDrawable drawable) { try { backgroundImage = TextureIO.newTexture(new URL("http://media.maps.com/images/downloads/World-Map-1200.gif"), false, "gif"); backgroundImage.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); backgroundImage.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); } catch (Exception e) { e.printStackTrace(); System.exit(1); } this.gl = drawable.getGL(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); this.glDrawable = drawable;
drawable.setGL(new DebugGL(drawable.getGL()));
float[] values = new float[2]; gl.glGetFloatv(GL.GL_LINE_WIDTH_GRANULARITY, values, 0); gl.glGetFloatv(GL.GL_LINE_WIDTH_RANGE, values, 0); gl.glEnable(GL.GL_LINE_SMOOTH); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE); gl.glLineWidth(1.5f); } private void applyShift() { if (shiftPossible) { updates++; if (updates >= updatesBeforeShift) { topLeft.x += dx; if (topLeft.x > backgroundImageWidth - canvas.getViewSize().width) { topLeft.x = backgroundImageWidth - canvas.getViewSize().width; } if (topLeft.x < 0) { topLeft.x = 0; } topLeft.y += dy; if (topLeft.y > backgroundImageHeight - canvas.getViewSize().height) { topLeft.y = backgroundImageHeight - canvas.getViewSize().height; } if (topLeft.y < 0) { topLeft.y = 0; } } } } public void display(GLAutoDrawable drawable) { calculateShift(); applyShift(); gl.glClearDepth(0.0); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); float verTop = 1.0f; float verLft = -1.0f; float verBtm = -1.0f; float verRit = 1.0f; float texBtm = backgroundImage.getImageTexCoords().bottom(); float texRit = backgroundImage.getImageTexCoords().right(); float verticalScale = (float) backgroundImageHeight / texBtm; float horizontalScale = (float) backgroundImageWidth / texRit; float newTop = (0.0f + topLeft.y) / verticalScale; float newLft = (0.0f + topLeft.x) / horizontalScale; float newBtm = ((float) JOGLTest.VIEW_SIZE.height + topLeft.y) / verticalScale; float newRit = ((float) JOGLTest.VIEW_SIZE.width + topLeft.x) / horizontalScale; gl.glEnable(GL.GL_TEXTURE_2D); backgroundImage.bind(); gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); gl.glBegin(GL.GL_QUADS); { gl.glTexCoord2f(newLft, newTop); gl.glVertex2f(verLft, verTop); gl.glTexCoord2f(newRit, newTop); gl.glVertex2f(verRit, verTop); gl.glTexCoord2f(newRit, newBtm); gl.glVertex2f(verRit, verBtm); gl.glTexCoord2f(newLft, newBtm); gl.glVertex2f(verLft, verBtm); } gl.glEnd(); gl.glDisable(GL.GL_TEXTURE_2D); gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE); } public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { } } |
Thanks for any help I get.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|