简体   繁体   English

OpenGL ES无法绘制

[英]OpenGL ES Wont draw

I have written a class that currently allows the loading of 3D Models via an array and a variable(specifying the amount of variables in the array). 我编写了一个类,该类当前允许通过数组和变量(指定数组中变量的数量)加载3D模型。 The code seems fine to me but doesn't draw anything on the screen. 该代码对我来说似乎很好,但在屏幕上什么也没画。

This is the Vertex Struct. 这是“顶点结构”。

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

This is the how the Model is initialised. 这就是初始化模型的方式。

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

The two class variables are declared in the header like so. 像这样在标题中声明了两个类变量。

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

It is called in my view controller like so. 像这样在我的视图控制器中调用它。

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];

The Model class is declared in the header like so. 像这样在标头中声明Model类。

@property (strong, nonatomic) VModel *model;

The VBOs are created by this method. VBO是通过这种方法创建的。

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

It is called after the Model is initialised in the view controller like so. 像这样在视图控制器中初始化模型后,将调用该方法。

[self.model CreateVBO];

The Model is rendered by this method. 通过此方法呈现模型。

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

It is called in this method. 在此方法中称为。

- (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];
}

Any help would be very much appreciated. 任何帮助将不胜感激。

I figured it out, I accidentally left out these lines when merging the code from a previous project. 我弄清楚了,合并上一个项目中的代码时,我不小心遗漏了这些行。

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

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

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