简体   繁体   English

LWJGL glVertexAttribPointer索引切换

[英]LWJGL glVertexAttribPointer index switching

As I'm learning OpenGL through LWJGL, I've been running through the tutorials on the LWJGL wiki. 在通过LWJGL学习OpenGL时,我一直在遍历LWJGL Wiki上的教程。 Specifically, this one . 具体来说, 这个

My problem is, that despite the fact that I have duplicated that code, call for call, function for function, the two vertex attribute pointers need to be switched (so the code looks like this: 我的问题是,尽管我已经重复了该代码,调用,函数,函数的事实,但仍需要切换两个顶点属性指针(所以代码看起来像这样:

GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, 
    // Put the colors in attribute list 1
GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes,
    Vertex.elementBytes * 4);

Switching it so that the position index is 1 and the color index is 0, makes a nice gradient render, as expected. 切换它以使位置索引为1且颜色索引为0,可以按预期进行漂亮的渐变渲染。 However, if I run it with the indices switched (the way I think it should work, I get this: 但是,如果我在切换索引的情况下运行它(我认为它应该工作的方式,我会得到:

令人困惑的精神错乱

Clearly, after studying this render for a while, the position and color data have been switched, and thus, switching the index numbers completely solves the problem, but I'm pretty sure I've followed everything correctly. 显然,在研究了一段时间的渲染之后,位置和颜色数据已经切换,因此,切换索引号可以完全解决问题,但是我敢肯定我正确地遵循了所有内容。 What is wrong with the code? 代码有什么问题? Why is this being such a bastard? 为什么这么混蛋?

You should not be hardcoding attribute id's like that, and just hoping that they end up linked to the right shader attributes. 您不应该这样对属性ID进行硬编码,而只是希望它们最终链接到正确的着色器属性。

You need to either use glBindAttribLocation (before glLinkProgram), or glGetAttribLocation, so that you can map shader variables to their id number. 您需要使用glBindAttribLocation(在glLinkProgram之前)或glGetAttribLocation,以便可以将着色器变量映射到其ID号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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