在IOS上的OpenGL ES 2.0着色器中绘制简单线
我很难通过OpenGL ES 2.0着色器绘制简单的线条。 我正在尝试将在IOS上的OpenGL ES 2.0着色器中绘制简单线,ios,opengl-es,shader,Ios,Opengl Es,Shader,我很难通过OpenGL ES 2.0着色器绘制简单的线条。 我正在尝试将glDrawArrays与GL\u行一起使用,但到目前为止,我没有成功 “我的顶点着色器”是您可以获得的最基本的材质: attribute vec4 Position; attribute vec4 SourceColor; uniform mat4 Projection; uniform mat4 ModelView; varying vec4 DestinationColor; void main(void)
glDrawArrays
与GL\u行一起使用,但到目前为止,我没有成功
“我的顶点着色器”是您可以获得的最基本的材质:
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;
}
和片段着色器:
varying lowp vec4 DestinationColor;
void main(void) {
gl_FragColor = DestinationColor;
}
我有一些疑问:
如何设置原点和终点坐标
在这种情况下,如何设置投影矩阵和模型视图矩阵?对于正方形,ModelView矩阵将保持其中心坐标,但对于直线会发生什么情况
有人举个例子吗
编辑:
好的,我有以下代码:
glLineVertices[0].Position[0] = origin.x;
glLineVertices[0].Position[1] = origin.y;
glLineVertices[0].Position[2] = 0;
glLineVertices[0].Color[0] = 1.0f;
glLineVertices[0].Color[1] = 0.0f;
glLineVertices[0].Color[2] = 0.0f;
glLineVertices[0].Color[3] = 0.0f;
glLineVertices[0].Normal[0] = 0.0f;
glLineVertices[0].Normal[1] = 0.0f;
glLineVertices[0].Normal[2] = 1.0f;
glLineVertices[1].Position[0] = end.x;
glLineVertices[1].Position[1] = end.y;
glLineVertices[1].Position[2] = 0;
glLineVertices[1].Color[0] = 1.0f;
glLineVertices[1].Color[1] = 0.0f;
glLineVertices[1].Color[2] = 0.0f;
glLineVertices[1].Color[3] = 0.0f;
glLineVertices[1].Normal[0] = 0.0f;
glLineVertices[1].Normal[1] = 0.0f;
glLineVertices[1].Normal[2] = 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);
使用渲染方法:
- (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);
}
通过这个投影矩阵:
_projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1.0f, 1.0f);
[shaderManager useShaderProgram:@"LineShader"];
glUniformMatrix4fv([shaderManager getUniform:@"Projection" ofShader:@"LineShader"], 1, 0, _projectionMatrix.m);
一旦我使用:
[lineDraw render];
什么也没发生。
有人知道为什么吗
假设“起点和终点”是指两条线的端点,则它们只是线的顶点,在本例中是发送到位置
属性的值。因此,当使用glDrawArrays
时,绑定到位置
属性的顶点数组/VBO(使用glvertexattributepointer
和适当的属性索引)包含(或应该包含)一个接一个存储的所有行的起点和终点。当然,绑定到SourceColor
属性的数组必须分别包含这些顶点的颜色
投影
矩阵一如既往地设置,它包含投影变换,通常是正交投影(例如,对于平行2D视图)或透视投影(对于距离较远的对象显示较小的“真实”视图)
ModelView
矩阵包含线集应有的任何变换,通常由模型变换和视图变换组成,模型变换将线集的顶点从其局部坐标系转换为世界坐标系,将顶点从世界空间变换到摄影机空间(然后通过投影变换从该空间变换顶点)
因此,对于一个正方形,它不仅需要包含sqaure的中心,它还取决于您想要如何变换它以及它未转换的顶点具有什么值(通过位置
属性发送到OpenGL的值)。请记住,发送到位置
属性的原始顶点位于线集的局部空间,然后通过模型视图
矩阵转换为世界空间(并进一步转换为视图空间)
因此,如果直线具有顶点(0,0)和(1,1),则直线的轴心点(其局部坐标系的原点)位于起点,而从(-1,0)到(1,0)的直线的局部原点位于其中心点。这取决于直线的实际顶点
当然,如果你觉得这一切听起来很陌生,你应该深入研究一下OpenGL和计算机图形学的基本知识。我终于明白了
这些简单的句子:
glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);
我们导致了这个问题,因为我暂时禁用了着色器中法线的使用,并注释掉了相应的属性。我不知道这样做就不会发生绘制,getError()也不会抛出任何错误
我将把上述答案看作是正确的,因为它帮助我弄清坐标和对应矩阵的计算。在本例中,对于普通的二维线,不需要任何特殊的modelViewMatrix,它可以保持为“GLKMatrix4Identity”
再次感谢:)在本例中,我使用的是投影矩阵:GLKMatrix4MakeOrtho(0,self.view.bounds.size.width,0,self.view.bounds.size.height,-1.0f,1.0f)代码>,并且,对于模型视图矩阵:GLKMatrix4MakeTranslation(0,0,0.0f)代码>,即任何类型的翻译。给定这些矩阵,我假设顶点应该在屏幕坐标中,例如,原点(100100)、端点(200200)?我设置了您建议的所有步骤(我成功地调整了我已经使用纹理正方形设置的一些函数),当我调用gldrawArray(GL_线等)时;什么也没发生…@NunoMiguelFonseca确实正确。@NunoMiguelFonseca那么你应该显示你的代码,否则很难分辨出任何错误。