native window not displayed

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:


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


#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);
}


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

ps. nice username, I’m sure the spam bots will love it :slight_smile: