No drawing in AWTGLCanvas

Started by Fool Running, April 23, 2007, 16:56:29

Previous topic - Next topic

Fool Running

I'm stuck on this problem:
I have an AWTGLCanvas in a JFrame (with other swing components). The canvas is repainted every time a slider is moved (to update a preview).
Initially the canvas draws correctly and everything seems fine. However, after a random amount of time of constantly moving the slider, the canvas will cease to be drawn.
The strange part is that it is still painted, just not with the OpenGL stuff I'm drawing, just with the clear color (I've confirmed this by clearing to a random color each paint).
I get no OpenGL errors and I can step through the code just fine and see that it is calling glVertex2f(), glTexCoord2f(), etc., just fine. Its like OpenGL has decided to ignore the calls.

Any ideas would be greatly appreciated. ;D

EDIT: Forgot to mention...  I have multiple AWTGLCanvases in my JFrame. The problem still happens with just one, but with multiple, they all seem to be affected at the same time.
i.e. When one stops drawing, they all stop drawing.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Matzon

can you possibly produce a small test case ?

Fool Running

Well, here is a "small" test case:
// LWJGLProblem.java

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.PixelFormat;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import static org.lwjgl.opengl.GL11.*;

public class LWJGLProblem extends JFrame implements ChangeListener{
    private JSlider slider = new JSlider();
    private MyCanvas canvas;
    
    /**
     * Creates a new LWJGLProblem
     */
    public LWJGLProblem(){
        setSize(640, 480);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(slider, BorderLayout.NORTH);
        
        try{
            canvas = new MyCanvas();
            add(canvas, BorderLayout.CENTER);
        }catch(LWJGLException e){
            e.printStackTrace();
        }
        slider.addChangeListener(this);
    }

    /**
     * Called when the slider moves
     * @param  e The event data
     */
    public void stateChanged(ChangeEvent e){
        canvas.repaint();
    }
    
    /**
     * Starts the program :)
     * @param  args The program arguments
     */
    public static void main(String[] args){
        LWJGLProblem frame = new LWJGLProblem();
        frame.setVisible(true);
    }
    
    /**
     * This is the AWTGLCanvas the will show the preview
     * @author  Tim Steenwyk
     */
    private class MyCanvas extends AWTGLCanvas{
        /**
         * Creates a new Canvas 
         * @throws  LWJGLException If we can't make a view
         */
        public MyCanvas() throws LWJGLException{
            super(new PixelFormat(8, 16, 0));
        }
        
        /**
         * Initializes OpenGL for the preview
         */
        public void initGL(){
            glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            glDisable(GL_LIGHTING);
            glDisable(GL_DEPTH_TEST);
            glDepthMask(false);
            glEnable(GL_SCISSOR_TEST);
            glEnable(GL_BLEND);
            glEnable(GL_TEXTURE_2D);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        }
        
        /**
         * Paints the preview
         */
        public void paintGL(){
            int width = getWidth();
            int height = getHeight();
            glClearColor((float)Math.random(), (float)Math.random(), (float)Math.random(), 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glViewport(0, 0, width, height);
            glScissor(0, 0, width, height);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glColor4f(1.0f, 0.0f, 0.0f, 0.4f);

            glDisable(GL_TEXTURE_2D);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glBegin(GL_QUADS);
                drawRectangle(5, 15, 108, 50);
                drawRectangle(15, 25, 108, 50);
                drawRectangle(25, 35, 108, 50);
                drawRectangle(35, 45, 108, 50);
                drawRectangle(45, 55, 108, 50);
            glEnd();
            
            if(glGetError() != GL_NO_ERROR){
                System.out.println("whoops");
            }
            
            try{
                swapBuffers();
            }catch(LWJGLException e){
                e.printStackTrace();
            }
        }
        
        /**
         * Draws a rectangle having the specified parameters 
         * @param  x The x location
         * @param  y The y location
         * @param  width The width of the rectangle
         * @param  height The height of the rectangle
         */
        private void drawRectangle(float x, float y, float width, float height){
            glVertex2f(x, y);
            glVertex2f(x, y + height);
            glVertex2f(x + width, y + height);
            glVertex2f(x + width, y);
        }
        
        /**
         * Handles errors that were thrown during a paint
         * @param  exception The exception that was thrown
         */
        protected void exceptionOccurred(LWJGLException exception){
            exception.printStackTrace();
        }
    }
}


Another strange thing is that today it seems to stop drawing everything, including the clear color.
Just move the slider back and forth for a while and it should stop drawing.
Hope you guys can reproduce it :)

By the way, here are my system specs if you need them:
Windows XP Pro SP2, Intel Core 2 Duo 6600, Nvidia Geforce 7950GT, 2GB ram
All drivers/patches are up-to-date

EDIT: I'm using Java v6.0
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Matzon

how long time is that random time ? - I've been doing it for minutes - no errors.

Fool Running

Quotehow long time is that random time ? - I've been doing it for minutes - no errors.
Its usually only a couple of seconds (at most 10 seconds).
Guess you can't reproduce it then :(

Maybe its just my computer? ???

EDIT: I'll try it on another computer I have here.

EDIT2: I tried it on another computer and couldn't reproduce it. It seems to just be this one computer.
Maybe its time I re-installed windows again  ::)
Well, thanks for trying :)
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Fool Running

Well, I reinstalled Windows and upgraded to LWJGL 1.1 and the problem is still happening on my machine :-\

Considering this is a clean install and it happens 99% of the time, I'm inclined to think that its a real problem.
I think its some kind of timing issue since it always seems to take a random amount of time, but I'm not sure what would cause this kind of behavior.

Anyone have any ideas of how to track it down? ???
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Matzon

I would download the source for lwjgl, recompile the lwjgl jar with debug statements added where appropriate.
Furthermore you could do a thread dump in the console to see which threads are deadlock'ed (if any)

Fool Running

Well, I put println() everywhere I could think of and ran it and nothing look out of the ordinary....
The context is made current correctly, the OpenGL drawing code gets called correctly, the buffer gets swapped correctly, etc. As near as I can tell everything is working the way it should, but nothing shows on the screen.
No deadlocked threads either.

I'm just gonna give up and hope it goes away ;D

Thanks for the suggestions, Matzon
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D