Android 2D opengl line circle - android

I am new to open gl, and have been trying to do some basic 2d openGL in android. I am able to set up my 2D view, and draw squares and triangles. I am trying to draw a circle and am not exactly sure how to do it. I have found several techniques while searching, one using triangles rotated around the center w/ the given radius, this will not work as I do not want a filled circle. I also found other suggestions to do this with lines moving around the outer edge of the circle.
I have chosen to implement the latter. The issue I am having is with the IndexBuffer that is passed into glDrawElements, if my circle(lines) buffer has too many points, I am unable to create the byte array to create the IndexBuffer, as the maximum value for a byte is 127, any help or direction on how to do this would be appreciated.

Use an IntBuffer, that should allow you to use as many indices as you'll ever need.

Related

Line width limitation in opengl

I am trying to build an android application for display the geographic data(like google map) with opengl ,and it seems that opengl have a limitation of the line width when drawing lines:
gl.glLineWidth(10); //or gl.glLineWidth(20);
......
gl.glDrawArrays(GL10.GL_LINES, 0, 2);
And this is what I got:
It seems that this is the max width of a line rendered by opengl.
However when I see the google map, I found that it can render a much width line for a road like this:
What's is the problem?
BTW, I wonder if it is desirable to use a game engine in my application?
Yes, OpenGL implementations do have a limitation for line widths. I believe it varies by implementation. There are a few options, but the easiest one is to just draw a rectangle that is as tall as you need and extends from point A to point B. If you need a 50 point line width, for example, you can get the 4 corners of the rectangle by finding the vector from A to B and projecting + and - 25 points along the normal to that vector at the end points.

Drawing a 2d convex hull shape on a 3d terrain

I have a 3d mesh, which is a terrain. This runs perfectly fine btw, but I want to have shapes moving accross this terrain. These shapes are flat on the landscape and are blob-like: They can change shape and should follow the contoures and the heightmap of the terrain. These shapes can be painted on the landscape or flow over it, that doesn't matter.
The shapes are meant to be blocks of armies moving across the map, and this should be happening Real-Time! Also: they are 2d convex hull shapes. Also they are just one color with an alpha value (like blue with alpha 0.25f).
The only problem is: I can't figure out how to do this and the question is: Can anyone tell me how to do it?
My first thoughts were just to copy the terrain vertex matrix, push it up a bit so it will be on top of the terrain, load this buffer into a VBO and update the index buffer according to the position and shape needed and then draw the shape. This is rather slow and inefficient, especially when the shape is moving and changing. Also, the resolution of the heightmap is 175x175, so the movement is not at all smooth but rather jaggy.
Then I thought, but rather new to this area, update the shape outlines to the fragment shader of the terrain and let the shader decide if a point lies in that area and change color accordingly. This also was a really slow option, but if anyone sees potential and a good way to do this, tell me!
The next option was to draw directly onto the texture, which is still in the failing stage. If someone has any good ideas on how to draw a scene to a flat area and then put that on a terrain mesh, that would be great!
So if anyone has a solution to draw a shape (or multiple) on a terrain? That would be awesome. Thanks in advance!

scalability of an 2D game with the help of openGl and some general questions

