glDrawArraysInstanced 无法正常工作
glDrawArraysInstanced not working properly
我是 opengl 新手
我正在创建两个立方体
当我使用 glDrawArraysInstanced 时。根据我的矩阵变换,我没有得到预期的结果
这是我使用不同数据的代码
float fovy=tanf(GLKMathDegreesToRadians(30));
float nearplane=0.1f;
float farplane=10.0f;
float aspectratio=self.view.frame.size.width/self.view.frame.size.height;
float height=fovy*nearplane;
float width=height*aspectratio;
GLKMatrix4 frustum=GLKMatrix4MakeFrustum(-width, +width, -height, height, nearplane, farplane);
GLKMatrix4 translate=GLKMatrix4Translate(frustum, 0, 0.9, -3);
GLKMatrix4 rotatey= GLKMatrix4Rotate(translate, GLKMathDegreesToRadians(54), 0, 1, 0);
GLKMatrix4 translate1=GLKMatrix4Translate(frustum, 0, -0.5, -3);
GLKMatrix4 rotatey1= GLKMatrix4Rotate(translate1, GLKMathDegreesToRadians(54), 1, 1, 0);
GLKMatrix4 matarray[]=
{
rotatey,rotatey1
};
glGenBuffers(1, &matbuffer);
glBindBuffer(GL_ARRAY_BUFFER, matbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(matarray), matarray, GL_STATIC_DRAW);
glVertexAttribPointer(fulltransform,4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*0));
glVertexAttribPointer(fulltransform+1, 4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*4));
glVertexAttribPointer(fulltransform+2, 4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*8));
glVertexAttribPointer(fulltransform+3, 4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*12));
glVertexAttribDivisor(fulltransform, 1);
glVertexAttribDivisor(fulltransform+1, 1);
glVertexAttribDivisor(fulltransform+2, 1);
glVertexAttribDivisor(fulltransform+3, 1);
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArraysInstanced(GL_TRIANGLES, 0, numvertex,2);
这是我的结果
http://i.stack.imgur.com/RWAWQ.png
如果我使用统一变量并使用 gldrawarray 两次,那么我会得到完美的结果。这是我的代码和结果
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float fovy=tanf(GLKMathDegreesToRadians(30));
float nearplane=0.1f;
float farplane=10.0f;
float aspectratio=self.view.frame.size.width/self.view.frame.size.height;
float height=fovy*nearplane;
float width=height*aspectratio;
GLKMatrix4 frustum=GLKMatrix4MakeFrustum(-width, +width, -height, height, nearplane, farplane);
GLKMatrix4 translate=GLKMatrix4Translate(frustum, 0, 0.9, -3);
GLKMatrix4 rotatey= GLKMatrix4Rotate(translate, GLKMathDegreesToRadians(54), 0, 1, 0);
glUniformMatrix4fv(uniformmatrix, 1, GL_FALSE, rotatey.m);
glDrawArrays(GL_TRIANGLES, 0, numvertex);
GLKMatrix4 translatey=GLKMatrix4Translate(frustum, 0,-0.5, -3);
GLKMatrix4 rotatex= GLKMatrix4Rotate(translatey, GLKMathDegreesToRadians(54), 1, 0, 0);
glUniformMatrix4fv(uniformmatrix, 1, GL_FALSE, rotatex.m);
glDrawArrays(GL_TRIANGLES, 0, numvertex);
http://i.stack.imgur.com/XnB9W.png
这是我的glsl代码
#version 300 es
in vec4 position;
in vec4 color;
in mat4 fulltransform;
uniform mat4 matrix;
out vec4 fragcolor;
void main()
{
// in mat4 fulltransform
fragcolor=color;
gl_Position=fulltransform*position;
// uniform mat4 matrix
fragcolor=color;
gl_Position=matrix*position;
}
另外我想知道这两种方法中哪一种在 fps 的情况下表现更好
我们将不胜感激。
GLuint programhandle=glCreateProgram();
glAttachShader(programhandle, vertex);
glAttachShader(programhandle, fragment);
glLinkProgram(programhandle);
GLint status;
position=glGetAttribLocation(programhandle,"position");
color=glGetAttribLocation(programhandle, "color");
uniformmatrix=glGetUniformLocation(programhandle, "matrix");
fulltransform=glGetAttribLocation(programhandle, "fulltransform");
glEnableVertexAttribArray(position);
glEnableVertexAttribArray(color);
glEnableVertexAttribArray(fulltransform);
glEnableVertexAttribArray(fulltransform+1);
glEnableVertexAttribArray(fulltransform+2);
glEnableVertexAttribArray(fulltransform+3);
glGetProgramiv(programhandle , GL_LINK_STATUS, &status);
if (status==GL_FALSE)
{
NSLog(@"program");
}
glDeleteShader(vertex);
glDeleteShader(fragment);
glUseProgram(programhandle);
"fulltransform" 是在 "glVertexAttribPointer(fulltransform,...)" 调用中从 glGetAttribLocation() 返回的吗,我没看到你在抽奖前调用 "glEnableVertexAttribArray(fulltransform + 0/1/2/3)"。
您的 glVertexAttribPointer()
调用中的步幅值看起来不对:
glVertexAttribPointer(fulltransform,4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*0));
glVertexAttribPointer(fulltransform+1, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*4));
glVertexAttribPointer(fulltransform+2, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*8));
glVertexAttribPointer(fulltransform+3, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*12));
跨度(第二个也是最后一个参数)是属性的后续值之间的字节差。由于您的属性是矩阵行,并且整个矩阵包含 16 个浮点值,因此一个矩阵的第一行(这是您的第一个属性)与下一个矩阵的第一行之间的差异是 16 个浮点值。所以这些调用应该是:
glVertexAttribPointer(fulltransform,4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*0));
glVertexAttribPointer(fulltransform+1, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*4));
glVertexAttribPointer(fulltransform+2, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*8));
glVertexAttribPointer(fulltransform+3, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*12));
作为健全性检查,如果您有交错属性,最后一个属性偏移量和该属性大小的总和通常应等于您用于所有属性的步幅值。或者,作为一个类似的测试,所有属性大小的总和应该等于步幅。修改后的步幅值就是这种情况,但您的原始代码并非如此。
我是 opengl 新手
我正在创建两个立方体
当我使用 glDrawArraysInstanced 时。根据我的矩阵变换,我没有得到预期的结果
这是我使用不同数据的代码
float fovy=tanf(GLKMathDegreesToRadians(30));
float nearplane=0.1f;
float farplane=10.0f;
float aspectratio=self.view.frame.size.width/self.view.frame.size.height;
float height=fovy*nearplane;
float width=height*aspectratio;
GLKMatrix4 frustum=GLKMatrix4MakeFrustum(-width, +width, -height, height, nearplane, farplane);
GLKMatrix4 translate=GLKMatrix4Translate(frustum, 0, 0.9, -3);
GLKMatrix4 rotatey= GLKMatrix4Rotate(translate, GLKMathDegreesToRadians(54), 0, 1, 0);
GLKMatrix4 translate1=GLKMatrix4Translate(frustum, 0, -0.5, -3);
GLKMatrix4 rotatey1= GLKMatrix4Rotate(translate1, GLKMathDegreesToRadians(54), 1, 1, 0);
GLKMatrix4 matarray[]=
{
rotatey,rotatey1
};
glGenBuffers(1, &matbuffer);
glBindBuffer(GL_ARRAY_BUFFER, matbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(matarray), matarray, GL_STATIC_DRAW);
glVertexAttribPointer(fulltransform,4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*0));
glVertexAttribPointer(fulltransform+1, 4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*4));
glVertexAttribPointer(fulltransform+2, 4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*8));
glVertexAttribPointer(fulltransform+3, 4, GL_FLOAT, GL_FALSE,sizeof(float)*4, (GLvoid*)(sizeof(float)*12));
glVertexAttribDivisor(fulltransform, 1);
glVertexAttribDivisor(fulltransform+1, 1);
glVertexAttribDivisor(fulltransform+2, 1);
glVertexAttribDivisor(fulltransform+3, 1);
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArraysInstanced(GL_TRIANGLES, 0, numvertex,2);
这是我的结果
http://i.stack.imgur.com/RWAWQ.png
如果我使用统一变量并使用 gldrawarray 两次,那么我会得到完美的结果。这是我的代码和结果
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float fovy=tanf(GLKMathDegreesToRadians(30));
float nearplane=0.1f;
float farplane=10.0f;
float aspectratio=self.view.frame.size.width/self.view.frame.size.height;
float height=fovy*nearplane;
float width=height*aspectratio;
GLKMatrix4 frustum=GLKMatrix4MakeFrustum(-width, +width, -height, height, nearplane, farplane);
GLKMatrix4 translate=GLKMatrix4Translate(frustum, 0, 0.9, -3);
GLKMatrix4 rotatey= GLKMatrix4Rotate(translate, GLKMathDegreesToRadians(54), 0, 1, 0);
glUniformMatrix4fv(uniformmatrix, 1, GL_FALSE, rotatey.m);
glDrawArrays(GL_TRIANGLES, 0, numvertex);
GLKMatrix4 translatey=GLKMatrix4Translate(frustum, 0,-0.5, -3);
GLKMatrix4 rotatex= GLKMatrix4Rotate(translatey, GLKMathDegreesToRadians(54), 1, 0, 0);
glUniformMatrix4fv(uniformmatrix, 1, GL_FALSE, rotatex.m);
glDrawArrays(GL_TRIANGLES, 0, numvertex);
http://i.stack.imgur.com/XnB9W.png
这是我的glsl代码
#version 300 es
in vec4 position;
in vec4 color;
in mat4 fulltransform;
uniform mat4 matrix;
out vec4 fragcolor;
void main()
{
// in mat4 fulltransform
fragcolor=color;
gl_Position=fulltransform*position;
// uniform mat4 matrix
fragcolor=color;
gl_Position=matrix*position;
}
另外我想知道这两种方法中哪一种在 fps 的情况下表现更好
我们将不胜感激。
GLuint programhandle=glCreateProgram();
glAttachShader(programhandle, vertex);
glAttachShader(programhandle, fragment);
glLinkProgram(programhandle);
GLint status;
position=glGetAttribLocation(programhandle,"position");
color=glGetAttribLocation(programhandle, "color");
uniformmatrix=glGetUniformLocation(programhandle, "matrix");
fulltransform=glGetAttribLocation(programhandle, "fulltransform");
glEnableVertexAttribArray(position);
glEnableVertexAttribArray(color);
glEnableVertexAttribArray(fulltransform);
glEnableVertexAttribArray(fulltransform+1);
glEnableVertexAttribArray(fulltransform+2);
glEnableVertexAttribArray(fulltransform+3);
glGetProgramiv(programhandle , GL_LINK_STATUS, &status);
if (status==GL_FALSE)
{
NSLog(@"program");
}
glDeleteShader(vertex);
glDeleteShader(fragment);
glUseProgram(programhandle);
"fulltransform" 是在 "glVertexAttribPointer(fulltransform,...)" 调用中从 glGetAttribLocation() 返回的吗,我没看到你在抽奖前调用 "glEnableVertexAttribArray(fulltransform + 0/1/2/3)"。
您的 glVertexAttribPointer()
调用中的步幅值看起来不对:
glVertexAttribPointer(fulltransform,4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*0));
glVertexAttribPointer(fulltransform+1, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*4));
glVertexAttribPointer(fulltransform+2, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*8));
glVertexAttribPointer(fulltransform+3, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*4, (GLvoid*)(sizeof(float)*12));
跨度(第二个也是最后一个参数)是属性的后续值之间的字节差。由于您的属性是矩阵行,并且整个矩阵包含 16 个浮点值,因此一个矩阵的第一行(这是您的第一个属性)与下一个矩阵的第一行之间的差异是 16 个浮点值。所以这些调用应该是:
glVertexAttribPointer(fulltransform,4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*0));
glVertexAttribPointer(fulltransform+1, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*4));
glVertexAttribPointer(fulltransform+2, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*8));
glVertexAttribPointer(fulltransform+3, 4, GL_FLOAT, GL_FALSE,
sizeof(float)*16, (GLvoid*)(sizeof(float)*12));
作为健全性检查,如果您有交错属性,最后一个属性偏移量和该属性大小的总和通常应等于您用于所有属性的步幅值。或者,作为一个类似的测试,所有属性大小的总和应该等于步幅。修改后的步幅值就是这种情况,但您的原始代码并非如此。