antialiasing using FSAA or multisampling

Started by polskyman, September 28, 2006, 12:51:55

Previous topic - Next topic

polskyman

Hi,

I would like to do a simple 4X antialiasing with the easy example of the applet. but I can get it to work antialiased.
Can you provide me the good sample of code to make it work antialiased ?

package org.lwjgl.test.applet;
 
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
 
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.applet.LWJGLInstaller;
 
public class AppletTestComplete extends Applet {
  
  public void init() {
    
    /* Install applet binaries */
    try {
      LWJGLInstaller.tempInstall();
    } catch (Exception le) {
      le.printStackTrace();
    }
    
    setLayout(new BorderLayout());
    try {
      Canvas canvas = new AppletCanvas();
      canvas.setSize(getWidth(), getHeight());
      add(canvas);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  /*
   * @see org.lwjgl.opengl.AWTGLCanvas
   */
  public class AppletCanvas extends AWTGLCanvas {
    
    float angle = 0;
    
    /*
     * @see org.lwjgl.opengl.AWTGLCanvas#AWTGLCanvas()
     */
    public AppletCanvas() throws LWJGLException {
      // Launch a thread to repaint the canvas 60 fps
      Thread t = new Thread() {
        public void run() {
          while (true) {
            if (isVisible()) {
              repaint();
            }
            Display.sync(60);
          }
        }
      };
      t.setDaemon(true);
      t.start();      
    }
    
    /*
     * @see org.lwjgl.opengl.AWTGLCanvas#paintGL()
     */
    public void paintGL() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
      GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, 640, 0, 480, 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW_MATRIX);
 
      GL11.glPushMatrix();
      GL11.glTranslatef(320, 240, 0.0f);
      GL11.glRotatef(angle, 0, 0, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2i(-50, -50);
      GL11.glVertex2i(50, -50);
      GL11.glVertex2i(50, 50);
      GL11.glVertex2i(-50, 50);
      GL11.glEnd();
      GL11.glPopMatrix();
 
      angle += 1;
 
      try {
        swapBuffers();
      } catch (Exception e) {
      }
    }   
  }
}


thank you for your help

Matzon

You should instantiate the applet with the constructor that allows a PixelFormat arg. Then set samples to 4 in the PixelFormat constructor

polskyman

thank you but could you give me the sample of code to achieve that because I do not see how to do it.
take for example the code over and tell me what to change

Evil-Devil

You can use the AWTGLCanvas Constructor that accepts a pixelformat.
QuoteAWTGLCanvas

public AWTGLCanvas(PixelFormat pixel_format)
           throws LWJGLException

   Create an AWTGLCanvas with the requested PixelFormat on the default GraphicsDevice.

   Parameters:
       pixelFormat - The desired pixel format. May not be null
       device - the device to create the canvas on.
   Throws:
       LWJGLException

Canvas canvas = new AppletCanvas(new PixelFormat(8,0,0,4));


Its like the default PixelFormat Constructor, except that samples is set to 4.

You have to add the Constructor to your applatcanvas class and call its parent.

polskyman

Sorry evil-devil but this sample code does not work.
I have no antialiased but standard jagged square.
if you have a working sample code please post it here or send it to me.
thanks

Matzon

check your drivers - works as expected with nvidia
specifically, check your forced AA and AF settings in the control panel

polskyman

AA works fine when forced.
if you know how to do matzon please post a full code sample doing AA this will help me a lot.
thanks

Matzon

using your code, I just added a call to the constructor accepting a pixelformat:
package org.lwjgl.test.applet;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

/*
 * @see org.lwjgl.opengl.AWTGLCanvas
 */
public class AppletCanvas extends AWTGLCanvas {

	float	angle	= 0;

	/*
	 * @see org.lwjgl.opengl.AWTGLCanvas#AWTGLCanvas()
	 */
	public AppletCanvas() throws LWJGLException {
		super(new PixelFormat(8, 0, 0, 4));
		// Launch a thread to repaint the canvas 60 fps
		Thread t = new Thread() {

			public void run() {
				while (true) {
					if (isVisible()) {
						repaint();
					}
					Display.sync(60);
				}
			}
		};
		t.setDaemon(true);
		t.start();
	}

	/*
	 * @see org.lwjgl.opengl.AWTGLCanvas#paintGL()
	 */
	public void paintGL() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, 640, 0, 480, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW_MATRIX);

		GL11.glPushMatrix();
		GL11.glTranslatef(320, 240, 0.0f);
		GL11.glRotatef(angle, 0, 0, 1.0f);
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2i(-50, -50);
		GL11.glVertex2i(50, -50);
		GL11.glVertex2i(50, 50);
		GL11.glVertex2i(-50, 50);
		GL11.glEnd();
		GL11.glPopMatrix();

		angle += 1;

		try {
			swapBuffers();
		} catch (Exception e) {
		}
	}
}

polskyman

Just compiled you sample but it does not work....

console report:

java.lang.ClassCastException

   at sun.applet.AppletPanel.createApplet(Unknown Source)

   at sun.plugin.AppletViewer.createApplet(Unknown Source)

   at sun.applet.AppletPanel.runLoader(Unknown Source)

   at sun.applet.AppletPanel.run(Unknown Source)

   at java.lang.Thread.run(Unknown Source)



please help me this is very important

polskyman