e576bb2002-10-11Martin Nilsson /* -*- c -*- || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
12b8c71998-11-03Marcus Comstedt  #include "global.h"
11a1be1999-07-16Marcus Comstedt #include "config.h"
12b8c71998-11-03Marcus Comstedt #include "stralloc.h" #include "pike_macros.h" #include "object.h" #include "program.h" #include "interpret.h" #include "builtin_functions.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
d919491998-11-03Marcus Comstedt #include "module_support.h"
1c89cc1999-07-18Marcus Comstedt #include "operators.h"
7c970f1999-07-29Marcus Comstedt #include "mapping.h"
7479a22003-11-13Martin Stjernholm #include "pike_types.h"
12b8c71998-11-03Marcus Comstedt 
8f3fe22017-03-09William Welliver #ifdef HAVE_OPENGL_GL_H #include <OpenGL/gl.h> #else
b76b9a1999-08-17Marcus Comstedt #ifdef HAVE_GL_GL_H
12b8c71998-11-03Marcus Comstedt #include <GL/gl.h>
b76b9a1999-08-17Marcus Comstedt #endif /* HAVE_GL_GL_H */
a944e62003-05-12 Eli Green #endif /* HAVE_OPENGL_GL_H */
12b8c71998-11-03Marcus Comstedt 
6dc2772000-07-28Fredrik Hübinette (Hubbe) 
6ad2372002-05-11Martin Nilsson #define sp Pike_sp
12b8c71998-11-03Marcus Comstedt #define ZT_INT 1 #define ZT_FLOAT 2 #define ZT_DOUBLE 4 #define ZT_ARRAY 8
2200902003-07-09Marcus Agehall union zvalue { GLint i; GLfloat f; GLdouble d; };
12b8c71998-11-03Marcus Comstedt struct zvalue4 { union { GLint i[4]; GLfloat f[4]; GLdouble d[4]; } v; int ty; }; union zvalue16 { GLint i[16]; GLfloat f[16]; GLdouble d[16]; };
1588bf1999-07-17Marcus Comstedt struct zimage { GLsizei width, height; GLenum format, type; GLvoid *pixels;
7c970f1999-07-29Marcus Comstedt  int alloc;
1588bf1999-07-17Marcus Comstedt };
12b8c71998-11-03Marcus Comstedt static int check_234_args(char *func, INT32 args, int mn, int mx, int ty, int rty, struct zvalue4 *ret) { struct svalue *s; int i, tt, tr=0;
5a17b52011-10-30Henrik Grubbström (Grubba)  if(args==1 && TYPEOF(sp[-1]) == T_ARRAY) {
12b8c71998-11-03Marcus Comstedt  struct array *a = sp[-1].u.array; args = a->size; if(args<mn || args>mx)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("%s: expected array of size %d-%d.\n", func, mn, mx);
12b8c71998-11-03Marcus Comstedt  s = ITEM(a); } else { if(args<mn || args>mx)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("%s: too %s arguments.\n", func, (args<mn? "few":"many"));
12b8c71998-11-03Marcus Comstedt  s = sp-args; } for(i=0; i<args; i++)
5a17b52011-10-30Henrik Grubbström (Grubba)  if(!((tt=1<<TYPEOF(s[i]))&ty)) {
12b8c71998-11-03Marcus Comstedt  char buf[32]; int n=0; buf[0]='\0'; for(tt=0; ty; tt++) if(ty&(1<<tt)) { ty&=~(1<<tt); if(n++)
4ae7882015-05-22Martin Nilsson  strncat(buf, " or ", sizeof(buf)-strlen(buf)-1); strncat(buf, get_name_of_type(tt), sizeof(buf)-strlen(buf)-1);
12b8c71998-11-03Marcus Comstedt  }
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("%s: got %s, expected %s.\n", func,
5a17b52011-10-30Henrik Grubbström (Grubba)  get_name_of_type(TYPEOF(s[i])), buf);
12b8c71998-11-03Marcus Comstedt  } else tr|=tt; if(tr==(BIT_INT|BIT_FLOAT)) tt=((rty&ZT_DOUBLE)? ZT_DOUBLE:ZT_FLOAT); else if(tr==BIT_FLOAT) tt=((rty&ZT_FLOAT)? ZT_FLOAT:ZT_DOUBLE); else if(tr!=BIT_INT)
a4a1722000-12-05Per Hedbor  Pike_error("Internal error in check_234_args for %s!\n", func);
12b8c71998-11-03Marcus Comstedt  else if(rty&ZT_INT) tt=ZT_INT;
13670c2015-05-25Martin Nilsson  else
12b8c71998-11-03Marcus Comstedt  tt=((rty&ZT_DOUBLE)? ZT_DOUBLE:ZT_FLOAT); ret->ty=tt;
13670c2015-05-25Martin Nilsson  for(i=0; i<args; i++)
12b8c71998-11-03Marcus Comstedt  switch(tt) { case ZT_INT: ret->v.i[i]=s[i].u.integer; break; case ZT_FLOAT:
5a17b52011-10-30Henrik Grubbström (Grubba)  ret->v.f[i]=(TYPEOF(s[i]) == T_INT?(float)s[i].u.integer:s[i].u.float_number);
13670c2015-05-25Martin Nilsson  break;
12b8c71998-11-03Marcus Comstedt  case ZT_DOUBLE:
5a17b52011-10-30Henrik Grubbström (Grubba)  ret->v.d[i]=(TYPEOF(s[i]) == T_INT?(double)s[i].u.integer:
12b8c71998-11-03Marcus Comstedt  s[i].u.float_number);
13670c2015-05-25Martin Nilsson  break;
12b8c71998-11-03Marcus Comstedt  } return args; }
70a2b41999-07-24Marcus Comstedt static int img_inited=0;
c9eefb2014-08-21Martin Nilsson static void img_init(void)
70a2b41999-07-24Marcus Comstedt { glPixelStorei(GL_PACK_SWAP_BYTES, 0); glPixelStorei(GL_PACK_LSB_FIRST, 0); glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_SKIP_ROWS, 0); glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
83c76d1999-07-25Marcus Comstedt  glPixelStorei(GL_PACK_ALIGNMENT, 1);
70a2b41999-07-24Marcus Comstedt  glPixelStorei(GL_UNPACK_SWAP_BYTES, 0); glPixelStorei(GL_UNPACK_LSB_FIRST, 0); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
83c76d1999-07-25Marcus Comstedt  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
70a2b41999-07-24Marcus Comstedt  img_inited=1; }
d269ba2003-05-31Per Hedbor static struct program *shm_program=NULL;
7c970f1999-07-29Marcus Comstedt static void check_img_arg(struct svalue *a, struct zimage *img,
1c89cc1999-07-18Marcus Comstedt  int arg, const char *func)
1588bf1999-07-17Marcus Comstedt { struct { void *img; INT_TYPE w, h;
7c970f1999-07-29Marcus Comstedt  } *rgbstor = NULL, *lumstor = NULL, *astor = NULL;
1212512001-12-06Per Hedbor  struct { unsigned char *p; size_t size; } *stor_mem;
7c970f1999-07-29Marcus Comstedt  unsigned char *pp; struct { unsigned char r, g, b; } *p1, *p2; INT32 cnt;
e0427f1999-08-18Marcus Comstedt  static struct program *image_program=NULL; if(image_program == NULL) {
5e9fc02015-08-18Per Hedbor  push_static_text("Image.Image");
e163b02003-12-12Martin Nilsson  SAFE_APPLY_MASTER("resolv",1); image_program=program_from_svalue(sp-1); pop_stack();
e0427f1999-08-18Marcus Comstedt  if(image_program == NULL)
5804752006-01-04Marek Habersack  Pike_error("%s: Can't resolve Image.Image!\n", func);
e0427f1999-08-18Marcus Comstedt  }
7c970f1999-07-29Marcus Comstedt  img->alloc = 0;
5a17b52011-10-30Henrik Grubbström (Grubba)  if(TYPEOF(*a) == T_MAPPING) {
7c970f1999-07-29Marcus Comstedt  struct svalue *v;
9898172000-08-11Henrik Grubbström (Grubba)  if((v=simple_mapping_string_lookup(a->u.mapping, "rgb"))) {
5a17b52011-10-30Henrik Grubbström (Grubba)  if(TYPEOF(*v) == T_OBJECT &&
13b5ed2014-05-26Per Hedbor  (rgbstor = get_storage(v->u.object, image_program)) != NULL &&
7c970f1999-07-29Marcus Comstedt  rgbstor->img != NULL) ; else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument %d to %s.\n", arg, func);
9898172000-08-11Henrik Grubbström (Grubba)  } if((v=simple_mapping_string_lookup(a->u.mapping, "luminance"))) {
5a17b52011-10-30Henrik Grubbström (Grubba)  if(TYPEOF(*v) == T_OBJECT &&
13b5ed2014-05-26Per Hedbor  (lumstor = get_storage(v->u.object, image_program)) != NULL &&
7c970f1999-07-29Marcus Comstedt  lumstor->img != NULL) ; else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument %d to %s.\n", arg, func);
9898172000-08-11Henrik Grubbström (Grubba)  } if((v=simple_mapping_string_lookup(a->u.mapping, "alpha"))) {
5a17b52011-10-30Henrik Grubbström (Grubba)  if(TYPEOF(*v) == T_OBJECT &&
13b5ed2014-05-26Per Hedbor  (astor = get_storage(v->u.object, image_program)) != NULL &&
7c970f1999-07-29Marcus Comstedt  astor->img != NULL) ; else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument %d to %s.\n", arg, func);
9898172000-08-11Henrik Grubbström (Grubba)  }
1212512001-12-06Per Hedbor  if( (v=simple_mapping_string_lookup(a->u.mapping, "mem" )) ) {
e163b02003-12-12Martin Nilsson  if(shm_program == NULL) {
5e9fc02015-08-18Per Hedbor  push_static_text("System.Memory");
e163b02003-12-12Martin Nilsson  SAFE_APPLY_MASTER("resolv",1); shm_program=program_from_svalue(sp-1); pop_stack(); if(shm_program == NULL) Pike_error("%s: Can't resolve System.Memory!\n", func); }
5a17b52011-10-30Henrik Grubbström (Grubba)  if( TYPEOF(*v) == T_OBJECT &&
13b5ed2014-05-26Per Hedbor  (stor_mem = get_storage( v->u.object, shm_program ) ) )
1212512001-12-06Per Hedbor  {
a564162006-09-27Marcus Comstedt  INT_TYPE x=0, y=0, row=0;
1212512001-12-06Per Hedbor  if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_w" )) ) img->width = v->u.integer; else Pike_error("Need mem_w for memory texture\n"); if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_h" )) ) img->height = v->u.integer; else Pike_error("Need mem_h for memory texture\n"); if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_format" )) ) img->format = v->u.integer; else Pike_error("Need mem_format for memory texture\n"); if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_type" )) ) img->type = v->u.integer; else Pike_error("Need mem_type for memory texture\n"); img->pixels = stor_mem->p;
94b3c32002-01-12Per Hedbor  if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_offset" )) )
81b8352003-02-14Per Hedbor  img->pixels = (char *)img->pixels + v->u.integer;
a564162006-09-27Marcus Comstedt  if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_x" )) ) x = v->u.integer; if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_y" )) ) y = v->u.integer; if( (v=simple_mapping_string_lookup(a->u.mapping, "mem_row" )) ) row = v->u.integer;
1212512001-12-06Per Hedbor  if(!img_inited) img_init();
a564162006-09-27Marcus Comstedt  if(x != 0 || y != 0 || row != 0) { glPixelStorei(GL_UNPACK_ROW_LENGTH, row); glPixelStorei(GL_UNPACK_SKIP_ROWS, y); glPixelStorei(GL_UNPACK_SKIP_PIXELS, x); img_inited = 0; /* Need to reset these later... */ }
1212512001-12-06Per Hedbor  return; } }
5a17b52011-10-30Henrik Grubbström (Grubba)  } else if(TYPEOF(*a) == T_OBJECT &&
13b5ed2014-05-26Per Hedbor  (rgbstor = get_storage(a->u.object, image_program)) !=
7c970f1999-07-29Marcus Comstedt  NULL && rgbstor->img != NULL) ; else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument %d to %s.\n", arg, func);
7c970f1999-07-29Marcus Comstedt  if(rgbstor==NULL && lumstor==NULL && astor==NULL)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Too few images specified.\n");
7c970f1999-07-29Marcus Comstedt  if(rgbstor!=NULL && lumstor!=NULL)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Can't have both rgb and luminance data.\n");
7c970f1999-07-29Marcus Comstedt  if((rgbstor != NULL && astor != NULL && (rgbstor->w != astor->w || rgbstor->h != astor->h)) || (lumstor != NULL && astor != NULL && (lumstor->w != astor->w || lumstor->h != astor->h)))
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Alpha channel must have same size as image!\n");
7c970f1999-07-29Marcus Comstedt  if(lumstor==NULL && astor==NULL) { /* RGB */ img->width = rgbstor->w; img->height = rgbstor->h; img->pixels = rgbstor->img; img->format = GL_RGB; img->type = GL_UNSIGNED_BYTE; } else if(rgbstor != NULL) { /* RGBA */ img->width = rgbstor->w; img->height = rgbstor->h;
d4caed2005-08-15Martin Nilsson  cnt = img->width * img->height; img->pixels = pp = xalloc( cnt*4 );
f096572005-08-14Rikard Gillemyr  img->alloc = 1;
7c970f1999-07-29Marcus Comstedt  img->format = GL_RGBA; img->type = GL_UNSIGNED_BYTE; p1 = rgbstor->img; p2 = astor->img; while(cnt--) { *pp++ = p1->r; *pp++ = p1->g; *pp++ = p1++->b; *pp++ = p2++->r; } } else if(astor == NULL) { /* Y */ img->width = lumstor->w; img->height = lumstor->h;
d4caed2005-08-15Martin Nilsson  cnt = img->width * img->height; img->pixels = pp = xalloc( cnt );
f096572005-08-14Rikard Gillemyr  img->alloc = 1;
7c970f1999-07-29Marcus Comstedt  img->format = GL_LUMINANCE; img->type = GL_UNSIGNED_BYTE; p1 = lumstor->img; while(cnt--) *pp++ = p1++->r; } else if(lumstor == NULL) { /* A */ img->width = astor->w; img->height = astor->h;
d4caed2005-08-15Martin Nilsson  cnt = img->width * img->height; img->pixels = pp = xalloc( cnt );
f096572005-08-14Rikard Gillemyr  img->alloc = 1;
7c970f1999-07-29Marcus Comstedt  img->format = GL_ALPHA; img->type = GL_UNSIGNED_BYTE; p1 = astor->img; while(cnt--) *pp++ = p1++->r; } else { /* YA */ img->width = lumstor->w; img->height = lumstor->h;
d4caed2005-08-15Martin Nilsson  cnt = img->width * img->height; img->pixels = pp = xalloc( cnt*2 );
0997cb2005-08-15Henrik Grubbström (Grubba)  img->alloc = 1;
7c970f1999-07-29Marcus Comstedt  img->format = GL_LUMINANCE_ALPHA; img->type = GL_UNSIGNED_BYTE; p1 = lumstor->img; p2 = astor->img; while(cnt--) { *pp++ = p1++->r; *pp++ = p2++->r; } }
70a2b41999-07-24Marcus Comstedt  if(!img_inited) img_init();
1588bf1999-07-17Marcus Comstedt }
d269ba2003-05-31Per Hedbor static void *get_mem_object( struct svalue *x ) { struct { unsigned char *p; size_t size; } *stor_mem; if(shm_program == NULL) {
5e9fc02015-08-18Per Hedbor  push_static_text("System.Memory");
e163b02003-12-12Martin Nilsson  SAFE_APPLY_MASTER("resolv",1);
d269ba2003-05-31Per Hedbor  shm_program=program_from_svalue(sp-1); pop_stack(); if(shm_program == NULL) Pike_error("Can't resolve System.Memory!\n"); }
5a17b52011-10-30Henrik Grubbström (Grubba)  if( TYPEOF(*x) != T_OBJECT ||
13b5ed2014-05-26Per Hedbor  !(stor_mem = get_storage( x->u.object, shm_program ) ) )
d269ba2003-05-31Per Hedbor  Pike_error("Expected System.Memory object\n"); return stor_mem->p; }
13670c2015-05-25Martin Nilsson 
d269ba2003-05-31Per Hedbor 
1588bf1999-07-17Marcus Comstedt static void release_img(struct zimage *img) {
7c970f1999-07-29Marcus Comstedt  if(img->alloc) free(img->pixels);
1588bf1999-07-17Marcus Comstedt }
12b8c71998-11-03Marcus Comstedt static int check_1n_args(char *func, INT32 args, int ty, int rty, union zvalue16 *ret) { int i, tt, tr=0, arr=0; struct svalue *s; if(args!=1)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("%s: too %s arguments.\n", func, (args<1? "few":"many"));
13670c2015-05-25Martin Nilsson 
5a17b52011-10-30Henrik Grubbström (Grubba)  if(TYPEOF(sp[-1]) == T_ARRAY) {
12b8c71998-11-03Marcus Comstedt  struct array *a = sp[-1].u.array; args = a->size; if(args>16)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("%s: array too large.\n", func);
12b8c71998-11-03Marcus Comstedt  arr++; s = ITEM(a); } else s = sp-args; for(i=0; i<args; i++)
5a17b52011-10-30Henrik Grubbström (Grubba)  if(!((tt=1<<TYPEOF(s[i]))&ty)) {
12b8c71998-11-03Marcus Comstedt  char buf[32]; int n=0; buf[0]='\0'; for(tt=0; ty; tt++) if(ty&(1<<tt)) { ty&=~(1<<tt); if(n++) strcat(buf, " or "); strcat(buf, get_name_of_type(tt)); }
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("%s: got %s, expected %s.\n", func,
5a17b52011-10-30Henrik Grubbström (Grubba)  get_name_of_type(TYPEOF(s[i])), buf);
12b8c71998-11-03Marcus Comstedt  } else tr|=tt; if(tr==(BIT_INT|BIT_FLOAT)) tt=((rty&ZT_DOUBLE)? ZT_DOUBLE:ZT_FLOAT); else if(tr==BIT_FLOAT) tt=((rty&ZT_FLOAT)? ZT_FLOAT:ZT_DOUBLE); else if(tr!=BIT_INT)
a4a1722000-12-05Per Hedbor  Pike_error("Internal error in check_1n_args for %s!\n", func);
12b8c71998-11-03Marcus Comstedt  else if(rty&ZT_INT) tt=ZT_INT;
13670c2015-05-25Martin Nilsson  else
12b8c71998-11-03Marcus Comstedt  tt=((rty&ZT_DOUBLE)? ZT_DOUBLE:ZT_FLOAT);
13670c2015-05-25Martin Nilsson  for(i=0; i<args; i++)
12b8c71998-11-03Marcus Comstedt  switch(tt) { case ZT_INT: ret->i[i]=s[i].u.integer; break; case ZT_FLOAT:
5a17b52011-10-30Henrik Grubbström (Grubba)  ret->f[i]=(TYPEOF(s[i]) == T_INT?(float)s[i].u.integer:s[i].u.float_number);
13670c2015-05-25Martin Nilsson  break;
12b8c71998-11-03Marcus Comstedt  case ZT_DOUBLE:
5a17b52011-10-30Henrik Grubbström (Grubba)  ret->d[i]=(TYPEOF(s[i]) == T_INT?(double)s[i].u.integer:s[i].u.float_number);
13670c2015-05-25Martin Nilsson  break; }
12b8c71998-11-03Marcus Comstedt  return tt|(arr?ZT_ARRAY:0); }
2200902003-07-09Marcus Agehall /* * This function copies an array from pike into a zvalue array. It * allocates all the memory needed to do the transfer and copies the * data. * */ static int check_1unlimited_args(char *func, INT32 args, int ty, int rty, union zvalue **ret) { int i, tt, tr=0, arr=0; struct svalue *s; if(args!=1) Pike_error("%s: too %s arguments.\n", func, (args<1? "few":"many"));
13670c2015-05-25Martin Nilsson 
2200902003-07-09Marcus Agehall  /* Make sure we were sent an array */
5a17b52011-10-30Henrik Grubbström (Grubba)  if(TYPEOF(sp[-1]) == T_ARRAY) {
2200902003-07-09Marcus Agehall  struct array *a = sp[-1].u.array; args = a->size; arr++; s = ITEM(a); } else Pike_error("%s: expected array.\n", func); /* Check what types of arguments that are in the array */ for(i=0; i<args; i++)
5a17b52011-10-30Henrik Grubbström (Grubba)  if(!((tt=1<<TYPEOF(s[i]))&ty)) {
2200902003-07-09Marcus Agehall  char buf[32]; int n=0; buf[0]='\0'; for(tt=0; ty; tt++) if(ty&(1<<tt)) { ty&=~(1<<tt); if(n++) strcat(buf, " or "); strcat(buf, get_name_of_type(tt)); } Pike_error("%s: got %s, expected %s.\n", func,
5a17b52011-10-30Henrik Grubbström (Grubba)  get_name_of_type(TYPEOF(s[i])), buf);
2200902003-07-09Marcus Agehall  } else tr|=tt; if(tr==(BIT_INT|BIT_FLOAT)) tt=((rty&ZT_DOUBLE)? ZT_DOUBLE:ZT_FLOAT); else if(tr==BIT_FLOAT) tt=((rty&ZT_FLOAT)? ZT_FLOAT:ZT_DOUBLE); else if(tr!=BIT_INT) Pike_error("Internal error in check_1unlimited_args for %s!\n", func); else if(rty&ZT_INT) tt=ZT_INT;
13670c2015-05-25Martin Nilsson  else
2200902003-07-09Marcus Agehall  tt=((rty&ZT_DOUBLE)? ZT_DOUBLE:ZT_FLOAT); ret[0] = (void *)xalloc(args * sizeof(union zvalue)); for(i=0; i<args; i++) switch(tt) { case ZT_INT: ((GLint *)ret[0])[i]=s[i].u.integer; break; case ZT_FLOAT:
5a17b52011-10-30Henrik Grubbström (Grubba)  ((GLfloat *)ret[0])[i]=(TYPEOF(s[i]) == T_INT?(float)s[i].u.integer:s[i].u.float_number);
13670c2015-05-25Martin Nilsson  break;
2200902003-07-09Marcus Agehall  case ZT_DOUBLE:
5a17b52011-10-30Henrik Grubbström (Grubba)  ((GLdouble *)ret[0])[i]=(TYPEOF(s[i]) == T_INT?(double)s[i].u.integer:s[i].u.float_number);
13670c2015-05-25Martin Nilsson  break; }
2200902003-07-09Marcus Agehall  return tt|(arr?ZT_ARRAY:0); }
d322f32003-09-14Martin Nilsson inline static void my_push_text(char * str) { if(!str) push_int(0); else push_text(str); }
2200902003-07-09Marcus Agehall 
d5a8812003-07-03Henrik Grubbström (Grubba) #define glClearColor3v(f) glClearColor((f)[0], (f)[1], (f)[2], 0.0) #define glClearColor4v(f) glClearColor((f)[0], (f)[1], (f)[2], (f)[3]) #define glClearAccum3v(f) glClearAccum((f)[0], (f)[1], (f)[2], 0.0) #define glClearAccum4v(f) glClearAccum((f)[0], (f)[1], (f)[2], (f)[3]) #define glEvalPoint1v(i) glEvalPoint1((i)[0]); #define glEvalPoint2v(i) glEvalPoint2((i)[0], (i)[1]);
a7a75c2003-07-03Henrik Grubbström (Grubba) static void my_glFrustum(INT32 args);
6c7d022003-07-08Per Hedbor static void my_glDeleteTextures( INT32 args );
ff197e2003-07-08Per Hedbor static void my_glGenTextures( INT32 args );
6c7d022003-07-08Per Hedbor static void my_glCallLists( INT32 args );
c9eefb2014-08-21Martin Nilsson static void pre_init(void)
d5a8812003-07-03Henrik Grubbström (Grubba) { add_function_constant("glFrustum", my_glFrustum, "function(float,float,float,float,float,float:void)", OPT_SIDE_EFFECT);
6c7d022003-07-08Per Hedbor  add_function_constant("glDeleteTextures", my_glDeleteTextures, "function(int...:void)", OPT_SIDE_EFFECT);
ff197e2003-07-08Per Hedbor  add_function_constant("glCallLists", my_glCallLists,"function(int...:void)",
6c7d022003-07-08Per Hedbor  OPT_SIDE_EFFECT);
ff197e2003-07-08Per Hedbor  add_function_constant("glGenTextures", my_glGenTextures, "function(int:array(int))", OPT_EXTERNAL_DEPEND);
d5a8812003-07-03Henrik Grubbström (Grubba) }
c9eefb2014-08-21Martin Nilsson static void post_init(void)
d5a8812003-07-03Henrik Grubbström (Grubba) { } @@
ff197e2003-07-08Per Hedbor static void my_glGenTextures( INT32 args ) {
3632a82017-12-11Martin Nilsson  GLuint *res;
ff197e2003-07-08Per Hedbor  struct array *a; int ntexts; int i;
5a17b52011-10-30Henrik Grubbström (Grubba)  if( args != 1 || TYPEOF(Pike_sp[-1]) != PIKE_T_INT )
ff197e2003-07-08Per Hedbor  Pike_error("Illegal argument to glGenTextures. Expected integer\n"); ntexts = Pike_sp[-1].u.integer; pop_stack();
3632a82017-12-11Martin Nilsson  res = xcalloc( sizeof( GLuint ), ntexts );
ff197e2003-07-08Per Hedbor  glGenTextures( ntexts, res ); a = allocate_array( ntexts ); for( i = 0; i<ntexts; i++ ) a->item[i].u.integer = res[i]; push_array( a ); free( res ); }
d5a8812003-07-03Henrik Grubbström (Grubba) 
6c7d022003-07-08Per Hedbor static void my_glDeleteTextures( INT32 args ) { /* optimize the (valid) args==0 case. */ if( args ) { GLuint *ls = (GLuint *)xalloc( sizeof( GLint ) * args ); int i; for( i = 0; i<args; i++ ) {
5a17b52011-10-30Henrik Grubbström (Grubba)  if( TYPEOF(Pike_sp[i-args]) != PIKE_T_INT ) {
5c41ab2005-08-15Martin Nilsson  free( ls );
f982742016-01-26Martin Nilsson  SIMPLE_ARG_TYPE_ERROR("glDeleteTextures", i+1, "int");
5c41ab2005-08-15Martin Nilsson  }
6c7d022003-07-08Per Hedbor  ls[i] = Pike_sp[i-args].u.integer; } pop_n_elems(args); glDeleteTextures( args, ls ); free( ls ); } } static void my_glCallLists( INT32 args ) { /* optimize the (valid) args==0 case. */ if( args ) { GLint *ls = (GLint *)xalloc( sizeof( GLint ) * args ); int i; for( i = 0; i<args; i++ ) {
5a17b52011-10-30Henrik Grubbström (Grubba)  if( TYPEOF(Pike_sp[i-args]) != PIKE_T_INT ) {
5c41ab2005-08-15Martin Nilsson  free( ls );
f982742016-01-26Martin Nilsson  SIMPLE_ARG_TYPE_ERROR("glCallLists", i+1, "int");
5c41ab2005-08-15Martin Nilsson  }
6c7d022003-07-08Per Hedbor  ls[i] = Pike_sp[i-args].u.integer; } pop_n_elems(args); glCallLists( args, GL_INT, ls ); free( ls ); } }
f5cd782003-07-04Henrik Grubbström (Grubba) static void my_glFrustum(INT32 args) {
ac8d7e2003-07-06Per Hedbor  FLOAT_TYPE right,left,bottom,top,close,away; GLdouble m[16];
391ac52018-08-05Martin Nilsson  get_all_args(NULL, args, "%f%f%f%f%f%f",
ac8d7e2003-07-06Per Hedbor  &left, &right, &bottom, &top, &close, &away);
1e9a892003-07-05Henrik Grubbström (Grubba)  if (right == left) { SIMPLE_ARG_ERROR("glFrustum", 2, "Arguments right and left must not be equal."); } if (bottom == top) { SIMPLE_ARG_ERROR("glFrustum", 4, "Arguments bottom and top must not be equal."); } if (close == away) { SIMPLE_ARG_ERROR("glFrustum", 6,
ac8d7e2003-07-06Per Hedbor  "Arguments near and away must not be equal.");
1e9a892003-07-05Henrik Grubbström (Grubba)  }
3b92b12003-07-03Martin Nilsson 
ac8d7e2003-07-06Per Hedbor  memset( m, 0, sizeof(m) ); m[0] = 2*close/(right-left); m[5] = 2*close/(top-bottom); m[8] = (right+left)/(right-left); m[9] = (top+bottom)/(top-bottom);
5a37182003-08-17Marcus Comstedt  m[10] = -(away+close)/(away-close);
ac8d7e2003-07-06Per Hedbor  m[11] = -1.0;
5a37182003-08-17Marcus Comstedt  m[14] = -2*away*close/(away-close);
ac8d7e2003-07-06Per Hedbor  glMultMatrixd( m ); pop_n_elems(args);
3b92b12003-07-03Martin Nilsson }