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.
Related
I have a vertex (Which i will not be showing/rendering in the scene)
float vertex[] = {
1.0f, 1.0f, 1.0f,
};
And i have a mesh, which i have translated and rotated using:
Matrix.translateM(World.mModelMatrix, tmOffset, globalPositionX, globalPositionY, globalPositionZ);
Matrix.rotateM(World.mModelMatrix, rmOffset, globalRotationZ, 0, 0, 1);
Matrix.rotateM(World.mModelMatrix, rmOffset, globalRotationY, 0, 1, 0);
Matrix.rotateM(World.mModelMatrix, rmOffset, globalRotationX, 1, 0, 0);
How can apply those translations and rotations to the vertex, and get its global position (x,y,z) after?
Use the Matrix.multiplyMV method:
float vertex[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float result[] = { 0.0f, 0.0f, 0.0f, 0.0f };
Matrix.multiplyMV(result, 0, matrix, 0, vertex, 0);
Note, that you will have to add a homogeneous coordinate to your vector to make it work.
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.
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.
I am trying to make a game and I need my startship(triangle) to be rotated around its own center. (It should looks like you just put a paper triangle on a table and rotating it)
The coords of my triangle is
public float coords[] = {
0.0f, 0.1f, 0.0f,
-0.05f, -0.1f, 0.0f,
0.05f, -0.1f, 0.0f,
// barrier, ignore this coords
-0.039f, -0.05f, 0.0f,
0.039f, -0.05f, 0.0f,
};
The triangle right in the center of axis. Right?
I am trying to rotate it with
Matrix.rotateM(rotateMatrix, 0, rotateZ, 0.0f, 0.0f, 1.0f);
But it looks like this:
And that's not what I wanted. Please, give me an advice what to do to make my triangle rotating looks just like a paper one on a table.
Edit: there is no projection matrix and translate matrix is 0 by x,y and z.
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.