Move Primitives (Triangles/Rectangles) with OpenGL ES 2.0 - android

I'd like to create some kind of simple tetris clone with OpenGL ES 2.0 for education purposes. So far I managed to draw a simple rectangle made of two triangles on the screen.
I'd like to use those primitive rectangles as my tetris blocks.
Now, my problem is how to move those rectangles as they should fall down like tetris blocks.
This is how I define my rectangle:
...
public Rectangle()
{
_vertices = new float[]
{
// x, y, z
// R, G, B, A
-1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
InitBuffer();
}
...
This is the code that draws the rectangle:
private void drawRectangle(final FloatBuffer aRectangleBuffer)
{
aRectangleBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aRectangleBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
aRectangleBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aRectangleBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}
The code is basically copied from this tutorial:Learn OpenGL ES - Android Lesson One: Getting Started
The only way to move the rectangle I can think of is to change the vertices in my _vertices-array. But that would mean to create a new array, a new VertexBuffer etc. on every draw and I don't think that this is the way to go.
Perhaps this is a dump question but although I'm starting to understand how OpenGL ES works, this one I have not figured out yet.
Any help is really appreciated.

There are a number of ways to handle this. Which way is best depends on your goals. Since the game you're writing is relatively undemanding GPU-wise, it makes sense to start with something simple.
In the example you cited, every vertex is being multiplied by u_MVPMatrix. If you update the matrix with the position before each draw call, you can use the same set of vertices to draw the shape anywhere on the screen (and change its scale, and rotate it, and all the other fancy stuff matrices let you do). This is the approach used by Android Breakout and parts of Grafika (see e.g. the "Hardware scaler exerciser").
If you get to the point where this approach isn't efficient enough, it's probably time to look into game engines (perhaps cocos2d-x?) rather than reinventing the texture-mapped wheel.

Related

OpenGL ES: Texture a plane

I've gotten the texture to load, but it seemingly displays a single color of the texture. I've targeted the texture coordinates first, and can't seem to get the texture to display right. the intended texture is below:
256x256
The texture is being displayed, but seemingly in a solid color. Maybe zoomed in to one pixel of the texture, or a warp of bad texture coordinates.:
Geometry:
static float squareCoords[] = {
-0.1f, 0.1f, 0.0f,
-0.1f, -0.1f, 0.0f,
0.1f, -0.1f, 0.0f,
0.1f, 0.1f, 0.0f };
Texture coordinates:
final float TextureCoordinates[] =
{
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
};
The texturing process is encapsulated inside the Entity class and I could include more code if it isn't immediately obvious to a second pair of eyes.
On an unforeseeable note, GLES20.glEnableVertexAttribArray(mTextureUniformHandle); was not set.

Draw a line using OpenGL|ES in Android NDK using C++

I want to draw a line in my Android NDK app using OpenGL|ES.
I am using the following code to draw the line on the screen.
GLfloat line[] = {
0,0,0,
100,100,0
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glVertexPointer(3, GL_FLOAT, 0, line);
glColorPointer(4, GL_FLOAT, 0, colors);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_LINES, 0, 2);
glFlush();
The above code will paint a Line on the Screen, but the issue I am facing is that if I change the coordinates in the array line e.g.
GLfloat line[] = {
0,0,0,
5,5,0
};
then also same line will be drawn on the screen i.e. there will be no change in the length of the line. Output attched:
what is the reason for this abnormal behavior ?
Without modifying matrices the default OpenGL coordinates are normalized (mapped 0-1). (0,0) is the center of the screen, (1,0) is the center right side, (0, -1) is the bottom of the screen, etc.
In your example (5,5) and (100,100) are in exactly the same direction relative to the center and offscreen so that is why you see the same line in both.

Android Invert a Bitmap at Runtime

I am trying to invert a bitmap by using a Paint ColorFilter
I used this link as a reference:
http://www.mail-archive.com/android-developers#googlegroups.com/msg47520.html
but it has absolutely no effect - bitmap is drawn normally can you tell me what I'm doing incorrectly?
Define float array:
float invert [] = {
-1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f
};
Setup Paint in constructor
ColorMatrix cm = new ColorMatrix(invert);
invertPaint.setColorFilter(new ColorMatrixColorFilter(cm));
Reference in Draw() method
c.drawBitmap(Bitmap, null, Screen, invertPaint);
EDIT: I was able to get it to work by having the paint assignment in the draw statement:
ColorMatrix cm = new ColorMatrix(invert);
invertPaint.setColorFilter(new ColorMatrixColorFilter(cm));
c.drawBitmap(rm.getBitmap(DefaultKey), null, Screen, invertPaint);
but now it renders really slow (probably because its setting up a complicated matrix every single frame) ...is there a reason it works when it's in the same method?
EDIT2:
NEVERMIND!!! Lol, the issue was that I had two constructors and I was only configuring the colorfilter in one of them...the proccess is still very CPU intensive and causes framerate issues
This is an old thread.
However: The Matrix is not good for inversion of anti-aliased images with transparency.
Should be:
float invert[] =
{
-1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f
};

Android GL_FLOAT convert to GL_SHORT in an OpenGL ES vertex array

float coords[] = {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.1f, 2.3f, 0.0f,
}
i have a simple coord array in floats. How I can convert it to GL_SHORT?
so not this: gl.glVertexPointer(dimension, GL10.GL_FLOAT, 0, mVertexBuffer);
but: gl.glVertexPointer(dimension, GL10.GL_SHORT, 0, mVertexBuffer_short);
The numbers would be exactly the same (as in {0,0,0, 0,1,0, ...}). If you had fractions you'd have to apply a suitable scaling matrix at render time so that you could represent your points as integers.

Texture coordinates in opengl android showing image reversed

I wrote opengl android code to show a bitmap on a square. But bitmap was drawn in reverse. When i change texture array combination to the commented code it is drawn correctly. But i insist my texture array must be as below . Am i thinking wrong ?
/** The initial vertex definition */
private float vertices[] = {
-1.0f, 1.0f, 0.0f, //Top Left
-1.0f, -1.0f, 0.0f, //Bottom Left
1.0f, -1.0f, 0.0f, //Bottom Right
1.0f, 1.0f, 0.0f //Top Right
};
/** Our texture pointer */
private int[] textures = new int[1];
/** The initial texture coordinates (u, v) */
private float texture[] = {
//Mapping coordinates for the vertices
// 1.0f, 0.0f,
// 1.0f, 1.0f,
// 0.0f, 1.0f,
// 0.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
};
/** The initial indices definition */
private byte indices[] = {
//2 triangles
0,1,2, 2,3,0,
};
Whereas Android uses the top-left corner as being 0,0 of the coordinate system, OpenGL uses the bottom-left corner being 0,0 which is why your texture gets flipped.
A common solution to this is to flip your texture at load time,
Matrix flip = new Matrix();
flip.postScale(1f, -1f);
Bitmap bmp = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), flip, true);
Actually, I think Will Kru's solution should have flipped the background around both axes
flip.postScale(-1f, -1f);
That solution worked for me!
This worked for me. (no need to create another bitmap and scale)
private float vertices[] = {
1.0f, -1.0f, 0.0f, //Bottom Right
1.0f, 1.0f, 0.0f, //Top Right
-1.0f, 1.0f, 0.0f, //Top Left
-1.0f, -1.0f, 0.0f, //Bottom Left
};

Categories

Resources