i am planing to reengineer my prototype of a 2D role-plaing game to a "real product". In that case i think about using openGl instead of Android Canvas.
The reason why i think about it is because i want the game to work on devices with different screen resolutions. To do so i thought about using a Cameraview from openGl faced on an wall where my 2D gametextures are moving. If the resolution of the current device is to small for the whole game-content i want to move the cameraview so that the character is always at the middle till the cameraframe gets to the edges of the "wall".
Is this a possible solution for it or would you rather chose a different way?
Is it even possible to draw sprites in openGl as i can do with canvas? Simply like several Layers above each other. At first the tiles than the figures with for example simple squares as lifebars (first background than the red life above it) and so on.
positionRect = new Rect(this.getPositionX(), this.getPositionY()
- (this.spriteHeight - Config.BLOCKSIZE), this.getPositionX()
+ Config.BLOCKSIZE, this.getPositionY() + Config.BLOCKSIZE);
spritRect = new Rect(0, 0, Config.BLOCKSIZE, spriteHeight);
canvas.drawBitmap(this.picture, spritRect, positionRect, null);
If so how do i start with getting the first background and maybe first Dot(an .png picture)? I didnt find any tutorial what gives me the right kick off. I know how to sett up the projekt for an GLSurfaceView and so on.
You will need a bit of adjustments but it is possible and quite easy. Since you can get a tutorial and start some simple programs I will just give you some pointers:
First of all you should look into projections. You can use "glFrustumf" or "glOrthof" on the projection matrix. First one is used more for 3D so use Ortho. The parameters inside this method will represent the coordinate system borders of your screen. If you want them to be the same as most "view" systems insert values: top=0, left=0, right=view.width, right=view.height.
Now you can create a square buffer instead of rect as in
float[] buffer =
{origin.x, origin.y,
origin.x, origin.y+size.height,
origin.x+size.width, origin.y+size.height,
origin.x+size.width, origin.y,
};
And texture coordinates as
float[] textureCoordinates =
{.0, .0,
.0, 1,
1, 1,
1, .0,
};
You will also need to load the texture(s) (in some initialization and only once if possible) for witch use google or stack overflow since it depends on the platform...
And this is pretty much all you need to join it in your draw method:
enableClientState(vertexArray)
enableClientState(texCoordArray)
enable(texture2d)
//for each object:
vertexPointer(buffer)
texCoordPointer(textureCoordinates) //unless all are same
bindTexture(theTextureRepresentingTheSpriteYouWant)
draw(triangleStrip, 4)
As for moving use translate on the model matrix:
pushMatrix()
translatef(x, y, .0)
drawScene()
popMatrix()
When working with opengl, you aren't constrained to the "window" dimensions. You will define a projection matrix and viewport for your world, and then to change the "camera", you would just play with the projection matrix and viewport. If I were you, i'd pick up a book on opengl before starting this project so you are aware of how opengl works.
Also, if you are working with java then you will want to use the GLSurfaceView class. This handles all of the threading and everything for you, so you don't need to worry about it.

Drawing 2D curves in Android canvas?

How we can draw 2D curves like this in Canvas?
All similar curves have mathematical formula or are fractals, but the canvas only has some methods for drawing simple shapes like a triangle or rectangle. I know that most curves can divide into simpler shapes such as with drawing a Heart Curve, but is there an easier way to draw 2D curves with the canvas?
Using the Path object you can do some fancier lines and curves in the Canvas. Just randomly found these two questions concerning those; I hope they can point you in the right direction as I personally have not used them.
Draw a perfect curve connecting three points
Bezier curve and canvas
If you look up how to draw 2D curves in general you'll either be drawing points or lines and it only looks smooth.
Your question has two parametric forms and one fractal form. All can be drawn with lines and points. The parametric forms can be done directly via the algorithms in your question or could be transformed into more general ones like the Bezier curve as mentioned by #leenephi
Most of the time the equations/algorithms for generating fractals are using lines, Koch Snowflake, or points, the Mandelbrot set. If you actually understand how to generate them, you'll see that fractals are less about drawing 'a curve' and more about process (recursion) and results (self-similarity).
You have to approximate the curve by simpler primitives for example short line segments.

Texture mapping to triangle strip from atlas Opengl ES

I'm new to opengl-es on android and struggling to get my head around the concept of texturing.
I am looking to produce a tilemap of various difference textures. I understand that it is better to use an atlas of all the combined textures so I don't repeatedly rebind. However I am unsure quite how to then map these textures on to my tilemap.
I understand the process of specifiying vertices and then coordinates of where on the texture map I wish to take them from (i drew a picture too!)
Click for image - curse newbies not allowed to post images :(
But my question is can I draw a triangle strip that is, in effect, longer than one "tile" but map a different area of the texture to that "tile".
So instead of drawing a triangle strip pretending to be a quad, one at a time for each tile, can I somehow draw a whole row of the tilemap (like 1,2,3,4 and cleverly shift around the texture coordinates so each "tile" is now from a different area of the texture? So for example I draw a triangle strip 4 tiles long but shift the texture coordinates so the first "tile" is the yellow of my texture the second red ... third blue... etc
If I've not explained myself too well apologies!
It might just be that this is not possible and I have to draw each one individually which seems like I've saved effort with an atlas, then had to draw them all out slowly anyway regardless. Hmm.
Sure, just adjust texture coordinates, that is how texture atlases work.

Categories

Resources