openGL glReadPixels and glDrawPixels

Started by
9 comments, last by dehseth 14 years, 6 months ago
hi, I just wanna copy a piece of current buffer and store it in memory. And draw those stored pixels to any position after... But it does not seems as it is sounds. I tried to copy them: if (m_pixels) { free(m_pixels); m_pixels = NULL; } m_pixels = (GLvoid*)malloc(4 * r.GetWidth() * r.GetHeight()); glReadPixels(r2.GetLeft(), r2.GetTop(), r2.GetWidth(), r2.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, m_pixels); and I tired to draw em: glDrawPixels(m_rect->GetWidth(), m_rect->GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, m_pixels); but i dont see anything... and the draw function is not working if I put glRasterPos(50, 50) before it?? I am new to opengl.. how do I accomplish that? thank you all...
MCA
Advertisement
Have you set up the projection and modelview matrix for the raster position to end up where you expect it to be? If you want to specify the raster position in window coordinates, I suggest you use glWindowPos instead.
hi Brother Bob,

I think last time you've solved my problem :)

Well I am using a game engine (Leadwerks) and that engine is doing all the work.. And I want to copy 2d and save to memory a 2d image pixels and can draw it at any time and at any position.

I am using windows opengl.h and there's no function defined glWindowPos. I heard ms did not declared every function in itself.

When I use this code it works and I see rectangle on screen:

glBegin(GL_POLYGON);
glVertex2i(m_left, m_top);
glVertex2i(m_right, m_top);
glVertex2i(m_right, m_bottom);
glVertex2i(m_left, m_bottom);
glEnd();

So I guess engine does not have any problem with using this. And when I use glReadPixels function I see memory value of pixels changed. So I copied something to there.. But I cannot redraw it.

And also this code piece is not working if I call glRasterPos2i(50, 50) and if I use glRasterPos2i(0, 0) I dont see anything on screen..

unsigned char pixel[100][4];
for(int x=0; x<100;x++)
{
pixel[x][0]=0xff;
pixel[x][1]=0x00;
pixel[x][2]=0x00;
pixel[x][3]=0xff;
}
glDrawPixels(20, 20, GL_RGBA, GL_UNSIGNED_BYTE, &pixel[0][0]);

any ideas?

MCA
glWindowPos was introduces in a later version that what Windows provides. Check the OpenGL subforum's FAQ on how to go beyond OpenGL 1.1 on Windows.

Have you checked for error states, so there's no errors generated by those calls? Have you disabled all other states for drawing, like lighting and texturing, so they don't affect the drawing?
I can draw a rectangle and text but cannot copy it to another place.
Here is my code:



CXRect r2(m_rect);
r2.CenterTo(&r);
r2.Draw();

m_font->Draw(m_caption->GetBuffer(), captionRect.GetLeft(), captionRect.GetTop() + captionRect.GetHeight() - m_font->GetTextMetric()->tmDescent, 1.0, 0.0, 0.0);

if (m_pixels) { free(m_pixels); m_pixels = NULL; }


m_pixels = (GLvoid*)calloc(256 * r2.GetWidth() * r2.GetHeight(), 1);
glReadPixels(r2.GetLeft(), r2.GetTop() + r2.GetHeight(), r2.GetWidth(), r2.GetHeight(), GL_RGBA, GL_COLOR_INDEX, m_pixels);


glRasterPos2i(200, 200);
glDrawPixels(r2.GetWidth(), r2.GetHeight(), GL_RGBA, GL_COLOR_INDEX, m_pixels);


When I run this, I see my rect and my text but there's no copy of it at (200, 200) This leadwerks engine I am using, has it's own buffering system and as far as I know it does not write to backbuffer immediately.. Could this be a problem?

MCA
Drawing of primitives and pixel rectangles goes to the same place, so if you can draw a primitive, you can draw a pixel rectangle. But you haven't commented on my last questions.
Sorry , I thought answer was clear. No I did not disable all other states for drawing, like lighting and texturing. I thought If I can draw a primitive I can draw other things.. But as you see in the code in previous post, I can draw rectangle, I can draw text, and I think I copy something cause I see change in memory; but I just cannot paste the bits..... (And I am doing a research about further opengl functions on windows, cause it only supports opengl 1.1)

Well here is what I actually wanna do: I want to create a custom buffer, a smaller buffer on RAM. Let's say it has a width of 200px and height of 50px. Afterwards I want to draw primitives (2D images) and text, or a 2D image texture. And I want to save this drawing to memory (copy image bitmap into memory using smt like glreadpixels) and as last part I just want to draw this saved texture/image/pixel data (whatever it is) to a specific position onto current buffer.

Well I can use openGL, CDC, or any other thing to do that (except directX)..

Thanks...
MCA
If you haven't disabled rendering states, perhaps they are left in a state that interfere with pixel rectangles in a negative way. This is not uncommon when you leave states enabled or in unknown states when you're done using them.

I suggest you provide a complete and working example we can copy-paste directly and run (a very primitive example using GLUT or SDL preferably). If you cannot demonstrate this problem with a complete application in, say, 50-100 lines, the problem is related to things you haven't mentioned yet.
Sorry Brother Bob,

but I just could not make a brief copy paste source code cause it contains too much classes and also this engine dll and other stuff. I am gonna try another solution for this. But first let me ask you some other question which is related with this.

I have a font class which prints given text with selected font with given color. In this class I've used glGenLists, wglUseFontBitmaps functions to create font and glCallLists to draw.

Here's my code about drawing text:

glColor3f(r, g, b);
glRasterPos2i(x, y);
glPushAttrib(GL_LIST_BIT);
glListBase(m_uiListBase);
glCallLists((GLsizei)_tcslen(string), GL_UNSIGNED_BYTE, (GLubyte*)string);
glPopAttrib();

When I use positive (x,y) position it draws with success. But If one of the parameter is negative like draw it to (-20, 200), text dissappers. It does not clip the first 20 pixels. How can I make it clip and draw rest of the text? (If I can solve this my all other problems will dissappear)..

Thank you....
MCA
As I said, if you cannot make a small test case, then your problem is likely dependent on the code you cannot remove. In that case, you should remove it piece by piece and see when the problem disappears and investigate how that piece can affect the problem.

The raster position, when set by glRasterPos, is transformed as every other vertex, and if it is outside the view volume, it is clipped and marked as invalid. Any operation using an invalid raster position is silently ignored. Thus, if you place it outside the view volume, you cannot use it for drawing.

The proper solution is to use glWindowPos, which bypasses any transformation and clipping. As an effect, raster positions can be set anywhere and will not be invalid.

This topic is closed to new replies.

Advertisement