在OpenGL3核心配置文件中使用矩阵作为顶点属性 [英] Using a matrix as vertex attribute in OpenGL3 Core Profile

查看:134
本文介绍了在OpenGL3核心配置文件中使用矩阵作为顶点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OSX上使用OpenGL3.2 Core Profile. 而且我想进行实例化绘图(glDrawArraysInstanced),在其中为每个实例传递一个矩阵.

I am using OpenGL3.2 Core Profile on OSX. And I want to do instanced drawing (glDrawArraysInstanced), where I pass a matrix for each instance.

我的顶点着色器构建良好:

My vertex shader builds just fine:

#version 150
in mediump vec4 position;
in mediump mat4 trf;
in lowp vec4 rgb;
out lowp vec4 colour;
uniform highp mat4 modelcamviewprojmat;
void main()
{
    mediump vec4 tpos = trf * position;
    gl_Position = modelcamviewprojmat * tpos;
    colour = rgb;
}

'trf'的绑定很好:

The binding of 'trf' went fine:

glBindAttribLocation(program, ATTRIB_TRF, "trf" );

但是如何传递数据? glVertexAttribPointer不能传递大于4个浮点数的值. 因此,此调用失败:

But how can I pass in my data? glVertexAttribPointer can not pass values larger than 4 floats. So this call fails:

glVertexAttribPointer( ATTRIB_TRF,   16, GL_FLOAT, 0, 16 * sizeof(float), ... );

我怀疑我需要用4个对glVertexAttribPointer的调用来替换它,每个调用都传递4个浮点数. 但是我可以为索引"(第一个参数)使用什么值?我是否需要改用4个矢量属性,并在GLSL顶点着色器中组合这四个矢量?如果是这样,哪种GLSL代码可以完成此任务?或者我可以使用BindAttribLocation的返回值并对所有行使用val + 0,val + 1,val + 2和val + 3?

I suspect that I need to replace it with 4 calls to glVertexAttribPointer, each passing 4 floats. But what value could I use for 'index' (first parm)? Do I need to use 4 vector attributes instead, and assemble the four vectors in GLSL vertex shader? If so, what kind of GLSL code accomplishes this? Or can I use the return value from BindAttribLocation and use val+0, val+1, val+2 and val+3 for all the rows?

推荐答案

根据此页面和我目前的实现在我的游戏中,通过硬件实例化,正确的方法是mat4属性占据4个属性位置.您绑定的一个和后面的3个.

According to this page and my current implementation of hardware instancing in my game, the proper way it's done is that a mat4 attribute takes up 4 attribute locations. The one you bind and the 3 following.

int pos = glGetAttribLocation(shader_instancedarrays.program, "transformmatrix");
int pos1 = pos + 0; 
int pos2 = pos + 1; 
int pos3 = pos + 2; 
int pos4 = pos + 3; 
glEnableVertexAttribArray(pos1);
glEnableVertexAttribArray(pos2);
glEnableVertexAttribArray(pos3);
glEnableVertexAttribArray(pos4);
glBindBuffer(GL_ARRAY_BUFFER, VBO_containing_matrices);
glVertexAttribPointer(pos1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(0));
glVertexAttribPointer(pos2, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 4));
glVertexAttribPointer(pos3, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 8));
glVertexAttribPointer(pos4, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 12));
glVertexAttribDivisor(pos1, 1);
glVertexAttribDivisor(pos2, 1);
glVertexAttribDivisor(pos3, 1);
glVertexAttribDivisor(pos4, 1);

这篇关于在OpenGL3核心配置文件中使用矩阵作为顶点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