PDA

View Full Version : GL_POINTS not drawing points in Android



guihazan
09-01-2012, 08:37 AM
Hi,

I posted this on android forum but i'm sure that this forum is a better place to post this question.

I've spent a full day trying to draw a series of points using GL_POINTS. I know i could use a texture, but i want to learn how to draw points. The resulting image (zoomed) is shown here:

http://www.totalcross.com/points.png

The code is written below. Any help on why are only 4 points appearing is greatly appreciated. I also tried to print just 2 points, one at the site of the other, but they dont work.


public class MainActivity extends Activity
{
class MyGLSurfaceView extends GLSurfaceView
{
public MyGLSurfaceView(Context context)
{
super(context);

setEGLContextClientVersion(2); // Create an OpenGL ES 2.0 context.
setEGLConfigChooser(8, 8, 8, 8, 0, 0);
getHolder().setFormat(PixelFormat.RGBA_8888);
setRenderer(new MyGLRenderer()); // Set the Renderer for drawing on the GLSurfaceView
// setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY) ; // Render the view only when there is a change in the drawing data (must be after setRenderer!)
}
}

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MyGLSurfaceView(this));
}
}

class MyGLRenderer implements GLSurfaceView.Renderer
{
byte[][] bitmap = {
{1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,0,1},
{1,1,1,1,1,1,1,1}};

static final int COORDS_PER_VERTEX = 3;

// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0,0,0, 1.0f };
float coords[] = new float[4 * COORDS_PER_VERTEX];
int w,h;

public void onSurfaceCreated(GL10 unused, EGLConfig config)
{
}

public void onDrawFrame(GL10 unused)
{
try
{
drawText();
} catch (Exception e) {debug(e.getMessage());}
}

private String vertexShaderCode(int w, int h)
{
return // http://www.songho.ca/opengl/gl_projectionmatrix.html
"attribute vec4 vPosition;" +
"mat4 projectionMatrix = mat4( 2.0/"+w+".0, 0.0, 0.0, -1.0,"+
"0.0, -2.0/"+h+".0, 0.0, 1.0,"+
"0.0, 0.0, -1.0, 0.0,"+
"0.0, 0.0, 0.0, 1.0);"+
"void main() {gl_Position = vPosition*projectionMatrix;}"; // the matrix must be included as a modifier of gl_Position
}

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {gl_FragColor = vColor;}";

private int mProgram;
private int mPositionHandle;
private int mColorHandle;

public void onSurfaceChanged(GL10 unused, int width, int height)
{
w = width; h = height;
// Adjust the viewport based on geometry changes, such as screen rotation
GLES20.glViewport(0, 0, width, height);

// prepare shaders and OpenGL program
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,ve rtexShaderCode(width,height));
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // create OpenGL program executables
GLES20.glUseProgram(mProgram); // Add program to OpenGL environment

// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // get handle to vertex shader's vPosition member
GLES20.glEnableVertexAttribArray(mPositionHandle); // Enable a handle to the vertices - since this is the only one used, keep it enabled all the time
}

float[] letter; int nletter;
private FloatBuffer letBuffer;
private void drawText()
{
setColor(0xFF00FFFF);
if (letter == null)
{
letter = new float[200];
for (int i = 0; i < bitmap.length; i++)
{
byte[]b = bitmap[i];
for (int j = 0; j < b.length; j++)
if (b[j] == 1)
{
letter[nletter++] = j;
letter[nletter++] = i;
nletter++;
}
}
ByteBuffer bb = ByteBuffer.allocateDirect(nletter * 4); // (# of coordinate values * 4 bytes per float)
bb.order(ByteOrder.nativeOrder());
letBuffer = bb.asFloatBuffer();
letBuffer.put(letter,0,nletter); letBuffer.position(0);
}

int nn = nletter/3;
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT,
false, COORDS_PER_VERTEX * nn, letBuffer);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0,nn);
}

public static int loadShader(int type, String shaderCode)
{
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}

public void setColor(int rgb)
{
color[0] = (float)((rgb >> 16 ) & 0xFF) / 255f;
color[1] = (float)((rgb >> 8 ) & 0xFF) / 255f;
color[2] = (float)((rgb ) & 0xFF) / 255f;
color[3] = (float)((rgb >>> 24) & 0xFF) / 255f;
GLES20.glUniform4fv(mColorHandle, 1, color, 0); // Set color for drawing the line
}
}


thanks,

guich

tksuoran
09-03-2012, 04:07 AM
Check your stride argument to glVertexAttribPointer() call. Stride "specifies the byte offset between consecutive generic vertex attributes".

guihazan
09-03-2012, 06:44 AM
Hi,

It doesnt work. In the more simple code below:



final int COORDS_PER_VERTEX = 3;
float[] letter; int nletter;
private FloatBuffer letBuffer;
private void drawText()
{
setColor(0xFF00FFFF);
letter = new float[200];
nletter = 0;
for (int i =0; i < 10; i++)
{
letter[nletter++] = i+10;
letter[nletter++] = 10;
letter[nletter++] = 0;
}

ByteBuffer bb = ByteBuffer.allocateDirect(nletter * 4); // (# of coordinate values * 4 bytes per float)
bb.order(ByteOrder.nativeOrder());
letBuffer = bb.asFloatBuffer();
letBuffer.put(letter,0,nletter);
letBuffer.position(0);

int nn = nletter/3;
int stride = COORDS_PER_VERTEX*nn;
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, stride, letBuffer);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0,nn);
}


I already tried several options for stride, with no success.

thx

guich

guihazan
09-03-2012, 07:04 AM
Hi,

That was the problem. Using 12 (as each coordinate has 3 floats * 4 bytes per float) it works!

thanks!!!

DerekD
02-26-2013, 05:49 PM
I tried your code, and putting '12' in various places, but to no avail.