简体   繁体   English

将带纹理的3D模型从.obj文件导入到iOS OpenGl ES 2.0

[英]Import textured 3d model from .obj file to iOs OpenGl E.S 2.0

Im am currently trying to convert 3D models in .obj format with texture to Objective c compatible code and render them with OpenGL ES 2.0. 我目前正在尝试将具有纹理的.obj格式的3D模型转换为与Objective c兼容的代码,并使用OpenGL ES 2.0进行渲染。

I am using [obj2opengl][1] from Heiko Behrens to convert the 3d model to a header file 我正在使用Heiko Behrens的[obj2opengl] [1]将3d模型转换为头文件

The 3d model has UV Mapping with this texture image. 3d模型具有纹理图像的UV贴图。

Everything is almost working, but somehow the textures are not rendered correctly and my cube shows up like this . 一切都几乎工作,但不知何故,纹理不能正确渲染和我的立方体显示像这样

I have tried all sorts of things but cannot figure out what i am doing wrong or i just don't get it. 我已经尝试过各种方法,但是无法弄清楚我在做什么错,或者我只是不明白。

Here is my code on how i render the cube: 这是我如何渲染多维数据集的代码:

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,
};

And my rendering code: 和我的渲染代码:

-(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: The appDelegate function where i set up my context. 更新:我在其中设置上下文的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;
}

My Scenes render function clears the buffer and calls the above renderInScene method on every object 我的场景渲染函数清除缓冲区并在每个对象上调用上述renderInScene方法

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

You need to set up your depth buffer 您需要设置深度缓冲区

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);
}

and then add to your frame buffer 然后添加到您的帧缓冲区

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM