如何使用glDrawElements同时保持纹理坐标正确吗? [英] How to use glDrawElements while keeping Texture Coordinates correct?

查看:179
本文介绍了如何使用glDrawElements同时保持纹理坐标正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用/打靶的OpenGL 4.20,4.20 GSLSL核心。使用C

我寻求与OpenGL图形,并已决定将基于区块的游戏。我在哪里,我想实际绘制一堆砖的点,想使用glDrawElements绘制单个的三角形。

相关code:

 常量浮动vertexPositions [] = {
  -1.0F,-0.8​​f,// 0
  -0.8f,-0.8​​f,// 1
  -0.6f,-0.8​​f,// 2
  -0.4f,-0.8​​f,// 3
  -0.2f,-0.8​​f,// 4
   0.0,-0.8​​f,// 5  -1.0F,-1.0F,// 6
  -0.8f,-1.0F,// 7
  -0.6f,-1.0F,// 8
  -0.4f,-1.0F,// 9
  -0.2f,-1.0F,// 10
   0.0,-1.0F,// 11//下面是纹理坐标。
  0.00f,1.00F,
  1.00F,1.00F,
  0.00f,1.00F,
  1.00F,1.00F,
  0.00f,1.00F,
  1.00F,1.00F,  0.00f,0.00f,
  1.00F,0.00f,
  0.00f,0.00f,
  1.00F,0.00f,
  0.00f,0.00f,
  1.00F,0.00f,
};常量GLubyte指数[] = {
  0,1,6,
  1,7,6
  1,2,7日,
  2,8,7,
  2,第3,第8,
  3,9,8,
  3,4,9,
  4,10,第9,
  4,5,10,
  5,11,10,
};

  glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(无效*)96);glDrawElements(GL_TRIANGLES,30,GL_UNSIGNED_BYTE,索引);

(还有其他的作品,但它主要是格式化/属性使等,如果需要,我会提供更多)。

基本上我得到的是:

  0 1 2 3 4 5
+ - + - + - + - + - +
| B |开发| C |开发| C |
+ - + - + - + - + - +
6 7 8 9 10 11

和我想要的是:

  0 1 2 3 4 5
+ - + - + - + - + - +
| B | C | C | C | C |
+ - + - + - + - + - +
6 7 8 9 10 11

其中B是纹理/平方米的正确方向和D是反向/体现。

我意识到自从顶点坐标分享这个事情发生,但是从我个人的想法/一些帖子下面,好像这是不可避免的。要么我保存的顶点和反映纹理,或我最后送3 * 2 * number_of_tiles顶点,这我觉得违背glDrawElements的目的。

资源还没有帮助:

这些事情我发现是相关的,但似乎(我)是不是去precated使用/没有回答/不太我问。随意点我回到这些职位之一,如果它实际上的确实的回答我的问题。

<一个href=\"http://www.gamedev.net/topic/602169-opengl-drawing-cube-using-gldrawelements-and-gltexcoordpointer/\">This似乎是说,glDrawElements是无用的立方体,而且你的必须的有各个顶点组成的面孔。这是否适用于2D网格呢?

答案<一个href=\"http://stackoverflow.com/questions/10592974/how-i-can-use-gldrawelements-for-many-squares\">this问题说(除其他事项外)两个广场使用相同的顶点位置,但不同的纹理坐标,必须重新用不同的四边形psented $ P $。但我不明白他得到的。

<一个href=\"http://stackoverflow.com/questions/10401605/coord-texture-array-does-not-works-correctly-with-gldrawelements\">This 之一我认为是接近。在答案:第一个环节被打破/访问被拒绝。该第二个链接有一个答案,基本上说的顶点必须是不同的。这是必需的?在第三个环节(第二code段),他们似乎只是产生所有36个顶点。对于一个多维数据集这可能是好的,但对n立方体..?我失去了他的例子来看,也许?

就是我请求甚至可能吗?我使用完全不正确功能我想要达到什么目的? UR DOIN IT RONG?

我已经想到了重新编排的坐标所以它看起来像:

  0 2 4 6 8 10
+ - + - + - + - + - +
| | | | | |
+ - + - + - + - + - +
1 3 5 7 9 11

不过我没怎么看,将解决这个问题,因为顶点仍然共享。

感谢,并随时纠正我/我的问题。第一个,所以我希望它需要修改。


解决方案

  

