微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可视化向量范数的问题OpenGL C++

如何解决可视化向量范数的问题OpenGL C++

我正在用 C++ 制作模型查看器,并尝试使用顶点、片段和几何着色器在我的模型上可视化我的向量范数,但是它只在屏幕上显示几行而不是每个向量一行,这里是我的着色器代码

    //Create vertex shader for visnorm
    const GLchar* vertexShaderVN =
        "#version 410\n " \
        "attribute vec3 a_Position;            " \
        "attribute vec3 in_normal;               " \
        "                                       " \
        "out VS_OUT {          " \
        "vec3 normal;            " \
        "} vs_out;            " \
        "uniform mat4 u_View;                  " \
        "uniform mat4 u_Model;                 " \
        "void main()               " \
        "{                " \
        "gl_Position = u_View * u_Model * vec4(a_Position,1.0); " \
        "mat3 normalMatrix = mat3(transpose(inverse(u_View * u_Model)));                          " \
        "vs_out.normal = normalize(vec3(vec4(normalMatrix * in_normal,0.0)));                                      " \
        "} " \
        "                                       ";
    //Create geometry shader for visnorm
    const GLchar* geometryShaderVN =
        "#version 410\n " \
        "layout (triangles) in;" \
        "layout (line_strip,max_vertices = 6) out;" \
        "in VS_OUT {" \
        "vec3 normal;" \
        "} gs_in[];" \
        "const float MAGNITUDE = 1.0; " \
        "uniform mat4 u_Projection;            " \
        "void GenerateLine(int index)" \
        "{" \
        "gl_Position = u_Projection * gl_in[index].gl_Position;" \
        "EmitVertex();" \
        "gl_Position = u_Projection * (gl_in[index].gl_Position + vec4(gs_in[index].normal,0.0) * MAGNITUDE); " \
        "EmitVertex();" \
        "EndPrimitive();" \
        "}" \
        "void main()" \
        "{" \
        "GenerateLine(0);" \
        "GenerateLine(1);" \
        "GenerateLine(2);" \
        "}" \
        "";
    //Create fragment shader for no visnorm
    const GLchar* fragmentShaderSrcVN =
        "#version 410\n " \
        "out vec4 FragColor;" \
        "void main()" \
        "{   " \
        "FragColor = vec4(1.0,1.0,0.0,1.0);" \
        "}                          " \
        "                          ";
    gluint programIdVN = LoadShader(vertexShaderVN,geometryShaderVN,fragmentShaderSrcVN);

以及我用于加载所述着色器的代码

gluint LoadShader(const GLchar* vert,const GLchar* geo,const GLchar* frag)
{
    //Create a new vertex shader,attach source code,compile it and check for errors
    gluint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderId,1,&vert,NULL);
    glCompileShader(vertexShaderId);
    GLint success = 0;
    glGetShaderiv(vertexShaderId,GL_COMPILE_STATUS,&success);
    //display any errors that occur in the vertex shader
    if (!success)
    {
        GLint maxLength = 0;
        glGetShaderiv(vertexShaderId,GL_INFO_LOG_LENGTH,&maxLength);
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(vertexShaderId,maxLength,&maxLength,&errorLog.at(0));
        std::cout << &errorLog.at(0) << std::endl;
        throw std::exception();
    }
    //Create a new geometry shader,compile it and check for errors
    gluint geometryShaderId = glCreateShader(GL_GEOMETRY_SHADER);
    glShaderSource(geometryShaderId,&geo,NULL);
    glCompileShader(geometryShaderId);
    glGetShaderiv(geometryShaderId,&success);
    //display any errors that occur in the vertex shader
    if (!success)
    {
        GLint maxLength = 0;
        glGetShaderiv(geometryShaderId,&maxLength);
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(geometryShaderId,&errorLog.at(0));
        std::cout << &errorLog.at(0) << std::endl;
        //throw std::exception();
    }
    //Create a new fragment shader,compile it and check for errors
    gluint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderId,&frag,NULL);
    glCompileShader(fragmentShaderId);
    glGetShaderiv(fragmentShaderId,&success);
    //display any errors that occur in the fragment shader
    if (!success)
    {
        GLint maxLength = 0;
        glGetShaderiv(fragmentShaderId,&maxLength);
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(fragmentShaderId,&errorLog.at(0));
        std::cout << &errorLog.at(0) << std::endl;
        throw std::exception();
    }
    //Create new shader program and attach our shader objects
    gluint programId = glCreateProgram();
    glAttachShader(programId,vertexShaderId);
    glAttachShader(programId,geometryShaderId);
    glAttachShader(programId,fragmentShaderId);
    //Ensure the VAO "position" attribute stream gets set as the first position during the link.
    glBindAttribLocation(programId,"a_Position");
    glBindAttribLocation(programId,"a_Texcord");
    glBindAttribLocation(programId,2,"in_normal");
    //Perform the link and check for failure
    glLinkProgram(programId);
    glGetProgramiv(programId,GL_LINK_STATUS,&success);
    if (!success)
    {
        GLint maxLength = 0;
        glGetProgramiv(programId,&maxLength);
        std::vector<GLchar> errorLog(maxLength);
        glGetProgramInfoLog(programId,&errorLog.at(0));
        std::cout << &errorLog.at(0) << std::endl;
        //throw std::exception();
    }
    //Detach and destroy the shader objects. These are no longer needed because we Now have a complete shader program
    glDetachShader(programId,vertexShaderId);
    glDeleteShader(vertexShaderId);
    glDetachShader(programId,geometryShaderId);
    glDeleteShader(geometryShaderId);
    glDetachShader(programId,fragmentShaderId);
    glDeleteShader(fragmentShaderId);
    //Find uniform locations
    GLint colorloc = glGetUniformlocation(programId,"u_Texcord");
    GLint modelLoc = glGetUniformlocation(programId,"u_Model");
    GLint viewLoc = glGetUniformlocation(programId,"u_View");
    GLint projectionLoc = glGetUniformlocation(programId,"u_Projection");
    if (modelLoc == -1)
    {
        throw std::exception();
    }
    if (projectionLoc == -1)
    {
        throw std::exception();
    }
    return programId;
}

如果有人对为什么这不能按预期工作有任何想法,那就太感谢了!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?