OpenGL:C中改编的红皮书示例的不稳定渲染

时间:2014-04-01 11:37:02

标签: c arrays pointers opengl

(请参阅最后的更新以解决我的具体案例......此处的问题更加简明扼要,此问题的解决方案更为全面:Cannot cast array to pointer

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

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

我开始做的事情有点不同,更多&#34;正确,&#34;喜欢不使用全局变量或看起来像滥用枚举的东西(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),这更正了指针的多重赋值。

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

1 个答案:

答案 0 :(得分:1)

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

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

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