[small fonts] need help [/small fonts]

Psssst, something is not quite right with this pic

http://www.realityflux.com/abba/displacement.jpg

If you didn’t guess it at this point, well let me explain: That’s a terrain mesh generated by two 196196 pictures and passed to the hardware as a vertex array.
Well it’s working, but the performance is horrible and I have few doubts regarding my code.
Anyways, this is the source (very short), now could someone help me out establishing why the performance is crawling so bad?
Cough
how do I make it work with Triangle strips instead of triangles? *cough

Well this is the terrain render loop I curently use, it’s probably not the best way to do it, but it was the most direct way to convert the GL_TRIANGLES loop I had before (very much like yours) into a GL_TRIANGLE_STRIP loop:


      private void renderGeo(GL gl, float fScale) {
            float vfHeights[][] = m_model.getHeights();
            Point3f origin = m_model.getOrigin();
            boolean bSwitchDirection = false;
            int w = m_model.getWidth();
            int h = m_model.getHeight();
            for (int i = 0; i < w; i++) {
                  if (bSwitchDirection) {
                        for (int j = 0; j < h; j++) {
                              gl.glTexCoord2f((float)(i + 1) / w, (float)j / h);
                              gl.glVertex3f(origin.x + (i + 1) * m_model.getPatchWidth(), origin.y + fScale * vfHeights[i + 1][j], origin.z + j * m_model.getPatchHeight());
                              gl.glTexCoord2f((float)i / w, (float)j / h);
                              gl.glVertex3f(origin.x + i * m_model.getPatchWidth(), origin.y + fScale * vfHeights[i][j], origin.z + j * m_model.getPatchHeight());
                        }
                  } else {
                        for (int j = h - 1; j >= 0; j--) {
                              gl.glTexCoord2f((float)i / w, (float)j / h);
                              gl.glVertex3f(origin.x + i * m_model.getPatchWidth(), origin.y + fScale * vfHeights[i][j], origin.z + j * m_model.getPatchHeight());
                              gl.glTexCoord2f((float)(i + 1) / w, (float)j / h);
                              gl.glVertex3f(origin.x + (i + 1) * m_model.getPatchWidth(), origin.y + fScale * vfHeights[i + 1][j], origin.z + j * m_model.getPatchHeight());
                        }
                  }
                  bSwitchDirection = !bSwitchDirection;
            }
      }

PS: Please ignore the silly names for the model’s width and height because the have nothing to do with width and height as experienced by the viewer. I still have to rename them to width/depth or something like that.

Nah dude, I was generating terrain meshes that way ever since I was introduced to 3D coding.
I wanna take advantage of Vertex Arrays, and in this case, I think there is something very wrong with the code I provided. Yet I can’t put my hand on it … :-X

I had a look at the code yesterday and I can’t say I saw anything wrong with it. I got about 27 fps on a p3 + onbaord i810.
I modified the code to use one triangle strip for each row of data instead of triangles but that only provided a gain of about 3 fps.

I used display lists and I got 510 fps instead of 66 :stuck_out_tongue:

Java Cool Dude: He, you were asking how to do triangle strips so that’s what I explained. Maybe you should have phrased your question better? :wink:

Anyway, I tried using display lists for my terrain it it slowed down horribly! It did okay for lots’s of smaller objects but the terrain itself just didn’t want to work :-/ It looked like it was consuming too much memory (even though there were only like 20K triangles) because after 2 seonds the application would become unresponsive and shutting it down made the hard drive trash for about 5 minutes!

PS: You say you get 510fps now, are you using any other threads in your app? If so how do you prevent starvation? Of I let my render thread go at full speed other thread hardly get any time anymore. But putting in a sleep(1) immediately drops my fps to around 80 (instead of 130-160).

[quote]Java Cool Dude: He, you were asking how to do triangle strips so that’s what I explained. Maybe you should have phrased your question better? :wink:

Anyway, I tried using display lists for my terrain it it slowed down horribly! It did okay for lots’s of smaller objects but the terrain itself just didn’t want to work :-/ It looked like it was consuming too much memory (even though there were only like 20K triangles) because after 2 seonds the application would become unresponsive and shutting it down made the hard drive trash for about 5 minutes!

PS: You say you get 510fps now, are you using any other threads in your app? If so how do you prevent starvation? Of I let my render thread go at full speed other thread hardly get any time anymore. But putting in a sleep(1) immediately drops my fps to around 80 (instead of 130-160).
[/quote]
Hey man I’m not looking into starting a pointless argument here, but when I was saying I want it to work with tirangle strips, I was referring to the following code:
gl.glDrawElements(gl.GL_TRIANGLE_STRIP, terrain.vertexIndices.length,gl.GL_UNSIGNED_INT, terrain.vertexIndices);

