[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can't get GL_LINES to show up when using glOrtho with values given using (JOGL) on Linux

Posted on 2011-10-22
6
Medium Priority
?
575 Views
Last Modified: 2012-08-14
I'm trying to solve a problem in another program and I think I have duplicated the
issue with this simpler program.

The example tries to draws 2 lines using  these parameters  to glOrtho

left:-1.000000
right:504.000000
bottom:483.000000
top:-1.000000
nearVal:-1.000000
farVal:1.000000


Here are the lines the vertexes are all in the range -0.5 to +0.5 which should fall into
the range above, but they will not display.
The original code to this example uses glOrtho2D and will run if the flag is set
originalCode = true;

I'm trying to solve a problem in another program that uses glOrtho with comparable values


public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GL2 gl2 = gl.getGL2();
       
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl2.glColor3f(0.0f, 1.0f, 0.0f);
        gl2.glPushMatrix();
        gl2.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f);
        gl2.glBegin(GL.GL_LINES);
        gl2.glVertex2f(-0.5f, 0.5f);
        gl2.glVertex2f(0.5f, -0.5f);
        gl2.glEnd();
        gl2.glPopMatrix();
        gl2.glColor3f(0.0f, 0.0f, 1.0f);
        gl2.glPushMatrix();
        gl2.glRotatef(rotAngle, 0.0f, 0.0f, 0.1f);
        gl2.glBegin(GL.GL_LINES);
        gl2.glVertex2f(0.5f, 0.5f);
        gl2.glVertex2f(-0.5f, -0.5f);
        gl2.glEnd();
        gl2.glPopMatrix();
        gl.glFlush();
        if (rotate) {
            rotAngle += 1f;
            System.out.println("ROTATION ANGLE is " + rotAngle);
        }
        if (rotAngle >= 360f) {
            rotAngle = 0f;
        }
    }




package example;

import com.jogamp.opengl.util.Animator;
import javax.media.opengl.awt.GLCanvas;


import java.awt.event.*;
import javax.swing.JFrame;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

public class Example extends JFrame implements GLEventListener, KeyListener {

    private GLCapabilities caps;
    private GLCanvas canvas;
    private float rotAngle = 0f;
    private boolean rotate = true;

    public Example() {
        super("Example");
        GLProfile glp = GLProfile.get(GLProfile.GL2);
        caps = new GLCapabilities(glp);
        canvas = new GLCanvas(caps);
        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);

        getContentPane().add(canvas);


        final Animator animator = new Animator(canvas);
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {

                new Thread(new Runnable() {

                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
    }

    public void run() {
        System.out.println("IN RUN");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(512, 512);
        setLocationRelativeTo(null);
        setVisible(true);
        canvas.requestFocusInWindow();
    }

    public static void main(String[] args) {
        new Example().run();
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GL2 gl2 = gl.getGL2();

        float values[] = new float[2];
        gl.glGetFloatv(GL2.GL_LINE_WIDTH_GRANULARITY, values, 0);
        System.out.println("GL.GL_LINE_WIDTH_GRANULARITY value is " + values[0]);
        gl.glGetFloatv(GL2.GL_LINE_WIDTH_RANGE, values, 0);
        System.out.println("GL.GL_LINE_WIDTH_RANGE values are " + values[0] + ", " + values[1]);
        gl.glEnable(GL.GL_LINE_SMOOTH);
        gl.glEnable(GL.GL_BLEND);
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE);
        gl.glLineWidth(1.5f);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GL2 gl2 = gl.getGL2();
        
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl2.glColor3f(0.0f, 1.0f, 0.0f);
        gl2.glPushMatrix();
        gl2.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f);
        gl2.glBegin(GL.GL_LINES);
        gl2.glVertex2f(-0.5f, 0.5f);
        gl2.glVertex2f(0.5f, -0.5f);
        gl2.glEnd();
        gl2.glPopMatrix();
        gl2.glColor3f(0.0f, 0.0f, 1.0f);
        gl2.glPushMatrix();
        gl2.glRotatef(rotAngle, 0.0f, 0.0f, 0.1f);
        gl2.glBegin(GL.GL_LINES);
        gl2.glVertex2f(0.5f, 0.5f);
        gl2.glVertex2f(-0.5f, -0.5f);
        gl2.glEnd();
        gl2.glPopMatrix();
        gl.glFlush();
        if (rotate) {
            rotAngle += 1f;
            System.out.println("ROTATION ANGLE is " + rotAngle);
        }
        if (rotAngle >= 360f) {
            rotAngle = 0f;
        }
    }
    boolean originalCode = false;

    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();
        GL2 gl2 = gl.getGL2();



        float left = -1.0f;
        float right = 1.0f;
        float bottom = (float) (-1.0 * (float) h / (float) w);
        float top = (float) (1.0 * (float) h / (float) w);

