示例#1
0
GLhandleARB compileFragmentShader(const char* fragmentShaderFileName)
	{
	/* Construct the full shader source file name: */
	std::string fullShaderFileName=CONFIG_SHADERDIR;
	fullShaderFileName.push_back('/');
	fullShaderFileName.append(fragmentShaderFileName);
	fullShaderFileName.append(".fs");
	
	/* Compile and return the fragment shader: */
	return glCompileFragmentShaderFromFile(fullShaderFileName.c_str());
	}
示例#2
0
void VolumeRayCasting::initContext(GLContextData& contextData) const
{
  DataItem* dataItem=new DataItem();
  contextData.addDataItem(this,dataItem);

  const char* datasetName = "bin/data/BostonTeapot.raw";
  int volumesize = 256*256*256;
  /*load sample data*/
  std::vector<unsigned char> volumeData;
  volumeData.resize(volumesize);
  std::ifstream ifs(datasetName, std::ios::binary);

  std::cout<<"open dataset file "<<std::endl;
  if(!ifs.is_open())
    {
      /* fail to open dataset file */
      std::cout<<"fail to open dataset file: "<<strerror(errno)<<std::endl;
      return;
    }
  ifs.read(reinterpret_cast<char *>(&volumeData.front()), volumesize * sizeof(float));
  ifs.close();

  /* Select the Volume Rendering texture object: */
  glBindTexture(GL_TEXTURE_3D,dataItem->volumeTex);
	
  /* Upload the Volume Rendering texture image: */
  glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, 32, 32, 32, 0,  GL_RED, GL_UNSIGNED_BYTE, &volumeData.front());
  glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_WRAP_R,GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	
  /* Protect the Volume Rendering texture object: */
  glBindTexture(GL_TEXTURE_3D,0);
  //debug
  std::cout<<dataItem->volumeTex<<std::endl;
  /* Select the Tansfer Function texture object: */
  glBindTexture(GL_TEXTURE_2D,dataItem->transferFuncTex);
	
  /* Upload the Tansfer Function texture image: */
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, transFuncData.size(), 1, 0, GL_RGBA, GL_FLOAT, &transFuncData.front());
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	
  /* Protect the Tansfer Function texture object: */
  glBindTexture(GL_TEXTURE_2D,0);
  //debug
  std::cout<<dataItem->transferFuncTex<<std::endl;

  glClearColor(0.0, 0.0, 0.0, 0.0);
  glEnable(GL_BLEND);
  glBlendEquation(GL_FUNC_ADD);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_CULL_FACE);
  glCullFace(GL_FRONT);
  glDisable(GL_DEPTH_TEST);
  glColorMask(true, true, true, true);
  glDepthMask(true);
  glEnable(GL_MULTISAMPLE);

  if(dataItem->haveShaders)
    {
      {
    GLhandleARB vertexShader=glCompileVertexShaderFromFile("bin/Shaders/VolumeRayCasting.vert");
    GLhandleARB fragmentShader=glCompileFragmentShaderFromFile("bin/Shaders/VolumeRayCasting.frag");

	dataItem->rayCastingShader=glLinkShader(vertexShader,fragmentShader);

	glDeleteObjectARB(vertexShader);
	glDeleteObjectARB(fragmentShader);
      }	
    }


  std::vector<Vector2f> rectVertices;
  rectVertices.push_back(Vector2f(0.0f,0.0f));
  rectVertices.push_back(Vector2f(0.0f,1.0f));
  rectVertices.push_back(Vector2f(1.0f,1.0f));
  rectVertices.push_back(Vector2f(1.0f,0.0f));
  glGenBuffersARB(1,&(dataItem->rectVArrayBufferId));
  glBindBufferARB(GL_ARRAY_BUFFER, dataItem->rectVArrayBufferId);
  glBufferDataARB(GL_ARRAY_BUFFER, rectVertices.size()*sizeof(Vector2f), &rectVertices.front(),GL_STATIC_DRAW);
  glBindBufferARB(GL_ARRAY_BUFFER, 0);

  GLuint index = glGetAttribLocationARB(dataItem->rayCastingShader, "Vertex");
  glGenVertexArrays(1,&(dataItem->rectVerticesArrayId));
  glBindVertexArray(dataItem->rectVerticesArrayId);
  glBindBufferARB(GL_ARRAY_BUFFER, dataItem->rectVArrayBufferId);
  glEnableVertexAttribArrayARB(index);
  glVertexAttribPointerARB(index,2, GL_FLOAT,false,0,NULL);
  glBindVertexArray(0);
  glBindBufferARB(GL_ARRAY_BUFFER, 0);

  //debug
  std::cout<<"initial 1"<<std::endl;

  glNewList(dataItem->displayListIds[0],GL_COMPILE);
  glBindVertexArray(dataItem->rectVerticesArrayId);
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  glBindVertexArray(0);
  glEndList();
  //debug
  std::cout<<"initial finish"<<std::endl;

}