Android and glOrthof - android

I am developing a simple tile world game for Android 2.3. I am trying to set up an orthographic view in opengl but my 1x1 tiles show up oblong -- oriented to the direction of the screen -- if the screen is in vertical position then the square is stretched vertically, if horizontal then it is stretched horizontally. Here is what I have in my onSurfaceChanged method:
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, 1.0f, 100.0f);
In my onDrawFrame method I have:
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
GLU.gluLookAt(gl,
0.0f, 0.0f, 6.0f, // eye translation
0.0f, 0.0f, 0.0f, // eye center
0.0f, 1.0f, 0.0f // eye world up
);
// draw objects
From the object onDraw method:
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFloatVertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glPushMatrix();
gl.glTranslatef(localTranslation.getX(), localTranslation.getY(), localTranslation.getZ());
gl.glScalef(localScale.getX(), localScale.getY(), localScale.getZ());
gl.glRotatef(mAngle, localRotation.getX(), localRotation.getY(), localRotation.getZ());
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glNormal3f(0.0f, 0.0f, -1.0f);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
I have used the same onSurfaceCreated, onDrawFrame, and onDraw methods with perspective mode and it worked without any distortion to the objects being drawn. I have tried a few different things pulled from the net (like replacing the right/bottom of the ortho call with width/height aspect ratio) but nothing seemed to correct the problem.
Here is what the screen looks like:
Any ideas? What am I missing? All that I want to see are perfect 1x1 squares that I can throw textures on without distortion.

Try this
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, 1.0f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

Ok this setting is what i use for both iOS and Android but when the screen ratio differs then positions of objects change.
glViewport(0, 0, backingWidth, backingHeight);
glOrthof(-1.0, //LEFT
1.0, //RIGHT
-1.0 * backingHeight / backingWidth, //BOTTOM
1.0 * backingHeight / backingWidth, //TOP
-2.0, //NEAR
100.0); //FAR
NOTE: That this works for normalised coordinates, which is what i use for all of my models.

Related

glRotatef not updating

I want to rotate a Quad along 360 degrees with the code below). I am varying "input" on another procedure (Range = 0 to 1).
Despite "input" changing correctly, I can not seem to update rotation of a quad on screen - It remains stuck at the first angle, e.g. 180 if input is 0.5.
#Override
public void onDrawFrame(GL10 gl) {
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, width, height, 0);
gl.glClearColor(0.f, 0.f, 0.f, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(1f, 1f, 1f, 1f);
gl.glPushMatrix();
gl.glTranslatef(width/2, height/2, 0.0f);
gl.glRotatef(360 * input, 0.0f, 0.0f, 1.0f);
gl.glPopMatrix();
bgQuad.setX(0);
bgQuad.setY(0);
bgQuad.draw(gl);
}
Any suggestions?
move popMatrix to after bgQuad.draw(gl);:
#Override
public void onDrawFrame(GL10 gl) {
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, width, height, 0);
gl.glClearColor(0.f, 0.f, 0.f, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(1f, 1f, 1f, 1f);
gl.glPushMatrix();
gl.glTranslatef(width/2, height/2, 0.0f);
gl.glRotatef(360 * input, 0.0f, 0.0f, 1.0f);
bgQuad.setX(0);
bgQuad.setY(0);
bgQuad.draw(gl);
gl.glPopMatrix();
}
glPopMatrix resets the matrix back to the most recent saved matrix and throws away the current matrix.

OpenGl drag square

