Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 法线化顶点和法线坐标Open GL ES 2.0_Objective C_Ios_Opengl Es - Fatal编程技术网

Objective c 法线化顶点和法线坐标Open GL ES 2.0

Objective c 法线化顶点和法线坐标Open GL ES 2.0,objective-c,ios,opengl-es,Objective C,Ios,Opengl Es,我在blender中创建了一个模型,并将其导出为.obj文件。我编写了一个解析器,可以读取纹理和法线顶点的坐标。我将所有坐标除以一个适用于程序的常数,以减小模式的大小,使其适合屏幕(这是一个临时措施)。这项工作很好,除了照明不工作,我留下了一个黑色的3D对象时,它应该是彩色的。在网上研究之后,我想这可能是因为法线的长度不是一?如果这是真的,我如何调整我的坐标,使它们适合屏幕并使照明工作 顶点着色器 // // Created by Jake Cunningham on 13/10/2012.

我在blender中创建了一个模型,并将其导出为.obj文件。我编写了一个解析器,可以读取纹理和法线顶点的坐标。我将所有坐标除以一个适用于程序的常数,以减小模式的大小,使其适合屏幕(这是一个临时措施)。这项工作很好,除了照明不工作,我留下了一个黑色的3D对象时,它应该是彩色的。在网上研究之后,我想这可能是因为法线的长度不是一?如果这是真的,我如何调整我的坐标,使它们适合屏幕并使照明工作

顶点着色器

//
//  Created by Jake Cunningham on 13/10/2012.
//  Copyright (c) 2012 Jake Cunningham. All rights reserved.
//

attribute vec4 position;
attribute vec3 normal;

varying lowp vec4 colorVarying;

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

attribute vec2 TextCo;
varying vec2 textCoOut;

void main()
{
vec3 eyeNormal = normalize(normalMatrix * normal);
vec3 lightPosition = vec3(0.0, 0.0, 1.0);
vec4 diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);

float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));

colorVarying = diffuseColor * nDotVP;

gl_Position = modelViewProjectionMatrix * position;
textCoOut = TextCo;
}
片段着色器:

//  Created by Jake Cunningham on 13/10/2012.
//  Copyright (c) 2012 Jake Cunningham. All rights reserved.
//

varying lowp vec4 colorVarying;

varying lowp vec2 textCoOut;
uniform sampler2D texture;

void main()
{
gl_FragColor = colorVarying * texture2D(texture, textCoOut);

}
来自视图控制器的代码

glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);


glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, loader.currentCountOfVerticies * sizeof(GLfloat) * 3, arrayOfVerticies, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));

glGenVertexArraysOES(1, &_normalArray);
glBindVertexArrayOES(_normalArray);

glGenBuffers(1, &_normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer);
glBufferData(GLKVertexAttribNormal, loader.currentCountOfNormals  * sizeof(GLfloat) * 3,loader.arrayOfNormals , GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));

glGenVertexArraysOES(1, &_textureArray);
glBindVertexArrayOES(_textureArray);

glGenBuffers(1, &_textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _textureBuffer);
glBufferData(GL_ARRAY_BUFFER, loader.currentCountOfTextureCoordinates * sizeof(GLfloat) * 2, loader.arrayOftextureCoOrdinates, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(0));


glBindVertexArrayOES(0);

如果使用着色器,则可以对GLSL代码中的顶点和法线使用
normalize()
操作


您还可以查看模型的缩放、居中和规格化,将OBJ文件转换为头文件,以便iOS实现。我还将该脚本扩展到包含对MTL文件的支持,并使其更加轻量级(还有一个Xcode示例)。

干杯。我实现了,但仍然得到了一个黑色立方体。可能还有其他错误。能否共享着色器代码?