LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: gdboling on April 19, 2004, 02:28:22

Title: What's the difference....
Post by: gdboling on April 19, 2004, 02:28:22
I'm new to LWJGL so bear with me.

I have 2 programs that are almost identicle except for on small section.  One of them displays the quad.  The other one does not.  

Here is the one that works..


package com.gthought.swing;

import org.lwjgl.DisplayMode;
import org.lwjgl.Display;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Window;
import org.lwjgl.opengl.GL11;

public class OpenGLWindow
{
   public static void main(String[] args)
   {
       try
       {
           int mode = -1;
           DisplayMode[] modes = Display.getAvailableDisplayModes();
           System.out.println("Found " + modes.length + " display modes");
           for (int i = 0; i < modes.length; i++)
           {
               
               if (modes[i].width == 640 && modes[i].height == 640 && modes[i].bpp == 16)
              {
                  mode = i;
               }
               if (mode != -1)
               {
                   Display.setDisplayMode(modes[mode]);
               }
            }
       }
           catch(Exception e)
           {

           }
       try
       {
           Window.create("Test Window");
       }
       catch(Exception e)
       {

       }

       while(!Window.isCloseRequested())
       {
           Window.update();
           try
           {

               Thread.sleep(100);
           }
           catch(Exception ee)
           {

           }
           render();
           if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
           {
               break;
           }
       }

       Window.destroy();
   }

   private static void render()
   {
       GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glPushMatrix();
GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);
GL11.glRotatef(45f, 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();

   }
}


This puts the display in full screen mode and I can see my quad.  But I want to work in windowd mode, so I commented out 1 section and changed the Window.create() method like this..


package com.gthought.swing;

import org.lwjgl.DisplayMode;
import org.lwjgl.Display;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Window;
import org.lwjgl.opengl.GL11;

public class OpenGLWindow
{
   public static void main(String[] args)
   {
       /*
       try
       {
           int mode = -1;
           DisplayMode[] modes = Display.getAvailableDisplayModes();
           System.out.println("Found " + modes.length + " display modes");
           for (int i = 0; i < modes.length; i++)
           {

               if (modes[i].width == 640 && modes[i].height == 640 && modes[i].bpp == 16)
              {
                  mode = i;
               }
               if (mode != -1)
               {
                   Display.setDisplayMode(modes[mode]);
               }
            }
       }
           catch(Exception e)
           {

           }
           */
       try
       {
           Window.create("Test Window", 50, 50, 640, 480, 16, 0, 0, 0, 0);
       }
       catch(Exception e)
       {

       }

       while(!Window.isCloseRequested())
       {
           Window.update();
           try
           {

               Thread.sleep(100);
           }
           catch(Exception ee)
           {

           }
           render();
           if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
           {
               break;
           }
       }

       Window.destroy();
   }

   private static void render()
   {
       GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glPushMatrix();
GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);
GL11.glRotatef(45f, 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();

   }
}


Now, I get the window to appear just fine.  But I can't see my quad.  Can someone tell me what I am doing wrong?

Thanks.

Gregg
Title: hmmmmmm
Post by: Fool Running on April 19, 2004, 13:46:06
I'm not sure without trying it, but it looks like your Window.update() needs to be called after you render in your loop, not before...

hope this helps  :)
Title: again....
Post by: Fool Running on April 19, 2004, 13:48:43
something like this:


       while(!Window.isCloseRequested())
       {
           try
           {

               Thread.sleep(100);
           }
           catch(Exception ee)
           {

           }
           render();
           Window.update();

           if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
           {
               break;
           }
       }


I'll try it later when I get back from work  :lol:
Title: What's the difference....
Post by: princec on April 19, 2004, 14:08:43
Actually - it doesn't matter where it gets called any more, as it's only called once per loop iteration.

Cas :)
Title: whoops
Post by: Fool Running on April 19, 2004, 20:58:25
I realized after I posted it that the Window.update() doesn't have to be at the bottom  :oops:  (and I call myself a programmer  :wink: )

anyways....

try changing

     GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);

to

     GL11.glTranslatef(Window.getWidth() / 2, Window.getHeight() / 2, 0.0f);



(I was trying this on version 0.8 so I had to play around with it, but I think that was the thing that made it work....  :roll:  )
Title: Yay!
Post by: lunarknight on April 23, 2004, 13:35:02
You're code really helped me get started with LWJGL. You should submit it as a getting started code... or maybe make a few modifications and submit it to nehe tutorials!

Things your skeleton/base code taught me...
1)How to change resolution
2)How to go full screen
3)How to draw a polygon/triangle on the screen.

Thanks!~
LK
Title: Something I don't understand...
Post by: lunarknight on April 23, 2004, 13:47:51
I guess what I don't understand is how to detect what Hz to use.
I want a 800x600 16bit full screen mode, but there's many different Hz values.
Title: What's the difference....
Post by: princec on April 23, 2004, 14:03:16
There is now extremely groovy display mode selector utility class in org.lwjgl.util - have a look in the CVS.

Basically you pass it a bunch of criteria and it tries to pick the display mode that best satisfies your criteria.

Everyone should use it :)

Cas :)
Title: What's the difference....
Post by: cfmdobbie on April 23, 2004, 18:29:45
Will do, as soon as it's in a downloadable distro!  Until then I'll use me own little class, presented here for your amusement:

package org.padcroft.clt.util;

import org.lwjgl.DisplayMode ;
import java.util.List ;
import java.util.ArrayList ;
import java.util.Iterator ;
import java.util.Collections ;
import java.util.Comparator ;
import java.lang.reflect.Field ;