          gl2.glViewport(0, 0, w, h);
            gl2.glMatrixMode(GL2.GL_PROJECTION);
            gl2.glLoadIdentity();


        if (originalCode) {
            if (w <= h) {
                glu.gluOrtho2D(left, right, bottom, top);
            } else {
                glu.gluOrtho2D(-1.0 * (float) w / (float) h, 
                        1.0 * (float) w / (float) h, -1.0, 1.0);
            }
            System.out.printf("\nleft:%f right:%f bottom:%f top:%f\n", left, right, bottom, top);
        } else {
            left = -1f;
            right = w;
            bottom = h;
            top = -1f;
            float nearVal = -1f;
            float farVal = 1f;

            gl2.glOrtho(left, right, bottom, top, nearVal, farVal);
            System.out.printf("\nleft:%f right:%f bottom:%f top:%f nearVal:%f farVal:%f \n", left, right, bottom, top, nearVal, farVal);
        }

        gl2.glMatrixMode(GL2.GL_MODELVIEW);
        gl2.glLoadIdentity();
    }

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

    public void keyTyped(KeyEvent key) {
    }

    public void keyPressed(KeyEvent key) {
        switch (key.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
                System.exit(0);
            case KeyEvent.VK_R:
                rotate = !rotate;
                canvas.display();
            default:
                break;
        }
    }

    public void keyReleased(KeyEvent key) {
    }

    public void dispose(GLAutoDrawable glad) {
    }
}

Open in new window

0
Comment
Question by:dev110
  • 3
  • 3
6 Comments
 
LVL 12

Accepted Solution

by:
satsumo earned 1400 total points
ID: 37014560
The line in the code is being draw from minus half a pixel to plus half a pixel, it's very small.

The width and height values passed in reshape will typically relate to the pixel resolution of the device.  I don't know the specifics of the system involved though I would be surprised if it was different.

If the display is 640x480, calling glOrtho (0, 0, 640, 480, -1, 1) will set the projection so that the view space is the same size as the display resolution.  So that a value of 1 in a vertex coordinate is equal to one pixel on the display.

If you do glOrtho (-1, 1, -1, 1, -1, 1) the line [-0.5, -0.5] to [0.5, 0.5] will span half of the view diagonally, whatever the resolution and shape of the display.

if you do glOrtho (0, w, 0, h, -1, 1) the vertex coordinate are in pixels.  Something like [100, 100] to [540, 380] would show a line across the 640x480 display..
0
 

Author Comment

by:dev110
ID: 37014718
so If I do glOrtho (-1, 1, -1, 1, -1, 1) and say the display window is 640x480

What does that mean exactly that the coordinate system for my display is glOrtho or
is that my clipping plane?

I did see the lines using what you told me to put in, thanks.

Unfortunately feeding that back into my other program, which is a space invader example
that i'm hacking into for learning purposes, it still doesn't show up.

So I must have another problem somewhere. The space invader game doesn't have the aliens firing and I'm trying to add a laser beam(my line) attached to the sprite.

I guess I can no rule out the glOrtho call though, so thank you.
0
 

Author Comment

by:dev110
ID: 37014767
Looks like it's a combination of things. What you said my line was too small and
my line width too small and lastly my call to  gl2.glColor3f(0.0f, 1.0f, 0.0f);
 is gettting totally ignored. The line is black against a black screen.
I only saw it because it went across an image also being displayed.

Any thought on why a color command would be ignored?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:satsumo
ID: 37014793
The way I think about it is that window/display/view is like looking through a letterbox into the world of your game.  glOrtho set the size of that letterbox.

So if you set glOrtho to from -1 to 1 you can see anything in the world between -1 and 1.  If you set it -20 to 600 thats what part of the world you see.

Many people set it so that the part of the world you see matches the pixel resolution, then you can specify the position of things in pixels.  However, there is no reason you have to do that.  You can set it to show any section of the world that suits the program.

Whatever you see through the letterbox is stretched to fill the display/windows/view, whether that window fills the screen or is only a small part of it.  Most programs make the size of the letterbox match the shape of the window/display (width vs. height) so that the world inside the view is not stretched in X or Y.

The clip planes are the sides of the letter box so you don't notice them.  The ones you will see are the near and far planes, which clip things when they are too far away or too near.  Is it possible the laser beam is too far or near?

Using glOrtho makes it a parallel projection, no perspective.
0
 

Author Comment

by:dev110
ID: 37014809
I just solved the problem

I had to disable this draw my line then renable it.

  gl.glDisable(GL.GL_TEXTURE_2D);

Thanks for your incite, really appreciate it.
0
 
LVL 12

Expert Comment

by:satsumo
ID: 37014851
Thanks for the points.  Getting used to OpenGL and 3D is always a bit tricky, you seem to be picking it up quickly.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question