I'm creating a curtain like animation triggered by onTouchEvent() where u can drag one end of a square to make it bigger or smaller.
My only problem is that instead of having a square on the entire screen, I get a small line on the top of the screen and i can expand and de-expand that line.
Why won't this code draw a square?
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
gl.glLoadIdentity(); // reset the matrix to its default state
gl.glOrthof(0, height, width, 0, -3, 8);
}
Vertices:
private float vertices[] = {
-1.0f, 1.0f, 0.0f, // 0, Top Left
-1.0f, -1.0f, 0.0f, // 1, Bottom Left
1.0f, -1.0f, 0.0f, // 2, Bottom Right
1.0f, 1.0f, 0.0f, // 3, Top Right
};
// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };
And the draw method in Square:
public void draw(GL10 gl,float x,float y) {
// Counter-clockwise winding.
gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
//Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
//Enable vertex buffer
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
//Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
You are missing this? after setting the projection mode.
gl.glMatrixMode(GL10.GL_MODELVIEW); // set modelview matrix to identity.
gl.glLoadIdentity();

Object not draw properly in openGl Es

I am drawing the 3D object but its not drawing correctly. Original 3D file does not have diagonally but When I am drawing then its showing the diagonal. Please help me to why its drawing the diagonal.
See this link : http://i.stack.imgur.com/Q4plC.png
Code
public void draw(GL10 gl) {
//gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(3, 0, v.size()/3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
model.loadGLTexture(gl, context);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glShadeModel(GL10.GL_SMOOTH);
}
/**
* Here we do our drawing
*/
public void onDrawFrame(GL10 gl) {
//Clear Screen And Depth Buffer
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, 0); //Move down 1.2 Unit And Into The Screen 6.0
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); //X
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); //Y
model.draw(gl); //Draw the square
xrot += xspeed;
yrot += yspeed;
}
/**
* If the surface changes, reset the view
*/
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
gl.glViewport(0, 0, width, height); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, width/height, 0.1f, 500.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
Thanks in advance.
Check the exact order of the vertices in vertexBuffer.
If the order jumped over the diagonal way on the rectangle, it will draw the diagonal line.

Convert canvas coordinate system to OpenGLES 2.0 coordinates

