ios - 在 IOS 上的 OpenGL ES 2.0 着色器中绘制简单的线条
<p><p>我很难通过 OpenGL ES 2.0 着色器绘制简单的线条。
我正在尝试将 <code>glDrawArrays</code> 与 <code>GL_LINES</code> 一起使用,但到目前为止我一直没有成功。</p>
<p>我的顶点着色器是你能得到的最基本的:</p>
<pre><code>attribute vec4 Position;
attribute vec4 SourceColor;
uniform mat4 Projection;
uniform mat4 ModelView;
varying vec4 DestinationColor;
void main(void) {
DestinationColor = SourceColor;
gl_Position = Projection * ModelView * Position;
}
</code></pre>
<p>片段着色器:</p>
<pre><code>varying lowp vec4 DestinationColor;
void main(void) {
gl_FragColor = DestinationColor;
}
</code></pre>
<p>我有一些疑问:</p>
<ol>
<li>如何设置起点和终点坐标?</li>
<li>在这种情况下如何设置 Projection 和 ModelView 矩阵?对于正方形,ModelView 矩阵将保持其中心坐标,但在 Line 的情况下会发生什么?</li>
</ol>
<p>有人有例子吗? </p>
<p>编辑:</p>
<p>好的,我有以下代码:</p>
<pre><code> glLineVertices.Position = origin.x;
glLineVertices.Position = origin.y;
glLineVertices.Position = 0;
glLineVertices.Color = 1.0f;
glLineVertices.Color = 0.0f;
glLineVertices.Color = 0.0f;
glLineVertices.Color = 0.0f;
glLineVertices.Normal = 0.0f;
glLineVertices.Normal = 0.0f;
glLineVertices.Normal = 1.0f;
glLineVertices.Position = end.x;
glLineVertices.Position = end.y;
glLineVertices.Position = 0;
glLineVertices.Color = 1.0f;
glLineVertices.Color = 0.0f;
glLineVertices.Color = 0.0f;
glLineVertices.Color = 0.0f;
glLineVertices.Normal = 0.0f;
glLineVertices.Normal = 0.0f;
glLineVertices.Normal = 1.0f;
glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertexStructureSize, glLineVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(shaderManager.currentAttributeVertexPosition);
glVertexAttribPointer(shaderManager.currentAttributeVertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), 0);
glEnableVertexAttribArray(shaderManager.currentAttributeSourceColor);
glVertexAttribPointer(shaderManager.currentAttributeSourceColor, 4, GL_FLOAT, GL_FALSE,sizeof(glLineVertex), (char *)12);
glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);
</code></pre>
<p>使用渲染方法:</p>
<pre><code>- (void)render
{
//It might as well be the Identity Matrix, nothing happens either way.
//modelViewMatrix = GLKMatrix4Identity;
modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, 0.0f);
glUniformMatrix4fv(shaderManager.currentUniformModelViewMatrix, 1, 0, modelViewMatrix.m);
glBindVertexArrayOES(vertexArray);
glDrawArrays(GL_LINES, 0, size);
}
</code></pre>
<p>还有这个投影矩阵:</p>
<pre><code>_projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1.0f, 1.0f);
;
glUniformMatrix4fv(, 1, 0, _projectionMatrix.m);
</code></pre>
<p>一旦我使用:</p>
<pre><code>;
</code></pre>
<p>什么都没有发生。
有谁知道为什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我终于明白了。</p>
<p>这些简单的线条:</p>
<pre><code>glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);
</code></pre>
<p>导致了这个问题,因为我暂时禁用了着色器中法线的使用,注释掉了对应的属性。不知道这样就不会产生draw,getError()也不会抛出任何错误。</p>
<p>不过,我会认为上述答案是正确的,因为它帮助我澄清了坐标和对应矩阵的计算。在这个例子中,普通的 2D 线,不需要任何特殊的 modelViewMatrix,它可以保持为“GLKMatrix4Identity”。</p>
<p>再次感谢:)</p></p>
<p style="font-size: 20px;">关于ios - 在 IOS 上的 OpenGL ES 2.0 着色器中绘制简单的线条,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/8289727/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/8289727/
</a>
</p>
页:
[1]