R
Randy Baden
I'm having a lot of difficulty understanding the behavior of some of the
ruby-opengl code. For those who've never used it, ruby-opengl provides
bindings for the OpenGL functions in C. For the most part it works
fine, but there's something funny about glReadPixels that I think I'm
missing. I'm posting it here because I can't find a decent resource for
ruby-opengl information, and I think my question is more about how
bindings work.
Here's the C function in question:
static VALUE
gl_ReadPixels(obj,arg1,arg2,arg3,arg4,arg5,arg6)
VALUE obj,arg1,arg2,arg3,arg4,arg5,arg6;
{
GLint x;
GLint y;
GLsizei width;
GLsizei height;
int format;
int type;
VALUE pixels;
x = (GLint)NUM2INT(arg1);
y = (GLint)NUM2INT(arg2);
width = (GLsizei)NUM2INT(arg3);
height = (GLsizei)NUM2INT(arg4);
format = NUM2INT(arg5);
type = NUM2INT(arg6);
if (format != -1 && type != -1) {
int type_size;
int format_size;
type_size = gltype_size(type) / 8;
format_size = glformat_size(format);
pixels =
allocate_buffer_with_string(width*height*format_size*type_size);
glReadPixels(x,y,width,height,format,type,(GLvoid*)RSTRING(pixels)->ptr);
return pixels;
}
return Qnil;
}
I feel like I understand everything going on in this function; my only
problem is with the return pixels line. The allocate_buffer_with_string
function just allocates a string of the specified size, so it seems like
pixels is just a string, and the value of the characters in that string
are modified by glReadPixels.
I guess my problem is that, when this function is called from the Ruby
side, the result is a string that looks like one of the following:
"\000\000\200?"
"\376n~?"
I don't really know what format the string has. Also, as you can see,
even though I'm passing constant parameters for the width, height,
format, and type, the strings have different lengths. What exactly is
this function supposed to be returning? The glReadPixels function in C
should be modifying the last parameter as if it were an 3-dimensional
array of size [width][height][format], and it should be putting float
values into the elements of the array. In my case it's very simple:
width, height, and format are all 1, so it's just an array with a single
element. I would think that this would then be just a float value, but
I don't have any idea how to convert those strings into a float value.
Does anyone have any ideas as to how this is working?
ruby-opengl code. For those who've never used it, ruby-opengl provides
bindings for the OpenGL functions in C. For the most part it works
fine, but there's something funny about glReadPixels that I think I'm
missing. I'm posting it here because I can't find a decent resource for
ruby-opengl information, and I think my question is more about how
bindings work.
Here's the C function in question:
static VALUE
gl_ReadPixels(obj,arg1,arg2,arg3,arg4,arg5,arg6)
VALUE obj,arg1,arg2,arg3,arg4,arg5,arg6;
{
GLint x;
GLint y;
GLsizei width;
GLsizei height;
int format;
int type;
VALUE pixels;
x = (GLint)NUM2INT(arg1);
y = (GLint)NUM2INT(arg2);
width = (GLsizei)NUM2INT(arg3);
height = (GLsizei)NUM2INT(arg4);
format = NUM2INT(arg5);
type = NUM2INT(arg6);
if (format != -1 && type != -1) {
int type_size;
int format_size;
type_size = gltype_size(type) / 8;
format_size = glformat_size(format);
pixels =
allocate_buffer_with_string(width*height*format_size*type_size);
glReadPixels(x,y,width,height,format,type,(GLvoid*)RSTRING(pixels)->ptr);
return pixels;
}
return Qnil;
}
I feel like I understand everything going on in this function; my only
problem is with the return pixels line. The allocate_buffer_with_string
function just allocates a string of the specified size, so it seems like
pixels is just a string, and the value of the characters in that string
are modified by glReadPixels.
I guess my problem is that, when this function is called from the Ruby
side, the result is a string that looks like one of the following:
"\000\000\200?"
"\376n~?"
I don't really know what format the string has. Also, as you can see,
even though I'm passing constant parameters for the width, height,
format, and type, the strings have different lengths. What exactly is
this function supposed to be returning? The glReadPixels function in C
should be modifying the last parameter as if it were an 3-dimensional
array of size [width][height][format], and it should be putting float
values into the elements of the array. In my case it's very simple:
width, height, and format are all 1, so it's just an array with a single
element. I would think that this would then be just a float value, but
I don't have any idea how to convert those strings into a float value.
Does anyone have any ideas as to how this is working?