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
Related
How to draw a circular line circle like this image?
How to achieve or any suggestions?
Not essential, but I'd like to draw it on Android.
You can draw this by simply drawing colored rectangles. Either create a method to draw a rect and call it for each of the rectangles or create a buffer holding all the vertex data and call it once.
For the first procedure the easiest way is using matrices. Assume you are drawing a rectangle at angle of height and width with some inner circle radius. Now all you need is a single rect vertex buffer as:
{
0.0, -0.5,
0.0, 0.5,
1.0, -0.5,
1.0, 0.5
}
This represents a 1x1 cube which is positioned perfectly to draw a line. Now to use matrices we want to identity which would draw the line in the center of your screen. Then rotate it to whatever angle you need, then move it forward so it is outside of the inner circle and in the end scale it to whatever size you need:
Matrix4x4 matrix = Matrix4x4.identity // Start with identity
matrix = matrix.rotate(angle, 0, 0, 1) // Rotate around Z
matrix = matrix.translate(radius, 0, 0) // Translate it forward, this is not a bug
matrix = matrix.scale(height, width, 1.0) // Scale it
This should be it. Now if you want to add some performance on drawing you should have a single draw call so you create a single buffer. You may still use exactly the same procedure but transform original vertex data by a target matrix on CPU and then pack the result into array.
I am not sure from your image if the bottom (inner) part of lines needs to be circular. To achieve this the easiest way would be to draw to alpha mask first and then use it in blending. To achieve this you need to clear color to (1,1,1,1). Then draw a circle only on alpha channel using color mask (FALSE, FALSE, FALSE, TRUE) and blending (ONE, ONE) then using a any transparent color like (0,0,0,0). This will make your background completely white but there will be a circle in the middle with zero alpha. So now reset back to default mask settings using (TRUE, TRUE, TRUE, TRUE) and start drawing your rectangles with blending (DST_ALPHA, ONSE_MINUS_DST_ALPHA). This should cut your rectangles nicely to show a correct circle in the middle.
Some more robust procedures exist for the same thing like drawing to stencil buffer instead of alpha channel.
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).
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.
I read the dimensions of a cube from a file (x,y,z) and create opengl vertex array with equally spaced points. I am able to display the points as a 3d point cube of dimension (x,y,z). However, I want to display small cubes instead of points so that the output will look like a grid of cubes of dimension x*Y*Z instead of 3d points. How can I achieve this in android openges1.0 in java?
Thanks.
You should create an index buffer which lists the order of vertices (from your vertex buffer) to visit, and then draw them as triangles (or ideally a triangle strip).
For instance, if you have four vertices, top left, top right, bottom left, bottom right, in that order, your index buffer will be something like [0, 1, 3, 2] to wind the vertices clockwise.
Since the vertices are equally spaced and axis-aligned, it should not be too hard to write a loop which will generate the appropriate index buffer for you.
I'm new in openGL ES. I read simple tutorials but i have problem with setup Ortho and draw screen size square. Could you provide simple code as example ?
A nice usage of the orthographic projection is that you can specify how big influence a single float will have in your application when you're setting your vertex data.
For example, consider this explanation:
float right = 5.0f
float top = 4.0f
glOrthof(0.0f, right, 0.0f, top);
In the image above; an object that has a quad (built from triangles) whose size is equal to 1 also corresponds to one of the squares.
Shortly, it doesn't matter which values you pass into the glOrtho() height and right arguments as long as you are setting correct float values in your vertices, which should correspond to one square.