开发者

Rainbow lines in openGL es

I writing an application that draws in openGL es 1.0 in the Android NDK and renders it on an android phone. So far, I can draw two lines. The problem is that those two lines are rainbow. I was trying to debug where coordinates are on screen (where 0,0 and 1,1 are) so I added color to the two lines I am drawing. One should be drawn in red, the other in green. The code I use to draw them is as follows:

void appInit()
{
    glEnable(GL_CULL_FACE);
}

void appRender(jint width, jint height)
{
    prepareFrame(width, height);

    drawLines();
}

void drawLines()
{
    glLoadIdentity();

    glPushMatrix();
    GLfloat color1[] = {0.0f,1.0f,0.0f,1.0f};
    drawLine( 1.0f,1.0f,2.0f,2.0f,color1);
    glPopMatrix();

    glPushMatrix();
    GLfloat color2[] = {1.0f,0.0f,0.0f,1.0f};
    drawLine(0.0f,0.0f,1.0f,1.0f, color2);
    glPopMatrix();
}

void drawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLflo开发者_如何学编程at y2, GLfloat * color)
{
    GLfloat vVertices[] =
    {x1, y1,
    x2, y2};

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    //glColor4f(color[0],color[1],color[2],color[3]);
    glColorPointer(4,GL_FLOAT,0,color);
    glVertexPointer(2, GL_FLOAT, 0, vVertices);
    glDrawArrays(GL_LINES, 0, 4);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
}

void prepareFrame(int width, int height)
{
    glViewport(0, 0, width, height);

    glClearColor(0.0,0.0,0.0,0.0);

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
}

appInit is called, then appRender is called over and over again with the screen dimensions. The result is this:

http://i.stack.imgur.com/G91oX.png

If anyone knows why they are drawing rainbows instead of the colors I specified, that would be wonderful. Bonus points if you can tell me what the coordinate system is like by default on android (no gluperspective or lookat is used).


Try creating a color array that has a color for each vertex. Right now I think you're reading off the end of the array into uninitialized memory when you try to render two vertexes, since you don't specify the color for the second vertex.

Also, I think the third argument for your glDrawArrays() call should be 2, not 4, since you're only rendering two vertexes.

I'm somewhat surprised it didn't crash with an access violation of some sort, honestly :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