Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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
C++ 使用Cg的体绘制:编译错误:语义“;“职位”;在此配置文件中不可见_C++_Opengl_Graphics_Gpu_Cg - Fatal编程技术网

C++ 使用Cg的体绘制:编译错误:语义“;“职位”;在此配置文件中不可见

C++ 使用Cg的体绘制:编译错误:语义“;“职位”;在此配置文件中不可见,c++,opengl,graphics,gpu,cg,C++,Opengl,Graphics,Gpu,Cg,我现在正在编码使用OpenGL、Cg和C++实现直接光线投射体绘制。我的计算机系统是Win7,图形是nVidia GTX570 实际上,我已经成功实现了直接光线投射体绘制的基本功能。但现在我想在其上添加照明,并使用局部照明模型。在添加了几行照明代码后,我得到Cg编译器错误。添加之前,它运行良好。 我使用的Cg配置文件是Cg\U配置文件\U VP40和Cg\U配置文件\U FP40 现在我发布我的Cg代码: #define kNumberOfRaySteps 800 // Define inter

我现在正在编码使用OpenGL、Cg和C++实现直接光线投射体绘制。我的计算机系统是Win7,图形是nVidia GTX570

实际上,我已经成功实现了直接光线投射体绘制的基本功能。但现在我想在其上添加照明,并使用局部照明模型。在添加了几行照明代码后,我得到Cg编译器错误。添加之前,它运行良好。

我使用的Cg配置文件是Cg\U配置文件\U VP40Cg\U配置文件\U FP40

现在我发布我的Cg代码:

#define kNumberOfRaySteps 800
// Define interface between the application and the vertex program
struct app_vertex
 {
   float4 Position     : POSITION;
   float4 TexCoord     : TEXCOORD1; 
   float4 Color        : COLOR0;
};

// Define the interface between the vertex- and the fragment programs
struct vertex_fragment
 {
   float4 Position    : POSITION; // For the rasterizer
   float4 TexCoord    : TEXCOORD0;
   float4 Color       : TEXCOORD1;
   float4 Pos         : TEXCOORD2;
};

struct fragment_out 
{
  float4 Color      : COLOR0;
};

//Blinn-Phong illumination
float3 shading(float3 N, float3 V, float3 L) {
    // material properties
    float3 Ka = float3(0.1, 0.1, 0.1); // ambient
    float3 Kd = float3(0.6, 0.6, 0.6); // diffuse
    float3 Ks = float3(0.2, 0.2, 0.2); // specular
    float n = 100.0; // shininess
    // light properties
    float3 lightColor = float3(1.0, 1.0, 1.0);
    float3 ambientLight = float3(0.3, 0.3, 0.3);
    // Calculate halfway vector
    float3 H= normalize(L + V);
    // Compute ambient term
    float3 ambient = Ka * ambientLight;
    // Compute the diffuse term
    float diffuseLight = max(dot(L, N), 0);
    float3 diffuse = Kd * lightColor * diffuseLight;
    // Compute the specular term
    float specularLight = pow(max(dot(H, N), 0), n);
    if (diffuseLight <= 0) specularLight = 0;
    float3 specular = Ks * lightColor * specularLight;
    return ambient + diffuse + specular;
}

// Raycasting vertex program implementation
vertex_fragment vertex_main( app_vertex IN )
{    
  vertex_fragment OUT;

  // Get OpenGL state matrices
  float4x4 ModelView = glstate.matrix.modelview[0];//model view matrix 0
  float4x4 ModelViewProj = glstate.matrix.mvp;//modelview-projection matrix

  // Transform vertex
  OUT.Position = mul( ModelViewProj, IN.Position );
  OUT.Pos =  mul( ModelViewProj, IN.Position ); 
  OUT.TexCoord = IN.TexCoord;
  OUT.Color = IN.Color;
  return OUT;
}

