Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl 在GLVertexAttributePointer中使用非零步长时,渲染完全失败_Opengl - Fatal编程技术网

Opengl 在GLVertexAttributePointer中使用非零步长时,渲染完全失败

Opengl 在GLVertexAttributePointer中使用非零步长时,渲染完全失败,opengl,Opengl,我正在尝试编写一个简单的OpenGL3程序。当顶点缓冲区包含压缩位置元素时,它可以正常工作,但当每个顶点都有其他数据成员时,它就不能正常工作 当Vertex被类型定义为SimpleVertex或ComplexVertex时,下面的代码的工作原理应该是相同的,但它不是 struct SimpleVertex { glm::vec3位置; SimpleVertex(float x,float y,float z):位置(x,y,z){ }; 结构复合顶点 { glm::vec3位置; glm::ve

我正在尝试编写一个简单的OpenGL3程序。当顶点缓冲区包含压缩位置元素时,它可以正常工作,但当每个顶点都有其他数据成员时,它就不能正常工作

当Vertex被类型定义为SimpleVertex或ComplexVertex时,下面的代码的工作原理应该是相同的,但它不是

struct SimpleVertex
{
glm::vec3位置;
SimpleVertex(float x,float y,float z):位置(x,y,z){
};
结构复合顶点
{
glm::vec3位置;
glm::vec3 normal;//在本例中将被忽略
复数顶点(浮点x,浮点y,浮点z):位置(x,y,z){
};
//当顶点为ComplexVertex时失败
typedef SimpleVertex顶点;
GLuint垂直id;
胶合指数;
闪烁att_位置;
初始化代码:

顶点顶点[]=
{
{0, 0, 0},
{1, 0, 0},
{0, 1, 0}
};
GLubyte索引[]={0,1,2};
glGenBuffers(1,&vert_id);
断言(0!=垂直id);
glBindBuffer(GL_数组_BUFFER,vert_id);
glBufferData(GL_数组_缓冲区、大小(顶点)和顶点[0],GL_静态_绘制);
glGenBuffers(1,&index_id);
断言(0!=索引\u id);
glBindBuffer(GL\u元素\u数组\u缓冲区,索引\u id);
glBufferData(GL元素数组缓冲区、sizeof(索引)和索引[0],GL静态绘图);
att_position=GLGetAttriblLocation(着色器_程序_id,“位置”);
断言(附件位置>=0);
断言(GL_NO_ERROR==glGetError());
渲染循环:

Vertex*dummy=nullptr;
断言(sizeof(glm::vec3)==3*sizeof(GLfloat));
//从理论上讲,无论大小,这都应该有效
//顶点成员的排列。
glvertexattributepointer(
att_位置,
3.
浮球,
GL_FALSE,
//这对于SimpleVertex是0,对于ComplexVertex是12,正如预期的那样
sizeof(顶点)-3*sizeof(GLfloat),
&(虚拟->位置);
断言(GL_NO_ERROR==glGetError());
GlenableVertexAttributeArray(附件位置);
glBindBuffer(GL_数组_BUFFER,vert_id);
glBindBuffer(GL\u元素\u数组\u缓冲区,索引\u id);
微量元素(
GL_三角形,
3.
GL_无符号字节,
nullptr);
顶点着色器:

#version 130

attribute vec3 position;

void main ()
{
    gl_Position = vec4 (position, 1);
}
片段着色器:

#version 130

out vec4 finalColor;

void main ()
{
    finalColor = vec4 (1.0, 1.0, 1.0, 1.0);
}
当顶点是SimpleVertex时,我得到一个未转换的白色三角形,很好。当我把它键入ComplexVertex时,我什么也得不到。发生了什么?谢谢

// This is 0 for SimpleVertex, 12 for ComplexVertex, as expected
这是你的问题。因为这肯定不是OpenGL所期望的

跨距是数组中从一个属性开始到下一个属性开始的字节偏移量。它是实现必须在该等式中使用的值,用于计算指向数组的
i
th元素的指针:

baseOffset + (stride * i)
允许您跨步通过0。这告诉OpenGL该属性是紧密压缩的,所以GL将自己计算实际的步幅。但这并不意味着步幅实际上是零;它只是告诉OpenGL自己计算

只要
type
是存储在数组中的实际类型,就应该使用
sizeof(type)