Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border around texture, OpenGL

This is a code I use to draw rectangle in my program:

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, maxTexCoordHeight);              glVertex2i(pos.x, pos.y + height);
    glTexCoord2f(0.0f, 0.0f);                           glVertex2i(pos.x, pos.y);
    glTexCoord2f(maxTexCoordWidth, 0.0f);               glVertex2i(pos.x + width, pos.y);
    glTexCoord2f(maxTexCoordWidth, maxTexCoordHeight);  glVertex2i(pos.x + width, pos.y + height);
glEnd();

It draws just a simple rectangle with specified texture, e.g. like this: enter image description here

I'd like to ask if it's possible in OpenGL to achieve border effect like this:

As you see inside this tile there's just a plain blue background which could be handled separately - just automatically resized texture. This can be achieved easily with a code snippet I gave, but the problem is with border.

If the border was supposed to be one color, I could try drawing empty, not-filled rectangle by using GL_LINES around my texture, but it's not.

Also if tiles were always with a fixed size, I could prepare a texure that would match it, but they HAVE TO be easily resizable without changing a bitmap file I use as texture.

So if it's not possible with basic OpenGL functions, what are the approaches to achieve this effect that would be most efficient and/or easy?

EDIT: It has to be 2D.

like image 696
Piotr Chojnacki Avatar asked Dec 12 '12 13:12

Piotr Chojnacki


1 Answers

This is a classical problem of GUIs with OpenGL and is often solved using the 9-cell-pattern. In this, you add the effect to the original image (or define it by other opengl-parameters) and split the rendered quad in nine quads: three rows and three columns.

You then make the height of the upper and bottom row fixed, as you make the width of the left and the right column fixed. The center quad is scaled so that your object fits the rectangle you want to fit. You then map only the border parts of the texture to the quads forming the outer cells, while you map the center of the texture to the center quad.


Related to what was said in the comments, you could also use actual 3D effects by making the quad 3D. Noone forces you to use perspectivic projection in that case, you can stay with Orthogonal projection (2D-Mode). OpenGL will always do 3D-calculations anyways.

like image 82
Jonas Schäfer Avatar answered Oct 26 '22 19:10

Jonas Schäfer