LWJGL Forum

Programming => OpenGL => Topic started by: phaelax on September 14, 2005, 05:58:41

Title: help newbie draw pixels
Post by: phaelax on September 14, 2005, 05:58:41
I'm trying to convert an existing program to use OGL because the native Java painting methods are just too slow. I have an array of pixel data updated constantly to draw animated fire.
(http://img.photobucket.com/albums/v204/phaelax/programming_projects/java_fire.jpg)

I'm new to this whole OGL stuff.

I've got my pixel buffer and called GL11.glDrawPixels(). I see 1 line of pixels at the very top of the screen, the bottom of my image I'm assuming.  I'm trying to position it on the screen so I can see the whole thing, but whenever I call GL11.glRasterPos2i() I can't see any pixels, no matter what coordinates I use.

Also, how can I display text? I couldn't find a glDrawText() method.
Title: help newbie draw pixels
Post by: hvor on September 14, 2005, 08:03:09
I found similar problems when I switch myself from Java to openGL. How to write damn string on the screen? YOu will have to load a texture that is containing all the characters, and than you will be able to write something. GO to see NeHe tutorials how exactly to do it...
Title: hmmmmm....
Post by: Fool Running on September 14, 2005, 13:03:46
Quotewhenever I call GL11.glRasterPos2i() I can't see any pixels, no matter what coordinates I use
Could you post some code? This seems weird :lol:
Title: help newbie draw pixels
Post by: napier on September 14, 2005, 15:42:10
In OpenGL the Y axis is reversed, so 0,0 is in the lower left corner, not the upper left as in Java.  Maybe that is affecting your results.

Warning: glDrawPixels is slow.  OpenGL is not optimized for pixel operations like that.  You'll get better results rendering into a texture.  You could search in opengl forums for techniques like particle systems, fire and other special effects.

As for text, go to http://nehe.gamedev.net and look at the "2D Texture Font" tutorial #17.

I have a text demo using LWJGL here: http://potatoland.com/code/gl/lwjgl_demoText.zip

And source code here: http://potatoland.org/code/gl/GLApp.java
Look at buildFont() and glPrint()
Title: help newbie draw pixels
Post by: phaelax on September 14, 2005, 18:48:40
Well here's the whole code, mostly created from that german tutorial. (babel fish is nice)

thx for the other info.


import java.awt.Color;
import java.util.Random;
import java.util.Date;

import java.nio.ByteBuffer;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.glu.GLU;
import org.lwjgl.opengl.OpenGLException;


public class JavaLWJGL2
{
   private DisplayMode displayMode = null;
   private String title;
   private int width = 320;
   private int height = 240;
   private byte[] fire = new byte[width*height*3];
   private ByteBuffer pixels;
   private int pos_y = 100;

   
   public JavaLWJGL2(String title)
   {
       this.title = title;
       randomizeFire();
       pixels = ByteBuffer.allocateDirect(width*height*3).put(fire);
       pixels.flip();
   }

   
   public void init()
   {
       initWindow();
       initKeyboard();
       initOpenGL();
       reSizeGLScene(displayMode.getWidth(), displayMode.getHeight());
       
       run();
   }
   
   private void initWindow()
   {
       try
       {
           displayMode = org.lwjgl.util.Display.getAvailableDisplayModes(
                               800,600,800,600,32,32,60,60)[0];
           Display.setDisplayMode(displayMode);
           Display.setFullscreen(false);
           Display.create();
           Display.setTitle(title);            
       }
       catch(LWJGLException e){System.out.println(e);}
   }
       


   private void initKeyboard()
   {
       try
       {
           if (!Keyboard.isCreated())
               Keyboard.create();
       }
       catch(LWJGLException e){System.out.println(e);}
   }


   private void initOpenGL()
   {
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0.0f,0.0f,0.0f,0.0f);
GL11.glClearDepth(1.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,GL11.GL_NICEST);
}

private void reSizeGLScene(int width, int height)
   {
if (height == 0)
height = 1;

       GL11.glViewport(0,0,width,height);                  // aktuellen viewport zurücksetzen
       GL11.glMatrixMode(GL11.GL_PROJECTION);              // Projection Matrix (PM) auswählen
       GL11.glLoadIdentity();                              // PM zurücksetzen
       GLU.gluPerspective(45.0f,(float)width/(float)height,0.1f,100.0f);   // 45Ã,° Perspektive
       GL11.glMatrixMode(GL11.GL_MODELVIEW);               // ModelView Matrix (MVM) auswählen
       GL11.glLoadIdentity();                              // MVM zurücksetzen
}


//make some random colored pixels
private void randomizeFire()
{
   Random rand = new Random(new Date().getTime());
   for(int i=0; i<fire.length;i++)
           fire[i] = (byte)rand.nextInt(256);
}


private void renderGLScene()
{
   randomizeFire();

   GL11.glRasterPos2i(10,pos_y+10);
   
   GL11.glDrawPixels(width, height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pixels);


}
 
 
private void cleanup()
{
   destroyWindow();
   System.exit(0);
}
 
private void destroyWindow()
{
   if (Display.isCreated())
      Display.destroy();
}

private void checkKeyboardInput()
{
   if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
      cleanup();
   if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
      pos_y -= 1;
   if (Keyboard.isKeyDown(Keyboard.KEY_UP))
      pos_y += 1;
   
}

private void run()
{
   while(true)
   {
      if(Display.isCloseRequested())
      {
          cleanup();
          break;
      }
      if(Display.isVisible() || Display.isDirty())
      {
          try
          {
              renderGLScene();
              Display.update();
          }
          catch(OpenGLException e){System.out.println(e);}
      }
      checkKeyboardInput();
  }
  }
   
   
   public static void main()
   {
       JavaLWJGL2 prog = new JavaLWJGL2("Java with Open GL");
       
       prog.init();
   }
}
Title: hmmmmmmm...
Post by: Fool Running on September 20, 2005, 17:41:22
GLU.gluPerspective(45.0f,(float)width/(float)height,0.1f,100.0f);   // 45Ã,° Perspektive
That is at least one possible problem (in reSizeGLScene). You are setting a perspective view that doesn't have the unit to pixels you want. You want to set a ortho view like this:
glOrtho(0, width, height, 0, -1, 1);
That will make 0, 0 the top left of the window. :lol: