last day (14 days later) » 

12:41 AM
Sorry to keep pestering, but you seem very informed. Either way, removing the asterisk makes the program crash and it gives errors. Something about unhandled exceptions and writing memory etc.
I feel like copying pixels and moving them around should be a simple process and I'm misunderstanding something basic for it to be this big of an issue.
I am using an example program that I modified that came from a book I'm using. Would you mind, if I sent you the whole program (its not huge) and let you look over the parts that are necessary and tell me what I'm screwing up?
 
To be honest, you are using an old part of the OpenGL API for this. These days the way people do this sort of thing is with shaders and Framebuffer Objects. The problem with glReadPixels (...) is that it forces the GPU to stop doing work, and send a copy of its memory to the CPU. Granted, shaders don't make this any more simple, but you would not be missing out on anything if you skipped learning glDrawPixels (...) and went for the programmable approach instead.
In fact, glDrawPixels (...) was removed from OpenGL 3.1. Might I ask what book and what version of OpenGL you are trying to learn?
 
LOL yeah... that doesn't surprise me. It's part of a class I'm taking on computer graphics. Universities never cease to amaze me with how much useless crap they want you to learn.
The book: "Computer Graphics with OpenGL" 4th ed
I actually had the assignment finished and done using matrix multiplication on vertexes, but then I realized the assignment called for "raster transformations" and so I'm trying to redo the assignment and I can't even copy a pink pixel and redraw it.
 
Ah, I see. glRasterPos* actually works the same way as glVertex* in terms of transformations. You would think that the raster position would be in terms of window coordinates, but it is not.
In fact, clipping raster positions works the same way as vertices, except if the raster position is clipped then no pixels in a command like glDrawPixels (...) will show up, even if they would have been on-screen.
But really, I think your problem is down to the way you are storing your array of pixel data.
 
When I remove the asterisk:
Unhandled exception at 0x698d4730 in FerryHW4_1.exe: 0xC0000005: Access violation reading location 0x00000013.
 
How is the entire array declared? I would try: GLubyte data [115 * 35 * 3]. The problem may also be that it's too large to store on the stack. static GLubyte data [115 * 35 * 3] could help if that is the case.
 
12:55 AM
I could probably shorten that, most of those methods were used to rotate the triangle without rasters
The relevant method is void displayFcn (void) toward the bottom of it where I used the raster methods
In fact that entire program came from the book with the exception of slight modificiations to the "displayFcn" method. The rest of it just finds the center point of an object, rotates and scales etc. and handles all the math associated with the transformations.
 
Rather than simply removing the asterisk, did you try one of the ways I wrote the array? With the asterisk you are allocating an array large enough to store 115 * 35 pointers; pointers are probably 4-byte (32-bit) or 8-byte (64-bit) depending on your system config. Once you remove the asterisk by itself and change nothing else, your array becomes 1/4 or 1/8 the size.
You need to make enough room in the array for 3 colors per-pixel, so that's 3 bytes per-pixel.
That's why I added the extra * 3
 
The array should be a 2d array right?
I did try it that way but it never loaded the display.
the window loaded and the program freezes without loading any content
And thats with both a 2d and single array
Well 1d array gives error: Run-Time Check Failure #2 - Stack around the variable 'data' was corrupted.
 
1:12 AM
It doesn't actually matter in a language like C. 2D arrays declared that way are just a convenience, the compiler creates a large 1D array large enough to hold all of the dimensions.
 
Oh, good to know.
You're being such a huge help, I can't tell you how much I appreciate your patience.
 
Another thing that just came to my mind is the pixel unpack aligment. By default OpenGL expects that each row in your image begins on a 4-byte boundary. You don't want this, glPixelStorei (GL_UNPACK_ALIGNMENT, 1); before calling glDrawPixels (...) and glPixelStorei (GL_PACK_ALIGNMENT, 1); before calling glReadPixels (...) may help to get rid of the weird dots in your screenshot.
It actually increases the storage requirements to have the row packing alignment != 1. And this could explain a buffer overrun.
This isn't an issue at all if you use a format like GL_RGBA because that always has 4-bytes per-pixel. This problem is mostly because you are using GL_RGB.
 
The current version: codepad.org/2AhC43SF
I've done everything you mentioned thus far. Is it possible that I'm drawing the triangle to a different buffer than what I'm copying from?
 
That is possible too... are you using double-buffered rendering?
Nevermind, I just saw in the code: GLUT_SINGLE.
In a single-buffered configuration, the read buffer is always GL_FRONT, so that shouldn't be an issue.
 
Hmm ok
 
1:23 AM
If you go back to your original code, with the GLubyte *data [...] and all, and use GL_RGBA instead of GL_RGB, does anything change? I still think those dots are data alignment issues.
 
Nope, it gives the same result
Here is a condensed less confusing version: codepad.org/5NtbmT5o
 
What if you set the width and height correctly? Right now you have them backwards in the call to glDrawPixels (...).
 
That's the result of the condensed version
That just makes the rectangle a horizontal one. (the rectangle partially visible above).
They are reversed because I had originally created a second array, and "swapped" the (x,y) values to do a manual 90 degree rotation.
This is with vixing the length/width
 
In your winReshapeFunc, can you try gluOrtho2D (0, newWidth, 0, newHeight);? Part of the problem with the functions you are using (glReadPixels and glDrawPixels) is that their width and height are in pixels, but the position is in object coordinates. The two are not necessarily the same scale.
 
You are a saint
I never in a million years would have figured that out. (maybe in a million years) but I'm a Java programmer, not really very accustomed to C++ and I wouldn't have ever recognized this problem
I'm so excited right now. Post that up as an answer and I'll accept it for all your hard work. I wish there were more I could do.
 
1:36 AM
Heh, yeah. I was starting to get suspicious when it was drawing white pixels overtop the triangle.
Can you make a link to the final version of the code that worked, so I can add enough details for an answer?
 
In the question or link it to you
 
In the chat
 
One last question... I was previously using the array data[115][35] and I had newData[35][115]. I looped through with a double for loop and swapped the values to do a 90 degree rotation.
 
One last thing, if you remove the two glPixelStorei (...) lines, does that break anything? I believe that is part of the solution.
 
How does that translate to a 1d array? [115 * 35 *3]
I did remove them just to see if they're relevant and it all broke
it was necessary
 
1:40 AM
Good, I thought so :)
newData[35][115] would be newData[35 * 115]
 
ok great thanks
 
2:04 AM
I turned our chat into an answer, let me know in comments if anything I wrote does not make sense. I'm going to head to bed now, I'm glad I could get things working for you.
 

last day (14 days later) »