Objective c opengles不会画画

Objective c opengles不会画画,objective-c,ios,xcode,opengl-es,3d,Objective C,Ios,Xcode,Opengl Es,3d,我已经编写了一个类,目前允许通过数组和变量(指定数组中变量的数量)加载三维模型。代码对我来说似乎很好,但没有在屏幕上画任何东西 这是顶点结构 typedef struct { float pos[3]; float texCoord[2]; float colour[4]; } Vertex; 这是初始化模型的方法 - (id)VModelWithArray:(Vertex[])Vertices count:(unsigned short)count { sel

我已经编写了一个类,目前允许通过数组和变量(指定数组中变量的数量)加载三维模型。代码对我来说似乎很好,但没有在屏幕上画任何东西

这是顶点结构

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;
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];
@property (strong, nonatomic) VModel *model;
[self.model CreateVBO];
它在我的视图控制器中被这样调用

@property (nonatomic, assign) Vertex *Vertices;
@property (nonatomic, assign) unsigned short count;
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];
@property (strong, nonatomic) VModel *model;
[self.model CreateVBO];
模型类在标头中声明,如下所示

@property (nonatomic, assign) Vertex *Vertices;
@property (nonatomic, assign) unsigned short count;
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];
@property (strong, nonatomic) VModel *model;
[self.model CreateVBO];
VBO是通过此方法创建的

- (void)CreateVBO
{
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*self.count, self.Vertices, GL_STATIC_DRAW);
}
- (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];
}
在视图控制器中初始化模型后,将调用它,如下所示

@property (nonatomic, assign) Vertex *Vertices;
@property (nonatomic, assign) unsigned short count;
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];
@property (strong, nonatomic) VModel *model;
[self.model CreateVBO];
模型是用这种方法渲染的

- (void)CreateVBO
{
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*self.count, self.Vertices, GL_STATIC_DRAW);
}
- (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];
}
它在这个方法中被调用

- (void)CreateVBO
{
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*self.count, self.Vertices, GL_STATIC_DRAW);
}
- (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];

我明白了,在合并前一个项目的代码时,我不小心漏掉了这些行

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