int pl110_MakeCurrent(pl110_Context *ctx ){
	int	mode;
	int	xsize;
	int	ysize;

	ZBuffer *zb;

	if( ctx->gl_context == NULL ){
		xsize = 640;
		ysize = 480;
		mode = ZB_MODE_5R6G5B;

		zb = ZB_open( xsize, ysize, mode, 0, NULL, NULL, NULL);
		
		if( zb == NULL ) {
			return 0;
		}

		/* initialisation of the TinyGL interpreter */
		glInit( zb );
		ctx->gl_context                     = gl_get_context();
		ctx->gl_context->opaque             = (void *) ctx;
		ctx->gl_context->gl_resize_viewport = pl110_resize_viewport;

		/* set the viewport */
		/*  TIS: !!! HERE SHOULD BE -1 on both to force reshape  */
		/*  which is needed to make sure initial reshape is  */
		/*  called, otherwise it is not called..  */
		ctx->gl_context->viewport.xsize = xsize;
		ctx->gl_context->viewport.ysize = ysize;
		glViewport( 0, 0, xsize, ysize );
	}
	return 1;
}
Exemple #2
0
/* we assume here that drawable is a window */
int nglXMakeCurrent( NGLXDrawable drawable,
                     NGLXContext ctx1)
{
  TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
  int mode, xsize, ysize;
  ZBuffer *zb;
  GR_WINDOW_INFO win_info;

  if (ctx->gl_context == NULL) {
      /* create the TinyGL context */
      GrGetWindowInfo(drawable, &win_info);

      xsize = win_info.width;
      ysize = win_info.height;

      /* currently, we only support 16 bit rendering */
      mode = ZB_MODE_5R6G5B;
      zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
      if (zb == NULL) {
          fprintf(stderr, "Error while initializing Z buffer\n");
          exit(1);
      }

      ctx->pixtype = MWPF_TRUECOLOR565;

      /* create a gc */
      ctx->gc = GrNewGC();
      
      /* initialisation of the TinyGL interpreter */
      glInit(zb);
      ctx->gl_context=gl_get_context();
      ctx->gl_context->opaque=(void *) ctx;
      ctx->gl_context->gl_resize_viewport=glX_resize_viewport;

      /* set the viewport : we force a call to glX_resize_viewport */
      ctx->gl_context->viewport.xsize=-1;
      ctx->gl_context->viewport.ysize=-1;
      
      glViewport(0, 0, xsize, ysize);
  }
  
  return 1;
}
Exemple #3
0
ostgl_context *
ostgl_create_context(const int xsize,
                     const int ysize,
                     const int depth,
                     void **framebuffers,
                     const int numbuffers)
{
  ostgl_context *context;
  int i;
  ZBuffer *zb;
   
  gl_assert(depth == 16); /* support for other depths must include bpp
                          convertion */
  gl_assert(numbuffers >= 1);
  
  context = gl_malloc(sizeof(ostgl_context));
  gl_assert(context);
  context->zbs = gl_malloc(sizeof(void*)*numbuffers);
  context->framebuffers = gl_malloc(sizeof(void*)*numbuffers);
  
  gl_assert(context->zbs != NULL && context->framebuffers != NULL);
  
  for (i = 0; i < numbuffers; i++) {
    context->framebuffers[i] = framebuffers[i];
    zb = ZB_open(xsize, ysize, ZB_MODE_5R6G5B, 0, NULL, NULL, framebuffers[i]);
    if (zb == NULL) {
      fprintf(stderr, "Error while initializing Z buffer\n");
      exit(1);
    }
    context->zbs[i] = zb;
  }
  if (++buffercnt == 1) {
    glInit(context->zbs[0]);
  }
  context->xsize = xsize;
  context->ysize = ysize;
  context->numbuffers = numbuffers;
  return context;
}
Exemple #4
0
int main(int argc, char **argv) {
    // initialize SDL video:
    int winSizeX=640;
    int winSizeY=480;
    if(SDL_Init(SDL_INIT_VIDEO)<0) {
        fprintf(stderr,"ERROR: cannot initialize SDL video.\n");
        return 1;
    }
    SDL_Surface* screen = NULL;
    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, 32, SDL_SWSURFACE)) == 0 ) {
        fprintf(stderr,"ERROR: Video mode set failed.\n");
        return 1;
    }

    // initialize TinyGL:
    unsigned int pitch;
    int	mode;
    switch( screen->format->BitsPerPixel ) {
    case  8:
        fprintf(stderr,"ERROR: Palettes are currently not supported.\n");
        return 1;
    case 16:
        pitch = screen->pitch;
        mode = ZB_MODE_5R6G5B;
        break;
    case 24:
        pitch = ( screen->pitch * 2 ) / 3;
        mode = ZB_MODE_RGB24;
        break;
    case 32:
        pitch = screen->pitch / 2;
        mode = ZB_MODE_RGBA;
        break;
    default:
        return 1;
        break;
    }
    ZBuffer *frameBuffer = ZB_open( winSizeX, winSizeY, mode, 0, 0, 0, 0);
    glInit( frameBuffer );

    // set viewport
    glViewport( 0, 0, winSizeX, winSizeY);
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

    // main loop:
    int frames=0;
    int x=0;
    double  t, t0, fps;
    char    titlestr[ 200 ];
    int running = GL_TRUE;
    t0 = (double)SDL_GetTicks()/1000.0;
    while( running ) {
        // calculate and display FPS (frames per second):
        t = (double)SDL_GetTicks()/1000.0;
        if( (t-t0) > 1.0 || frames == 0 ) {
            fps = (double)frames / (t-t0);
            sprintf( titlestr, "Spinning Triangle (%.1f FPS)", fps );
            SDL_WM_SetCaption(titlestr,0);
            t0 = t;
            frames = 0;
        }
        ++frames;

        // Clear color buffer
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Select and setup the projection matrix
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 65.0f, (GLfloat)winSizeX/(GLfloat)winSizeY, 1.0f, 100.0f );

        // Select and setup the modelview matrix
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        glRotatef(-90, 1,0,0);
        glTranslatef(0,0,-1.0f);

        // Draw a rotating colorful triangle
        glTranslatef( 0.0f, 14.0f, 0.0f );
        glRotatef( 0.3*(GLfloat)x + (GLfloat)t*100.0f, 0.0f, 0.0f, 1.0f );
        glBegin( GL_TRIANGLES );
        glColor3f( 1.0f, 0.0f, 0.0f );
        glVertex3f( -5.0f, 0.0f, -4.0f );
        glColor3f( 0.0f, 1.0f, 0.0f );
        glVertex3f( 5.0f, 0.0f, -4.0f );
        glColor3f( 0.0f, 0.0f, 1.0f );
        glVertex3f( 0.0f, 0.0f, 6.0f );
        glEnd();

        // swap buffers:
        if ( SDL_MUSTLOCK(screen) && (SDL_LockSurface(screen)<0) ) {
            fprintf(stderr, "SDL ERROR: Can't lock screen: %s\n", SDL_GetError());
            return 1;
        }
        ZB_copyFrameBuffer(frameBuffer, screen->pixels, pitch);
        if ( SDL_MUSTLOCK(screen) ) SDL_UnlockSurface(screen);
        SDL_Flip(screen);

        // check if the ESC key was pressed or the window was closed:
        SDL_Event evt;
        while( SDL_PollEvent( &evt ) ) switch(evt.type) {
        case SDL_KEYDOWN:
            if(evt.key.keysym.sym==SDLK_ESCAPE)
                running=0;
            break;
        case SDL_QUIT:
            running=0;
            break;
        }
    }
    // cleanup:
    ZB_close(frameBuffer);
    if(SDL_WasInit(SDL_INIT_VIDEO))
        SDL_QuitSubSystem(SDL_INIT_VIDEO);
    SDL_Quit();
    return 0;
}
Exemple #5
0
int minigl_MakeCurrent(GAL_Surface *surface, minigl_Context *ctx, unsigned short client_width, unsigned short client_height)
{
	int	mode;
	int	xsize;
	int	ysize;
	int	n_colors = 0;
	ZBuffer *zb;

	if( ctx->gl_context == NULL )
	{
		// create the TinyGL context

		xsize = client_width;
		ysize = client_height;

		// we ensure that xsize and ysize are multiples of 2 for the zbuffer.
		xsize &= ~3;
		ysize &= ~3;

		switch( surface->format->BitsPerPixel )
		{
		case  8:
			{
				ctx->indexes = (unsigned char *)malloc(ZB_NB_COLORS);
				if (ctx->indexes == NULL)
					return 0;
				for(mode=0;mode<ZB_NB_COLORS;mode++)
					ctx->indexes[mode]=mode;

				ctx->palette = (unsigned int *)calloc (ZB_NB_COLORS, sizeof(int));
				if (ctx->palette == NULL)
				{
					free(ctx->indexes);
					ctx->indexes = NULL;
					return 0;
				}

				ctx->pitch = surface->pitch * 2;
				n_colors = ZB_NB_COLORS;
				mode = ZB_MODE_INDEX;

				ctx->pDitherBuf = (unsigned char *)malloc(xsize * ysize);
			}
			break;

		case 16:
			{
				ctx->pitch = surface->pitch;
				mode = ZB_MODE_5R6G5B;
			}
			break;

		case 24:
			{
				ctx->pitch = ( surface->pitch * 2 ) / 3;
				mode = ZB_MODE_RGB24;
			}
			break;

		case 32:
			{
				ctx->pitch = surface->pitch / 2;
				mode = ZB_MODE_RGBA;
			}
			break;

		default:
			return 0;
		}

		zb = ZB_open( xsize, ysize, mode, n_colors, (unsigned char*)ctx->indexes, (int *)ctx->palette, NULL);

		if( zb == NULL ){
//			fprintf( stderr, "Error while initializing Z buffer\n" );
			if (ctx->indexes != NULL )
			{
				free(ctx->indexes);
				ctx->indexes = NULL;
			}
			if (ctx->palette != NULL )
			{
				free(ctx->palette);
				ctx->palette = NULL;
			}
			return 0;
		}

		if (ctx->palette != NULL)
		{
			GAL_Color *pal=(GAL_Color *)calloc(ZB_NB_COLORS,sizeof(GAL_Color));
			if (pal!=NULL)
			{
				for (mode=0; mode<ZB_NB_COLORS; mode++)
				{
					pal[mode].r = (ctx->palette[mode]>>16) & 0xFF;
					pal[mode].g = (ctx->palette[mode]>>8) & 0xFF;
					pal[mode].b = (ctx->palette[mode]) & 0xFF;
				}

				GAL_SetColors(surface, pal, 0, ZB_NB_COLORS);
				free(pal);
			}
		}