为什么glDrawArrays因GL_INVALID_OPERATION而失败?

时间:2016-01-02 14:50:36

标签: opengl go

我正在使用OpenGL绘制一个矩形,我正在使用golang。

我现在不使用红色,绿色和蓝色的参数。

var vertices = []float64{ 
    -1.0, -1.0,  0.0,   // V1 - bottom left 
    -1.0,  1.0,  0.0,   // V2 - top left 
    1.0, -1.0,  0.0,    // V3 - bottom right 
    1.0,  1.0,  0.0,    // V4 - top right 
}

func DisplayWrite(red []byte, green []byte, blue []byte) {
    const GL_TRUE = 1
    const GL_FALSE = 0
    const GL_FLOAT = 0x1406
    const GL_VERTEX_ARRAY = 0x8074
    const GL_TRIANGLE_STRIP = 0x0005
    C.glLoadIdentity();
    chkGlErr("glLoadIdentity")
    C.glTranslatef(0.0, 0.0, -5.0);
    chkGlErr("glTranslatef")
    C.glEnableClientState(GL_VERTEX_ARRAY);
    chkGlErr("glEnableClientState")
    C.glColor4f(1.0, 1.0, 1.0, 1.0);
    chkGlErr("glColor4f")
    C.glVertexPointer(3, GL_FLOAT, 0, (unsafe.Pointer)(&vertices));
    chkGlErr("glVertexPointer")
    C.glDrawArrays(GL_TRIANGLE_STRIP, 0, (C.GLsizei)(len(vertices) / 3));
    chkGlErr("glDrawArrays")
    C.glDisableClientState(GL_VERTEX_ARRAY);
    chkGlErr("glDisableClientState")
    const GL_UNSIGNED_BYTE = 0x1401
    //C.glDrawPixels(Width, Height, GL_RED, GL_UNSIGNED_BYTE, &red[0])
    //C.glDrawPixels(Width, Height, GL_GREEN, GL_UNSIGNED_BYTE, &green[0])
    //C.glDrawPixels(Width, Height, GL_GREEN, GL_UNSIGNED_BYTE, &blue[0])
}

问题是当我像这样调用glDrawArrays时:

C.glDrawArrays(GL_TRIANGLE_STRIP, 0, (C.GLsizei)(len(vertices) / 3));

它什么都没画,错误是GL_INVALID_OPERATION。 我画画的方式有问题吗?

这是设置部分:

func DisplayInit() DispInitStatus {
    //(EGL and OpenGL ES initialization codes...)

    C.glViewport(0, 0, (C.GLsizei)(dWidth), (C.GLsizei)(dHeight));
    chkGlErr("glViewport")
    C.glMatrixMode(GL_PROJECTION);
    chkGlErr("glMatrixMode")
    C.glLoadIdentity();
    chkGlErr("glLoadIdentity")

    makeFrustum(45.0, (float64)(dWidth) / (float64)(dHeight), 0.1, 100.0)

    C.glMatrixMode(GL_MODELVIEW);
    chkGlErr("glMatrixMode")
    C.glLoadIdentity();
    chkGlErr("glLoadIdentity")

    //C.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    //chkGlErr("glHint")
    C.glClearColor(1.0, 0.0, 0.0, 1.0);
    chkGlErr("glClearColor")
    C.glClear(GL_COLOR_BUFFER_BIT);
    chkGlErr("glClear")
    fmt.Println("EGL/GLES is now ready")
    return Disp_Ok
    //No error with these.
}

主要功能

(DisplayUpdate执行glFlush和eglSwapBuffers。它没有问题。)

func main() {
    runtime.LockOSThread()
    DisplayInit()
    red := make([]byte, dWidth*dHeight)
    gre := make([]byte, dWidth*dHeight)
    blu := make([]byte, dWidth*dHeight)
    DisplayWrite(red, gre, blu)
    DisplayUpdate()
    for {
    }
}

我用Google搜索并发现了这个:

Why does OpenGL's glDrawArrays() fail with GL_INVALID_OPERATION under Core Profile 3.2, but not 3.3 or 4.2?

我尝试使用上面的runtime.LockOSThread(),但仍然无效。

ADD:添加了makeFrustum。

func makeFrustum(fovY, aspectRatio, front, back float64) {
    tangent := math.Tan(fovY / 2 * (3.14159265 / 180.0));   // tangent of half fovY
    height := front * tangent;          // half height of near plane
    width := height * aspectRatio;      // half width of near plane
    // params: left, right, bottom, top, near, far
    C.glFrustumf((C.GLfloat)(-width), (C.GLfloat)(width), (C.GLfloat)(-height), (C.GLfloat)(height), (C.GLfloat)(front), (C.GLfloat)(back));
    chkGlErr("glFrustumf")
}

这也不会导致错误。

这与gluPerspective一样。

https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml

1 个答案:

答案 0 :(得分:0)

C.glEnableClientState(GL_VERTEX_ARRAY);
C.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (unsafe.Pointer)(&vertices));

glVertexPointerglVertexAttribPointer功能不同。如果你使用旧的“客户端状态”顶点数组,那么你的意思是glVertexPointerglVertexAttribPointer用于执行基于着色器的渲染。