rescaling areas

This is a discussion on rescaling areas within the C Programming forums, part of the General Programming Boards category; I'm making a gui, in which you can (basically) create windows, and areas. Areas are just 'areas' inside the window, ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    rescaling areas

    I'm making a gui, in which you can (basically) create windows, and areas. Areas are just 'areas' inside the window, in which you can draw. Each area is defined by its drawArea, which is calculated like this :

    Code:
    void getDrawArea(Area* a, Rect *area){
    	Window *win;
    
    	win = a->parentWindow;
    
    	area->x = a->bottomLeftPoint->x;
    	area->y = a->bottomLeftPoint->y;
    	area->w = a->topRightPoint->x - area->x;
    	area->h = a->topRightPoint->y - area->y;
    
    	if (a->drawEdges){
    		int w = a->edgeWidth;
    
    		area->x = area->x + w;
    		area->y = area->y + w;
    		area->w = area->w - 2*w;
    		area->h = area->h - 2*w;
    	}
    }
    The problem happens when I want to rescale the window. When the window is rescaled, a scalefactor is calculated like this :
    Code:
    getClientRectCoords(win, clientRect);
    scalex = (double)(clientRect->w) / (double)(clientRectOrig->w);
    scaley = (double)(clientRect->h) / (double)(clientRectOrig->h);
    When I rescale the window (and so also the areas), I want the width of the areaEdges to remain the same, so I replace the getDrawArea() function by this :
    Code:
    void getDrawArea(Area*a, Rect *area){
    	Window *win;
    
    	win = a->parentWindow;
    
    	area->x = a->bottomLeftPoint->x * scalex;
    	area->y = a->bottomLeftPoint->y * scaley;
    	area->w = a->topRightPoint->x - area->x * scalex;
    	area->h = a->topRightPoint->y - area->y * scaley;
    
    	if (a->drawEdges){
    		int w = a->edgeWidth;
    
    		area->x = area->x + w;
    		area->y = area->y + w;
    		area->w = area->w - 2*w;
    		area->h = area->h - 2*w;
    	}
    }
    Now the problem is that there are sometimes small spaces between the drawn areas. It looks like a rounding error, but I don't know how it is caused. Can someone help me on this ?

    An example of a wrongly resized window can be found here :
    original : http://users.pandora.be/willem.packet1/original.jpg
    wrongly resized : http://users.pandora.be/willem.packet1/resized.jpg
    The black stripes is where it went wrong.

    In case it is needed, here is a part of the drawing routine :
    Code:
    void drawAreaDefault(int areaHandle){
    	Area*a;
    	Window *win;
    	Color *c;
    	Rect area;
    	int x, y, w, h;
    
    	a = getAreaFace(areaHandle);
    	win = a->parentWindow;
    
    	getArea(a, &area);
    	x = area.x;
    	y = area.y;
    	w = area.w;
    	h = area.h;
    	c = a->backGroundColor;
    
    	glViewport(x, y, w, h);
    	glScissor(x, y, w, h);
    
    	glMatrixMode (GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(0, w, 0, h, -1, 1);
    
    	glMatrixMode (GL_MODELVIEW);
    	glLoadIdentity();
    
    	glEnable(GL_BLEND);
    	glColor4f(c->r, c->g, c->b, c->a);
    	glBegin(GL_QUADS);
    		glVertex2i(0, 0);
    		glVertex2i(0, h);
    		glVertex2i(w, h);
    		glVertex2i(w, 0);
    	glEnd();
    	glDisable(GL_BLEND);
    }
    
    void drawAreaEdgesDefault(int areaHandle){
    	Area*a;
    	Window *win;
    	Color *c;
    	Rect area;
    	int x, y, w, h, width;
    
    	a = getArea(areaHandle);
    	win = a->parentWindow;
    
    	c = a->parent->edgeColor;
    
    	width = a->edgeWidth;
    
    	getDrawArea(a, &area);
    	x = area.x - width;
    	y = area.y - width;
    	w = area.w + width + width;
    	h = area.h + width + width;
    
    	glViewport(x, y, w, h);
    	glScissor(x, y, w, h);
    
    	glMatrixMode (GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(0, w, 0, h, -1, 1);
    
    	glMatrixMode (GL_MODELVIEW);
    	glLoadIdentity();
    
    	//draws the edges as 4 rectangles
    	glColor3f(c->r, c->g, c->b);
    	glBegin(GL_QUADS);
    		glVertex2i(0, 0);
    		glVertex2i(0, h);
    		glVertex2i(width, h);
    		glVertex2i(width, 0);
    
    		glVertex2i(w - width, 0);
    		glVertex2i(w - width, h);
    		glVertex2i(w, h);
    		glVertex2i(w, 0);
    
    		glVertex2i(0, 0);
    		glVertex2i(0, width);
    		glVertex2i(w, width);
    		glVertex2i(w, 0);
    
    		glVertex2i(0, h - width);
    		glVertex2i(0, h);
    		glVertex2i(w, h);
    		glVertex2i(w, h - width);
    	glEnd();
    
    	//to give the edges a more '3d look'
    	glLineWidth(1.0f);
    	glBegin(GL_LINES);
    		glColor3f(c->r + 0.1f, c->g + 0.1f, c->b + 0.1f);
    		glVertex2i(width - 1, width - 1);
    		glVertex2i(width - 1, h - width + 1);
    		glVertex2i(width - 1, width - 1);
    		glVertex2i(w - width + 1, width - 1);
    
    		glColor3f(c->r - 0.1f, c->g - 0.1f, c->b - 0.1f);
    		glVertex2i(w - width, width - 1);
    		glVertex2i(w - width, h - width + 1);
    		glVertex2i(width - 1, h - width);
    		glVertex2i(w - width + 1, h - width);
    	glEnd();
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You might want to try posting this in the Game Programming forum (or asking a moderator to move it there).

    I don't know much OpenGL, but I do know that you will get rounding errors if you just pass a double to a function that expects an int. Try this rounding code:
    Code:
    void function(int x);
    
    double d = something;
    function((int)(d+.5));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >scalex = (double)(clientRect->w) / (double)(clientRectOrig->w);
    >scaley = (double)(clientRect->h) / (double)(clientRectOrig->h);
    Are scalex and scaley declared as doubles?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inputting-Outputting specific areas of a file
    By Todd88 in forum C++ Programming
    Replies: 19
    Last Post: 09-10-2008, 07:51 PM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  3. Replies: 3
    Last Post: 12-20-2006, 03:53 PM
  4. cool text areas
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-14-2002, 02:27 PM
  5. volumes and areas
    By oscuro in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 09:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21