Turned out that if I wanted it that way, I would have to create a vertex array for every signle row or column; I feared that a lot, and Pepi confirmed it :’(

Using display lists boosts the frame rate tremendously as shown on the following screenshot:

http://www.realityflux.com/abba/displacement2.jpg

I’m gonna try to return to the glVertex call and see how it fairs, but I doubt it’ll be faster thatn DLs.


import net.java.games.jogl.*;
import net.java.games.jogl.util.GLUT;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class displacementMap implements KeyListener
{
  initRenderer  renderer;
  Animator      loop;
  GLCanvas      canvas;
  Terrain       terrain;
  JFrame        frame;
  boolean       keys[] = new boolean[255];
  int           screenWidth,
                screenHeight,
                canvasHeight,
                canvasWidth,
                xLocation,
                yLocation,
                list;


  public static void main(String []args) {
    displacementMap demo = new displacementMap();
  }

  displacementMap(){
    int fullScreen = JOptionPane.showConfirmDialog(        null, "Would you like to run in fullscreen mode?",
                                                   "Fullscreen",  JOptionPane.YES_NO_OPTION);
    if(fullScreen!=0)
      JFrame.setDefaultLookAndFeelDecorated(true);

    frame          = new JFrame("Displacement map");
    screenWidth    = Toolkit.getDefaultToolkit().getScreenSize().width;
    screenHeight   = Toolkit.getDefaultToolkit().getScreenSize().height;

    switch(fullScreen){
      case 0:
        frame.setUndecorated(true);
      break;
      default:
        canvasWidth  = 640;
        canvasHeight = 480;
        xLocation    = (screenWidth  - canvasWidth )>>1;
        yLocation    = (screenHeight - canvasHeight)>>1;
        frame.setLocation(xLocation,yLocation);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    canvas  = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
    canvas.setSize(new Dimension(canvasWidth,canvasHeight));
    canvas.addGLEventListener((renderer = new initRenderer()));
    canvas.addKeyListener(this);
    canvas.requestFocus();

    frame.getContentPane().add(canvas,BorderLayout.CENTER);
    frame.addWindowListener(new shutDownWindow());
    frame.addKeyListener(this);

    if(fullScreen==0){
      GraphicsEnvironment.getLocalGraphicsEnvironment().
      getDefaultScreenDevice().setFullScreenWindow(frame);
      GraphicsEnvironment.getLocalGraphicsEnvironment().
      getDefaultScreenDevice().setDisplayMode((new DisplayMode(640, 480, 32,
                                               DisplayMode.REFRESH_RATE_UNKNOWN)));
    }
    else
      frame.pack();
    frame.setVisible(true);
  }

  public class initRenderer
               implements GLEventListener
  {
    public void init(GLDrawable drawable){
      GL  gl         = drawable.getGL();

      terrain = new Terrain(frame, "Data/height.jpg", "Data/texture.jpg");
      gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
      gl.glEnableClientState(gl.GL_COLOR_ARRAY);
      gl.glNewList((list = gl.glGenLists(1)),gl.GL_COMPILE);
      gl.glVertexPointer(3, gl.GL_FLOAT, 0, terrain.vertexData);
      gl.glColorPointer( 3, gl.GL_FLOAT, 0, terrain.colorData );
      gl.glDrawElements(gl.GL_TRIANGLES, terrain.vertexIndices.length,gl.GL_UNSIGNED_INT, terrain.vertexIndices);
      gl.glEndList();
      loop  = new Animator(drawable);
      loop.start();
    }

    public void display(GLDrawable drawable){
      GL  gl         = drawable.getGL();
      GLU glu        = drawable.getGLU();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
      gl.glLoadIdentity();
      glu.gluLookAt(96,196,250, 96,0,96,0,1,0);
      gl.glCallList(list);
    }

    public void reshape(GLDrawable drawable,
                        int xstart,int ystart,
                        int width, int height){
      GL gl   = drawable.getGL();
      GLU glu = drawable.getGLU();
      
      height  = (height == 0) ? 1 : height;

      gl.glViewport(0,0,width,height);
      gl.glMatrixMode(gl.GL_PROJECTION);
      gl.glLoadIdentity();

      glu.gluPerspective(50.0f,(float)width/height,10.f,1700.0f);
      gl.glMatrixMode(gl.GL_MODELVIEW);
      gl.glLoadIdentity();
    }

    public void displayChanged(GLDrawable drawable,
                               boolean modeChanged,
                               boolean deviceChanged){}
  }

  public void processKeyboard(){}

  public void keyReleased(KeyEvent evt){
    keys[evt.getKeyCode()] = false;
  }

  public void keyPressed (KeyEvent evt){
    keys[evt.getKeyCode()] = true;

    if(keys[KeyEvent.VK_ESCAPE]){
      loop.stop();
      System.exit(0);
    }
  }

  public void keyTyped   (KeyEvent evt){}

  public class shutDownWindow extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      loop.stop();
    }
  }
}

[quote] Turned out that if I wanted it that way, I would have to create a vertex array for every signle row or column; I feared that a lot, and Pepi confirmed it
[/quote]
You do? Is there any reason why that won’t work? (not that I’m doubting either one of you, just curious as to why it works that way).