第二个链接有一个答案,基本上说的顶点必须是不同的。这是必需的?


是的。你想每个四边形有一个从0到1的纹理坐标这意味着,组成第一款四右侧的位置不能使用相同的纹理坐标作为弥补的左侧位置第二个四边形。

,因为你不能有相同的位置与两个不同的纹理坐标,则必须复制你的位置。

I'm using/targetting OpenGL 4.20, GSLSL 4.20, core. Using C.

I'm exploring graphics with OpenGL and have decided to make a tile based game. I'm at the point where i'd like to actually draw a bunch of tiles, and am trying to use glDrawElements to draw individual triangles.

Relevant code:

const float vertexPositions[] = {
  -1.0f, -0.8f, //0
  -0.8f, -0.8f, //1
  -0.6f, -0.8f, //2
  -0.4f, -0.8f, //3
  -0.2f, -0.8f, //4
   0.0f, -0.8f, //5

  -1.0f, -1.0f, //6
  -0.8f, -1.0f, //7
  -0.6f, -1.0f, //8
  -0.4f, -1.0f, //9
  -0.2f, -1.0f, //10
   0.0f, -1.0f, //11

//The following are the texture coordinates.
  0.00f, 1.00f,
  1.00f, 1.00f,
  0.00f, 1.00f,
  1.00f, 1.00f,
  0.00f, 1.00f,
  1.00f, 1.00f,

  0.00f, 0.00f,
  1.00f, 0.00f,
  0.00f, 0.00f,
  1.00f, 0.00f,
  0.00f, 0.00f,
  1.00f, 0.00f,
};

const GLubyte indices[] = {
  0, 1, 6, 
  1, 7, 6, 
  1, 2, 7, 
  2, 8, 7, 
  2, 3, 8, 
  3, 9, 8,
  3, 4, 9,
  4, 10, 9,
  4, 5, 10,
  5, 11, 10,
};

with

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void *)96);

glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_BYTE, indices);

(There are other pieces, but it's mostly formatting/attribute enabling etc. I'll provide more if needed).

Basically what i get is:

0  1  2  3  4  5
+--+--+--+--+--+
|b | d|b |d |b |
+--+--+--+--+--+
6  7  8  9 10 11

and what i want is:

0  1  2  3  4  5
+--+--+--+--+--+
|b |b |b |b |b |
+--+--+--+--+--+
6  7  8  9 10 11

Where "b" is the correct orientation of the texture/square and "d" is the reverse/reflected.

I realize that since the vertex coordinates are shared this is going to happen, but from my own thoughts/some posts below it seems like it is unavoidable. Either I conserve vertices and have reflected textures, or I end up sending 3*2*number_of_tiles vertices, which i feel defeats the purpose of glDrawElements.

Resources that haven't helped:

These are things I've found that are related, but seem (to me) to be either deprecated usage/not answered/not quite what I'm asking. Feel free to point me back to one of these posts if it in fact does answer my question.

This seems to say that glDrawElements is useless for a cube, and that you must have individual vertices making up faces. Does this apply to a 2d grid as well?

The answer to this question says (among other things) "Two squares with the same vertex positions, but different texture coordinates must be represented with different quads." but I don't understand what he's getting at.

This one I thought was close. In the answer: The first link is broken/access denied. The second link has an answer that basically says the vertices must be distinct. Is this required? In the third link (second code snippet) they seem to just generate all 36 vertices. For one cube this is probably ok, but for n cubes..? Am i missing the point of his example, maybe?

Is what I'm requesting even possible? Am I using completely incorrect functions for what I'm trying to achieve? "UR DOIN IT RONG"?

I've thought of re-arranging the coordinates so it looks like:

0  2  4  6  8 10
+--+--+--+--+--+
|  |  |  |  |  |
+--+--+--+--+--+
1  3  5  7  9 11

But I didn't see how that would fix the issue, since vertices are still shared.

Thanks, and feel free to correct me/my question. First one, so I expect it to need revising.

解决方案

The second link has an answer that basically says the vertices must be distinct. Is this required?

Yes. You want each individual quad to have texture coordinates that go from 0 to 1. Which means that the positions that make up the right side of the first quad can't use the same texture coordinates as the positions that make up the left side of the second quad.

And since you cannot have the same position with two different texture coordinates, you must duplicate your positions.

这篇关于如何使用glDrawElements同时保持纹理坐标正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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