2013-10-11 22 views
0

我目前正在使用OpenGL ES 2.0開發一些OpenGL基礎。使用指針不工作來設置頂點

如果我將頂點設置爲頂部的常量,則三角形將被繪製;如果我嘗試在使用指針的方法中設置它們,則不會繪製任何內容。 我錯過了什麼?

#import "CometGLViewController.h" 

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

//vertices store information for each "point" used to draw a triangle 
Vertex* Vertices; // NOT DRAWING 
//const Vertex Vertices[] = { 
// {{0, 0, 0},{0, 0, 0, 1}}, 
// {{1, 1, 0}, {1, 0, 0, 1}}, 
// {{0, 1, 0}, {1, 0, 0, 1}}, 
//}; // THIS WORKS 

//Used to reuse vertices 
const GLubyte Indices[] = { 
    0, 1, 2, 
}; 

...一些其他的東西

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

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

    if (!self.context) { 
     NSLog(@"Failed to create ES context"); 
    } 

    GLKView *view = (GLKView *)self.view; 
    view.context = self.context; 
    view.drawableMultisample = GLKViewDrawableMultisample4X; 
    [self setupVertices]; 
    [self setupGL]; 
} 

...

#pragma mark - GLKViewDelegate 

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{ 
    glClearColor(_curRed, 0.5, 0.5, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    [self.effect prepareToDraw]; 

    glBindVertexArrayOES(_vertexArray); 
    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0); 
} 

...

#pragma mark - OpenGL stuff 
- (void)setupGL { 

    [EAGLContext setCurrentContext:self.context]; 
    glEnable(GL_CULL_FACE); 

    glGenVertexArraysOES(1, &_vertexArray); 
    glBindVertexArrayOES(_vertexArray); 
// ----- create new buffer, work with "vertexBuffer", glBufferData sends data for opengl usage 
    glGenBuffers(1, &_vertexBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 

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

// ----- Setup vertices attributes 
    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position)); 
    glEnableVertexAttribArray(GLKVertexAttribColor); 
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color)); 
} 

- (void)tearDownGL { 

    [EAGLContext setCurrentContext:self.context]; 

    glDeleteBuffers(1, &_vertexBuffer); 
    glDeleteBuffers(1, &_indexBuffer); 
    glDeleteVertexArraysOES(1, &_vertexArray); 

    self.effect = nil; 

} 

- (void)setupVertices 
{ // test 
    int points = 3; 
    Vertices = malloc(sizeof(Vertex) * points); 
    Vertex v = {{0, 0, 0}, {0, 0, 0, 1}}; 
    Vertices[0] = v; 
    Vertex v1 = {{1, 1, 0}, {0, 1, 0, 1}}; 
    Vertices[1] = v1; 
    Vertex v2 = {{0, 1, 1}, {1, 0, 0, 1}}; 
    Vertices[2] = v2; 
} 

@end 
+0

Objective C對我來說是完全不可讀的,但我可以發誓,在這段代碼中沒有任何地方可以實際分配或填充任何數據的「* Vertices」。如果您可以包含該代碼,那將有所幫助。 –

+1

他確實填充了頂點。它是在他的頭文件中定義的,並且當調用viewDidLoad方法時,調用setUpOpenGL,然後調用setUpVertices來分配和填充它(也許他在發佈後編輯過,但我沒有看到編輯)。 –

+0

@BenPious:哇,你說得對。我最近升級到了OS X 10.8,現在它隱藏了代碼清單中的滾動條,所以我錯過了整個函數:' - (void)setupVertices'。我一定要弄清楚這個設置在哪裏,並改變它:) –

回答

2

變化

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 

glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * 3, Vertices, GL_STATIC_DRAW); 

您的問題是的sizeof(頂點)的工作方式不同的三分球比在棧上分配的數組。

+0

不,不工作:(沒有形狀繪製 – Timm

+0

nvm,工作!!非常感謝! – Timm