简体   繁体   中英

glDrawArraysInstanced not working properly

I am new in opengl

I am creating two cubes

when i am using glDrawArraysInstanced . According to my matrix transformation i am not getting expected result

Here is my code which i am using with the varying data

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);

Here is my result

http://i.stack.imgur.com/RWAWQ.png

if i use uniform variable and use gldrawarray two times then I get the perfect result. Here is my code and result

  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

Here is my glsl Code

#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;
}

Also i want to know which one of these two methods will perform better in case of fps

All help will be appreciated.

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);

是在调用“ glVertexAttribPointer(fulltransform,...)”中从glGetAttribLocation()返回的“ fulltransform”,在绘制之前我没有看到您调用“ glEnableVertexAttribArray(fulltransform + 0/1/2/3)”。

The stride values in your glVertexAttribPointer() calls look off:

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));

The stride (second but last argument) is the difference in bytes between subsequent values of the attribute. Since your attributes are matrix rows, and the entire matrix contains 16 float values, the difference between for example the first row (which is your first attribute) of one matrix and the first row of the next matrix is 16 float values. So these calls should be:

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));

As a sanity check, if you have interleaved attributes, the sum of the last attribute offset and the size of that attribute should normally add up to the stride value you use for all attributes. Or, as a similar test, the sum of all attribute sizes should add up to the stride. This is the case with the revised stride value, but was not true for your original code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM