I am making an application that has a bunch of small windows and controls in it (2D rendering), and I would like to render each window and control to its own bitmap. This is what have so far:
uses dglOpenGL;
...
var BMP: TBitmap;
DC, RC: HDC;
...
function TMainForm.Init: Boolean;
begin
Result := InitOpenGL;
if Result = True then
begin
BMP := TBitmap.Create;
BMP.PixelFormat := pf24bit;
BMP.Width := 1280;
BMP.Height := 1024;
DC := (BMP.Canvas.Handle);
RC := CreateRenderingContext(DC,
[opGDI, opDoubleBuffered], // tried changing, didn't help
24,
24,
0,
0,
0,
0);
ActivateRenderingContext(DC, RC);
glClearColor(0.27, 0.4, 0.7, 0.0); // light blue
glViewport(0, 0, 1280, 1024);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, 1280, 0, 1024, -1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
end;
end;
Rendering procedure:
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
// red quad
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex2f(100, 100);
glVertex2f(1280-100, 100);
glVertex2f(1280-100, 1024-100);
glVertex2f(100, 1024-100);
glend;
// swap
SwapBuffers(DC);
But there is no output.
If I use MainForm.Canvas.Draw(0, 0, BMP); then a white rectangle will appear.
I want to do rendering stuff on bitmaps because I can do a lot of things with bitmaps (draw other images, paint text, blur), but if there another way to do off-screen render then it's okay...
So how do I set up my application for off-screen rendering?
OpenGL contexts must be created matching the target device context. For windows they're created in a different way than for bitmaps. See
http://msdn.microsoft.com/en-us/library/windows/desktop/dd368826(v=vs.85).aspx notably the dwFlags, where there are among others
PFD_DRAW_TO_WINDOW The buffer can draw to a window or device surface.
PFD_DRAW_TO_BITMAP The buffer can draw to a memory bitmap.
However you shouldn rush ahead and create a render context for your DIB DC. Why? Because it will be slow as hell, as OpenGL render contexts on a DIB section will use a software rasterizer supporting only OpenGL-1.1 running on the CPU.
Instead you should create a Framebuffer Object, attach a color renderbuffer attachment and when finished to a glReadPixels into your DIBSection. Much easier, much faster.
Update due to comment request
(I have no idea why StackOverflow doesn't get the syntax coloring right, i.e. figuring out where's a comment and where not)
// flushes the OpenGL error queue and
// counts the total number of errors
int flushGLErrors(void)
{
int i = 0;
while( glGetError() != GL_NO_ERROR ) {
i++;
}
return i;
}
// returns a HBITMAP or NULL.
// The HBITMAP must be freed using DeleteObject
HBITMAP ReadPixelsToHBITMAP(
int x,
int y,
int width,
int height )
{
void *pdata = NULL;
/* Note that this values only makes sense if we know a target
* output size like if we put the image to paper. */
const int physical_resolution = 2835; /* 72 DPI */
BITMAPINFOHEADER bmih = {
/* .biSize = */ sizeof(bmih),
/* .biWidth = */ width,
/* .bi.Height = */ height,
/* .biPlanes = */ 1, /* mandatory */
/* .biBitCount = */ 24, /* 8 bits per pixel */
/* .biCompression = */ BI_RGB, /* uncompressed */
/* .biSizeImage = */ 0, /* implicit */
/* .biXPelsPerMeter = */ physical_resolution, /* ignored */
/* .biYPelsPerMeter = */ physical_resolution, /* ignored */
/* .biClrUsed = */ 0, /* no palette */
/* .biClrImportant = */ 0
};
HBITMAP hbm = CreateDIBSection(
hdc, /* may be different than the DC used for OpenGL */
(PBITMAPINFO)&bmih, /* can do this cast, because no palette is used */
DIB_RGB_COLORS,
&pdata,
NULL,
0
);
if( !hbm ) {
return NULL;
}
flushGLErrors();
glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
if( glGetError() != GL_NO_ERROR ) {
DeleteObject(hbm);
return NULL;
}
glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, pdata);
if( glGetError() != GL_NO_ERROR ) {
DeleteObject(hbm);
return NULL;
}
return hbm;
}
Related
i'm sorry for what i did. i edited.
i'd like to use Fillrect on 32bit HBITMAP which is Created with CreateDIBSection
but i can't make rect visible in color that i want to.
(i Drawed a fillrect with CreateSolidBrush blue(RGB(0, 0, 255)) on 32bit HBITMAP(hdcbmp), but it doesn't appear blue.)
here is source code
is there anyway to show rect color that i want to?
sorry for my poor english.
void DrawAlphaBitmap(HWND hWnd, ULONG uWidth, ULONG uHeight)
{
BLENDFUNCTION bf;
HBITMAP hbitmap;
HBITMAP hOldBitmap;
BITMAPINFO bmi;
PVOID pvBits;
HDC hdcwnd = GetDC(hWnd);
HDC hdcbmp = CreateCompatibleDC(hdcwnd);
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = uWidth;
bmi.bmiHeader.biHeight = uHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 4;
hbitmap = CreateDIBSection(hdcbmp, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);
hOldBitmap = (HBITMAP)SelectObject(hdcbmp, hbitmap);
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 0xff;
bf.AlphaFormat = AC_SRC_ALPHA;
RECT rc2 = { 100, 100, 200, 200 };
FillRect(hdcbmp, &rc2, CreateSolidBrush(RGB(0, 0, 255)));
AlphaBlend(hdcwnd, 0, 0, uWidth, uHeight, hdcbmp, 0, 0, uWidth, uHeight, bf);
SelectObject(hdcbmp, hOldBitmap);
DeleteObject(hbitmap);
DeleteDC(hdcbmp);
ReleaseDC(hWnd, hdcwnd);
}
From documentation for BLENDFUNCTION:
AlphaFormat:
This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha).
In this case, alpha channel is not set. CreateDIBSection initializes the alpha values to zero. When AC_SRC_ALPHA is set, AlphaBlend ignores pixels whose alpha value is zero. Modify your code as follows:
//bf.AlphaFormat = AC_SRC_ALPHA; <- remove
bf.AlphaFormat = 0; //replace with 0
Side note, you have resource leak in creation of HBRUSH handle. Change the code to
HBRUSH hbrush = CreateSolidBrush(RGB(0, 0, 255));
RECT rc2 = { 0, 0, w, h };
FillRect(memdc, &rc2, hbrush);
DeleteObject(hbrush);
Ideally, your function prototype should be void DrawAlphaBitmap(HDC hdc, ULONG uWidth, ULONG uHeight); so that HDC can be passed directly, for example from BeginPaint/EndPaint in WM_PAINT message.
I'm trying to start experimenting with OpenGL and Windows GUI stuff. I'm running Windows 7 on my dev box and running Visual Studio 2013. When I run the below code, I don't receive any errors and all the of window information seems correct but nothing is drawn to the screen from OpenGL.
For a first program I was looking to put an overlay on top of Notepad, then draw a line/plus sign somewhere in the screen.
First I create a window:
myWindow = CreateWindowEx(
WS_EX_TRANSPARENT | WS_EX_TOPMOST | WS_EX_LAYERED,
WINDOW_NAME,
WINDOW_NAME,
WS_POPUP | WS_VISIBLE | WS_MAXIMIZE,
g_NotepadCoords.left,
g_NotepadCoords.top,
g_width,
g_height,
NULL, NULL, g_me, NULL);
The globals and coordinates for the window are calculated using FindWindowA and GetClientRect. Both of these function calls succeed and return reasonable values. After CreateWindowEx, I use the following function calls to set properties and check support, etc:
SetLayeredWindowAttributes(myWindow, RGB(0, 0, 0), 255, LWA_ALPHA | LWA_COLORKEY);
if (S_OK != DwmIsCompositionEnabled(&bIsEnabled)){...}
Right now HandleWindowMessage has WM_CREATE, WM_CLOSE, WM_DESTROY and WM_PAINT enabled. SetupOpenGL is called in WM_CREATE, and the drawing happens in WM_PAINT.
WM_CREATE and WM_PAINT:
case WM_CREATE:
{
printf("WM_CREATE\n");
g_Hdc = GetDC(hWnd);
SetupGL();
return 0;
}
break;
case WM_PAINT:
{
GLenum err;
const GLubyte *errString;
RGB colorToDraw = { 0 };
drawPlus(g_.middleX, g_.middleY, 12, 12, 4, &colorToDraw);
err = glGetError();
errString = gluErrorString(err);
printf("ERROR DRAWING? %s\n", errString);
}
break;
As far as I can tell I set up OpenGL correctly; no errors are printed:
void SetupOpenGL()
{
__try{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | // Format Must Support Window, OPENGL, Composition
PFD_SUPPORT_OPENGL |
PFD_SUPPORT_COMPOSITION |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, // RGBA
32,
0, 0, 0, 0, 0, 0,
8, // alpha
0, 0, 0, 0, 0, 0, 0,
8, // stencil Buffer
0,
PFD_MAIN_PLANE, // Main Drawing Layer
0, 0, 0, 0
};
PixelFormat = ChoosePixelFormat(g_Hdc, &pfd);
if (0 == PixelFormat)
{
printf("ChoosePixelFormat failed %d\n", GetLastError());
__leave;
}
if (!SetPixelFormat(g_Hdc, PixelFormat, &pfd))
{
printf("SetPixelFormat failed %d\n", GetLastError());
__leave;
}
if (!DescribePixelFormat(g_Hdc, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
{
printf("DescribePixelFormat failed %d\n", GetLastError());
__leave;
}
g_MainHGLRC = wglCreateContext(g_Hdc);
if (!g_MainHGLRC)
{
printf("wglCreateContext failed %d\n", GetLastError());
__leave;
}
if (!wglMakeCurrent(g_Hdc, g_MainHGLRC))
{
printf("wglMakeCurrent failed %d\n", GetLastError());
__leave;
}
glMatrixMode(GL_PROJECTION);
glClearColor(0.f, 0.f, 0.f, 0.f);
}
__finally{}
}
And then drawPlus is super simple:
void drawPlus(float x, float y, float width, float height, float lineWidth, RGB* RGB)
{
printf("drawPlus(%f,%f,%f,%f,%f,[%d,%d,%d])", x, y, width, height, lineWidth, RGB->red, RGB->green, RGB->blue);
glLineWidth(lineWidth);
glBegin(GL_LINES);
glColor4f(RGB->red, RGB->green, RGB->blue, 1);
glVertex2f(x, y + height/2);
glVertex2f(x, y - height/2);
glVertex2f(x + width/2, y);
glVertex2f(x - width/2, y);
glEnd();
}
When I run it, I don't receive any errors, I can see the executable creating another window and I've confirmed that drawPlus is getting called correctly and with good parameters. However, there's nothing actually drawn to the screen so I'm not sure what I'm missing. I just ran a quick check on the coordinates and for the Notepad windows elements I got:
top: 86
bottom: 916
left: 170
right: 609
And then for where to draw the plus I got
x = 219
y = 412
Which both seem reasonable so it's not my windows being mismatched or etc.
Please tell me if the question's vogue, I need the answe as soon as possible
for more information about the problem you can refer to this. I just didn't understand how to manage buffers properly.
A red rectangle drawn on 2D texture disappears right after being drawn
Being in the last stages of customizing the class COpenGLControl:
I have created two instances of the class in my MFC Dialog:
whenever the extent of zoom is changed in the bigger window, it is drawn as a red rectangle on the smaller window. which is always in full extent mode. In order to stablish such a relation between two instances, I have used the concept of user defined messages and send the message to the parent of the class.
The main trouble
based on the information above:
1- when I pan in the bigger window (mean I cause the user defined message be sent rapidly and m_oglWindow2.DrawRectangleOnTopOfTexture() be called rapidly I see a track of red rectangles shown but immediately disappeared in the smaller window
2- CPU-usage immediately get's high when panning from 3% to 25%
3- In other navigation tasks liked Fixed zoom in,Fixed zoom out,Pan and etc a red rectangle flashes and then immediately disappears, I mean it seems that the red rectangle is there just when the control is in the function m_oglWindow2.DrawRectangleOnTopOfTexture() but I want the rectangle be there until the next call off m_oglWindow2.DrawRectangleOnTopOfTexture()
4- making calls to glDrawBuffer(GL_FRONT_AND_BACK) and glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) causes the texture in the smaller window to get off and on even if the mouse is idle
I know the problem is most because of the lines glClear, glDrawBuffer, SwapBuffers in the following codes. But I don't know exactly how to solve
void COpenGLControl::OnTimer(UINT nIDEvent)
{
wglMakeCurrent(hdc,hrc);
switch (nIDEvent)
{
case 1:
{
// Clear color and depth buffer bits
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw OpenGL scene
oglDrawScene();
// Swap buffers
SwapBuffers(hdc);
break;
}
default:
break;
}
CWnd::OnTimer(nIDEvent);
wglMakeCurrent(NULL, NULL);
}
void COpenGLControl::DrawRectangleOnTopOfTexture()
{
wglMakeCurrent(hdc, hrc);
//glDrawBuffer(GL_FRONT_AND_BACK);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_ENABLE_BIT|GL_CURRENT_BIT);
glDisable(target);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(RectangleToDraw.at(0),RectangleToDraw.at(1));
glVertex2f(RectangleToDraw.at(0),RectangleToDraw.at(3));
glVertex2f(RectangleToDraw.at(2),RectangleToDraw.at(3));
glVertex2f(RectangleToDraw.at(2),RectangleToDraw.at(1));
glEnd();
glPopAttrib();
SwapBuffers(hdc);
wglMakeCurrent(NULL, NULL);
}
void COpenGLControl::OnDraw(CDC *pDC)
{
// TODO: Camera controls
wglMakeCurrent(hdc,hrc);
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,1,0);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glScalef(m_fZoom,m_fZoom,1.0);
wglMakeCurrent(NULL, NULL);
}
Remember:
OnDraw function is just called twice in the smaller window first when initializing the window and second when calling m_oglWindow2.ZoomToFullExtent() and then for each call of OnDraw in the bigger window, there's a call to the DrawRectangleOnTopOfTexture() in the smaller one but this function DrawRectangleOnTopOfTexture() is never called in the bigger window
It'll be favor of you if:
you correct my code
introduce me an excellent tutorial on how to use buffers in multiple
drawings that can not be done in a single function or a single thread (An excellent tutorial about buffers eg.color buffers and etc in opengl
------------------------------------------------------------------------------------------ -----------------------------------------------------------------------------------------
I just added explanations below to provide further information about how's the class is working if it's needed. If you think it's bothering viewers just edit it to remove which of the parts that you feel is not required. But please do help me.
the oglInitialize sets initial parameters for the scene:
void COpenGLControl::oglInitialize(void)
{
// Initial Setup:
//
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // bit depth
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24, // z-buffer depth
8,0,PFD_MAIN_PLANE, 0, 0, 0, 0,
};
// Get device context only once.
hdc = GetDC()->m_hDC;
// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, m_nPixelFormat, &pfd);
// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
// Basic Setup:
//
// Set color to use when clearing the background.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
// Turn on backface culling
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
// Turn on depth testing
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// Send draw request
OnDraw(NULL);
wglMakeCurrent(NULL, NULL);
}
example of a navigation task:
PAN:
void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (WantToPan)
{
if (m_fLastX < 0.0f && m_fLastY < 0.0f)
{
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;
}
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;
if (nFlags & MK_MBUTTON)
{
m_fPosX += (float)0.05f*m_fZoomInverse*diffX;
m_fPosY -= (float)0.05f*m_fZoomInverse*diffY;
}
if (WantToSetViewRectangle)
setViewRectangle();
OnDraw(NULL);
}
CWnd::OnMouseMove(nFlags, point);
}
the most important part: before calling OnDraw in each of the navigation functions if the client-programmer has set WantToSetViewRectangle as true means that he wants the view rectangle for the window to be calculated and calls setViewRectangle() which is as follows. It sends a message to the parent in case of an update for ViewRectangle:
void COpenGLControl::setViewRectangle()
{
CWnd *pParentOfClass = CWnd::GetParent();
ViewRectangle.at(0) = -m_fPosX - oglWindowWidth*m_fZoomInverse/2;
ViewRectangle.at(1) = -m_fPosY - oglWindowHeight*m_fZoomInverse/2;
ViewRectangle.at(2) = -m_fPosX + oglWindowWidth*m_fZoomInverse/2;
ViewRectangle.at(3) = -m_fPosY + oglWindowHeight*m_fZoomInverse/2;
bool is_equal = ViewRectangle == LastViewRectangle;
if (!is_equal)
pParentOfClass ->SendMessage(WM_RECTANGLECHANGED,0,0);
LastViewRectangle.at(0) = ViewRectangle.at(0);
LastViewRectangle.at(1) = ViewRectangle.at(1);
LastViewRectangle.at(2) = ViewRectangle.at(2);
LastViewRectangle.at(3) = ViewRectangle.at(3);
}
this is how we use the class in the client code:
MyOpenGLTestDlg.h
two instances of class:
COpenGLControl m_oglWindow;
COpenGLControl m_oglWindow2;
MyOpenGLTestDlg.cpp
apply texture on the windows and set both of them to full extent in the OnInitDlg
m_oglWindow.pImage = m_files.pRasterData;
m_oglWindow.setImageWidthHeightType(m_files.RasterXSize,m_files.RasterYSize,m_files.eType);
m_oglWindow.m_unpTimer = m_oglWindow.SetTimer(1, 1, 0);
m_oglWindow2.pImage = m_files.pRasterData;
m_oglWindow2.setImageWidthHeightType(m_files.RasterXSize,m_files.RasterYSize,m_files.eType);
m_oglWindow2.m_unpTimer = m_oglWindow2.SetTimer(1, 20, 0);
m_oglWindow2.ZoomToFullExtent();
m_oglWindow.ZoomToFullExtent();
want pan, zoomtool and setViewRectangle be active for the bigger window but not for the smaller one:
m_oglWindow.WantToPan = true;
m_oglWindow.WantToUseZoomTool = true;
m_oglWindow.WantToSetViewRectangle = true;
handling the user-defined message in the parent. exchange the ViewRectangle data to the smaller window and draw the red rectangle:
LRESULT CMyOpenGLTestDlg::OnRectangleChanged(WPARAM wParam,LPARAM lParam)
{
m_oglWindow2.RectangleToDraw = m_oglWindow.ViewRectangle;
m_oglWindow2.DrawRectangleOnTopOfTexture();
return 0;
}
Here's the full customized class if you're interested in downloading it and fix my problem.
The problem is that you're drawing on a timer and when your application receives a WM_PAINT message. MFC invokes your OnDraw (...) callback when it needs to repaint the window, you should move ALL of your drawing functionality into OnDraw (...) and call OnDraw (...) from your timer function.
void COpenGLControl::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case 1:
{
OnDraw (NULL);
break;
}
default:
break;
}
CWnd::OnTimer(nIDEvent);
}
void COpenGLControl::OnDraw(CDC *pDC)
{
// TODO: Camera controls
wglMakeCurrent(hdc,hrc);
// Clear color and depth buffer bits
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
gluLookAt (0,0,1,0,0,0,0,1,0);
glTranslatef (m_fPosX, m_fPosY, 0.0f);
glScalef (m_fZoom,m_fZoom,1.0);
// Draw OpenGL scene
oglDrawScene();
// Swap buffers
SwapBuffers(hdc);
wglMakeCurrent(NULL, NULL);
}
void COpenGLControl::DrawRectangleOnTopOfTexture()
{
glPushAttrib(GL_ENABLE_BIT|GL_CURRENT_BIT);
glDisable(target);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(RectangleToDraw.at(0),RectangleToDraw.at(1));
glVertex2f(RectangleToDraw.at(0),RectangleToDraw.at(3));
glVertex2f(RectangleToDraw.at(2),RectangleToDraw.at(3));
glVertex2f(RectangleToDraw.at(2),RectangleToDraw.at(1));
glEnd();
glPopAttrib();
}
And only ever make calls to wglMakeCurrent (...) within OnDraw (...). This function is really meant for situations where you're rendering into multiple render contexts, or drawing using multiple threads.
I'm looking to use OpenGl to blend 4 images to the screen. The first image contains the background and the other 3 are images contain some cartoon with transparency. My goal is to render those 4 images at once. At every call to render I'm updating the top 3 images with new images to compose the new frame.
I'm new to OpenGL and so far I'm able to achieve blending those images however I'm noticing some horrible issues when I render. I'm seeing some of the top 3 images are missing sometimes or some are rendered but look like they where cropped by the invisible man...
Each lines represents a different image.
Image cropped issue:
Image cropped and one image missing:
How it should look like:
Any help figuring out the issue I would greatly appreciate it! Below the code I'm using.
Here is the code I use to render the images.
void MoviePreview::prepareTexture (GLuint texture, GLint format, int w, int h)
{
// Bind to generated texture
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glShadeModel(GL_FLAT);
//glShadeModel(GL_SMOOTH);
glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, 0);
if (GL_RGBA == format)
{
//glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
// Crop the texture rectangle
GLint rect[] = {0, h, w, -h};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect);
glDrawTexiOES(0, 0, 0, w, h);
}
void MoviePreview::resize (int w, int h)
{
LOGI("native_gl_resize %d %d", w, h);
// init open gl
//glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_DITHER);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_FOG);
glDisable(GL_CULL_FACE);
glDisable(GL_STENCIL_TEST);
glDisable(GL_COLOR_LOGIC_OP);
glDisable(GL_ALPHA_TEST);
//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);//NEW
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//NEW
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
// Generate one texture object
glGenTextures(MAX_TEXTURES, mTexture);
check_gl_error("glGenTextures");
int frameHeight = h;//image.rows;
int frameWidth = w;//image.cols;
// first texture is our background texture
prepareTexture(mTexture[0], GL_RGBA, mBg.cols, mBg.rows);
prepareTexture(mTexture[1], GL_RGBA, frameWidth, frameHeight);
prepareTexture(mTexture[2], GL_RGBA, frameWidth, frameHeight);
prepareTexture(mTexture[3], GL_RGBA, frameWidth, frameHeight);
mSurfaceWidth = w;
mSurfaceHeight = h;
}
void MoviePreview::render (int64_t* imageIds, const int images)
{
int i = 0;
double sleepDuration = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// TODO try to see if we can just get away from always loading the bg
// since it doesn't change often might be worth loading it once and that
// is it...
//glBindTexture(GL_TEXTURE_2D, mTexture[0]);
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mBg.cols, mBg.rows, GL_RGBA, GL_UNSIGNED_BYTE, mBg.ptr());
//glDrawTexiOES(0, 0, 0, mSurfaceWidth, mSurfaceHeight);
// TODO pass the image batch loader
// load images
for (i=0; i<images; i++)
{
if (0 < imageIds[i])
{
sprintf(mTempPath, "%s/f1_%lld.png",mProjectPath.c_str(), imageIds[i]);
mImageLoader[i].loadImage(mTempPath);
}
}
if (0 < mFrameDuration)
{
// here we try to control the suggested frame rate
// set. We calculate since the last show image time
// if we should sleep or not...
if (0 < mLastDrawTimestamp) {
sleepDuration = mFrameDuration - (now_ms() - mLastDrawTimestamp);
}
if (0 < sleepDuration) {
usleep((long)sleepDuration*NANO_IN_MS);
}
}
// draw images
i = 0;
for (i=0; i<images; i++)
{
if (0 < imageIds[i])
{
cv::Mat img = mImageLoader[i].getImage();
if (!img.empty())
{
glBindTexture(GL_TEXTURE_2D, mTexture[i+1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, img.cols, img.rows, GL_RGBA, GL_UNSIGNED_BYTE, img.ptr());
glDrawTexiOES(0, 0, 0, img.cols, img.rows);
}
}
}
mLastDrawTimestamp = now_ms();
}
The issue ended up being with my image sources getting modified by another thread while OpenGl was trying to draw them...
I'm trying to use NDK to display a bitmap on a full screen. The requirement force me to develop with completely native code. I use Skia to draw a SkBitmap, then display it by Opengl APIs. When my application first time run on a real android device, it always works well. However, after I keep opening and closing the program for several times, it will show a bad image. Why does this happen?
Function engine_init_display is to initialize OpenGL ES and EGL, create bitmap and load texture.
static int engine_init_display(struct engine* engine){
// initialize OpenGL ES and EGL
/*
* Here specify the attributes of the desired configuration.
* Below, we select an EGLConfig with at least 8 bits per color
* component compatible with on-screen windows
*/
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_DEPTH_SIZE, 16,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
EGLint w, h, dummy, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(display, 0, 0);
/* Here, the application chooses the configuration it desires. In this
* sample, we have a very simplified selection process, where we pick
* the first EGLConfig that matches our criteria */
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
* As soon as we picked a EGLConfig, we can safely reconfigure the
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
context = eglCreateContext(display, config, NULL, NULL);
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
return -1;
}
eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
engine->display = display;
engine->context = context;
engine->surface = surface;
engine->width = w;
engine->height = h;
engine->state.angle = 0;
engine->format=format;
// Initialize GL state.
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
SkBitmap bitmap;
GLvoid* bitmapBuffer;
createBitmap(bitmap,bitmapBuffer,width,height);
drawBitmap(bitmap,width,height);
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&sTexture);
glBindTexture(GL_TEXTURE_2D,sTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)bitmapBuffer);
glDisable(GL_TEXTURE_2D);
glFinish();
clearBitmapBuffer(bitmap,bitmapBuffer);
//engine_draw_frame(engine);
return 0;
}
Function render is to display a bitmap
void render(struct engine* engine, int width, int height){
glViewport((engine->width-width)/2, (engine->height-height)/2, width, height);
glClearColorx((GLfixed)(0.1f * 65536),(GLfixed)(0.2f * 65536),(GLfixed)(0.3f * 65536), 0x10000);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D,sTexture);
glFrontFace(GL_CW);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoords);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glFlush();
eglSwapBuffers(engine->display, engine->surface);
}
And when the window is being closed, engine_term_display will be called
static void engine_term_display(struct engine* engine) {
if (engine->display != EGL_NO_DISPLAY) {
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (engine->context != EGL_NO_CONTEXT) {
eglDestroyContext(engine->display, engine->context);
}
if (engine->surface != EGL_NO_SURFACE) {
eglDestroySurface(engine->display, engine->surface);
}
eglTerminate(engine->display);
}
engine->display = EGL_NO_DISPLAY;
engine->context = EGL_NO_CONTEXT;
engine->surface = EGL_NO_SURFACE;
}
update
android_main is the main entry point of my native application. I find that when this main function has returned, the whole application is still running.
void android_main(struct android_app* state) {
struct engine engine;
// Make sure glue isn't stripped.
app_dummy();
memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;
state->onInputEvent = engine_handle_input;
engine.app = state;
ANativeActivity_setWindowFlags(engine.app->activity, AWINDOW_FLAG_FULLSCREEN, 0);
if (state->savedState != NULL) {
// We are starting with a previous saved state; restore from it.
engine.state = *(struct saved_state*)state->savedState;
}
while(1){
int ident;
int events;
struct android_poll_source* source;
if((ident=ALooper_pollAll(-1, NULL, &events,(void**)&source))>=0){
// Process this event.
if (source != NULL) {
source->process(state, source);
}
if (state->destroyRequested != 0) {
engine_term_display(&engine);
return;
}
}
}
}
I fix it by myself, use exit(0) instead of return to finish the activity completely.
if (state->destroyRequested != 0) {
engine_term_display(&engine);
exit(0);
}