0

我的顶点着色器:

attribute vec4 vertexPosition;
attribute vec2 vertexTexCoord;

varying vec2 texCoord;

uniform mat4 modelViewProjectionMatrix;

void main()
{
    gl_Position = modelViewProjectionMatrix * vertexPosition;
    texCoord = vertexTexCoord;
}

我的fragmentShder:

precision mediump float;
varying vec2 texCoord;

uniform sampler2D texSampler2D;

void main()
{
    gl_FragColor = texture2D(texSampler2D, texCoord);
}

初始化着色器:

 if (shader2D == nil) {
    shader2D = [[Shader2D alloc] init];
    shader2D.shaderProgramID = [ShaderUtils compileShaders:vertexShader2d :fragmentShader2d];
    if (0 < shader2D.shaderProgramID) {
        shader2D.vertexHandle = glGetAttribLocation(shader2D.shaderProgramID, "vertexPosition");
        shader2D.textureCoordHandle = glGetAttribLocation(shader2D.shaderProgramID, "vertexTexCoord");
        shader2D.mvpMatrixHandle = glGetUniformLocation(shader2D.shaderProgramID, "modelViewProjectionMatrix");
        shader2D.texSampler2DHandle  = glGetUniformLocation(shader2D.shaderProgramID,"texSampler2D");
    }
    else {
        NSLog(@"Could not initialise shader2D");
    }
}
return shader2D;

渲染:

GLKMatrix4 mvpMatrix;
mvpMatrix = [self position: position];
mvpMatrix = GLKMatrix4Multiply([QCARutils getInstance].projectionMatrix, mvpMatrix);
glUseProgram(shader.shaderProgramID);
glVertexAttribPointer(shader.vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)vertices);
glVertexAttribPointer(shader.textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)texCoords);
glEnableVertexAttribArray(shader.vertexHandle);
glEnableVertexAttribArray(shader.textureCoordHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, [texture textureID]);
glUniformMatrix4fv(shader.mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&mvpMatrix);
glUniform1i(shader.texSampler2DHandle, 0);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)indices);
glDisableVertexAttribArray(shader.vertexHandle);
glDisableVertexAttribArray(shader.textureCoordHandle);

当一个纹理坐标对应一个且只有一个顶点坐标时,它似乎可以正常工作(Number of texCoords== Number of vertices

我的问题:openGL 是否将纹理坐标分配给一个且只有一个顶点?也就是说,当纹理坐标和顶点坐标不是一一对应的时候,渲染结果会是什么?

4

1 回答 1

1

是的,顶点和texCoords之间需要一一对应——所有通过OpenGL管道传递的信息都是每个顶点的,所以每个法线和每个texCoord都必须有一个顶点。

但是请注意,您可以(并且经常需要)为空间中的同一点拥有多个 texCoords、法线或其他每个顶点数据:例如,如果您将纹理贴图包裹在球体周围,则会有矩形纹理末端相交的“接缝”。在这些点上,您需要拥有多个占据同一点的顶点。

于 2013-02-23T07:11:30.727 回答