# Trying to Draw a 2D Triangle in OpenGL ES 2.0

I'm trying to convert a code from OpenGL to OpenGL ES 2.0 (for the BlackBerry PlayBook). So far what I got is this (just the part of the code that should draw the triangle):

void setupScene() {
glClearColor(250, 250, 250, 1);
glViewport(0, 0, 600, 1024);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void drawScene() {
setupScene();

glColorMask(0, 0, 0, 1);

const GLfloat triangleVertices[] = {
100,   100,
150,  0,
200,   100
};

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, triangleVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 2);
}

void render() {
drawScene();
bbutil_swap();
}


The problem is that when I launch the app instead of showing me the triangle the screen just flickers (very fast) from white to gray. Any idea what I'm doing wrong?

Also, here is the entire code if you need: Full source code

## 2 Answers

What you are trying to do is available in the project: http://code.google.com/p/opengles-book-samples/

The OpenGL ES 2.0 does not support the fixed rendering pipeline, so you will need to come up with your own shaders. Take a closer look at the utility functions to compile and load shaders, also, the "Hello Triangle" sample... Very good way to get started if you already know your way around OpenGL.

You have to use shaders. And I also believe the arguments of glDrawArrays are wrong - you need to pass the number of indices as the last one.

Oh, and you're using fairly large values for the triangle's position, but you have no view and projection matrix set up, so they will be way outside the screen.

As for the flickering, you need to call glClear per draw, not just once.

• He's clearing it every draw render->setupScene->setupScene – kravemir Aug 5 '12 at 10:13
• Ah, I should learn to read:) – maul Aug 5 '12 at 20:56