ipad1 vs ipad2:使用OpenGL进行不同的渲染结果

时间:2012-05-03 14:13:51

标签: ios ipad opengl-es-2.0 ipad-2

当我在ipad1上运行我的应用程序时,当我在ipad2上运行相同的应用程序时,我会看到不同的结果。我对屏幕中间的书本大小感兴趣。

iPad1 result

iPad2 result

我的代码如下。

- (void)drawGallery{

glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);

ResourceManager *rm = [ResourceManager sharedInstance];
GLuint currProgram = [rm getProgram:PROGRAM_BASIC_LIGHTNING];

glUseProgram(currProgram);

glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewport(0, 0, 768, 1024 + 2 * GALLERY_OFFSET_Y);

GLfloat modelviewProj[16];

for (Sheet *sh in self.interfaceManager.gallery.sheets){

    if ([self.interfaceManager.gallery shouldDrawSheet:sh]){

        [self MakePerspectiveMatrix:modelviewProj 
                            OriginX:0.0 
                            OriginY:0.0 
                              Width:SCREEN_WIDTH 
                             Height:SCREEN_HEIGHT
                           Rotation:sh.rotation
                       TranslationX:sh.translationX 
                       TranslationZ:sh.translationZ
                             ScaleX:1.0
                             ScaleY:SCREEN_HEIGHT/(SCREEN_HEIGHT + 2 * GALLERY_OFFSET_Y)];


        vertexDataTextured *model;
        if (sh.type == SHEET_TYPE_LEFT){
            model = (vertexDataTextured *)[rm getModel:MODEL_SHEET_LEFT];
        } else {
            model = (vertexDataTextured *)[rm getModel:MODEL_SHEET_RIGHT];
        }


        // update uniform values

        glUniformMatrix4fv(basic_lighting_uniforms[BASIC_LIGHTING_UNIFORM_MODEL_VIEW_PROJECTION_MATRIX], 1, GL_FALSE, modelviewProj);

        glUniform1f(basic_lighting_uniforms[BASIC_LIGHTING_UNIFORM_ROTATION], degreeToRadians(-sh.rotation));

        glActiveTexture(GL_TEXTURE0);
        GLuint texture = [rm getTexture:TEXTURE_COLORING_PICTURE Index1:self.currentBook.number Index2:sh.number];
        glBindTexture (GL_TEXTURE_2D, texture);


        glUniform1i(basic_lighting_uniforms[BASIC_LIGHTING_UNIFORM_TEXTURE], 0);

        glVertexAttribPointer(BASIC_LIGHTING_ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(vertexDataTextured), &model[0].vertex);
        glEnableVertexAttribArray(BASIC_LIGHTING_ATTRIB_VERTEX);

        glVertexAttribPointer(BASIC_LIGHTING_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(vertexDataTextured), &model[0].texCoord);
        glEnableVertexAttribArray(BASIC_LIGHTING_ATTRIB_TEX_COORDS);

        glDrawArrays(GL_TRIANGLES, 0, 6);  

        if (![self validateProgram:currProgram]) {
            NSLog(@"Failed to validate program: (%d)", currProgram);
        }
    }
}
}

顶点着色器:

uniform mat4 modelViewProjectionMatrix;
uniform float rotation;
attribute lowp vec3 position;
attribute lowp vec2 texCoords;
varying lowp vec2 fTexCoords;
varying lowp vec4 computed_color;

void main()
{ 
    fTexCoords = texCoords;

    vec4 postmp = vec4(position.xyz, 1.0);
    vec4 newposition = modelViewProjectionMatrix * postmp;
    gl_Position = newposition;

    vec3 ambient_color = vec3(0.5, 0.5, 0.5);
    vec3 norm = vec3(sin(rotation), 0.0, cos(rotation));

    vec3 light_position1 = vec3(15.0, 0.0, 20.0);
    vec3 light_direction1 = normalize( vec3 (0.2, 0.0, 1.0) );
    vec4 light_diffuse_color1 = vec4 (0.4, 0.4, 0.4, 1.0);
    float ndotl1 = max(0.0, dot(light_direction1, norm));
    vec4 temp_color1 = ndotl1 * light_diffuse_color1 * 15.0 / (length(light_position1 - newposition.xyz));

    vec3 light_position2 = vec3(-30.0, 0.0, 25.0);
    vec3 light_direction2 = normalize( vec3(-0.2, 0.0, 1.0) );
    vec4 light_diffuse_color2 = vec4 (0.5, 0.5, 0.5, 1.0);
    float ndotl2 = max(0.0, dot(light_direction2, norm));
    vec4 temp_color2 = ndotl2 * light_diffuse_color2 * 15.0 / (length(light_position2 - newposition.xyz));

    computed_color = vec4(temp_color1.rgb + temp_color2.rgb + ambient_color, 1.0);
}

片段着色器:

precision lowp float;
uniform sampler2D texture;
varying vec2 fTexCoords;
varying lowp vec4 computed_color;

void main()
{
    gl_FragColor = texture2D(texture, fTexCoords) * computed_color;
}

1 个答案:

答案 0 :(得分:1)

问题是位置精度低。感谢Brad Larson。