Home  >  Article  >  Backend Development  >  Why Does My Go OpenGL Triangle Remain Invisible?

Why Does My Go OpenGL Triangle Remain Invisible?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 05:55:30381browse

 Why Does My Go OpenGL Triangle Remain Invisible?

OpenGL Vertex Buffer Fails to Display Triangle in Go

In an attempt to replicate a tutorial on creating a simple triangle using OpenGL in Go, an individual encountered difficulties in getting the vertex buffer to display the shape. While the window opened and the background turned blue, the triangle remained invisible.

The code snippet provided involves creating a vertex buffer and then drawing the triangle using glDrawArrays. However, in the c-version of the code which works, the AttribPointer function takes a parameter (void*)0 to specify the array buffer offset, while in the Go-version, the provided offset argument crash the application, and nil doesn't seem to work either.

The updated solution involved using the work branch of banthar bindings and altering the calls to AttribPointer and BufferData as follows:

<code class="go">vertexAttrib.AttribPointer(
    3,     // size
    gl.FLOAT, //type
    false, // normalized?
    0,     // stride
    nil) // array buffer offset

data := []float32{0, 1, 0, -1, -1, 0, 1, -1, 0}
[...]
gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, data, gl.STATIC_DRAW)</code>

By passing the size in bytes to BufferData and setting the array buffer offset to nil in AttribPointer, the problem was resolved, and the triangle was successfully displayed.

The above is the detailed content of Why Does My Go OpenGL Triangle Remain Invisible?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn