Question on working of viewport & parallel projection
I have created a triangle(in android) and given below is the specification of the vertices size, viewport size & parallel projection size.
vertices.put(new float[] { 0.0f, 0.0f,
319.0f, 0.0f,
160.0f, 479.0f });
gl10.glViewport(0, 0, 160, 480);
gl10.glOrthof(0, 160, 0, 480, 1, -1);
The actual resolution of the screen is 320*480, see:
- glsurfaceview.getWidth() = 320
- glsurfaceview.getHeight() = 480
Given below is the snapshot of the output I recieved in emulator,
What I don't understand is, I have set the viewport to only half the with of the screen and I was expecting to view only half the triangle (Believe anything exceeding the viewport area should be clipped). Given below is the picture I was expecting,
I just could not figure out the reason for this behavior. Can some help me on this.
Given below is the actual code I am working on,
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
rand = new Random(); //Random number generator
glsurfaceview = new GLSurfaceView(this);
glsurfaceview.setRenderer(this);
setContentView(glsurfaceview);
}
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglconfig) {
Log.v("#MYAPP", "MyCanonActivity : Inside onSurfaceCreated");
byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[] { 0.0f, 0.0f,
319.0f, 0.0f,
160.0f, 479.0f });
vertices.flip();
}
@Override
public void onDrawFrame(GL10 gl10) {
gl10.glViewport(0, 0, 160, 480);
gl10.glClearColor(0,0,0,1);
gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl10.glMatrixMode(GL10.GL_PROJECTION);
gl10.glLoadIdentity();
gl10.glOrthof(0, 160, 0, 480, 1, -1);
gl10.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl10.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
gl10.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
@Override
public void onSurfaceChanged(GL10 gl10, int width, int height) {
Log.v("#MYAPP", "MyCanon开发者_如何学CActivity : Inside onSurfaceChanged");
}
@Override
public void onResume() {
super.onResume();
glsurfaceview.onResume(); //Start rendering thread also
Log.v("#MYAPP", "MyCanonActivity : onResume");
}
@Override
public void onPause() {
super.onPause();
glsurfaceview.onPause();
Log.v("#MYAPP", "MyCanonActivity : onPause");
}
Whilst I cannot find a definition of 'viewport' in terms of computer graphics I can offer this explanation.
The viewport does indeed clip the viewing area. Imagine if you will you are inside a boat looking out of one of its port-holes. Your view of what is outside the boat is limited by the size of the port hole, or in other words your view of the much bigger outside world is 'clipped' by the port-hole. A view port in terms of computer graphics works the same way except the viewport is malleable i.e. you can easily change it.
So the clipping you described above is as you would expect, it does not force the triangle to be draw to fit inside the viewport. If you DID want to make the triangle to be completely drawn inside the Viewport then you must transform the co-ordinates of the vertices to the Viewport.
Actually your assumptions are correct and your picture should look indeed like the second one given your code snippet. It may be, that you didn't clear the framebuffer (which still has the whole size) after drawing the full sized triangle, so your rendering actually contains the whole triangle from the previous pass and the half one rendered on top of it.
And by the way, I hope you have done the glOrtho
call on the correct matrix stack (projection) and did not mess up the matrix stacks, although this might give completely different results.
精彩评论