// Raycasting fragment program implementation
fragment_out fragment_main( vertex_fragment IN,
                            uniform sampler2D tex, //back face
                            uniform sampler2D preint_table,//pre-intergration classification
                            uniform sampler3D volume_tex, //volume data
                            uniform float stepsize,
                            //add local illumination
                            uniform float3 lightPosition,//the external light position
                            uniform float3 eyePosition,//the eye position
                            uniform sampler3D normal_vec//normal vectors
                        )     
{
  fragment_out OUT;
  // find the right place to lookup in the backside buffer
  //normalize the spatial position to range [0,1]
  float2 texc = ((IN.Pos.xy / IN.Pos.w) + 1) / 2; 
  // the start position of the ray is stored in the texturecoordinate
  float4 start = IN.TexCoord; 
  float4 back_position  = tex2D(tex, texc);
  float3 dir = float3(0,0,0);
  dir.x = back_position.x - start.x;
  dir.y = back_position.y - start.y;
  dir.z = back_position.z - start.z;
  // the length from front to back is calculated and used to terminate the ray
  float len = length(dir.xyz); 
  float3 norm_dir = normalize(dir);
  float delta = stepsize;
  float3 delta_dir = norm_dir * delta;
  float delta_dir_len = length(delta_dir);
  float3 vec1 = start.xyz;//position of back point
  float3 vec2 = start.xyz;//position of front point
  float4 col_acc = float4(0,0,0,0);
  float alpha_acc = 0;//accumulation of alpha value(opacity)
  float length_acc = 0;
  float4 color_sample;
  float alpha_sample;
  float4 lookup = float4(0.0,0.0,0.0,0.0);
   //set background color to blue
  float4 backgroundColor = float4(1.0,1.0,1.0,0.0);


  //loops:sample points
  for(int i = 0; i < kNumberOfRaySteps; i++)
  {
      vec2 += delta_dir;

      lookup.x = (tex3D(volume_tex,vec1)).x;//scalar value of back point
      lookup.y = (tex3D(volume_tex,vec2)).x;//scalar value of front point
      color_sample = tex2D(preint_table,lookup.xy);
      alpha_sample = color_sample.a;

     //add code for illumination
     float3 N = tex3D(normal_vec,start.xyz).xyz;//normal vector
     //calculate light and viewing directions
     float3 L = normalize(lightPosition - IN.Position.xyz);
     float3 V = normalize(eyePosition - IN.Position.xyz);

      col_acc   += (1.0 - alpha_acc) * color_sample * alpha_sample * 2;

      //add ilumination color 
      col_acc += float4(shading(N,V,L),0.0);

      alpha_acc += (1.0 - alpha_acc) * alpha_sample;
      vec1 += delta_dir;//next sample point
      length_acc += delta_dir_len;//accumulation of ray length in the cube
      //change the background color from black to other color
      if(length_acc >= len) 
          col_acc += (1.0 - alpha_acc) * backgroundColor;
      // terminate if opacity > 1 or the ray is outside the volume
      if(length_acc >= len || alpha_acc > 0.99) break; 
  }

  OUT.Color =  col_acc;

  return OUT;
}
然后程序运行得很好,当然我不能得到照明效果。 因此,我猜计算照明效果的函数着色有问题

但我想不出来。
有人能告诉我怎么解决这个问题吗?

最后,我解决了这个问题

此错误的原因是我无法访问片段着色器中的位置参数,因为顶点着色器的位置输出只会传递给光栅化器,而不会传递给片段着色器。因此我需要将其作为非位置参数传递,例如,TEXCOORD。在此程序中,我可以访问参数Pos,而不是Position,以获得正确的结果


不幸的是,没有人回答我。原因是没有很多人使用Cg和GPU编程吗?我想知道为什么。

差不多三年后,你成了救生员。我不知道为什么没有任何关于Cg工作原理的文档或帮助。它分散在像这样的小地方。
col_acc += float4(shading(N,V,L),0.0);