2013-04-04 83 views
1

我正在嘗試將具有紋理的.obj格式的3D模型轉換爲Objective c兼容代碼並使用OpenGL E.S. 2.0。從.obj文件導入帶紋理的3d模型到iOs OpenGl E.S 2.0

我使用[obj2opengl] [1]從的Heiko Behrens的3D模型,以轉換爲一個標題文件

所述3D模型具有this紋理圖像UV映射。

一切都快成功了,但不知何故紋理渲染不正確,我的立方體顯示爲this

我已經嘗試了各種各樣的東西,但無法弄清楚我做錯了什麼,或者我只是不明白。

這是我對我如何呈現立方體代碼:

unsigned int boxNumVerts = 36; 

float boxVerts [] = { 
    // f 1/1/1 2/2/1 3/3/1 4/4/1 
    -0.5, -0.5, 0.5, 
    -0.5, -0.5, -0.5, 
    0.5, -0.5, -0.5, 
    // f 1/1/1 2/2/1 3/3/1 4/4/1 
    -0.5, -0.5, 0.5, 
    0.5, -0.5, 0.5, 
    0.5, -0.5, -0.5, 
    // f 5/5/2 6/6/2 7/7/2 8/8/2 
    -0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5, 
    0.5, 0.5, -0.5, 
    // f 5/5/2 6/6/2 7/7/2 8/8/2 
    -0.5, 0.5, 0.5, 
    -0.5, 0.5, -0.5, 
    0.5, 0.5, -0.5, 
    // f 1/9/3 4/10/3 6/6/3 5/5/3 
    -0.5, -0.5, 0.5, 
    0.5, -0.5, 0.5, 
    0.5, 0.5, 0.5, 
    // f 1/9/3 4/10/3 6/6/3 5/5/3 
    -0.5, -0.5, 0.5, 
    -0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5, 
    // f 4/11/4 3/12/4 7/7/4 6/6/4 
    0.5, -0.5, 0.5, 
    0.5, -0.5, -0.5, 
    0.5, 0.5, -0.5, 
    // f 4/11/4 3/12/4 7/7/4 6/6/4 
    0.5, -0.5, 0.5, 
    0.5, 0.5, 0.5, 
    0.5, 0.5, -0.5, 
    // f 3/13/5 2/14/5 8/8/5 7/7/5 
    0.5, -0.5, -0.5, 
    -0.5, -0.5, -0.5, 
    -0.5, 0.5, -0.5, 
    // f 3/13/5 2/14/5 8/8/5 7/7/5 
    0.5, -0.5, -0.5, 
    0.5, 0.5, -0.5, 
    -0.5, 0.5, -0.5, 
    // f 2/2/6 1/1/6 5/5/6 8/8/6 
    -0.5, -0.5, -0.5, 
    -0.5, -0.5, 0.5, 
    -0.5, 0.5, 0.5, 
    // f 2/2/6 1/1/6 5/5/6 8/8/6 
    -0.5, -0.5, -0.5, 
    -0.5, 0.5, -0.5, 
    -0.5, 0.5, 0.5, 
}; 


float boxTexCoords [] = { 
    // f 1/1/1 2/2/1 3/3/1 4/4/1 
    0.5016, 0.7508, 
    0.2504, 0.7508, 
    0.2504, 1.002, 
    // f 1/1/1 2/2/1 3/3/1 4/4/1 
    0.5016, 0.7508, 
    0.5016, 1.002, 
    0.2504, 1.002, 
    // f 5/5/2 6/6/2 7/7/2 8/8/2 
    0.5016, 0.4996, 
    0.5016, 0.2484, 
    0.2504, 0.2484, 
    // f 5/5/2 6/6/2 7/7/2 8/8/2 
    0.5016, 0.4996, 
    0.2504, 0.4996, 
    0.2504, 0.2484, 
    // f 1/9/3 4/10/3 6/6/3 5/5/3 
    0.7528, 0.4996, 
    0.7528, 0.2484, 
    0.5016, 0.2484, 
    // f 1/9/3 4/10/3 6/6/3 5/5/3 
    0.7528, 0.4996, 
    0.5016, 0.4996, 
    0.5016, 0.2484, 
    // f 4/11/4 3/12/4 7/7/4 6/6/4 
    0.5016, -0.00279999999999991, 
    0.2504, -0.00279999999999991, 
    0.2504, 0.2484, 
    // f 4/11/4 3/12/4 7/7/4 6/6/4 
    0.5016, -0.00279999999999991, 
    0.5016, 0.2484, 
    0.2504, 0.2484, 
    // f 3/13/5 2/14/5 8/8/5 7/7/5 
    -0.0008, 0.2484, 
    -0.0008, 0.4996, 
    0.2504, 0.4996, 
    // f 3/13/5 2/14/5 8/8/5 7/7/5 
    -0.0008, 0.2484, 
    0.2504, 0.2484, 
    0.2504, 0.4996, 
    // f 2/2/6 1/1/6 5/5/6 8/8/6 
    0.2504, 0.7508, 
    0.5016, 0.7508, 
    0.5016, 0.4996, 
    // f 2/2/6 1/1/6 5/5/6 8/8/6 
    0.2504, 0.7508, 
    0.2504, 0.4996, 
    0.5016, 0.4996, 
}; 