I started drawing hexagonal grid on canvas with Path object. I did all the calculations on how many hexagons go on a particular display, depending on the hexagon size.
I have all my hexagon coordinates calculated depending on canvas. But now, I have serious performance issues and have to port this to OpenGL.
Because the algorithm works in canvas, I'm trying to convert those "canvas" hexagon coordinates to OpenGL coordinate system with GLU.gluUnProject.
"for loop"
float near[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLU.gluUnProject(b.hexes[ii][jj].points[ii].x,
b.hexes[ii][jj].points[ii].y, 0, mg.mModelView, 0,
mg.mProjection, 0, viewport, 0, near, 0);
vertices[zz] = (float) ((near[0])/near[3]);
zz++;
vertices[zz] = (float) ((near[1])/near[3]);
zz++;
vertices[zz] = 0;
Because I lack opengl knowledge, I dont know how to set glViewport,gluPerspective,glTranslatef to 2D world that is "the same" as canvas.
So my question is:
How to set those three thing that my first hexagon (on canvas, the first one is top left) to be equal on openGL drawing surface world?
Update
Thank you all for you're interest of helping on my problem. But:
I now set my 18 float vertices array as follows:
[20.0, 0.0, 0.0, 60.0, 0.0, 0.0, 80.0, 34.641018, 0.0, 60.0, 69.282036, 0.0, 20.0, 69.282036, 0.0, 0.0, 34.641018, 0.0]
Z is always 0.
ByteBuffer vertexByteBuffer = ByteBuffer
.allocateDirect(vertices.length * 4);
vertexByteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = vertexByteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
and draw:
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, vertices.length/3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
Before draw I set:
onDrawFrame()
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
onSurfaceChanged()
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, width, height, 0, -1, 1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
You have no need to transform yourself the vertices coordinates. All you have to do is to provide the correct projection matrix to OpenGL.
Basically, this would be something along the lines of :
glViewport(0, 0, canvasWidth, canvasHeight);
glMatrixMode(GL_PROJECTION); // or some matrix uniform if using shaders
glLoadIdentity();
glOrtho(0, canvasWidth, canvasHeight, 0, -1, 1); // this will allow to pass vertices in 'canvas pixel' coordinates
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Depending on the order in which you're passing your vertices, you might want to make that culling is disabled.

Android Opengl GLU.gluLookAt doesn't work

I'm new to OpenGL and I can't figure out how to use gluLookAt. Below is my source -- Any help will be much appreciated.
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbientBuffer); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuseBuffer); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer);
gl.glEnable(GL10.GL_LIGHT0);
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
gl.glDisable(GL10.GL_DITHER);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
cube.loadGLTexture(gl, this.context);
}
public void onDrawFrame(GL10 gl) {
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
GLU.gluLookAt(gl, xrot, yrot, 0.0f, 0.0f, xrot, yrot, 0.0f, 0.0f, 0.0f);
//Clear Screen And Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); //Reset The Current Modelview Matrix
//Check if the light flag has been set to enable/disable lighting
if(light) {
gl.glEnable(GL10.GL_LIGHTING);
} else {
gl.glDisable(GL10.GL_LIGHTING);
}
//Check if the blend flag has been set to enable/disable blending
if(blend) {
gl.glEnable(GL10.GL_BLEND); //Turn Blending On ( NEW )
gl.glDisable(GL10.GL_DEPTH_TEST); //Turn Depth Testing Off ( NEW )
} else {
gl.glDisable(GL10.GL_BLEND); //Turn Blending On ( NEW )
gl.glEnable(GL10.GL_DEPTH_TEST); //Turn Depth Testing Off ( NEW )
}
//Drawing
gl.glTranslatef(0.0f, 0.0f, z); //Move z units into the screen
gl.glScalef(0.8f, 0.8f, 0.8f); //Scale the Cube to 80 percent, otherwise it would be too large for the screen
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); //X
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); //Y
cube.draw(gl, filter); //Draw the Cube
//Change rotation factors
xrot += xspeed;
yrot += yspeed;
}
/**
* If the surface changes, reset the view
*/
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
gl.glViewport(0, 0, width, height); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
Two things I can see. One, since glulookat is defined as
gluLookAt ( eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ )
Your call should be changed to be GLU.gluLookAt(gl, xrot, yrot, 0.0f, 0.0f, xrot, yrot, 0.0f, 1.0f, 0.0f);
Notice the new up vector '0.0, 1.0, 0.0'. Basically says the y-axis is where you want 'up' to be.
Also, you seem to be using rotation values for the rest of the call. The first triplet should be the position of where you are looking, and the second vector should be a reference position, normally where your viewer is. Look at http://developer.android.com/reference/android/opengl/GLU.html
Second issue, if you call loadIdentity after a glulookat call, I am pretty sure since it is loading the identity matrix, you will loose the transform that glulookat performs. So try adding glulookat after you have placed your geometry.
Here is what I am basically saying in code:
public void onDrawFrame(GL10 gl) {
//cleaned up the reset code
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//Check if the light flag has been set to enable/disable lighting
if(light) {
gl.glEnable(GL10.GL_LIGHTING);
} else {
gl.glDisable(GL10.GL_LIGHTING);
}
//Check if the blend flag has been set to enable/disable blending
if(blend) {
gl.glEnable(GL10.GL_BLEND); //Turn Blending On ( NEW )
gl.glDisable(GL10.GL_DEPTH_TEST); //Turn Depth Testing Off ( NEW )
} else {
gl.glDisable(GL10.GL_BLEND); //Turn Blending On ( NEW )
gl.glEnable(GL10.GL_DEPTH_TEST); //Turn Depth Testing Off ( NEW )
}
//Drawing
gl.glTranslatef(0.0f, 0.0f, z); //Move z units into the screen
gl.glScalef(0.8f, 0.8f, 0.8f); //Scale the Cube to 80 percent, otherwise it would be too large for the screen
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); //X
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); //Y
//change the perspective matrix to look at the rotating cube (0,0,z), from (0,0,0)
//with (0,1,0) as the up vector
GLU.gluLookAt(gl, 0.0f, 0.0, z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
cube.draw(gl, filter); //Draw the Cube
//Change rotation factors
xrot += xspeed;
yrot += yspeed;
}

Categories

Resources