OpenGL着色器:两个对象和一条线来连接它们 [英] openGL shader: two object and one line to connect them

查看:158
本文介绍了OpenGL着色器:两个对象和一条线来连接它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用着色器和可编程管道,我需要用一条线连接两个网格(可能是彩色的),但我只有这两个对象的模型矩阵。这条线应该从这些对象的中心开始,所以我假设两点的对象坐标应该是:

 起点:0,0,0 
结束点:0,0,0

连接两个对象,我假设我将不得不在这些着色器中乘以它们各自对象的模型矩阵。然后把所有东西都乘以视图矩阵(这对两个对象是共同的)。最后是投影矩阵。

我总是在顶点数组中使用着色器,如何将两个不同的顶点传递给着色器?我是否也应该使用制服?我应该使用什么函数来画线,因为glDrawElements和glDrawArrays都需要数据数组?创建一个包含你想用GL_LINES绘制的所有行的世界空间点的VBO。



最简单的就是使用glDrawArrays。如果你有极端数量的需要在(10.000s +)之间的行的对象对,那么将所有点存储在VBO中可能是有意义的,然后使用IBO(索引缓冲区)来说明VBO中的哪些点通过行。

行顶点仍然必须由视图矩阵转换。



下面是一个例子:
GLuint vao,vbo;

  //生成并绑定vao 
glGenVertexArrays(1,& VAO);
glBindVertexArray(vao);

//产生并绑定缓冲区对象
glGenBuffers(1,& vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);

struct Point {
float x,y,z;
Point(float x,float y,float z)
:x(x),y​​(y),z(z)
{}
};

//创建一条线所需的两个点。对于更多行,只需添加另一个点对
std :: vector< Point> vertexData;
vertexData.push_back(Point(-0.5,0.f,0.f));
vertexData.push_back(Point(+0.5,0.f,0.f));

//填充数据
size_t numVerts = vertexData.size();
glBufferData(GL_ARRAY_BUFFER,sizeof(Point)* numVerts,& vertexData [0],GL_STATIC_DRAW);

//设置泛型attrib指针
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,numVerts * sizeof(Point),(char *)0 + 0 * sizeof(GLfloat));

//unbindvao
glBindVertexArray(0);

//在渲染循环中:
glBindVertexArray(vao);
glDrawArrays(GL_LINES,0,numVerts);

如果您需要在运行时添加更多的点对,请更新std :: vector, VBO并调用glBufferData。


I'm using shaders and the programmable pipeline, I need to connect two meshes with a line (maybe colored) but I just have the model matrices of these two objects. The line should start at the center of these objects so I assume the object-coords of the two points should be:

start point: 0,0,0
end point: 0,0,0

to connect the two objects I assume I'll have to multiply these points in the shader by their respective object's model matrix. And then multiply everything by the view matrix (this is common to the two objects). And eventually by the projection matrix.

I've always used shaders with vertex arrays, how would I pass two different vertices to a shader? Should I use uniforms for them too? And what function should I use to draw the line since both glDrawElements and glDrawArrays require a data array?

解决方案

The most straightforward method would be to create a VBO containing the worldspace points of all the lines you wish to draw with GL_LINES.

The easiest is to use glDrawArrays. If you have extreme amounts of object pairs that need lines between (10.000s+) then it might make sense to store all the points in a VBO, and then use a IBO (index buffer) to say what points in the VBO are connected by a line.

The line vertices would still have to be transformed by the view matrix.

Here's an example: GLuint vao, vbo;

// generate and bind the vao
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

// generate and bind the buffer object
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

struct Point{ 
    float x,y,z; 
    Point(float x,float y,float z) 
                    : x(x), y(y), z(z) 
    {} 
};

// create two points needed for a line. For more lines, just add another point-pair
std::vector<Point> vertexData;
vertexData.push_back( Point(-0.5, 0.f, 0.f) );
vertexData.push_back( Point(+0.5, 0.f, 0.f) );

// fill with data
size_t numVerts = vertexData.size();
glBufferData(GL_ARRAY_BUFFER, sizeof(Point)*numVerts, &vertexData[0], GL_STATIC_DRAW);

// set up generic attrib pointers
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, numVerts*sizeof(Point), (char*)0 + 0*sizeof(GLfloat));

// "unbind" vao
glBindVertexArray(0);

// In your render-loop:
glBindVertexArray(vao);
glDrawArrays(GL_LINES, 0, numVerts);

If you need to add more point-pairs during runtime, update the std::vector, bind the VBO and call glBufferData.

这篇关于OpenGL着色器:两个对象和一条线来连接它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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