2

まず第一に、私はフランス人なので、私の英語で申し訳ありません.

私は OpenGL ES が初めてで、これらの Vertices で単純な三角形を描画しようとしています:

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{0.0, 1.0, -2.0}, {1, 0, 0, 1}},
    {{1.0, 0.0, -2.0}, {1, 0, 0, 1}},
    {{-1.0, 0.0, -2.0}, {1, 0, 0, 1}},
};

およびこれらのインデックス;

const GLubyte Indices[] = {
    0,1,2
};

しかし、三角形は表示されません... インデックスを 0,2,3 に変更すると、頂点 0,2 と黒い頂点を持つ三角形が表示されます: {{0,0,0},{0,0, 0,1}} ですが、4 番目の頂点がありません... まったくわかりません。

これが私のXcodeビューコードです:

//
//  EAGLView.m
//  OpenGlintro
//
//  Created by Arnaud Miguet on 01/12/12.
//  Copyright (c) 2012 Tap‘n'Develop. All rights reserved.
//

#import "EAGLView.h"

@implementation EAGLView
+ (Class) layerClass {
    return [CAEAGLLayer class];
}

- (void)setupVBOs {

    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    GLuint indexBuffer;
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CAEAGLLayer *EAGLLayer = (CAEAGLLayer *) super.layer;
        EAGLLayer.opaque = YES;

        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

        if (!context || ![EAGLContext setCurrentContext:context]) {
            [self release];
            return nil;
        }

        GLuint framebuffer , renderbuffer;

        glGenBuffers(1, &framebuffer);
        glGenBuffers(1, &renderbuffer);

        glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);

        [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:EAGLLayer];
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
        glViewport(10, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
        [self compileShaders];
        [self setupVBOs];
        [self render];
    }
    return self;
}


- (void)render {
    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    // 1
    glViewport(0, 0, self.frame.size.width, self.frame.size.height);

    // 2
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
                      sizeof(Vertex), 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
                      sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));

    // 3
    glDrawElements(GL_TRIANGLES, sizeof(Indices),
               GL_UNSIGNED_BYTE, 0);

    [context presentRenderbuffer:GL_RENDERBUFFER];
}

- (GLuint)compileShader:(NSString*)shaderName withType:(GLenum)shaderType {

    // 1
    NSString* shaderPath = [[NSBundle mainBundle] pathForResource:shaderName
                                                       ofType:@"glsl"];
    NSError* error;
    NSString* shaderString = [NSString stringWithContentsOfFile:shaderPath
                                                   encoding:NSUTF8StringEncoding error:&error];
    if (!shaderString) {
        NSLog(@"Error loading shader: %@", error.localizedDescription);
        exit(1);
    }

    // 2
    GLuint shaderHandle = glCreateShader(shaderType);

    // 3
    const char * shaderStringUTF8 = [shaderString UTF8String];
    int shaderStringLength = [shaderString length];
    glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength);

    // 4
    glCompileShader(shaderHandle);

    // 5
    GLint compileSuccess;
    glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
    if (compileSuccess == GL_FALSE) {
        GLchar messages[256];
        glGetShaderInfoLog(shaderHandle, sizeof(messages), 0, &messages[0]);
        NSString *messageString = [NSString stringWithUTF8String:messages];
        NSLog(@"%@", messageString);
        exit(1);
    }

    return shaderHandle;

}

- (void)compileShaders {

    // 1
    GLuint vertexShader = [self compileShader:@"SimpleVertex"
                                 withType:GL_VERTEX_SHADER];
    GLuint fragmentShader = [self compileShader:@"SimpleFragment"
                                   withType:GL_FRAGMENT_SHADER];

    // 2
    GLuint programHandle = glCreateProgram();
    glAttachShader(programHandle, vertexShader);
    glAttachShader(programHandle, fragmentShader);
    glLinkProgram(programHandle);

    // 3
    GLint linkSuccess;
    glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
    if (linkSuccess == GL_FALSE) {
        GLchar messages[256];
        glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]);
        NSString *messageString = [NSString stringWithUTF8String:messages];
        NSLog(@"%@", messageString);
        exit(1);
    }

    // 4
    glUseProgram(programHandle);

    // 5
    _positionSlot = glGetAttribLocation(programHandle, "Position");
    _colorSlot = glGetAttribLocation(programHandle, "SourceColor");
    glEnableVertexAttribArray(_positionSlot);
    glEnableVertexAttribArray(_colorSlot);
}

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{0.0, 1.0, -2.0}, {1, 0, 0, 1}},
    {{1.0, 0.0, -2.0}, {1, 0, 0, 1}},
    {{-1.0, 0.0, -2.0}, {1, 0, 0, 1}},
};

const GLubyte Indices[] = {
    0,2,3
};

@end
4

1 に答える 1

0

バッファーをバインドする前に、頂点配列を追加します。

GLuint vao;
glGenVertexArray(1, &vao);
glBindVertexArray(vao);
于 2012-12-01T12:31:06.440 に答える