Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dbrayford

macrumors member
Original poster
Feb 22, 2010
41
0
Hi,

I've written a JNI OpenGL program, but the native OpenGL window isn't
being shown. I am new to cocoa and Apple windowing development and
don't understand why the window isn't shown even though the print
statements in the OpenGL drawing code are all executed. Below is a
section of code in C and java that should create and display the
window then draw a simple OpenGL object. I am probably missing something very straightforward, but I just can't see it.

Thanks
David

Code:
// object-C code
int InitWindowMac(JNIEnv * env, jobject panel)
{
       std::cout << "Enteringing InitWindowMac" << std::endl;

       jboolean result = JNI_FALSE;
       jint lock = 0;

       std::cout << "Before result = JAWT_GetAWT(env, &awt);" << std::endl;

       // get the AWT
   awt.version = JAWT_VERSION_1_4;

       result = JAWT_GetAWT(env, &awt);

       if(result != JNI_FALSE)
               std::cout << "JNI_FALSE" << std::endl;

       std::cout << "After result = JAWT_GetAWT(env, &awt);" << std::endl;

       if( env->ExceptionOccurred() )
       {
               env->ExceptionDescribe();
       }
       assert(result != JNI_FALSE);

       std::cout << "After JAWT_GetAWT assert" << std::endl;

       ds = awt.GetDrawingSurface(env, panel);

       std::cout << "After ds = awt.GetDrawingSurface(env, panel);" << std::endl;

       if( env->ExceptionOccurred() )
       {
               env->ExceptionDescribe();
       }
       assert(ds != NULL);

       std::cout << "After awt.GetDrawingSurface(env, panel) assert" << std::endl;


       lock = ds->Lock(ds);
       if( env->ExceptionOccurred() )
       {
               env->ExceptionDescribe();
       }
       assert( (lock & JAWT_LOCK_ERROR) == 0 );

       std::cout << "After awt.GetDrawingSurface(env, panel) assert" << std::endl;

       dsi = ds->GetDrawingSurfaceInfo(ds);
       if(dsi)
       {
               dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
               if( env->ExceptionOccurred() )
               {
                       env->ExceptionDescribe();
               }
       }
       else
       {
               std::cout << "dsi is null exiting" << std::endl;
               return 0;
       }

       std::cout << "Before NSView *view = dsi_mac->cocoaViewRef;" << std::endl;

       // get the corresponding peer from the calling panel
       NSView *view = dsi_mac->cocoaViewRef;

       std::cout << "Before NSView NSWindow *window = [view Window];" << std::endl;

       if(view)
               std::cout << "view !=null" << std::endl;
       else
       {
               std::cout << "view is null exiting" << std::endl;
               return 0;
       }

       // get the coregraphics from the parent window
       NSWindow *window = [view window];

       std::cout << "Before NSOpenGLPixelFormatAttribute attrs[]" << std::endl;

       NSOpenGLPixelFormatAttribute attrs[] = {

               NSOpenGLPFAScreenMask,

               CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),

               NSOpenGLPFAColorSize, 24,

               NSOpenGLPFADepthSize, 16,

               NSOpenGLPFADoubleBuffer,

               NSOpenGLPFAAccelerated,

               0

       };

       NSOpenGLPixelFormat *pixelFormat;

       std::cout << "Before pixelFormat = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attrs];" << std::endl;

       pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];


       cp = new GraphicsContextProperties();
       std::cout << "after cp = new GraphicsContextProperties();" << std::endl;
       cp->context = NULL;
       cp->str = "";

       context = [NSOpenGLContext alloc];

       if ( context )
       {
               std::cout << "context != null" << std::endl;

               // Set the current OpenGL context
               [context makeCurrentContext];

               std::cout << "after [context makeCurrentContext];" << std::endl;
       }
       else
       {
               std::cout << "context == null exiting" << std::endl;
               exit(0);
       }

       cp->context = context;

       // match java
       NSRect windowRect = [window frame];
       CGAffineTransform xform = CGAffineTransformMake(1, 0, 0, -1,
dsi->bounds.x, windowRect.size.height-dsi->bounds.y);
       CGContextConcatCTM( (CGContext*)context, xform );

       if(cp)
               BuildLinkList(env, panel, cp);
       else
       {
               std::cout << "cp == null exiting" << std::endl;
               exit(0);
       }
       return 1;
}


// get native window that corresponds to the current java window
JNIEXPORT jboolean JNICALL Java_AvistoGL_setForDrawing(JNIEnv * env,
jobject panel)
{
       jclass cls = env->GetObjectClass(panel);
       jmethodID method = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
       jstring name = (jstring) env->CallObjectMethod(panel,method);
       const char * chr = env->GetStringUTFChars(name, 0);

       std::string str = chr;
       cp = contextList->retrieve_item(str);

               if( !cp )
               {
                       std::cout << "graphics context is not in the graphics context
list"<< std::endl;
                       return false;
               }
               int ret = GetCurrentContext();
               return ret;

}


