Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  native window not displayed  (Read 528 times)
0 Members and 1 Guest are viewing this topic.
Offline dbrayford@sci.utah.edu

Junior Newbie





« Posted 2010-05-26 19:48:55 »

I am trying to port a sample jni OpenGL Windows application to linux, but the OpenGL window isn't being displayed. Can anyone point me in the right direction. Unfortunately, I can't use jogl as I can't rewrite the existing software that was developed on Windows, which means I need to port the native graphics code to linux. I am comfortable porting the 3D graphics code as it requires little modification, but don't understand how to create the OpenGL window and attach it to the Java canvas in linux.

Java Code:
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  
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import javax.swing.Timer;
 
public class MyWindow extends Canvas implements ActionListener
{
    static {
        System.out.println( "java.library.path := " + System.getProperty("java.library.path") );
        System.loadLibrary("MyWindow");
    }
 
    public final static int TIMER_SECONDS = 100;
 
   public static void main( String[] argv )
    {
        Frame f = new Frame();
        f.setSize(300,400);
        f.setLayout(new BorderLayout());
        f.setTitle("OpenGL from Java");
 
        MyWindow w = new MyWindow();
        f.add(w,BorderLayout.CENTER);
        f.add(new Label("test"),BorderLayout.SOUTH);
        f.setBounds(300,300,300,300);
        f.setVisible(true);
    }
 
    private boolean bInitOpenGL = false;

    public void actionPerformed(ActionEvent evt)  
    {
        if(bInitOpenGL == false)
        {
            bInitOpenGL = true;
            initializeOpenGL();
        }
        paintOpenGL();
    }  
 
    public void addNotify()
    {
        super.addNotify();
        javax.swing.Timer timer = new Timer(TIMER_SECONDS,this);
        timer.start();
    }

    public void removeNotify()
    {
        super.removeNotify();
        cleanupOpenGL();
    }
 
    public native void paintOpenGL();
    public native void initializeOpenGL();
    public native void cleanupOpenGL();

}


C Code
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  
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include "jawt_md.h"
#include "JAWT_Info.h"
#include "MyWindow.h"
 
GLXContext cx;
 
JNIEXPORT void JNICALL Java_MyWindow_initializeOpenGL(JNIEnv *env, jobject panel)
{
   XVisualInfo *vi;
   XInitThreads();
   XSetWindowAttributes swa;
   GLXDrawable win;
   Bool doubleBuffer = True;
 
   static int dblBuf[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE,
   1, GLX_DEPTH_SIZE, 12, GLX_DOUBLEBUFFER, None};
 
   static int snglBuf[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE,
   1, GLX_DEPTH_SIZE, 12, None};
 
   JAWT_Info info(env, panel);
   Display *dpy = info.dsi_x11->display;
   win = info.dsi_x11->drawable;
 
   if (dpy == NULL)
   return;
 
   if (win == NULL)
   return;
 
   Colormap cmap;
 
   // get the device context (DC)

   vi = glXChooseVisual(dpy, DefaultScreen(dpy),dblBuf);
   if (vi == NULL)
   {
      vi = glXChooseVisual(dpy, DefaultScreen(dpy),snglBuf);
      if (vi == NULL)
      {
         printf("apesta vi\n");
         exit(-1);
      }
      doubleBuffer = False;
   }
   cx = glXCreateContext(dpy, vi, None, True);
   if (cx == NULL)
   {
      printf("apesta vi\n");
      exit(-1);
   }
 
   // Create an X colormap for use.
  cmap = XCreateColormap(dpy,RootWindow(dpy, vi->screen), vi->visual, AllocNone);
   swa.colormap = cmap;
   swa.border_pixel = 0;
   swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
 
   // bind the rendering context to the window
  glXMakeCurrent(dpy, win, cx);
}
 
JNIEXPORT void JNICALL Java_MyWindow_paintOpenGL(JNIEnv *env, jobject panel)
{
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT );
 
    glPushMatrix();
       glBegin( GL_TRIANGLES );
      glColor3f( 1.0f, 0.0f, 0.0f );
      glVertex2f( 0.0f, 1.0f );
      glColor3f( 0.0f, 1.0f, 0.0f );
      glVertex2f( 0.87f, -0.5f );
      glColor3f( 0.0f, 0.0f, 1.0f );
      glVertex2f( -0.87f, -0.5f );
       glEnd();
    glPopMatrix();
 
 
    glFlush();
 }
 
 
JNIEXPORT void JNICALL Java_MyWindow_cleanupOpenGL(JNIEnv *env, jobject panel)
{
   // get the window handle
  JAWT_Info info(env, panel);
   Display* dpy2 = info.dsi_x11->display;
   if( dpy2 == NULL)
   return;
 
   glXMakeCurrent( dpy2, None, NULL );
   glXDestroyContext(dpy2, cx);
}
Offline appel

JGO Wizard


Medals: 49
Projects: 5


I always win!


« Reply #1 - Posted 2010-06-01 01:30:47 »

Take a look at http://www.lwjgl.org/

ps. nice username, I'm sure the spam bots will love it Smiley

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!