iDevGames Forums
texture no square surface - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: texture no square surface (/thread-1581.html)



texture no square surface - kendric - Mar 20, 2009 09:11 AM

In opengl es I would like to use a texture, but only have a portion of it drawn.
For example I have a picture of a beach ball and I want to draw only x degrees of it as an arc. Can you fill a non rectangle shape with a rectangluar source image without distorting the image? If this isn't clear, consider the pacman character. He is a circle but not a 360 degree one. He stops short and is really like a 270 degree circle.


texture no square surface - backslash - Mar 20, 2009 11:57 AM

The short answer is yes.
Depending on what exactly you are hoping to achieve you should investigate how texture mapping works and how blending modes work. In the pacman example you give, I would draw him in a square polygon and use alpha to leave the blank space around him. If you draw a triangle it takes a triangular section out of your texture using the three coordinates you give it. Many triangles can be assembled into any shape you like.


texture no square surface - kendric - Mar 20, 2009 12:51 PM

I can't use that option. This is for dynamic arcs. So a picture of pacman, draw arc from 100 degrees to 180 degrees and you would see a yellow pie slice show up. Does that make sense? Unless i could make my texture co-ords be in the arc shape?


texture no square surface - JustinFic - Mar 20, 2009 01:03 PM

kendric Wrote:I can't use that option. This is for dynamic arcs. So a picture of pacman, draw arc from 100 degrees to 180 degrees and you would see a yellow pie slice show up. Does that make sense? Unless i could make my texture co-ords be in the arc shape?

What you want is gluPartialDisk. The spec is here:

http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/partialdisk.html

And a tutorial is here:

[url="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18"]
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18[/url]

If you don't have access to glu, (like if you're on the iPhone) you can have your circular shape be centered in a texture. Then, when drawing your arc, you could set up a triangle fan from the center of your shape outward. I'd just use sin and cos to get the coords in world space. Similarly, you can use sin and cos to get your texture coordinates (which, in your texture, would contain your circle shape. So the points making a circle of radius 0.5 centered at texture coordinate 0.5, 0.5.)


texture no square surface - AnotherJake - Mar 20, 2009 01:10 PM

Sounds like you're going to have some serious pinching problems near the center. It's hard to compensate for that distortion accurately. Paul Bourke had a good page on this as it relates to sphere mapping. I had a link bookmarked but he apparently changed the site structure. I think this was the page I started with. You can dig around his site for a bunch more related spherical stuff, but the idea I'd roll with is to come up with a way of (for lack of a better term) "anti-pinching" the texture where you map it on the center of your triangle fan.


texture no square surface - kendric - Mar 20, 2009 03:17 PM

Well the pinching thing may be a problem, but I would like to see what this looks like. I tried what was suggested and have this:
-(void) drawGameArcAt:(CGPoint)center radius:(float)radius startArc:(float)startArc endArc:(float)endArc fillIn:(bool)fillIn
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
// Set a blending function to use
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Enable blending
glEnable(GL_BLEND);
//Set the texture to use
Texture *texture=[textures objectAtIndex:1%frames];
[texture bind];
if (endArc < startArc)
{
float temp = endArc;
endArc = startArc;
startArc = temp;
}

glColor4f(1,1,1,1);
//2 for the remainder when we draw the last arc
//2 for the start point since its a triangle fan
int numVertices=(int)(2+2.0f*(endArc-startArc)/ARC_SEGMENT)+2;
GLfloat spriteVertices[numVertices];
//GLshort textureVertices[] =
// {
// 0, 1,
// 1, 1,
// 0, 0,
// 1, 0,
// };
GLfloat textureVertices[numVertices];
float theta;
int i=0;
spriteVertices[0]=center.x;
spriteVertices[1]=center.y;
textureVertices[0]=0.5;
textureVertices[1]=0.5;
i+=2;
float radius2=radius*2;
for (theta = startArc; theta < endArc; theta += ARC_SEGMENT)
{
float x=sinf(theta)*radius;
float y=cosf(theta)*radius;
textureVertices[i]=(radius+x)/radius2;
textureVertices[i+1]=(radius+y)/radius2;
spriteVertices[i]=center.x+x;
spriteVertices[i+1]=center.y+y;
i+=2;
}
if(theta>endArc)
{
float x = (float) sin(endArc) * radius;
float y = (float) cos(endArc) * radius;
textureVertices[i]=(radius+x)/radius2;
textureVertices[i+1]=(radius+y)/radius2;
spriteVertices[i]=center.x+x;
spriteVertices[i+1]=center.y+y;
i+=2;
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer (2, GL_FLOAT , 0, spriteVertices);

glTexCoordPointer(2, GL_FLOAT, 0, textureVertices);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays (GL_TRIANGLE_STRIP, 0, i/2);

}

Needless to say it didn't work. I see a semi circle of dots, with a fater triangle on the left side.

I will keep looking at this, if anyone else has a suggestion though please do Smile
P.S The semi circle part is correct, i was rendering only half a circle in my test.


texture no square surface - kendric - Mar 20, 2009 03:34 PM

I changed my sprite image and I see a definite effect. The color changed and i see one of the triangle arcs only. So its doing something. If i turn off textures, it looks the same with just white so I am prob doing something wrong in the actual strip and nothing to do with the texture itself.


texture no square surface - kendric - Mar 20, 2009 05:26 PM

Yeah!. I was using strip instead of triangle fan. Switched that and it worked. No texture pinch either. Looks exactly like what I wanted.