// java code

       public void gl_draw()
       {

               // ensure that the OpenGL is correctly initialized
               if(isInitalized == false)
               {
                       try
                       {
                               makeOpenGLWindow(1);
                       }
                       catch(Exception e)
                       {
                               e.printStackTrace();
                               return;
                       }
                       gl_init();
                       isInitalized = true;
               }

               // find the correct panel to draw the OpenGL into.
               setForDrawing();

               System.out.println("gl_draw()");
               if(!picking)
               {
                       reSizeGLScene( getWidth(), getHeight() );
                       System.out.println("!picking");
               }

               //draw
               glClearColor(backgroundColour[0], backgroundColour[1],
backgroundColour[2], backgroundColour[3]);

               System.out.println("After glClearColor");

               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
               System.out.println("After glClear");
               glMatrixMode(GL_MODELVIEW);
               System.out.println("After glMatrixMode");
               glLoadIdentity();
               System.out.println("After glLoadIdentity");

               gluLookAt(eye_x, eye_y, eye_z, lookatX, lookatY, lookatZ, upX, upY, upZ);
               System.out.println("After gluLookAt");

               glDisable(GL_LIGHTING);
               glDisable(GL_LIGHT0);

               System.out.println("After glDisable");

               glEnable(GL_CULL_FACE);
               glCullFace(GL_BACK);

               glTranslatef(0.0f, 0.0f, 5.0f);

               System.out.println("After glTranslate");

               // 3rd blue
               glColor3f(0.0f, 0.0f, 1.0f);
               glPushName(21);
                       System.out.println("After glPushName");
                       glPushMatrix();
                               glTranslatef(1.3f, 0f, 10.0f);
                               System.out.println("After glTranslate");
                               glBegin(GL_TRIANGLES);
                                       glVertex3f(0.0f, 0.0f, 0.0f);
                                       glVertex3f(2.0f, 0.0f, 0.0f);
                                       glVertex3f(1.0f, 2.0f, 0.0f);
                               glEnd();
                       glPopMatrix();
               glPopName();

               System.out.println("After blue");

               glDisable(GL_CULL_FACE);
               glEnable(GL_LIGHTING);
               glEnable(GL_LIGHT0);

               System.out.println("Before glFlush()");

               glFlush();
               System.out.println("After glFlush()");
           swapBuffers();
               System.out.println("After swapBuffers()");
       }
 
Have you run this C code without any JNI business? Just write a little "driver" in main to run the method and see what happens. Once you have that code, if it's not working, post it all so we can investigate a more basic version of your program.

-Lee
 
Not sure of your intention with this program

but have you considered using jogl?. It is basically an abstraction layer that takes care of handling all the down and dirty JNI stuff for you(plus its cross-platform which is always an added bonus). I used it where I used to work and can say it works quite well. If you can I would consider this instead of doing JNI, because JNI GUI stuff can get pretty nasty pretty quickly.
 
OpenGL on Mac

Hi,

Currently I am familiarising myself with JNI Libraries and OpenGL.
First I wrote native functions passing and returning different data types such as floats and integers to try out the functionality of the JNI Libraries. Succeeding in this I moved on to write a native function which creates a window and displays some shapes using drawing commands of OpenGL. I am having some problems on the creation of the window since when calling these functions from a java program (via a native jni library) I am getting some errors

2010-08-20 21:22:31.460 java[8348:903] GLUT Warning: The following is a new check for GLUT 3.0; update your code.
2010-08-20 21:22:31.461 java[8348:903] GLUT Fatal Error: redisplay needed for window 1, but no display callback.

It is important to note that this function was written using exactly the same statements from a C-program I have already wrote before and which was functioning appropriately. I tried to do some research for any solutions that might exist however I had no success till now. What I found was some posts from people having the same problem, however no solutions.
I also did try to get into some thread basics to create a new thread for the OpenGL part of the native function just in case there is a conflict between the JNI native thread and the processes invoked from OpenGL. However still the same error occurred (as above in red).
Next approach I took was to build a dynamic library again in C but now being called from a C main program (rather than from Java and building a JNI library) and the application runs as expected.
Thus in my opinion there may be some conflict between JNI and OpenGL since accessing a library having the same commands from main C functioned well.
I also did some research on JOGL ( Java OpenGL) as an alternative to directly calling native drawing functions from our JNI. I downloaded the sources from :
https://jogl.dev.java.net/servlets/...derID=11509&expandFolder=11509&folderID=11508

An exception is occuring:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.nio.DirectByteBuffer cannot be cast to com.sun.opengl.impl.macosx.JAWT_MacOSXDrawingSurfaceInfo
at com.sun.opengl.impl.macosx.MacOSXOnscreenGLDrawable.lockSurface(MacOSXOnscreenGLDrawable.java:180)
at com.sun.opengl.impl.macosx.MacOSXOnscreenGLContext.makeCurrentImpl(MacOSXOnscreenGLContext.java:57)
at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:134)
at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:182)
at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:265)
at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
at javax.media.opengl.GLCanvas.paint(GLCanvas.java:142)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
at sun.awt.RepaintArea.paint(RepaintArea.java:241)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
at java.awt.Component.dispatchEventImpl(Component.java:4790)
at java.awt.Component.dispatchEvent(Component.java:4544)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
 
What is the complete console output from your method call?

LeanneAttard: Let's focus on resolving the OP's problem first!

Also the following snippet of code appears incorrect:
Code:
       if(result != JNI_FALSE)
               std::cout << "JNI_FALSE" << std::endl;

perhaps you meant
Code:
       if(result == JNI_FALSE)
               std::cout << "JNI_FALSE" << std::endl;
 
solution to Native window not shown in JNI

i had it solved by getting the nsview (NSView* nsview = (dsi_mac->cocoaViewRef);) as you did but then i used the code below


NSOpenGLPixelFormatAttribute attrs [] = {NSOpenGLPFAAccelerated,0};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];

//associate the context with the view
[context setView:nsview];

/* to test whether the view is the actual java frame
NSRect rect = [nsview frame];
printf("rect.size: %f\n",(rect.size));*/

//set our context as the current context
[context makeCurrentContext];

if(context== NULL)
{
printf("context is NULL\n");
return 0;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.