如果我使用GLSL,如何生成OBJ网格文件

时间:2015-11-16 02:01:48

标签: c++ opengl glsl vertex-shader geometry-shader

我想从代码中生成OBJ文件,使用GLSL文件生成网格,现在我可以从代码中获取顶点信息,但是如何从.geom.glsl文件中提取三角形信息并导出它进入一个OBJ文件?

此外,是否有任何助手功能呢?如果没有,我应该如何编写代码以获取geom.glsl文件中的点和三角形信息?

附上geom.glsl:

#version 400 core
#extension GL_EXT_geometry_shader4 : enable

layout(lines, invocations = 1) in;
layout(triangle_strip, max_vertices = 100) out;

uniform mat4 matLightView;
uniform mat4 matViewProjection;

uniform vec3 lightPos;
uniform vec3 camPos;
uniform int  isExplicit;
in vec4 VertPosition[];
in vec4 VertColor[];
in vec3 VertNormal[];
in vec3 VertTexture[];

in float VertLengthTotal[];
in float VertLengthFromBeginning[];

out vec3 GeomNormal;
out vec2 GeomTexCoords;
out float GeomDiffuse;
out float GeomThickness;
out vec4 texCoordA;
out vec4 texCoordB;


const float PI2 = 2 * 3.141592654;


void main()
{
   // for(int i=0; i<gl_VerticesIn-1; ++i)
    for (int i = 0; i<gl_in.length ()-1; ++i)
    {        
        //Reading Data
        vec4 posS = VertPosition[i];       
        vec4 posT = VertPosition[i+1];

        vec3 vS = VertColor[i].xyz;
        vec3 vT = VertColor[i+1].xyz;

        vec3 tS = VertTexture[i].xyz;
        vec3 tT = VertTexture[i+1].xyz;

        float thickS = VertColor[i].w;
        float thickT = VertColor[i+1].w;    

        //Computing
        vec3 v11 = normalize(vS);        
        vec3 v12 = normalize(cross(vS, tS));

        vec3 v21 = normalize(vT);
        vec3 v22 = normalize(cross(vT, tT)); 

        float rS = max(0.0001, thickS); 
        float rT = max(0.0001, thickT);


        int pS = 10;
        int pT = 10;

        int forMax = 16;   

        //Light Pos
        vec4 lPos = normalize(vec4(-lightPos.x, -lightPos.y, -lightPos.z, 1));
        vec3 L = normalize(lPos.xyz);   

        for(int k=0; k<=forMax; ++k)
        {
            float angle = k * (PI2 / forMax);

            vec3 newPS = posS.xyz + (v11 * sin(angle) + v12 * cos(angle)) * rS;
            vec3 newPT = posT.xyz + (v21 * sin(angle) + v22 * cos(angle)) * rT; 

            float scale = 1.0f;
            float texX =  float(k) / float(forMax);

            float edgeLength = length(posS - posT);

            float sTexY = (VertLengthFromBeginning[i]   * scale);
            float tTexY = (VertLengthFromBeginning[i+1] * scale);

            //Source Vertex           
            vec3 N = normalize(posS.xyz - newPS);                      
            texCoordB = matLightView * vec4(newPS, 1);
            GeomNormal = N;
            GeomThickness = rS;
            GeomDiffuse = rS < 0.0005 ? 0.0f : max(dot(N, L), 0.0);  
            GeomTexCoords = vec2(texX, sTexY);
            gl_Position = matViewProjection * vec4(newPS, 1);
            EmitVertex();


            //Target Vertex                               
            N = normalize(posT.xyz - newPT);
            texCoordB = matLightView * vec4(newPT, 1);
            GeomNormal = N;
            GeomThickness = rT;
            GeomDiffuse = rT < 0.0005 ? 0.0f : max(dot(N, L), 0.0);  
            GeomTexCoords = vec2(texX, tTexY);
            gl_Position = matViewProjection * vec4(newPT, 1);
            EmitVertex();            
        }       
    }

    EndPrimitive();                         
}

而vert.glsl:

#version 400 core

#define VERT_POSITION   0
#define VERT_NORMAL     1
#define VERT_COLOR      2
#define VERT_TEXTURE    3

layout(location = VERT_POSITION) in vec4 Position;
layout(location = VERT_NORMAL)   in vec4 Normal;
layout(location = VERT_COLOR)    in vec4 Color;
layout(location = VERT_TEXTURE)  in vec4 Texture;

out vec4  VertPosition;
out vec3  VertNormal;
out vec3  VertTexture;
out vec4  VertColor;

out float VertLengthFromBeginning;
out float VertLengthTotal;

uniform mat4 matModel;

void main()
{         
    VertPosition = matModel * Position;
    VertNormal   = Normal.xyz;          // Direction
    VertColor    = Color;               // V from PTF, VertColor.w = thick
    VertTexture  = Texture.xyz;         // Tangent   
    VertLengthFromBeginning = Normal.w; // Global Texture Coordinates
    VertLengthTotal = Texture.w;        // total length of chain
}

很多谢谢!!

0 个答案:

没有答案