Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java libgdx-GLGS-创建顶点着色器,让对象波动_Java_Opengl_Libgdx_Vertex Shader_Projection Matrix - Fatal编程技术网

Java libgdx-GLGS-创建顶点着色器,让对象波动

Java libgdx-GLGS-创建顶点着色器,让对象波动,java,opengl,libgdx,vertex-shader,projection-matrix,Java,Opengl,Libgdx,Vertex Shader,Projection Matrix,我有一些网格-对象,比如一个平面矩形: private void generateMesh(float xshift_mesh, float yshift_mesh, float zshift_mesh, float size_mesh) { float size = size_mesh/2; mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), Vert

我有一些
网格
-对象,比如一个平面矩形:

private void generateMesh(float xshift_mesh, float yshift_mesh, float zshift_mesh, float size_mesh) {
    float size = size_mesh/2; 
    mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0), VertexAttribute.Normal());
    mesh.setVertices(new float[] 
    // Position XYZ       Color RGBA  Texture Coordinates UV   Normal XYZ
    //|-,-,-,|            |-,-,-,-,|  |-,-,|                   |-,-,-|
    {
        -size+xshift_mesh, -size+yshift_mesh, zshift_mesh,  1, 1, 1, 1,     0, 1,    0, 0, -1,
        size+xshift_mesh, -size+yshift_mesh, zshift_mesh,   1, 1, 1, 1,     1, 1,    0, 0, 1,
        size+xshift_mesh,  size+yshift_mesh, zshift_mesh,   1, 1, 1, 1,     1, 0,    0, 0, 1,
        -size+xshift_mesh,  size+yshift_mesh, zshift_mesh,  1, 1, 1, 1,     0, 0,    0, 0, -1
        });
        mesh.setIndices(new short[] {0, 1, 2,2, 3, 0});
}
我的任务是创建一个顶点着色器,让这样的对象以某种方式波动

我的想法是: 首先,我为顶点着色器提供了一个
统一的
变量,该变量每秒从0变为1,反之亦然:

Date endDate = new Date();
float numSeconds = (float)((endDate.getTime() - startDate.getTime()) / 1000);
float even = (numSeconds % 2);
...

shader.setUniformMatrix("u_worldView", cam.combined);//WVP-Matrix
shader.setUniformi("u_texture", 0);
shader.setUniformf("u_even", even);
矩形对象的每个顶点都有一个
Normal
向量,如上面的
generateMesh
方法所示。我可以在顶点着色器中访问并使用此向量:

uniform float u_even;
uniform mat4 u_worldView;
attribute vec4 a_color;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
attribute vec4 a_normal;

varying vec4 v_color;
varying vec2 v_texCoords;

void main() {
    v_color = a_color;
    v_texCoords = a_texCoord0;
    if(u_even > 0.5)  gl_Position = u_worldView * a_position + a_normal;
    else gl_Position = u_worldView * a_position - a_normal;
}
我期望对象每秒都会改变,这在某种程度上是这样的,但我每次都会操纵摄影机的位置。如果
Normal
向量的值均为0,则视图位置也会来回移动

我也尝试过:

if(u_even > 0.5)  gl_Position = u_worldView * (a_position + a_normal);
else gl_Position = u_worldView * (a_position - a_normal);
但更糟糕的是

那么,如何在不改变整个视点的情况下改变顶点的位置呢


我感谢你的帮助

已经有一段时间了。我只想结束问题的解决

问题在于
generateMesh
函数,特别是
NORMAL XYZ
值:

private void generateMesh(float xshift_mesh, float yshift_mesh, float zshift_mesh, float size_mesh) {
    if(size_mesh < 0) {
        return; 
    }
    float size = size_mesh/2; 
    mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0), VertexAttribute.Normal());
    mesh.setVertices(new float[] 
      //<------------ Position XYZ ------------------------>   <-Color RGBA->   <Texture Coordinates UV>   <Normal XYZ>
      //|               -,                  -,           -,|   |-, -, -, -, |   | -, -, |                  |-, -, -|
    {   
        -size+xshift_mesh,  -size+yshift_mesh, zshift_mesh,     1, 1, 1, 1,       0, 1,                     0, 0, 1,
         size+xshift_mesh,  -size+yshift_mesh, zshift_mesh,     1, 1, 1, 1,       1, 1,                     0, 0, 1,
         size+xshift_mesh,   size+yshift_mesh, zshift_mesh,     1, 1, 1, 1,       1, 0,                     0, 0, -1,
        -size+xshift_mesh,   size+yshift_mesh, zshift_mesh,     1, 1, 1, 1,       0, 0,                     0, 0, -1
    });
    mesh.setIndices(new short[] {0, 1, 2,2, 3, 0});
}
private void generateMesh(浮点xshift_网格、浮点yshift_网格、浮点zshift_网格、浮点大小_网格){
如果(尺寸_网格<0){
返回;
}
浮子尺寸=筛孔尺寸/2;
网格=新网格(真、4、6、VertexAttribute.Position()、VertexAttribute.ColorUnpacket()、VertexAttribute.TexCoords(0)、VertexAttribute.Normal());
网格。设置顶点(新浮点[]
//         
//|               -,                  -,           -,|   |-, -, -, -, |   | -, -, |                  |-, -, -|
{   
-大小+X移位网格,-大小+Y移位网格,Z移位网格,1,1,1,1,0,1,0,0,1,
大小+X移位网格,-大小+Y移位网格,Z移位网格,1,1,1,1,1,0,0,1,
大小+X移位网格,大小+Y移位网格,Z移位网格,1,1,1,1,0,0,0,0,-1,
-大小+X移位网格,大小+Y移位网格,Z移位网格,1,1,1,0,0,0,0,-1
});
setindex(新的短[]{0,1,2,2,3,0});
}

size
传递给此方法的值是多少?我说的对吗,你想让这个对象前后捕捉,使其自身反转?当我听到“wave”这个词时,我想到的是平滑的来回运动,而不是像你的代码那样来回跳跃。@Tenfour04
mesh_size
是矩形的大小。顶点的XYZ坐标通过
size=mesh_size/2
计算,因此矩形的大小将为
mesh_size
。实现只是第一种方法。在找到更好的方法来更改矩形的顶点之前,我首先要解决相机的问题……我想问的是,在测试这个参数时,您为该参数传递了什么具体的数字。我想说的是,你的方法接受一个任意的
size
属性,该属性用于顶点的位置,但不用于法线,因此它们可能有几个数量级的不同。顺便说一句,您肯定需要在第二次尝试中使用的括号。@Tenfour04 Ok thx。我使用以下参数调用该方法:
generateMesh(1.5f、1.0f、0.0f、1.0f)。据我所知,法向量的大小应该是1,所以它应该独立于矩形的大小。。。或者不是?这是真的,但是你应该把某种振幅传递给着色器,让它乘以。但在这种情况下,你在同一个量级范围内,所以它不会引起你的问题。我看不出你发布的代码有任何问题,除了缺少括号。问题出在别处。如果
a_normal
为0,并且
u_even
是唯一正在更改的着色器输入,那么屏幕上会出现任何运动是没有意义的。