开发者

Problem with getting simple VBO to work on Mac

I am trying to rework the accepted answer of this question to work in a Cocoa application.

I have made a class called OpenGLView that is a subclass of NSOpenGLView and in my Xib file I have an OpenGL view with a custom class of OpenGLView.

And I have made my OpenGLView class implementation the following:

#import "OpenGLView.h"
#include <OpenGL/OpenGL.h>
#include <OpenCL/OpenCL.h>

@implementation OpenGLView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- 开发者_运维知识库(void)drawRect:(NSRect)dirtyRect
{    
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glShadeModel(GL_FLAT);
    glEnableClientState(GL_VERTEX_ARRAY);
    float data[][2] = {{50,50},{100,50},{75,100}};
    glGenBuffers(1,&ID);
    glBindBuffer(GL_ARRAY_BUFFER, ID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f,0.0f,0.0f);
    glBindBuffer(GL_ARRAY_BUFFER, ID);
    glVertexPointer(3, GL_FLOAT, 3*sizeof(float), 0);
    glDrawArrays(GL_TRIANGLES,0,3);
    glFlush(); 

}

@end

Basically, (with the exception of glewInit()) I have copied the init method and copied and pasted the display() functions contents in my -(void)drawRect method. When I run the program I get no errors or warning, no runtime errors, but I get only a black screen with no triangle. But, there is a white screen, as the glClear(GL_COLOR_BUFFER_BIT) should do.


Let's start with the basics:

float data[][2] = {{50,50},{100,50},{75,100}};

This is an array of 3 pairs of floats. So each vertex position contains 2 floats. With 3 vertices, that's 6 floats total.

glVertexPointer(3, GL_FLOAT, 3*sizeof(float), 0);

You're telling OpenGL that you are giving it 3 floats per vertex (the first 3). You put 6 floats in the buffer object. Since you're rendering a single triangle, you need 3 vertices, which means OpenGL will try to read nine floats.

Indeed, you seem to have copied this wrong somehow, as the original one is correct, for the data:

glVertexPointer(2, GL_FLOAT, 2*sizeof(float), 0);

Now, that may not be the only thing wrong with your code, as you didn't post the initialization routines that were used in the answer you linked to. Considering that you put the code from the answer's "init" function into your "dirtyRect" function, I'd guess you have bigger problems.

There is a reason why the answer had separate "init" and "display". Because some things are initialization (the creation of the buffer object and setting of various state), while other things are things you do every time (clearing the buffer, setting the pointer and glDrawArrays).

You should find a way to maintain this separation of initialization from updating in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