在 GLKit 中使用 GL_POINTS (iOS 5)

发布于 2025-01-07 17:05:36 字数 2632 浏览 0 评论 0原文

OpenGL 新手,我在 iOS 5 上使用 GLKit 和 GL_LINE_LOOP 成功显示线,但是用 GL_POINTS 显示点给我带来了麻烦。具体来说,我无法显示除单个像素之外的任何内容。我在网上搜索过但没有成功,所以我希望有人能指出我所遗漏的内容。

这是我的测试代码...

//positions and colors, vertices and indices...
typedef struct {
    float Position[2];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{50, 50}, {0, 0, 1, 1}},
    {{200, 50}, {0, 0, 1, 1}},
    {{200, 200}, {0, 0, 1, 1}},
    {{50, 200}, {0, 0, 1, 1}}
};

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


@interface AppViewController() {
    GLuint _vertexBuffer;
    GLuint _indexBuffer;
}

...
//some unrelated properties, the @implementation and other methods
...

- (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;
    [EAGLContext setCurrentContext:self.context];

    self.effect = [[GLKBaseEffect alloc] init];

    GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1024, 1024);
    self.effect.transform.projectionMatrix = projectionMatrix;

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

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

    [self.effect prepareToDraw];

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

    glEnableVertexAttribArray(GLKVertexAttribPosition);        
    glVertexAttribPointer(GLKVertexAttribPosition, 2, 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));

    //this worked for drawing my lines...
    //glLineWidth(10);
    //glDrawElements(GL_LINE_LOOP, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

    //this does NOT work for drawing 10-pixel points. single pixel only.
    glEnable(GL_POINT_SMOOTH);
    glPointSize(10);
    glDrawElements(GL_POINTS, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

感谢您的宝贵时间!

New with OpenGL, I am successfully displaying lines using GLKit on iOS 5 with GL_LINE_LOOP, but displaying points with GL_POINTS is giving me trouble. Specifically, I am unable to display anything but a single pixel. I've scoured the web with no success, so I'm hoping someone can point out what I'm missing.

Here is my test code...

//positions and colors, vertices and indices...
typedef struct {
    float Position[2];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{50, 50}, {0, 0, 1, 1}},
    {{200, 50}, {0, 0, 1, 1}},
    {{200, 200}, {0, 0, 1, 1}},
    {{50, 200}, {0, 0, 1, 1}}
};

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


@interface AppViewController() {
    GLuint _vertexBuffer;
    GLuint _indexBuffer;
}

...
//some unrelated properties, the @implementation and other methods
...

- (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;
    [EAGLContext setCurrentContext:self.context];

    self.effect = [[GLKBaseEffect alloc] init];

    GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1024, 1024);
    self.effect.transform.projectionMatrix = projectionMatrix;

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

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

    [self.effect prepareToDraw];

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

    glEnableVertexAttribArray(GLKVertexAttribPosition);        
    glVertexAttribPointer(GLKVertexAttribPosition, 2, 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));

    //this worked for drawing my lines...
    //glLineWidth(10);
    //glDrawElements(GL_LINE_LOOP, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

    //this does NOT work for drawing 10-pixel points. single pixel only.
    glEnable(GL_POINT_SMOOTH);
    glPointSize(10);
    glDrawElements(GL_POINTS, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2025-01-14 17:05:36

我不认为 glPointSize() 是 OpenGL ES 2.0 的一部分。

尝试

gl_PointSize = 10.0; //has to be a floating point value

在你的顶点着色器中设置......

I don't think glPointSize() is part of OpenGL ES 2.0.

Try setting…

gl_PointSize = 10.0; //has to be a floating point value

…in your vertex shader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文