【发布时间】: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),从而更正了对指针的多重赋值。
这不是严格的建议,但我相信这是一个可行的解决方案。
【问题讨论】: