gl.glVertex3f question

i have created a “Frame”, and then added a “GLCanvas”
to it.

In the GLCanvas, i tried to draw a “TRIANGLES” with the
following code:

gl.glBegin(GL.GL_TRIANGLES);
gl.glVertex3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(1.0f, 0.0f, 0.0f);
gl.glVertex3f(1.0f, 1.0f, 0.0f);
gl.glEnd();

But the parameters: “0.0f” and “1.0f” seem to be relative to
the Frame width and height. And the Frame’s left top corner is
always (-0.1f,-0.1f), left bottom corner (-0.1f,0.1f) and so on.

What should i do to create a TRIANGLE with absolute values?
(not dependent on Frame height, width)

Thanx.

hier is the complete code:

import net.java.games.jogl.GLEventListener;
import net.java.games.jogl.GL;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.DebugGL;

public class c_renderer implements GLEventListener {
private GL gl;
private GLDrawable glDrawable;

  public void init(GLDrawable drawable) {
        this.gl = drawable.getGL();
        this.glDrawable = drawable;
        drawable.setGL(new DebugGL(drawable.getGL()));
        System.out.println("Init GL is " + gl.getClass().getName());
  }
  public void display(GLDrawable drawable) {
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        gl.glColor3f(1.0f, 0.0f, 0.0f);

        gl.glBegin(GL.GL_TRIANGLES);
        gl.glVertex3f(0.0f, 0.0f, 0.0f);
        gl.glVertex3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(1.0f, 1.0f, 0.0f);
        gl.glEnd();

        gl.glBegin(GL.GL_QUADS);
        gl.glVertex3f(-0.1f, 0.1f, 0.0f);
        gl.glVertex3f(0.1f, 0.1f, 0.0f);
        gl.glVertex3f(0.1f, -0.1f, 0.0f);
        gl.glVertex3f(-0.1f, -0.1f, 0.0f);
        gl.glEnd();
  }
  public void reshape(
        GLDrawable drawable,
        int x,
        int y,
        int width,
        int height) {
  }
  public void displayChanged(
        GLDrawable drawable,
        boolean modeChanged,
        boolean deviceChanged) {
  }

}

//*********************************************
import net.java.games.jogl.*;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class c_openGL_test {
public static void main(String[] args) {
try {
Frame testFrame = new Frame(“TestFrame”);
testFrame.setSize(200, 200);

              GLCapabilities glCaps = new GLCapabilities();
              glCaps.setRedBits(8);
              glCaps.setBlueBits(8);
              glCaps.setGreenBits(8);
              glCaps.setAlphaBits(8);

              GLCanvas canvas =
                    GLDrawableFactory.getFactory().createGLCanvas(glCaps);

              canvas.setSize(400, 400);
              testFrame.add(canvas);

              canvas.addGLEventListener(new c_renderer());

              final Animator animator = new Animator(canvas);
              testFrame.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                          animator.stop();
                          System.exit(0);
                    }
              });
              testFrame.show();
              animator.start();
        } catch (Exception e) {
              e.printStackTrace();
        }
  }

}

hmm, I don’t see anywhere where you set your projection matrix. I dunno how the default will behave (is it even defined as being an identity at startup?). But basically an OpenGL viewport is usually independant of the actual pixel size it takes up. Usually this is what you want (your scene scales to fit as needed) but if not you just need to take it into account when setting up your projection.

In ‘somewhere’ you need to call glOrtho or glPerspective before drawing (be that either in your render or reshape methods). If you use glOrtho with the actual pixel dimensions you can set vertices with pixel locations without a problem.

thanx ;D

This got me questioning myself as to how one would be able to implement a ‘near’ pixel perfect positioning system but one that is still relative to the current screen resolution. hmmm.

In the past I’ve always set glOrtho(0.0f,screenWidth, 0.0f, screenHeight, 0.0f, -100.0f).

So, if I needed a 10 x 10 pixel graphic at postion 100, 100 in the screen, I would simply use vertex commands centered around the 100 x 100 mark. This works fine, except when I want to change the resolution, and now need the 10 x 10 texture at a position which is a factor of the original.

I could stick with my absolute ‘one resolution’ scheme, but hate sticking to one resoultion. Any ideas or tricks?

Regards,

ribot.

Integer math is your friend.

gl.glVertex2f(100screenWidth/targetScreenWidth, 100screenHeight/targetScreenHeight);

Assuming all the variables are ints there, that’ll scale the vertex position along with the screensize without placing it at uneven pixel positions.

Keep in mind that GL_LINE and GL_POINT behaves different from triangle based rendering, so you need to offset those by 0.5 pixels for precise rendering.