screen coordinates

Problems with Canvas, OpenGL, etc...

screen coordinates

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

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 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?


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 {

   public void onDrawFrame(GL10 gl) {
      // TODO Auto-generated method stub
      gl.glTranslatef(0.0f, 0.0f, -2.0f);

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

   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // TODO Auto-generated method stub
      gl.glClearColor(0.1f, 0.1f, 0.1f, 0.5f);

   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[] = {
      FloatBuffer vertexBuffer;
      ShortBuffer indexBuffer;
      //sort vertex buffer
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vertexBuffer = vbb.asFloatBuffer();
      //sort index buffer
      ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
      indexBuffer = ibb.asShortBuffer();
      gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
      gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

Posts: 2
Joined: Sun Feb 13, 2011 1:33 pm


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

Who is online

Users browsing this forum: No registered users and 2 guests