objective-c - OpenGL ES 不会绘制

标签 objective-c ios xcode opengl-es 3d

我编写了一个类,目前允许通过数组和变量(指定数组中变量的数量)加载 3D 模型。代码对我来说似乎没问题,但没有在屏幕上绘制任何内容。

这是顶点结构。

typedef struct
{
    float pos[3];
    float texCoord[2];
    float colour[4];
} Vertex;

这就是模型的初始化方式。

- (id)VModelWithArray:(Vertex[])Vertices count:(unsigned short)count
{
    self.Vertices = Vertices;
    self.count = count;
    return self;
}

这两个类变量是这样在头文件中声明的。

@property (nonatomic, assign) Vertex *Vertices;
@property (nonatomic, assign) unsigned short count;

它在我的 View Controller 中是这样调用的。

Vertex array[] = {
        {{1, -1, 0}, {1, 0}, {1, 0, 0, 1}},
        {{1, 1, 0}, {1, 1}, {0, 1, 0, 1}},
        {{-1, 1, 0}, {0, 1}, {1, 1, 1, 1}}
    };

    unsigned short elements = sizeof(array)/sizeof(Vertex);
    self.model = [[VModel alloc] VModelWithArray:array count:elements];

模型类在 header 中声明如下。

@property (strong, nonatomic) VModel *model;

VBO 是通过这种方法创建的。

- (void)CreateVBO
{
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*self.count, self.Vertices, GL_STATIC_DRAW);
}

像这样在 View Controller 中初始化模型后调用它。

[self.model CreateVBO];

模型是通过这个方法渲染的。

- (void)Render
{
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, pos));
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, texCoord));
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, colour));
    glDrawArrays(GL_TRIANGLES, 0, self.count);
    glDisableVertexAttribArray(GLKVertexAttribPosition);
    glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
    glDisableVertexAttribArray(GLKVertexAttribColor);
}

在这个方法中调用。

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{
    glClearColor(0.5, 0.5, 0.5, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    [self.effect prepareToDraw];
    [self.model Render];
}

非常感谢任何帮助。

最佳答案

我想通了,我在合并以前项目的代码时不小心遗漏了这些行。

[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];

关于objective-c - OpenGL ES 不会绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194123/

相关文章:

ios - 我如何为 objective-c 中的按钮设置动态约束

objective-c - NSPredicate 'OR' 基于 NSArray 键的过滤

ios - Swift - UIImagePickerController - 如何使用它?

iphone - 如何将第二行 UIPickerView 保存到 NSUserDefaults?

ios - 如何在 Swift 中创建弱引用字典?

iphone - 在另一个数组中搜索数组元素 - iphone 应用程序

ios - 使用 NSPredicate 过滤 NSArray 并找到相似的字符串

c++ - 测量库调用和回调之间的时间

ios - ARKit 插入 VC 捕获事件 Swift 4

ios - 找不到用于 -lAFNetworking Xcode 问题的库