screen coordinates

Problems with Canvas, OpenGL, etc...

screen coordinates

Postby Scooby » Sun Feb 13, 2011 1:43 pm

Hi
I'm an opengl newbie that's been working through some online tutorials. I've set myself the test of putting together some code which will allow my to draw colored (and later textured) rectangles on screen. The tutorials at http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/ have been an excellent starting point. However, I'd like some way to draw rects using screen coords, maybe starting at 0,0 for the top left, and 640, 480 for the bottom right.

So far I've came up with the code below and found that changing the values the square vertices will make this possible, but I'm not too sure which calculations I need to get my squares where I want them, presumably something involving the z value in gltranslate in the draw function. The plan is that draw square will take x,y,width,height values.
I'm a bit rusty with matrix algerbra and planning to brush up on that asap, but a gentle push in the right direction would be appreciated.
BTW. It did look like glortho may be the answer, but some conflicting info suggests this isn't supported in opengl es?

Thanks.

Code: Select all
package com.GUITest;

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

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

public class CustomRenderer implements Renderer {


   @Override
   public void onDrawFrame(GL10 gl) {
      // TODO Auto-generated method stub
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      gl.glTranslatef(0.0f, 0.0f, -2.0f);
      this.drawSquare(gl);
      gl.glLoadIdentity();
      
   }

   @Override
   public void onSurfaceChanged(GL10 gl, int width, int height) {
      // TODO Auto-generated method stub
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL10.GL_PROJECTION);
      gl.glLoadIdentity();
      GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.0f);
      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();
      
   }

   @Override
   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // TODO Auto-generated method stub
      gl.glClearColor(0.1f, 0.1f, 0.1f, 0.5f);
      gl.glShadeModel(GL10.GL_SMOOTH);
      gl.glClearDepthf(1.0f);
      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

   }
   
   private void drawSquare(GL10 gl)
   {
      /*float X = -10;
      float Y = 0;
      float W = 10;
      float H=10;
      float Z = 0;
      float vertices[] = {
            X,Y + H,Z,  X + W,Y + H,Z,     X + W, Y, Z,   X,Y,Z};*/
      float vertices[] = {
            -0.1f,  0.1f, 0.0f,
            -0.1f, -0.1f, 0.0f,
             0.1f, -0.1f, 0.0f,
             0.1f, 0.1f, 0.0f,};
         
      short indices[] = {
               0,1,2,0,2,3
         };
      
      FloatBuffer vertexBuffer;
      ShortBuffer indexBuffer;
      
      
      //sort vertex buffer
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vbb.order(ByteOrder.nativeOrder());
      vertexBuffer = vbb.asFloatBuffer();
      vertexBuffer.put(vertices);
      vertexBuffer.position(0);
      
      //sort index buffer
      ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
      ibb.order(ByteOrder.nativeOrder());
      indexBuffer = ibb.asShortBuffer();
      indexBuffer.put(indices);
      indexBuffer.position(0);
      
      gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
      gl.glFrontFace(GL10.GL_CCW);
      gl.glEnable(GL10.GL_CULL_FACE);
      gl.glCullFace(GL10.GL_BACK);
      
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
      
      gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

      
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glDisable(GL10.GL_CULL_FACE);
      
   }
}
Scooby
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 13, 2011 1:33 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests