Repeat texture on all surface without stretch jogl ES - android

i want repeat a texture automat without stretch , example , if have a texture 256x256 and one 512x512 , secound texture (512 x 512) is big like square and no repeat , but first need repet 4 time , but i want do automat , i know if i set param to texture like
{0,0 ,0,4,4,4,4,0}
first texture will repeat 4 time , but if change texture dimension need change again coord. Any suggestion ?

You can use glTexParameteri with GL_REPEAT, GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T.
You have to pass some texture coordinates from 0.0 to 2.0 so that the texture is repeated 4 times (2 * 2).

Related

Libgdx - textures

If I create a texture of .PNG 1024 x 1024 and draw a circle in the middle that is 124 x 124, around it would be empty, does it use the same amount of RAM as if I would draw a circle 124 x 124 on a 124x124 space?
I could make all sprites in photoshop on a 1920 x 1080, and lay it out there already and then import all in game at a 0, 0 coordinate and all would lay out perfectly!
Transparency has nothing to do with RAM usage - it depends on how big in decoded image it means that for picture 16x16 it will need 4 times more memory than for 8x8 no matter if it is transparent or not
Second thing is that by using pictures like this you will impact rendering performance because renderer will need to "iterate" over all pixels - also these transparent

Android OpenGL2.0 intersection between two textures

I'm making game in OpenGL2.0 and I want to check are two sprites have intersection but i don't need to check intersection between two rectangles.I have two sprites with texture,some part of texture is transparent,some not. I need to check intersection between sprites only on not trasnparent part.
Example: http://i.stack.imgur.com/ywGN5.png
The easiest way to determine intersection between two sprites is by Bounding Box method.
Object 1 Bounding Box:
vec3 min1 = {Xmin, Ymin, Zmin}
vec3 max1 = {Xmax, Ymax, Zmax}
Object 2 Bounding Box:
vec3 min2 = {Xmin, Ymin, Zmin}
vec3 max2 = {Xmax, Ymax, Zmax}
You must precompute the bounding box by traversing through the vertex buffer array for your sprites.
http://en.wikibooks.org/wiki/OpenGL_Programming/Bounding_box
Then during each render frame check if the bounding boxes overlap (compute on CPU).
a) First convert the Mins & Maxs to world space.
min1WorldSpace = modelViewMatrix * min1
b) Then check their overlap.
I need to check intersection between sprites only on not trasnparent part.
Checking this test case maybe complicated depending on your scene. You may have to segment your transparent sprites into a separate sprite and compute their bounding box.
In your example it looks like the transparent object is encapsulate inside an opaque object so it's easy. Just compute two bounding boxes.
I don't think there's a very elegant way of doing this with ES 2.0. ES 2.0 is a very minimal version of OpenGL, and you're starting to push the boundaries of what it can do. For example in ES 3.0, you could use queries, which would be very helpful in solving this nicely and efficiently.
What can be done in ES 2.0 is draw the sprites in a way so that only pixels in the intersection of the two end up producing color. This can be achieved with either using a stencil buffer, or with blending (see details below). But then you need to find out if any pixels were rendered, and there's no good mechanism in ES 2.0 that I can think of to do this. I believe you're pretty much stuck with reading back the result, using glReadPixels(), and then checking for non-black pixels on the CPU.
One idea I had to avoid reading back the whole image was to repeatedly downsample it until it reaches a size of 1x1. It would originally render to a texture, and then in each step, sample the current texture with linear sampling, rendering to a texture of half the size. I believe this would work, but I'm not sure if it would be more efficient than just reading back the whole image.
I won't provide full code for the proposed solution, but the outline looks like this. This is using blending for drawing only the pixels in the intersection.
Set up an FBO with an RGBA texture attached as a color buffer. The size does not necessarily have to be the same as your screen resolution. It just needs to be big enough to give you enough precision for your intersection.
Clear FBO with black clear color.
Render first sprite with only alpha output, and no blending.
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
glDisable(GL_BLEND);
// draw sprite 1
This leaves the alpha values of sprite 1 in the alpha of the framebuffer.
Render the second sprite with destination alpha blending. The transparent pixels will need to have black in their RGB components for this to work correctly. If that's not already the case, change the fragment shader to create pre-multiplied colors (multiply rgb of the output by a).
glColorMask(GL_TRUE GL_TRUE, GL_TRUE, GL_TRUE);
glBlendFunc(GL_DST_ALPHA, GL_ZERO);
glEnable(GL_BLEND);
// draw sprite 2
This renders sprite 2 with color output only where the alpha of sprite 1 was non-zero.
Read back the result using glReadPixels(). The region being read needs to cover at least the bounding box of the two sprites.
Add up all the RGB values of the pixels that were read.
There was overlap between the two sprites if the resulting color is not black.

OpenGL ES - Repeating same texture over different vertices

Suppose I have a polygon ( a lengthy rectangle ) made up of several triangles using GL_TRIANGLE_STRIP.
Now imagine I have a square texture that should be mapped against each of the small square that make up the rectangle. How do I accomplish this?
You need to repeat your texture along the geometry. For this, you need to setup your texture's GL_TEXTURE_WRAP_S to GL_REPEAT.
Then you can assign texture coordinates for your vertices :
(0,0), (0,1) for the 2 vertices on the first column
(1,0), (1,1) for the 2 vertices on the 2nd column
(2,0), (2,1) for 3rd column
and so on

how to use background image for texture in openGL

i want to use background image with texture but texture get images with power of 2 so i made image size (800x480 to 512x512).
but now it is showing image with some blank space.
how can i show image on entire screen. and also want to horizontally scrollable .
Well, if this is on Android, that means you don't have glDrawPixels, so the only way you could be "showing" an image would be to render a textured quad. So just make the quad whatever size you need.
You can also draw multiple textures, just by drawing one texture to one location, then drawing another texture to another location.
You need to use the right texture coordinates when rendering your background. You need to compute the texture coordinates so that you are having an offset for the axis perpendicular to the black borders.
I guess you resized your 800x480 image so that it fits in 512x512 pixels, making you end up with the actual background image (inside the 512x512 texture) being 512x307?
This would mean that your u texture coordinate offset would need to be (512.0 - 307.0) / 2.0 / 512.0 ~ 0.2, so your texture coordinates would need to be (0.0,0.0), (0.0,0.2), (1.0,0.2), (1.0,0.0).

How do I recursively draw a hex map in opengl?

I'm new to GL and wanted to create a tiled map as a self tuorial. I want to create a small (maybe 7 hexes wide / tall) hex map. My first thought was to just create a method to draw one hex and then just translate the appropriate offset and place the new hex. But this doesn't seem effcient. Any Idea's? Alos as a side question, how do I determine if a MotionEvent is with in the are of a given hex?
Extensive hex grid information.
To determine if a MotionEvent is within a certain hex you have to convert the coords passed in via the motion event to your OpenGL World coords. Its just like a unit conversion, you know the screen goes from 0 - WIDTH and your GL world lets say goes from -1 to 1.
(xCoord / (Width - 0)) * (1 - (-1)) = xCoordWorld
will give you the xCoord from 0 to 2, then subtract 1 to get it in -1 to 1.
As far as the hexes go I've always used 'art' hexes. Draw the hex out in paint then render a bunch of squares with that piece of art on them, fast and easy to swap a hex out for another hex.

Categories

Resources