Slow performance

Started by Nova_C, January 22, 2005, 22:39:13

Previous topic - Next topic

Nova_C

I'm brand new with both lwjgl and openGL and was trying to use the space invaders tutorial to learn. Before using lwjgl, I was using Java2D to create a graphics engine that turned out pretty good, but the openGL bindings in lwjgl convinced me to switch, as well as the openAL and input support.

Anyway, I tried taking the space invaders code and adapting it to what I was trying to achieve. Basically, I'm trying to load a .jpg image and have it move with the arrow keys. It works, after a fashion. I have no pauses in the code, but I only get about one move every two or three seconds. One of the main problems with what I'm attempting is that much of the code on Space Invaders is uncommented, so I don't know what a lot of things do (Especially considering the javadoc for the GL11 class contains no information on what things do). Here's my code so far. The GenericTools class in one I've created to make tools or methods that I use frequently exportable. Don't worry about it, it only does one thing for this code, anyway, and it works fine.

import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.lwjgl.input.*;
import org.lwjgl.examples.spaceinvaders.TextureLoader;
import org.lwjgl.examples.spaceinvaders.Texture;
import java.io.IOException;

public class Conflict
{

  public static void main(String[] args)
  {
    DisplayMode mode = new DisplayMode(640, 480);
    try
    {
      mode = GenericTools.getDisplayMode(1024, 768, 32, 70);
    }
    catch(LWJGLException e)
    {
      if(e.equals("Unable to find appropriate display mode."))
      {
        System.out.println("Your display is not supported.");
        System.exit(0);
      }
    }
    try
    {
      Display.setDisplayMode(mode);
      Display.setTitle("Conflicts of Interest");
      Display.create();
      Display.setFullscreen(true);
    }
    catch(Exception e)
    {
      System.out.println("Exception " + e);
    }
    Mouse.setGrabbed(true);
    GenericTools.setGL2D();
    GL11.glOrtho(0, 1024, 768, 0, -1, 1);
    TextureLoader textureLoader = new TextureLoader();
    Texture sprite;
    int xloc = 512;
    int yloc = 384;
    try
    {
      Keyboard.create();
      Keyboard.poll();
      sprite = textureLoader.getTexture("image.jpg");
      while(!Keyboard.isKeyDown(Keyboard.KEY_Q))
      {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
        GL11.glPushMatrix();
        sprite.bind();
        
      	GL11.glTranslatef(xloc, yloc, 0);		
      	GL11.glColor3f(1,1,1);
  		
      	// draw a quad textured to match the sprite
      	GL11.glBegin(GL11.GL_QUADS);
      	{
  	      GL11.glTexCoord2f(0, 0);
  	      GL11.glVertex2f(0, 0);
  	      GL11.glTexCoord2f(0, sprite.getHeight());
  	      GL11.glVertex2f(0, 100);
  	      GL11.glTexCoord2f(sprite.getWidth(), sprite.getHeight());
  	      GL11.glVertex2f(100,100);
  	      GL11.glTexCoord2f(sprite.getWidth(), 0);
  	      GL11.glVertex2f(100,0);
      	}
      	GL11.glEnd();
  		
      	// restore the model view matrix to prevent contamination
      	GL11.glPopMatrix();

        Display.update();
        Keyboard.poll();
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT))
          xloc--;
        else if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
          xloc++;
        else if(Keyboard.isKeyDown(Keyboard.KEY_UP))
          yloc--;
        else if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
          yloc++;
      }
    }
    catch(IOException e)
    {
      System.out.println("Error loading sprite.");
      System.exit(0);
    }
    catch(LWJGLException e1)
    {
      System.out.println("LWJGL Error reported: " + e1);
      System.exit(0);
    }
  }
}


The part that I am particularly having trouble with is the glTexCoord and glVertex2f methods. I don't understand why I'm passing them what I'm passing them. (The image.jpg file is 100x100x24) Any help would be appreciated.

EDIT: Oh, and if you want to mention that my code isn't very object oriented, don't worry. I'm just trying to figure out lwjgl and once I know what to break up and where, it'll follow convention more closely.

Chman

Sorry, haven't time to look at your code (although it seems to be good), but one thing I must say about LWJGL, you must know how to program with OpenGL, I mean, you should look at core OpenGL tutorials (here for example). It's why all GL functions are uncommented in the javadoc.

Chman

CaptainJester

I think it is moving slow because you have a pretty big ortho mode screen going.

Try this:
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) 
  xloc-=5; 
else if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) 
  xloc+=5; 
else if(Keyboard.isKeyDown(Keyboard.KEY_UP)) 
  yloc-=5; 
else if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) 
  yloc+=5;


Also you should make your textures a power of 2 size, ie 128x128 or 64x64. upt to 1024x1024
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)

tomb

Here is some OpenGL docs that may be usefull:
GL man pages - documents what the different gl function does
OpenGL red book - Good idee to read this whole book, cover to cover.

Nova_C

Thanks for the advice, guys, I'm checking those pages now. One thing, though, did lwjgl change recently? I only ask because ALL the tutorials I've found reference a static Window class, which doesn't exist in the version of lwjgl I have (Most recent - 0.94?). Obviously, I used the Display class in the same way, but was this change very recent? And for curiousity's sake, why was it changed? Thanks.

Chman

Hum yes the 0.93 or 0.92 I don't remember breaks all the code with that removed Window class. I've put up to date tutorials on my website, you could take a look there for some examples (here).

Chman

elias4444

tomb - THANK YOU so much for those books references!!!!! Things like that are hard to find for newbies (we just don't know about them unless someone shares).

THANK YOU THANK YOU!!!
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

Nova_C

Okay, I've started making a lot of sense out of what I was trying to do. Thanks guys. Also, I resolved my performance issue. Not my fault. I was using Nvidia's release 60 drivers (61.77) and I just found out that ALL openGL apps run slow. Release 65 (66.9?) don't work on my machine (I get the driver infinite loop error whenever it tries to render in 3D), so I went back to the year old release 55 drivers. And now it all runs so fast :D. Incidentally, this also resolved all the WoW graphics issues I've been having. Strange.