开发者

Getting problem in rotating the line around the z axis in opengl?

I have drawn one line having vertices (0.2f, 0.2f, 0.0f) & (-0.2f, -0.2f, 0.0f) in OpenGL. As z component of line co-ordinates is missing it implies that line is in XY plane. Now i am trying to rotate the line around the z axis using glRotatef function. Ideally line rotation should have taken a circular path(at least it should have looked like a circle when rotated it by 1 to 360) but somehow it is looking more like a ellipse, more stretched towards y-axis (my guess is that, may be because of depth effect). but as my line is completely in XY plane(As Z component is missing in line co-ordinates) why am i getting such depth effect? I have posted my code herewith this question. Please point me to the right direction?

GLSurfaceViewActivity code file:

package com.mobi.trials.gldemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class GLSurfaceViewActivity extends Activity {

SimpleGLSurfaceView glView = null;

/** Called when the activity is first created. */
@Override
p开发者_开发百科ublic void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("GLDemo", "In the activity");
    glView = new SimpleGLSurfaceView(this);
    setContentView(glView);
}

@Override
protected void onPause() {
    super.onPause();
    glView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    glView.onResume();
}

}

SimpleGLSurfaceView code file:

package com.mobi.trials.gldemo;

import android.content.Context;
import android.opengl.GLSurfaceView;

public class SimpleGLSurfaceView extends GLSurfaceView {

public SimpleGLSurfaceView(Context context) {
    super(context);
    final DrawGLSurfaceCanvasView renderer = new DrawGLSurfaceCanvasView(this);

    setFocusable(true);
    setFocusableInTouchMode(true);
    setRenderer(renderer);
}

}

DrawGLSurfaceCanvasView code file:

package com.mobi.trials.gldemo;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;

public class DrawGLSurfaceCanvasView implements GLSurfaceView.Renderer {

SimpleLine line;
private float rotationAngle = 0.0f;
float lineVertices[] = {  0.2f, 0.2f, 0f,
          -0.2f, -0.2f, 0f };

public DrawGLSurfaceCanvasView(GLSurfaceView view) {
}

@Override
public void onDrawFrame(GL10 gl) {

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

    gl.glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);

    if(rotationAngle <= -360.0f)
    {
        rotationAngle = 0.0f;
    }
    rotationAngle -= 1f;

    line.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    line = new SimpleLine(lineVertices);
}

}

SimpleLine code file :

package com.mobi.trials.gldemo;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class SimpleLine {
float vertices[];
private FloatBuffer vertexBuffer;

public SimpleLine(float[] vertices) {
    super();
    this.vertices = vertices;
}

public void draw(GL10 gl)
{
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // Update : Added orthographic projection matrix.
    // Update II : Corrected the values of orthgraphic projection matrix.
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glDrawArrays(GL10.GL_LINES, 0, 2);
}

}


I don't think what you're observing is a depth effect. It's just that you did not set any projection matrix (resulting in picking up the default: the identity). So with a modelview matrix set as identity, you end up with [-1:1] representing the extents of your window.

[-.2,.2] ends up representing 20% of your window. I assume your window is not square, so you end up drawing an ellipse with an eccentricity of your aspect ratio.

You need to look at tutorials that explain how the projection matrix is typically set (it typically involves the size of your window and or its aspect ratio).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