[need another set of eyes] update rect slanting image

I’m having a real problem with my code. I’m working on rewriting gx to use opengl es. My setup for the graphics portion are as follows:

  1. 1 texture which simulates the screen buffer
  2. 2 arrays of unsigned short to hold the current screen buffer & the previous screen buffer

Well, it all works fine when I’m updating the entire screen ( when the current screen buffer is different from the old screen buffer from 0,0-max width,max height ) because I subteximage2d the screen buffer to the texture.

The angling occurs when I use the old screen buffer to hold the changed screen area and subteximage2d that buffer to the texture.

I guess I’m just asking anyone if I’m missing something blatantly obvious… I’ve gone through this for a couple of days now & I may just be overlooking something.

Note: glesClipRect is a RECT where top and left are “normal”, while bottom is the height of the clip rect.


int GXEndDraw() {
	char szText[255];

	frame++;

	gxLog("GXEndDraw:
");
	// glBindTexture(GL_TEXTURE_2D,glesTex);

	unsigned long curTime = GetTickCount() - firstTime;

	if ( ( curTime - m_lastTime ) > 1000 ) {
		sprintf(szText,"GXEndDraw: FPS: %d
",frame);
		gxLog(szText);
		m_lastTime = curTime;
		frame = 0;
	}
	glClear(GL_COLOR_BUFFER_BIT);

	// create a simple mark to show we are using ngx
	for ( int y=0;y<25;y++) {
		for ( int x=0;x<25;x++) {
			GXSetPixel(x,y,x,y,x);
		}
	}	

	RECT r;
	int x,y;
	
	GLushort * glnew;
	GLushort * glold;
	int lineheight = m_curProp.cbxPitch*m_curProp.cxWidth;
	r.left = lineheight;
	r.top = m_curProp.cyHeight;
	r.bottom = 0;
	r.right = 0;
	int width,height;

	if ( memcmp(glesPixels,glesOldPixels,nBufsize)) {
		// scene has changed
		int bot = glesClipRect.top + glesClipRect.bottom;
		if ( bot > m_curProp.cyHeight ) {
			bot = m_curProp.cyHeight;
		}
		for(y=glesClipRect.top;y<bot;y++) {
			for(x=0;x<m_curProp.cxWidth;x++) {
				glnew = (GLushort*)(glesPixels+(y*lineheight+x*m_curProp.cbxPitch));
				glold = (GLushort*)(glesOldPixels+(y*lineheight+x*m_curProp.cbxPitch));
				if ( *glnew != *glold ) {
					if ( y < r.top ) {
						r.top = y;
					} else if ( y > r.bottom ) {
						r.bottom = y;
					}
					if ( x < r.left ) {
						r.left = x;
					} else if ( x > r.right ) {
						r.right = x;
					}
				}
			}
		}
	
		if (( r.left <= glesClipRect.left ) &&
			( r.top <= glesClipRect.top ) &&
			( r.right >= m_curProp.cxWidth ) &&
			( r.bottom >= ( glesClipRect.bottom - glesClipRect.top ))) {
			// blit the whole screen
			glTexSubImage2D(
				GL_TEXTURE_2D,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
				0,
				0,
				glesClipRect.top,
				m_curProp.cxWidth,
				glesClipRect.bottom,
				GL_RGB,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
				GL_UNSIGNED_SHORT_5_6_5,
				glesPixels
			);
		} else {
			// blit only the changed parts
			if ( r.left > 0 ) {
				r.left--;
			};
			if ( r.right < m_curProp.cxWidth ) {
				r.right++;
			}
			if ( r.top > 0 ) {
				r.top--;
			}
			if ( r.bottom < m_curProp.cyHeight ) {
				r.bottom++;
			}
			sprintf(szText,"GXEndDraw: cliprect (%d,%d)-(%d,%d)
",
				r.left,
				r.top,
				r.right,
				r.bottom);
			gxLog(szText);

			width = r.right - r.left +1;
			height = r.bottom - r.top +1;
			
			//	copy changed area into temp pixel store
			for(y=r.top;y<=r.bottom;y++) {
				if ( width < 20 && height < 20 ) {
					sprintf(szText,"[W:%03d|H:%03d|O:%03d|P:%03d]
",
						width,
						height,
						(y-r.top),
						y);
					gxLog(szText);
				}
				for(x=r.left;x<=r.right;x++) {
					glnew = (GLushort*)(glesPixels+
						(y*lineheight+
						x*m_curProp.cbxPitch));
					glold = (GLushort*)(glesOldPixels+
						((y-r.top)*width*m_curProp.cbxPitch+
						(x-r.left)*m_curProp.cbxPitch));
					*glold = *glnew;
					if ( width < 20 && height < 20 ) {
						sprintf(szText,"[%03d|%03d|%04x]",
							(x-r.left),
							x,
							*glold);
						gxLog(szText);
					}
				}
				if ( width < 20 && height < 20 ) {
					gxLog("
");
				}
			}
			// blit changed zone
			glTexSubImage2D(
				GL_TEXTURE_2D,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
				0,
				r.left,
				r.top,
				width,
				height,
				GL_RGB,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
				GL_UNSIGNED_SHORT_5_6_5,
				glesOldPixels
			);
		}
	} else {
		/* 
		do nothing since scene has not changed
		*/	
	}
	GLuint error;
	
	error = glGetError();
	if ( error > 0 ) {
		wsprintf((LPTSTR)szText,L"Last Error: %x",error);
//		MessageBox(NULL,(LPCTSTR)szText,L"Error",MB_OK|MB_ICONERROR);
		SetLastError(error);
		return 0;
	}

	glDrawTexiOES(0,0,1,m_curProp.cxWidth,m_curProp.cyHeight);

	eglSwapBuffers(glesDisplay, glesSurface);

	return 1;
	// return gxEndDraw();			  
};

Difficult to say without seeing the picture, and the code was a bit too long for me to study carefully. However, here’s a guess: If the stride (number of pixels you need to skip between lines) is wrong, the resulting image may appear slanted.

Kari

Yeah, I noticed that the code tags really elongated my code. I was hoping it’d be something stupid on my part. lol. basically, what I’m trying to do is to only update the screen texture with the changed frame elements. The cliprect is calculated like so:


rect->left = max width
rect->top = max height
rect->right = 0
rect->bottom = 0

line width = max width * bytes per pixel
y=x=0

repeat y++
 repeat x++
  if old pixmap[x][y] != new pixmap[x][y]
   if x < rect->left
    rect->left = x
   else if x > rect->right
   rect->right = x
  endif

  if y < rect->top
    rect->top = y
  else if y > rect->bottom
   rect->bottom = y
  endif
 endif 
 until line width
until max height

rect now holds the clip rect. If the clip rect is the entire screen, then I blit the new pixmap into the texture and return.

Otherwise, I pad the rect 1 pixel in all directions. Next, I copy the updated cliprect region from the new pixmap into the old pixmap so I don’t allocate more space.


x = rect.left
y = rect.top
width = rect.right - rect.left + 1
height = rect.bottom - rect.top + 1
repeat y++ 
  repeat x++
    old pixmap[x-rect.left][x-rect.top] = new pixmap[x][y]
  until rect.right
until rect.bottom

Then I blit old pixmap using offsets rect.left, rect.top; width & height

That’s about it…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.