Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [JOGL] Unable to Draw a Square  (Read 2245 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2014-10-16 17:47:16 »

Hey,

I'm trying to learn how to use JOGL and it's been going pretty-well so-far. I've modified most of the core code of a particle system to use JOGL to do all of the rendering.

The only problem I have now is getting something to actually render to the screen. The first piece of code below is the Driver class that I've hacked together. From all of the example I've seen, it should work. Although it's entirely possible that I've messed something up.
The second piece of code is the render method used on all particles. The render() method call in the display() method of the Driver class below ends up calling the render methods of all of the particles.

The only changes made to the program were to the Driver class, the render() method of the Particle class, and to the few render() methods that just pass along variables until the render() methods of the particles are called.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
package core;

import com.jogamp.opengl.util.Animator;
import effect.Effect;
import effect.RainbowSnow;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import java.awt.*;

public class Driver implements GLEventListener {
    // Testing Stuff:
    private static Effect[] effect = new Effect[1];
    // End Testing Stuff.

   public static void main(String[] args) {
        GLProfile.initSingleton();
        GLProfile glp = GLProfile.get(GLProfile.GL2);
        GLCapabilities caps = new GLCapabilities(glp);

        // Create the frame.
        Frame frame = new Frame();
        frame.setTitle("Particle Test");
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.requestFocus();

        /*
        // Create the screen.
        Screen screen = new Screen();
        screen.setSize(new Dimension(frame.getWidth(), frame.getHeight()));
        screen.setFocusable(true);
        screen.setVisible(true);
        screen.setBackground(Color.black);
        frame.add(screen);

        frame.setVisible(true);

        screen.start();
        */


        GLCanvas screen = new GLCanvas(caps);
        screen.addGLEventListener(new Driver());

        screen.setVisible(true);
        frame.add(screen);
        frame.setVisible(true);

        // Testing Stuff:
            effect[0] = new RainbowSnow(0.0f, 0.0f, (short)1920);
            // effect[0] = new SplitWave(0.0, 0.0, 1920);
            // effect[0] = new Snow(0.0, 0.0, 1920);
            // effect[0] = new Fire(512.0, 512.0);
        // End Testing Stuff.

        Animator animator = new Animator(screen);
        animator.start();


   }

    @Override
    public void init(GLAutoDrawable drawable) {
        // put your OpenGL initialization code here

        drawable.getGL().setSwapInterval(1); // Ennable V-Sync
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
        // put your cleanup code here
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        update();
        render(drawable);
    }

    // When called this updates all of the game's logic.
    private void update() {
        for(Effect e : effect) {
            e.update();
        }

        /*
        if(KEY.isKeyPressed(KeyEvent.VK_ESCAPE) || KEY.isKeyPressed(KeyEvent.VK_ALT) && KEY.isKeyPressed(KeyEvent.VK_F4)) {
            isProgramRunning = false;
            System.exit(0);
        }
        */

    }

    // When called this updates the screen.
    private void render(final GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

        for(Effect e : effect) {
            e.render(gl);
        }

        gl.glFlush();
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        // called when user resizes the window
    }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
/**
    * Renders the particle to the screen.
    * @param gl The graphics object to render with.
    * @param isOval Whether or not to render the particle as an oval.
    */

   public void render(final GL2 gl, final boolean isOval) {

      if(isOval) {
            // TODO Figure out the math for this.
      } else {
            gl.glBegin(GL2.GL_POLYGON);
            gl.glColor4f(color.getRed() / 255, color.getGreen() / 255, color.getBlue() / 255, color.getAlpha() / 100);
            gl.glVertex2f(xCurrent, yCurrent); // Top Left
            gl.glVertex2f(xCurrent + SIZE, yCurrent); // Top Right
            gl.glVertex2f(xCurrent + SIZE, yCurrent + SIZE); // Bottom Right
            gl.glVertex2f(xCurrent, yCurrent + SIZE); // Bottom Left
            gl.glEnd();
      }
   }




If anyone can spot an error or can tell me what I may have done wrong, thanks.
Offline trollwarrior1
« Reply #1 - Posted 2014-10-16 17:55:40 »

Did you try to change the glClearColor? That should 'ensure' that you have setup JOGL rendering correctly.

Also, I would check if color.getRed() for examples returns float and not an int, because
1  
2  
3  
125 / 255  = 0 
and
125.f / 255 = 0.5f
Offline tyeeeee1
« Reply #2 - Posted 2014-10-16 18:09:49 »

Did you try to change the glClearColor? That should 'ensure' that you have setup JOGL rendering correctly.

Also, I would check if color.getRed() for examples returns float and not an int, because
1  
2  
3  
125 / 255  = 0 
and
125.f / 255 = 0.5f


Ahh... Yep, that fixed the issue with the color. I do have one issue left now. All of the particles being rendered are absolutely massive. They take up most of the screen in quick flashes.

I think I remember something about using views when using OpenGL. Could it be that the view is just super zoomed-in?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2014-10-16 18:16:10 »

You need to use projection matrix for that. Not sure how to set it up in JOGL, but in LWJGL there is a method

https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml

EDIT---
1  
2  
3  
glMatrixMode(GL_PROJECTION);
glOrtho(0, Screen.width, 0, Screen.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
Offline gouessej
« Reply #4 - Posted 2014-10-17 08:12:31 »

Not sure how to set it up in JOGL
glOrtho in JOGL 2

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (45 views)
2018-11-16 13:46:03

hadezbladez (49 views)
2018-11-16 13:41:33

hadezbladez (26 views)
2018-11-16 13:35:35

hadezbladez (20 views)
2018-11-16 13:32:03

EgonOlsen (1897 views)
2018-06-10 19:43:48

EgonOlsen (1936 views)
2018-06-10 19:43:44

EgonOlsen (1286 views)
2018-06-10 19:43:20

DesertCoockie (1717 views)
2018-05-13 18:23:11

nelsongames (1411 views)
2018-04-24 18:15:36

nelsongames (2041 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!