【问题标题】:OpenGL: Erratic rendering from adapted red book example in COpenGL:来自 C 中改编的红皮书示例的不稳定渲染
【发布时间】:2014-04-01 11:37:01
【问题描述】:

(请参阅最后的更新以解决我的具体情况......这里的问题更简洁,解决方案在这个问题中更彻底地涵盖:Cannot cast array to pointer


我成功地运行了第一个示例(蓝色三角形),然后将其从 C++ 重构为 C99,也成功了。我基于这个 C 版本开始了一个新项目,它通过算法生成点,但在其他功能上与仍在工作的示例相同,并且渲染的图像在不同的执行之间不一致。

程序应在屏幕的上半部分绘制一条蓝色抛物线,但会在一个空的黑框和一条从窗口中心到左或右中点的水平线之间变化。

我开始做一些不同的事情,更“正确”,比如不使用全局变量或在我看来像是滥用枚举(NumVAOs、NumBuffers),但事情并没有像我预期的那样发生越来越多地更改以匹配示例代码。使用-std=c99 -Wall -pedantic 编译一切正常,无论 glew 和 freeglut 是静态链接还是动态链接,结果都是一样的。

我真的看不出有什么问题。我验证了抛物线函数返回了正确的结果,并且数据的格式与书中的相同。由于结果不一致,我怀疑这可能与 malloc 没有清除顶点数组的内存有关,但我使用 calloc 得到了相同的结果。似乎屏幕坐标的行为不正常,我无法想象为什么。

完整来源位于 https://github.com/markmccormack/concentrator

我很确定问题出在我的抛物线代码上,因为这是唯一真正的区别,所以这里是相关部分:

/* parabola.h
 * generate parabolas in the form y = ax^2
 * return a compact set of 2D points describing it
 */

#ifndef __PARABOLA_H__
#define __PARABOLA_H__

#include <GL/glew.h>

GLfloat**
v_parabola_by_a ( GLfloat a, GLuint num_points ) ;

#endif /* __PARABOLA_H__ */

和实施:

#include <stdio.h>
#include <stdlib.h>
#include "parabola.h"

GLfloat**
v_parabola_by_a ( GLfloat a, GLuint num_points ) {
    /* Array of X and Y values */
    GLfloat **points ;
    points = calloc( num_points, sizeof(GLfloat[2]) ) ;
    if( points == NULL ) {
        fprintf( stderr, "Could not allocate memory, exiting.\n" ) ;
        exit(EXIT_FAILURE) ;
    } else {
        int iter_alloc ;
        for( iter_alloc = 0; iter_alloc <= num_points; iter_alloc++ ) {
            points[iter_alloc] = calloc( 2, sizeof(GLfloat) ) ;
        }
    }

    /* Each point is incremented along x by 'inc_x,' for a -1,+1 range */
    GLfloat inc_x = 2.0 / (float)num_points ; 

    int iter_point ;
    for (iter_point = 0; iter_point <= num_points; iter_point++ ) {
        GLfloat x_value = (GLfloat)iter_point * inc_x - 1.0 ;
        GLfloat y_value = x_value * x_value * a ; /* y = a x^2 */
        points[iter_point][0] = x_value ;
        points[iter_point][1] = y_value ;
    }

    return points ;
}

GLfloat** 数组像这样传递给 OpenGL:

GLfloat **points ;
points = calloc( num_points, sizeof(GLfloat[2]) ) ;
if( points == NULL ) {
    fprintf( stdout, "Could not allocate memory.\n" ) ;
    exit( EXIT_FAILURE ) ;
} else {
    int iter_alloc ;
    for( iter_alloc = 0; iter_alloc <= num_points; iter_alloc++ ) {
        points[iter_alloc] = calloc( 2, sizeof(GLfloat) ) ;
    }
}

points = v_parabola_by_a( 1.0, num_points ) ;
/* Create & bind renderable asset */
glGenVertexArrays( NumVAOs, VAOs ) ;
glBindVertexArray( VAOs[Parabola] ) ;
/* Create & bind vertex array */
glGenBuffers( NumBuffers, Buffers ) ;
glBindBuffer( GL_ARRAY_BUFFER, Buffers[ArrayBuffer] ) ;
/* Populate bound vertex array with the asset */
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW ) ;

非常感谢任何帮助或建议!


我遇到的核心问题是我缺乏了解指针与数组的关系,错误地假设在这种情况下两者可以互换使用。具体来说,使用像 n-D 数组这样的指针数组是不正确的,虽然可以以这种方式存储数据,但在将其作为内存块传递之前,必须将其解包到平面数组中。这些是修复程序的更改:

  • 顶点数组从GLfloat**更改为GLfloat*
  • glBufferData 的 size 参数从 sizeof(points) 更改为 num_points*2*sizeof(GLfloat)
  • 生成顶点数组的函数已从GLfloat** v_parabola_by_a( float a, int num_points ) ; 更改为void v_parabola_by_a( float a, int num_points, GLfloat* output) ;,其中memcpy(output, points, num_points*2*sizeof(GLfloat)) 代替了return(points),从而更正了对指针的多重赋值。

这不是严格的建议,但我相信这是一个可行的解决方案。

【问题讨论】:

    标签: c arrays pointers opengl


    【解决方案1】:
    glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW ) ;
    

    这是错误的。 glBufferData 需要平面数组,而不是指向浮点数组的指针数组。另外,sizeof(points) 只是一个指针的大小,而不是数组的大小。

    分配 2 个浮点数的数组是没有意义的。创建一个可以容纳所有数据的大数组,并填充它 [0] = first_x, [1] = first_y, [2] = second_x, ...

    【讨论】:

    • 我实际上首先尝试过这种方式,我下面的示例以这种方式为两个三角形提供了一个数组“vertices[6][2]”,它可以工作。我会做出建议的更改,看看效果如何。感谢您的快速回复
    • 做到了,可变长度数组为一个元素提供 sizeof(),因此将 sizeof(GLfloat) 乘以调用 glBufferData 中的元素数就可以得到完整的集合。非常感谢,我知道它必须非常简单,我只是很愚蠢。 glBufferData 调用看不出 [32] 和 [16][2] 之间的区别,但无论如何我都更喜欢平面数组。
    • 没有。 float** 内存布局和float [N][M] 之间存在显着差异。在后一种情况下,数组实际上会展开为一维数组,因此您可以将其用作普通数组。在第一种情况下,它将是指向数组的指针数组 - 这绝不是一维的,并且不能与 glBufferData 一起使用。您使用的是指针数组,所以我指出的实际上是一个错误。
    • 有趣,在进一步阅读了指针和数组之后,我明白了你的意思,这是苹果到橘子的意思,或者也许通过产品过道的方向更合适。这无疑提高了我对指针的理解,并表明我在 C 语言中一直在处理数组错误。感谢您帮助纠正该问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多