我的渲​​染代碼:

-(void)setup 
{ 
    self.drawingMode = GL_TRIANGLES; 
    vertexData = [NSMutableData dataWithBytes:boxVerts length:sizeof(float)*boxNumVerts*3]; 
    textureCoordinateData = [NSMutableData dataWithBytes:boxTexCoords length:sizeof(float)*boxNumVerts*2]; 
    // vertexNormalData = [NSMutableData dataWithBytes:PrisonCellNormals length:sizeof(float)*PrisonCellNumVerts*3]; 
} 

-(void)renderInScene:(FRPGScene *)_scene 
{ 
    if (texture != nil) 
     { 
      effect.texture2d0.envMode = GLKTextureEnvModeReplace; 
      effect.texture2d0.target = GLKTextureTarget2D; 
      effect.texture2d0.name = texture.name; 
      [effect.texture2d0 setEnabled:YES]; 
     } 

     effect.transform.modelviewMatrix = self.modelviewMatrix; 
     effect.transform.projectionMatrix = _scene.projectionMatrix; 

     [effect prepareToDraw]; 

     // setup 
     glEnable(GL_BLEND); 

     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

     glEnableVertexAttribArray(GLKVertexAttribPosition); 

     glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, self.vertices); 

     if (!useConstantColor) 
     { 
      glEnableVertexAttribArray(GLKVertexAttribColor); 
      glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, self.vertexColors); 
     } 

     if (texture != nil) 
     { 
      glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 
      glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates); 
     } 

     glDrawArrays(drawingMode, 0, self.numVertices); 

     glDisable(GL_BLEND); 

     // teardown 
     glDisableVertexAttribArray(GLKVertexAttribPosition); 

     if (!useConstantColor) 
     { 
      glDisableVertexAttribArray(GLKVertexAttribColor); 
     } 

     if (texture != nil) 
     { 
      glDisableVertexAttribArray(GLKVertexAttribTexCoord0); 
     } 
} 

UPDATE: 該功能的appDelegate哪裏設置我的背景。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 
    [EAGLContext setCurrentContext:context]; 

    GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:context]; 
    view.delegate = self; //Makes sure the views drawInRect is called 

    FRPGViewController *controller = [[FRPGViewController alloc] init]; 
    controller.delegate = self; 
    controller.view = view; 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.rootViewController = controller; 
    [self.window makeKeyAndVisible]; 

    scene = [[FRPGTestLevel alloc] init]; 

    //Set screen aspect ratio boundaries ipad 4:3 iphone 3:2 
    CGRect rect = view.bounds; 

    scene.left = -100.0f; 
    scene.right = 100.0f; 
    scene.bottom = -100.0f/(rect.size.width/rect.size.height); // -75.0.. 
    scene.top = 100.0f/(rect.size.width/rect.size.height); // 75.0.. 
    [scene setup]; 

    [controller setScene:scene]; 


    return YES; 
} 

我的場景渲染功能,清除緩衝區,並調用上述renderInScene方法中的每個對象

-(void)render 
{ 
    glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); 
    glClear(GL_COLOR_BUFFER_BIT); 
    [object renderInScene:scene]; 
} 
+0

你應該發佈你的代碼來設置OpenGL上下文。你有沒有深度緩衝?我注意到你沒有*清除你的深度緩衝區。對glClear的調用應該像glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);' – 2013-04-04 20:08:29

+0

對我來說,這看起來像是一個前/後面的問題。我沒有瀏覽所有頂點,但應確保按逆時針順序定義所有多邊形,以告訴OpenGL這是多邊形的正面。嘗試使用背面剔除不使用'glCullFace(GL_BACK);'和'glEnable(GL_CULL_FACE);' – guitarflow 2013-04-04 20:48:07

+0

感謝您的時間來繪製多邊形的邊。我已更新我的帖子並添加了上下文創建。我按照你的建議嘗試了剔除面部設置,但這給了我一個更奇怪的效果。 http://imageshack.us/photo/my-images/27/test2ic.png/ – user2237669 2013-04-04 21:10:47

回答

0

你需要設置你的深度緩衝

GLuint _depthRenderBuffer; 

- (void)setupDepthBuffer 
{ 
    glGenRenderbuffers(1, &_depthRenderBuffer); 
    glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderBuffer); 
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, self.frame.size.width, self.frame.size.height); 
} 

,然後添加到您的幀緩衝區

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);