Getting GLUT to work with OSMesa

View: New views
5 Messages — Rating Filter:   Alert me  

Getting GLUT to work with OSMesa

by Alexander Mont-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

I have a GLUT-based data visualization application that allows a user to view a scene and manipulate that view using a UI. I want to add a “scripting mode” to the application where the user specifies an input file containing a sequence of commands to execute, and then the program executes those commands and then writes the contents of the display to a file.

 

I am trying to use OSMesa for this by using an OSMesa rendering context instead of the one generated by GLUT. I have tried to do this by calling the functions needed to make the OSMesa context instead of the glutCreateWindow() function. However, this doesn’t work: it gives an access violation exception as soon as glutPostRedisplay() is called.

 

Is it even possible to get GLUT to recognize an OSMesa rendering context, and if so, how?

 

(I am using the GLUT libraries that are generated by Mesa, not the system’s native GLUT libraries.)

 

-Alexander Mont

 

(Note that I already have the scripting mode working with the system’s own OpenGL libraries, and copying the contents of a window to a file from there. The reason I want to use OSMesa is because I want to run the scripting mode on machines which do not have windowing systems or even graphics cards.)


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Mesa3d-users mailing list
Mesa3d-users@...
https://lists.sourceforge.net/lists/listinfo/mesa3d-users

Re: Getting GLUT to work with OSMesa

by Brian Paul :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alexander Mont wrote:

> I have a GLUT-based data visualization application that allows a user to
> view a scene and manipulate that view using a UI. I want to add a
> “scripting mode” to the application where the user specifies an input
> file containing a sequence of commands to execute, and then the program
> executes those commands and then writes the contents of the display to a
> file.
>
>  
>
> I am trying to use OSMesa for this by using an OSMesa rendering context
> instead of the one generated by GLUT. I have tried to do this by calling
> the functions needed to make the OSMesa context instead of the
> glutCreateWindow() function. However, this doesn’t work: it gives an
> access violation exception as soon as glutPostRedisplay() is called.
>
>  
>
> Is it even possible to get GLUT to recognize an OSMesa rendering
> context, and if so, how?

GLUT doesn't know anything about OSMesa, but I think it should be
possible to use both libraries in one app.

Basically, if you're starting with a regular GLUT app, you should be
able to create and bind an OSMesa rendering context and draw something
off-screen.  But then, when you're done drawing with OSMesa, you should
try calling glutSetWindow() to re-bind the original GLUT rendering context.

If you try that and have trouble, if you could send me a simple test
program I could look into it.


> (I am using the GLUT libraries that are generated by Mesa, not the
> system’s native GLUT libraries.)
>
>  
>
> -Alexander Mont
>
>  
>
> (Note that I already have the scripting mode working with the system’s
> own OpenGL libraries, and copying the contents of a window to a file
> from there. The reason I want to use OSMesa is because I want to run the
> scripting mode on machines which do not have windowing systems or even
> graphics cards.)


-Brian

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Mesa3d-users mailing list
Mesa3d-users@...
https://lists.sourceforge.net/lists/listinfo/mesa3d-users

Re: Getting GLUT to work with OSMesa

by Alexander Mont-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You may have misunderstood what I was attempting to do.

I do not want to have *two* separate rendering contexts: one for the
offscreen rendering and one for the onscreen rendering. Rather, I want to
have a *single* offscreen rendering context, but I want the GLUT display
functionality to still work.

The attached file is a simple test program that explains what I am
attempting to do. The OSTEST preprocessor definition controls whether OSMesa
is used, while the CREATEWINDOW preprocessor definition controls whether
GLUTCreateWindow is called.

If OSTEST is turned off, it is a simple GLUT application that uses the
window, and it works correctly. If OSTEST is turned on, what I am doing is
creating the window, then switching the rendering context to the OSMesa one.
In this case, when I run the program, a window comes up, which appears
broken. If you wait several seconds and then close the window, however, the
files will have come up as desired.