/**
* @author cfmdobbie
*/
public class DisplayModeFilter
{
// Required DisplayMode settings
private int width = -1 ;
private int height = -1 ;
private int bpp = -1 ;
private int freq = -1 ;


// List of sorts to apply to DisplayModes
private List sortOrder = new ArrayList(10) ;

// Private Comparator class to sort DisplayModes
private static class DisplayModeFieldComparator
implements Comparator
{
private final Field field ;
private final boolean ascending ;

private DisplayModeFieldComparator(final String fieldName, final boolean ascending)
{
try
{
field = DisplayMode.class.getField(fieldName) ;
}
catch(NoSuchFieldException e)
{
throw new AssertionError(e) ;
}

this.ascending = ascending ;
}

public int compare(Object e1, Object e2)
{
DisplayMode dm1 = (DisplayMode)e1 ;
DisplayMode dm2 = (DisplayMode)e2 ;

int f1, f2 ;

try
{
f1 = field.getInt(dm1) ;
f2 = field.getInt(dm2) ;
}
catch(IllegalAccessException e)
{
throw new AssertionError(e) ;
}

if(f1 < f2)
return (ascending ? -1 : 1) ;
else if(f1 > f2)
return (ascending ? 1 : -1) ;
else
return 0 ;
}
}

// Comparators
private static final Comparator WIDTH_ASCENDING = new DisplayModeFieldComparator("width", true) ;
private static final Comparator HEIGHT_ASCENDING = new DisplayModeFieldComparator("height", true) ;
private static final Comparator BPP_ASCENDING = new DisplayModeFieldComparator("bpp", true) ;
private static final Comparator FREQ_ASCENDING = new DisplayModeFieldComparator("freq", true) ;

private static final Comparator WIDTH_DESCENDING = new DisplayModeFieldComparator("width", false) ;
private static final Comparator HEIGHT_DESCENDING = new DisplayModeFieldComparator("height", false) ;
private static final Comparator BPP_DESCENDING = new DisplayModeFieldComparator("bpp", false) ;
private static final Comparator FREQ_DESCENDING = new DisplayModeFieldComparator("freq", false) ;

// Constructor
public DisplayModeFilter()
{
}

// Property setters
public DisplayModeFilter setWidth(final int width) { this.width = width ; return this ; }
public DisplayModeFilter setHeight(final int height) { this.height = height ; return this ; }
public DisplayModeFilter setBpp(final int bpp) { this.bpp = bpp ; return this ; }
public DisplayModeFilter setFreq(final int freq) { this.freq = freq ; return this ; }

// Sorting order functions
public DisplayModeFilter sortByWidthAscending() { sortOrder.add(WIDTH_ASCENDING) ; return this ; }
public DisplayModeFilter sortByHeightAscending() { sortOrder.add(HEIGHT_ASCENDING) ; return this ; }
public DisplayModeFilter sortByBppAscending() { sortOrder.add(BPP_ASCENDING) ; return this ; }
public DisplayModeFilter sortByFreqAscending() { sortOrder.add(FREQ_ASCENDING) ; return this ; }

public DisplayModeFilter sortByWidthDescending() { sortOrder.add(WIDTH_DESCENDING) ; return this ; }
public DisplayModeFilter sortByHeightDescending() { sortOrder.add(HEIGHT_DESCENDING) ; return this ; }
public DisplayModeFilter sortByBppDescending() { sortOrder.add(BPP_DESCENDING) ; return this ; }
public DisplayModeFilter sortByFreqDescending() { sortOrder.add(FREQ_DESCENDING) ; return this ; }

// Filter method
public DisplayMode[] filter(final DisplayMode[] inModes)
{
final List filteredModes = new ArrayList(inModes.length) ;

for(int i = 0 ; i < inModes.length ; i++)
{
if(
(width == -1 || width == inModes[i].width) &&
(height == -1 || height == inModes[i].height) &&
(bpp == -1 || bpp == inModes[i].bpp) &&
(freq == -1 || freq == inModes[i].freq)
)
{
filteredModes.add(inModes[i]) ;
}
}

// Now sort filteredModes according to sort spec
final Iterator i = sortOrder.iterator() ;
while(i.hasNext())
{
final Comparator comparator = (Comparator)i.next() ;
Collections.sort(filteredModes, comparator) ;
}

// Convert to array, and return
DisplayMode[] outModes = (DisplayMode[])filteredModes.toArray(new DisplayMode[0]) ;

return outModes ;
}
}


Used like this:

DisplayModeFilter filter = new DisplayModeFilter();
filter.setWidth(1024).setHeight(768).setBpp(32);
filter.sortByFreqDescending();
DisplayMode[] modes = Display.getAvailableDisplayModes();
DisplayMode[] filtered = filter.filter(modes);


Rather overkill these days, but back when I first wrote it we were deciding things like alpha and depth buffers on display creation, as well!
Title: Re: Yay!
Post by: gdboling on May 09, 2004, 21:18:33
Quote from: "lunarknight"You're code really helped me get started with LWJGL. You should submit it as a getting started code... or maybe make a few modifications and submit it to nehe tutorials!

Things your skeleton/base code taught me...
1)How to change resolution
2)How to go full screen
3)How to draw a polygon/triangle on the screen.

Thanks!~
LK

For some reason I am not getting notified when this thread is updated and I have chosen that option.  Anyway, no problem.  Glad my misunderstanding could help someone. :)
Title: Re: whoops
Post by: gdboling on May 09, 2004, 21:18:54
Quote from: "Fool Running"I realized after I posted it that the Window.update() doesn't have to be at the bottom  :oops:  (and I call myself a programmer  :wink: )

anyways....

try changing

     GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);

to

     GL11.glTranslatef(Window.getWidth() / 2, Window.getHeight() / 2, 0.0f);



(I was trying this on version 0.8 so I had to play around with it, but I think that was the thing that made it work....  :roll:  )

Yes, this seemed to do the trick.  Thanks.