[SOLVED] OpenGL: Draw Quad. No visible result.

Put your problem here if it does not fit any of the other categories.

[SOLVED] OpenGL: Draw Quad. No visible result.

Postby Raymond » Mon Oct 12, 2009 3:22 pm

My code:
Code: Select all
public class DummyState implements State {
   private FloatBuffer mVertexBuffer;
   
    @Override
    public void Clear(GL10 gl) {
       gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

    @Override
    public void Init(GL10 gl) {
       float square[] = {
          -0.25f, -0.25f, 0f,
          0.25f, -0.25f, 0f,
          -0.25f, 0.25f, 0f,
          0.25f, 0.25f, 0f
       };
       
       ByteBuffer vbb = ByteBuffer.allocateDirect(square.length * 4);
       vbb.order(ByteOrder.nativeOrder());
       
       mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(square);
        mVertexBuffer.position(0);
    }

    @Override
    public void Render(GL10 gl) {
       gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
       
       gl.glMatrixMode(GL10.GL_PROJECTION);
       gl.glLoadIdentity();
       
       gl.glMatrixMode(GL10.GL_MODELVIEW);
       gl.glLoadIdentity();
       gl.glTranslatef(-1.5f, 0.0f, -6.0f);
       
       gl.glShadeModel(GL10.GL_FLAT);
       
       gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
       gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
       gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mVertexBuffer);
       gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 4);
    }

    @Override
    public void TouchEvent(MotionEvent event) {
    }
}


Init and Render functions are both called. So I assume I'm missing an OpenGL call. Any idea?
Last edited by Raymond on Thu Oct 15, 2009 9:27 am, edited 1 time in total.
Raymond
Freshman
Freshman
 
Posts: 3
Joined: Mon Oct 12, 2009 3:20 pm

Top

Postby Raymond » Wed Oct 14, 2009 6:46 pm

I updated the code. This code runs on the emulator but no visible result. This is the full code, so if something is missing here, its probably missing in the code below its probably missing in my application, so feel free to point it out.

Code: Select all
package com.asdf.error;

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

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class Error extends Activity {
    public class FOO extends GLSurfaceView implements GLSurfaceView.Renderer {
       private FloatBuffer mVertexBuffer;
      public FOO(Context context) {
         super(context);
         
         float square[] = {
                -0.25f, -0.25f, 0f,
                0.25f, -0.25f, 0f,
                -0.25f, 0.25f, 0f,
                0.25f, 0.25f, 0f
             };
             
         ByteBuffer vbb = ByteBuffer.allocateDirect(square.length * 4);
          vbb.order(ByteOrder.nativeOrder());
             
          mVertexBuffer = vbb.asFloatBuffer();
          mVertexBuffer.put(square);
          mVertexBuffer.position(0);
         
         setRenderer(this);
      }

      @Override
      public void onDrawFrame(GL10 gl) {
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
          
          gl.glMatrixMode(GL10.GL_PROJECTION);
          gl.glLoadIdentity();
          
          gl.glMatrixMode(GL10.GL_MODELVIEW);
          gl.glLoadIdentity();
          gl.glTranslatef(-1.5f, 0.0f, -6.0f);
          
          gl.glShadeModel(GL10.GL_FLAT);
          
          gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
          gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
          gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mVertexBuffer);
          gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
      }

      @Override
      public void onSurfaceChanged(GL10 gl, int width, int height) {
         gl.glViewport(0, 0, width, height);
      }

      @Override
      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      }     
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FOO f = new FOO(this);
        setContentView(f);
    }
}
Raymond
Freshman
Freshman
 
Posts: 3
Joined: Mon Oct 12, 2009 3:20 pm

Postby chimaera » Thu Oct 15, 2009 1:57 am

1) When you translate to -1.5f on the x axis, you're out of the frustrum.
2) Your vertex definition is wrong, you should set it to 3 coordinates per vertex

Here is a working code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package test.test;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6.  
  7. import javax.microedition.khronos.egl.EGLConfig;
  8. import javax.microedition.khronos.opengles.GL10;
  9.  
  10. import android.app.Activity;
  11. import android.content.Context;
  12. import android.opengl.GLSurfaceView;
  13. import android.os.Bundle;
  14.  
  15. public class test extends Activity {
  16.     public class FOO extends GLSurfaceView implements GLSurfaceView.Renderer {
  17.        private FloatBuffer mVertexBuffer;
  18.       public FOO(Context context) {
  19.          super(context);
  20.          
  21.          float square[] = {
  22.                 -0.25f, -0.25f, 0f,
  23.                 0.25f, -0.25f, 0f,
  24.                 -0.25f, 0.25f, 0f,
  25.                 0.25f, 0.25f, 0f
  26.              };
  27.              
  28.          ByteBuffer vbb = ByteBuffer.allocateDirect(square.length * 4);
  29.           vbb.order(ByteOrder.nativeOrder());
  30.              
  31.           mVertexBuffer = vbb.asFloatBuffer();
  32.           mVertexBuffer.put(square);
  33.           mVertexBuffer.position(0);
  34.          
  35.          setRenderer(this);
  36.       }
  37.  
  38.       @Override
  39.       public void onDrawFrame(GL10 gl) {
  40.          gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  41.            
  42.           gl.glMatrixMode(GL10.GL_PROJECTION);
  43.           gl.glLoadIdentity();
  44.            
  45.           gl.glMatrixMode(GL10.GL_MODELVIEW);
  46.           gl.glLoadIdentity();
  47.           gl.glTranslatef(0.0f, 0.0f, -6.0f);
  48.            
  49.           gl.glShadeModel(GL10.GL_FLAT);
  50.            
  51.           gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
  52.           gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  53.           gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
  54.           gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  55.       }
  56.  
  57.       @Override
  58.       public void onSurfaceChanged(GL10 gl, int width, int height) {
  59.          gl.glViewport(0, 0, width, height);
  60.       }
  61.  
  62.       @Override
  63.       public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  64.       }      
  65.     }
  66.     @Override
  67.     public void onCreate(Bundle savedInstanceState) {
  68.         super.onCreate(savedInstanceState);
  69.         FOO f = new FOO(this);
  70.         setContentView(f);
  71.     }
  72. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Postby Raymond » Thu Oct 15, 2009 9:27 am

Tested it and it works. Thanks!
Raymond
Freshman
Freshman
 
Posts: 3
Joined: Mon Oct 12, 2009 3:20 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests