From: Jason Roelofs Date: 2007-08-07T03:38:53+09:00 Subject: Re: Problem with glReadPixels in ruby-opengl ------=_Part_37836_26564007.1186425537749 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Interesting way of doing it, that's for sure, but all you'll need to do is treat the string like an array. e.g.: pixels = GL.ReadPixels(...) pixels[0] # r of first pixel pixels[1] # g of first pixel pixels[2] # b of first pixel. The crazy /000 stuff you see is simply a nice way of displaying characters that aren't of the a-zA-Z0-9 character range. As RGB values are 0-255, you can save the stuff in a single character. So while it's actually a string, just treat the results as an array of numbers. HTH Jason On 8/6/07, Randy Baden wrote: > > 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? > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_37836_26564007.1186425537749--