What I want to do is have exactly this behavior, except for it not to have
to create an actual window at all. This is because I want to run it on a
machine (such as a cluster node) that does not have a windowing system. If I
try to avoid creating the window by simply removing the call to
GLUTCreateWindow() (which can be done in this test program by removing the
#define CREATEWINDOW) then the program immediately fails with an access
violation when glutMainLoop() is called.

Is it even possible to use GLUT without creating an actual window, and if so
how?

(It is possible that the only way to do this would be to modify the GLUT
libraries that come with Mesa. If that is the case, then I would be willing
to submit any modifications I make back to the Mesa project so that other
users who desire the same functionality could use it.)

-Alexander Mont

-----Original Message-----
From: Brian Paul [mailto:brian.paul@...]
Sent: Tuesday, June 24, 2008 11:11 AM
To: Alexander Mont
Cc: mesa3d-users@...; glenn.forney@...
Subject: Re: [Mesa3d-users] Getting GLUT to work with OSMesa

Alexander Mont wrote:

> I have a GLUT-based data visualization application that allows a user to
> view a scene and manipulate that view using a UI. I want to add a
> "scripting mode" to the application where the user specifies an input
> file containing a sequence of commands to execute, and then the program
> executes those commands and then writes the contents of the display to a
> file.
>
>  
>
> I am trying to use OSMesa for this by using an OSMesa rendering context
> instead of the one generated by GLUT. I have tried to do this by calling
> the functions needed to make the OSMesa context instead of the
> glutCreateWindow() function. However, this doesn't work: it gives an
> access violation exception as soon as glutPostRedisplay() is called.
>
>  
>
> Is it even possible to get GLUT to recognize an OSMesa rendering
> context, and if so, how?
GLUT doesn't know anything about OSMesa, but I think it should be
possible to use both libraries in one app.

Basically, if you're starting with a regular GLUT app, you should be
able to create and bind an OSMesa rendering context and draw something
off-screen.  But then, when you're done drawing with OSMesa, you should
try calling glutSetWindow() to re-bind the original GLUT rendering context.

If you try that and have trouble, if you could send me a simple test
program I could look into it.


> (I am using the GLUT libraries that are generated by Mesa, not the
> system's native GLUT libraries.)
>
>  
>
> -Alexander Mont
>
>  
>
> (Note that I already have the scripting mode working with the system's
> own OpenGL libraries, and copying the contents of a window to a file
> from there. The reason I want to use OSMesa is because I want to run the
> scripting mode on machines which do not have windowing systems or even
> graphics cards.)

-Brian

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GL/osmesa.h"
#include "GL/glu.h"
#include "GL/glut.h"

int imagenum = 0;
int Width = 1280;
int Height = 1024;
OSMesaContext ctx;
void* buffer;

#define OSTEST
#define CREATEWINDOW

static void
write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
{
   const int binary = 1;
   FILE *f = fopen( filename, "w" );
   if (f) {
      int i, x, y;
      const GLubyte *ptr = buffer;
      if (binary) {
         fprintf(f,"P6\n");
         fprintf(f,"# ppm-file created by osdemo.c\n");
         fprintf(f,"%i %i\n", width,height);
         fprintf(f,"255\n");
         fclose(f);
         f = fopen( filename, "ab" );  /* reopen in binary append mode */
         for (y=height-1; y>=0; y--) {
            for (x=0; x<width; x++) {
               i = (y*width + x) * 4;
               fputc(ptr[i], f);   /* write red */
               fputc(ptr[i+1], f); /* write green */
               fputc(ptr[i+2], f); /* write blue */
            }
         }
      }
      else {
         /*ASCII*/
         int counter = 0;
         fprintf(f,"P3\n");
         fprintf(f,"# ascii ppm file created by osdemo.c\n");
         fprintf(f,"%i %i\n", width, height);
         fprintf(f,"255\n");
         for (y=height-1; y>=0; y--) {
            for (x=0; x<width; x++) {
               i = (y*width + x) * 4;
               fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
               counter++;
               if (counter % 5 == 0)
                  fprintf(f, "\n");
            }
         }
      }
      fclose(f);
   }
}

void display(void)
{
        glClear(GL_COLOR_BUFFER_BIT);
        double low = 0.002 * (imagenum);
        double high = (0.002 * (imagenum)) + 0.20;
        glColor3f(1.0,1.0,1.0);
        glBegin(GL_POLYGON);
        glVertex3f(low,low,0.0);
        glVertex3f(high,low,0.0);
        glVertex3f(high,high,0.0);
        glVertex3f(low,high,0.0);
        glEnd();
        glFlush();
        if(imagenum < 400)
        {
                imagenum++;
                glutPostRedisplay();
        }
#ifdef OSTEST
        if(imagenum == 100)
                write_ppm("image100.ppm", (GLubyte*)buffer, Width, Height);
        if(imagenum == 250)
                write_ppm("image250.ppm", (GLubyte*)buffer, Width, Height);
#endif
}

void init(void)
{
        glClearColor(0.0,0.0,0.0,0.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}

int main(int argc, char** argv)
{
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(250,250);
        glutInitWindowPosition(100,100);
#ifdef CREATEWINDOW
        glutCreateWindow("hello");
#endif
#ifdef OSTEST
    ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL );
    buffer = malloc( Width * Height * 4 * sizeof(GLubyte) );
    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height );
#endif
        init();
        glutDisplayFunc(display);
        glutMainLoop();
        return 0;
}
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Mesa3d-users mailing list
Mesa3d-users@...
https://lists.sourceforge.net/lists/listinfo/mesa3d-users

Re: Getting GLUT to work with OSMesa

by Brian Paul :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alexander Mont wrote:

> You may have misunderstood what I was attempting to do.
>
> I do not want to have *two* separate rendering contexts: one for the
> offscreen rendering and one for the onscreen rendering. Rather, I want to
> have a *single* offscreen rendering context, but I want the GLUT display
> functionality to still work.
>
> The attached file is a simple test program that explains what I am
> attempting to do. The OSTEST preprocessor definition controls whether OSMesa
> is used, while the CREATEWINDOW preprocessor definition controls whether
> GLUTCreateWindow is called.
>
> If OSTEST is turned off, it is a simple GLUT application that uses the
> window, and it works correctly. If OSTEST is turned on, what I am doing is
> creating the window, then switching the rendering context to the OSMesa one.
> In this case, when I run the program, a window comes up, which appears
> broken. If you wait several seconds and then close the window, however, the
> files will have come up as desired.
>
> What I want to do is have exactly this behavior, except for it not to have
> to create an actual window at all. This is because I want to run it on a
> machine (such as a cluster node) that does not have a windowing system. If I
> try to avoid creating the window by simply removing the call to
> GLUTCreateWindow() (which can be done in this test program by removing the
> #define CREATEWINDOW) then the program immediately fails with an access
> violation when glutMainLoop() is called.
>
> Is it even possible to use GLUT without creating an actual window, and if so
> how?

No, I don't think that's possible.  I seem to recall that GLUT should
issue an error message if you try to enter the main loop without
creating a window.


> (It is possible that the only way to do this would be to modify the GLUT
> libraries that come with Mesa. If that is the case, then I would be willing
> to submit any modifications I make back to the Mesa project so that other
> users who desire the same functionality could use it.)

I guess what I'm wondering is this: why make any GLUT calls at all if
you're just going to use OSMesa to render to an off-screen buffer?

-Brian

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Mesa3d-users mailing list
Mesa3d-users@...
https://lists.sourceforge.net/lists/listinfo/mesa3d-users

Parent Message unknown Re: Getting GLUT to work with OSMesa

by Brian Paul :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Glenn Forney wrote:
> I guess we need to understand the off screen drawing capabilities of
> Mesa better.  I saw the word "batch" in some of the documentation and
> thought this capability was what we needed.  We were hoping to be able
> to modify an OpenGL/GLUT application so that it would not require the
> presence of a video card.

That's do-able, but you'll have to disable all the GLUT calls that
depend on having a real window.


> In particular ...
>
> This is what we are trying to do.  We have a very large interactive
> OpenGL/GLUT based visualization application (Smokeview).  Smokeview
> reads in a large amount of data generated on a Linux cluster.   If the
> data bandwidth  is good (like at our office) then there is not a problem
> we can run Smokeview moving the data where it is needed.  Sometimes
> though our users may be located far away from the Linux cluster.  We
> would then like to run SMokeview on the cluster (where the data is) and
> have it render PNG's to briefly examine the state of a modeling run.

This is a non-interactive task, right?  That is, you want to produce
images without a bunch of keyboard/mouse actions, right?

If the app requires keyboard/mouse input, that requires a window.


>  We
> would then transfer small number PNG files over the internet rather than
> large data sets.  So the question then is how to run (if it is possible)
> an OpenGL/GLUT app without it connected to the host's video card.  We
> don't care that the graphics performance will be slow since we are only
> going to be generating a few image files.
> I did come up with one solution, to use the X11 server Xming.  This
> works at our work but again is to slow when we are away from our local
> fast intranet.
>
> Any pointers will be appreciated.

My guess is you can simply disable all the GLUT calls and implement some
mechanism to invoke your drawing code (which would use OSMesa) at
whatever interval you desire.

-Brian


> glenn
>
> Brian Paul wrote:
>> Alexander Mont wrote:
>>> You may have misunderstood what I was attempting to do.
>>>
>>> I do not want to have *two* separate rendering contexts: one for the
>>> offscreen rendering and one for the onscreen rendering. Rather, I
>>> want to
>>> have a *single* offscreen rendering context, but I want the GLUT display
>>> functionality to still work.
>>>
>>> The attached file is a simple test program that explains what I am
>>> attempting to do. The OSTEST preprocessor definition controls whether
>>> OSMesa
>>> is used, while the CREATEWINDOW preprocessor definition controls whether
>>> GLUTCreateWindow is called.
>>>
>>> If OSTEST is turned off, it is a simple GLUT application that uses the
>>> window, and it works correctly. If OSTEST is turned on, what I am
>>> doing is
>>> creating the window, then switching the rendering context to the
>>> OSMesa one.
>>> In this case, when I run the program, a window comes up, which appears
>>> broken. If you wait several seconds and then close the window,
>>> however, the
>>> files will have come up as desired.
>>>
>>> What I want to do is have exactly this behavior, except for it not to
>>> have
>>> to create an actual window at all. This is because I want to run it on a
>>> machine (such as a cluster node) that does not have a windowing
>>> system. If I
>>> try to avoid creating the window by simply removing the call to
>>> GLUTCreateWindow() (which can be done in this test program by
>>> removing the
>>> #define CREATEWINDOW) then the program immediately fails with an access
>>> violation when glutMainLoop() is called.
>>>
>>> Is it even possible to use GLUT without creating an actual window,
>>> and if so
>>> how?
>>
>> No, I don't think that's possible.  I seem to recall that GLUT should
>> issue an error message if you try to enter the main loop without
>> creating a window.
>>
>>
>>> (It is possible that the only way to do this would be to modify the GLUT
>>> libraries that come with Mesa. If that is the case, then I would be
>>> willing
>>> to submit any modifications I make back to the Mesa project so that
>>> other
>>> users who desire the same functionality could use it.)
>>
>> I guess what I'm wondering is this: why make any GLUT calls at all if
>> you're just going to use OSMesa to render to an off-screen buffer?
>>
>> -Brian
>>
>>
>


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Mesa3d-users mailing list
Mesa3d-users@...
https://lists.sourceforge.net/lists/listinfo/mesa3d-users