Home  >  Article  >  Backend Development  >  Why Isn\'t My OpenGL Triangle Rendering in Go, But It Works in C?

Why Isn\'t My OpenGL Triangle Rendering in Go, But It Works in C?

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 04:07:02610browse

Why Isn't My OpenGL Triangle Rendering in Go, But It Works in C?

OpenGL Vertex Buffer Not Rendering Triangle in Go

Despite following the instructions from a tutorial, users have encountered an issue where the Go version fails to display a triangle while its C counterpart successfully does. This discrepancy is attributed to a potential discrepancy in providing arguments to the vertexAttrib.AttribPointer() method.

In the Go code, the following arguments are passed to the method:

<code class="go">vertexAttrib.AttribPointer(
  3,   // Size
  0,   // Normalized?
  0,   // Stride
  nil,  // Array buffer offset
)</code>

However, in the C code, the equivalent method is called with a different set of arguments:

<code class="c">glVertexAttribPointer(
  0,
  3,   // Size
  GL_FLOAT,   // Type
  GL_FALSE,  // Normalized?
  0,   // Stride
  (void*)0   // Array buffer offset
)</code>

The argument corresponding to the array buffer offset is conspicuous by its absence in the Go code. This argument specifies the offset in bytes from the beginning of the buffer to the start of the vertex data for a given attribute. By omitting this argument in the Go code, the vertex data may not be properly retrieved.

To resolve the issue, the work branch of the banthar bindings should be used, and the following arguments should be passed to the vertexAttrib.AttribPointer() method:

<code class="go">vertexAttrib.AttribPointer(
  3,     // Size
  gl.FLOAT,   // Type
  false,  // Normalized?
  0,     // Stride
  nil    // Array buffer offset
)</code>

Additionally, the data sent to the server should be multiplied by 4 to represent the number of bytes representing the data. For instance, if the data was described as:

<code class="go">data := []float32{0, 1, 0, -1, -1, 0, 1, -1, 0}</code>

it should be used as:

<code class="go">gl.BufferData(gl.ARRAY_BUFFER, len(data) * 4, data, gl.STATIC_DRAW)</code>

The above is the detailed content of Why Isn\'t My OpenGL Triangle Rendering in Go, But It Works in C?. 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