Exemplo n.º 1
0
void GlShaderProgram::addShaderFromSourceCode(const GlShader::ShaderType shaderType, const std::string &shaderSrc) {
    GlShader *shader = new GlShader(shaderType);
    shader->setAnonymousCreation(true);
    shader->compileFromSourceCode(shaderSrc);
    addShader(shader);

}
Exemplo n.º 2
0
//====================================================== Отрисовка кубика ===============================================
void render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	shader.use();
	shader.setUniform(Unif_matrix, Matrix_projection);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO_element);
	glEnableVertexAttribArray(Attrib_vertex);
	glBindBuffer(GL_ARRAY_BUFFER, VBO_vertex);
	glVertexAttribPointer(Attrib_vertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(Attrib_color);
	glBindBuffer(GL_ARRAY_BUFFER, VBO_color);
	glVertexAttribPointer(Attrib_color, 3, GL_FLOAT, GL_FALSE, 0, 0);


	// Отрисовываем
	glDrawElements(GL_TRIANGLES, Indices_count, GL_UNSIGNED_INT, 0);

	glutSwapBuffers();
	angleCube -= 0.5f; // Угол поворота
}
Exemplo n.º 3
0
void reRender(REid render)
{
  // initialize variables
  Render* r = (Render*)render;
  if(r == null)
    return; //error
  if(r->scene == null)
    return; //error
  GlRasScene& scene = *(GlRasScene*)r->scene;

  //todo: scene.background.render()
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // temporary: render everything in white
  glColor3f(1.0f, 1.0f, 1.0f);

  // set up window
  {
    glViewport(0, 0, 800, 600);
  }

  //todo: set up camera
  {
    glMatrixMode(GL_PROJECTION);
    if(r->camera)
    //if(false)
    {
      Matrix4 transformation;
      r->camera->getTransform(transformation);
      glLoadMatrixf(transformation);
    }
    else
    {
      //TEMP:
      glLoadIdentity();
      gluPerspective(45.0f, 800.0f/600.0f,0.1f,100.0f); // todo: replace with custom function
      //todo: set backplane to world max and front plane to geom closest to camera??? (This will allow best z-buffer precision)

      /*float rotX, rotY, rotZ;
      camera.rotation.get(rotX, rotY, rotZ);
      glRotatef(-rotX*180.0/PI, 1.0f, 0.0f, 0.0f);
      glRotatef(-rotY*180.0/PI, 0.0f, 1.0f, 0.0f);
      glRotatef(-rotZ*180.0/PI, 0.0f, 0.0f, 1.0f);*/

      /*float locX, locY, locZ;
      camera.location.get(locX, locY, locZ);
      glTranslatef(-locX, -locY, -locZ);*/
      glTranslatef(0.0f, -0.0f, -20.0f);
    }
  }

  // todo: set up world
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  //// render test quad
  //{
  //  glColor3f(0.5f, 0.5f, 0.5f);
  //  glBegin(GL_QUADS);
  //    glVertex3f(-1.0f, 1.0f, 0.0f);
  //    glVertex3f( 1.0f, 1.0f, 0.0f);
  //    glVertex3f( 1.0f,-1.0f, 0.0f);
  //    glVertex3f(-1.0f,-1.0f, 0.0f);
  //  glEnd();
  //  glColor3f(1.0f, 1.0f, 1.0f);
  //}

  // render scene
  {
    // enable render options
    glEnable(GL_DEPTH_TEST);

    // clear background & zbuffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  }

  // old: precalculate ambient occlusion data (now done in endscene)
  for(list<Geometry*>::iterator i = scene.geometry.begin(); i != scene.geometry.end(); ++i)
  {
    // initialize variables
    if((*i)->type != MESH_GEOMETRY)
      continue;
    Mesh& geometry          = *(Mesh*)*i;
    Points* points          = geometry.points;
    Vertices* vertices      = geometry.vertices;
    Edges* edges            = geometry.edges;
    Primitives* primitives  = geometry.primitives;
    Map* map                = geometry.map;

    if(vertices == null)
      continue; // error - no vertices

    // retrieve attributes
    GlRasElements* locationElements = null,
                 * indexElements = null,
                 * normAreaElements = null;
    GlStream* vertexStream = null,
            * indexStream = null,
            * normAreaStream = null;

    if(points)
      locationElements = (GlRasElements*)points->getAttributes(RE_LOCATION);

    if(locationElements != null)
    {
      indexElements = (GlRasElements*)vertices->getAttributes(RE_INDEX);
      normAreaElements = (GlRasElements*)vertices->getAttributes(RE_EXT_BENT_NORMAL_AREA);
    }
    else
    {
      locationElements = (GlRasElements*)vertices->getAttributes(RE_LOCATION);
      if(locationElements == null)
        return; //error
      normAreaElements = (GlRasElements*)vertices->getAttributes(RE_EXT_BENT_NORMAL_AREA);
      indexElements = null;
    }

    vertexStream = locationElements->getStream();
    if(indexElements)
      indexStream = indexElements->getStream();
    if(normAreaElements)
      normAreaStream = normAreaElements->getStream();

    // calculate ambient occlusion
    GlStream occlusionStream;
    if(normAreaStream)
    {
      // create occlusion stream (one occlusion value per vertex)
      occlusionStream.create(vertexStream->getLength(), VEC4_FLOAT16_STREAM, null); //FLOAT16_STREAM?

      // create (& bind) occlusion kernel
      GlKernel kernel;
      kernel.build(fsCalculateDiscOcclusion1Pass, vsTexCoord1);
      kernel.bind();

      // set kernel parameters
      kernel.setUniform1i("vertices", 0);
      kernel.setUniform1i("normDiscs", 1);
      kernel.setUniform1f("nEmitters", (float)vertexStream->getLength());

      // bind vertices, vertex normals & disc area
      vertexStream->bindTexture(0);
      normAreaStream->bindTexture(1);

      // create & bind render target
      GlRenderTarget renderTarget;
      renderTarget.create();
      renderTarget.bind();

      // bind occlusion values as output
      occlusionStream.bindOutput(0);

      // execute kernel
      kernel.execute();

      // unbind occlusion values from output
      occlusionStream.unbindOutput(0);

      // unbind & destroy render target
      renderTarget.unbind();
      renderTarget.destroy();

      // unbind discs and vertices
      normAreaStream->unbindTexture(1);
      vertexStream->unbindTexture(0);

      // destroy kernel
      //GlKernel::restoreFixedFunction();
      kernel.unbind();
      kernel.destroy();
    }

    // render scene geometry
    {
      // temporary: disable textures (shouldn't be needed once shaders are used)
      glActiveTexture(GL_TEXTURE1);
      glDisable(GL_TEXTURE_2D);
      glActiveTexture(GL_TEXTURE0);
      glDisable(GL_TEXTURE_2D);
      glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

      // create (& bind) shader program
      GlShader shader;
      uint occlusionAttributeIndex = 0;
      if(normAreaStream)
      {
        shader.build(fsRenderAO, vsRenderAO);
        shader.bind();

        // get shader parameters
        occlusionAttributeIndex = shader.getAttribute("occlusion");
      }

      // bind vertices
      GLuint VBO = vertexStream->bindVertexBuffer();
      glVertexPointer(3, GL_FLOAT, 0, (char*)null);
      glEnableClientState(GL_VERTEX_ARRAY);

      // bind normals
      /* todo:
      GLuint NBO = normalStream->bindVertexBuffer();
      glNormalPointer(GL_FLOAT, 0, (char*)null);
      glEnableClientState(GL_NORMAL_ARRAY);*/

      // bind occlusion values
      if(normAreaStream)
      {
        GLuint ABO = occlusionStream.bindVertexAttributeBuffer();
        glVertexAttribPointer(occlusionAttributeIndex, 4, GL_FLOAT, GL_FALSE, 0, (char*)null);
        glEnableVertexAttribArray(occlusionAttributeIndex);
      }

      // bind indices
      GLuint IBO = indexStream->bindIndexBuffer();

      //todo: is this correct??
      /*NOT SUPPORTED BY OPENGL: if(indexStream->getInternElementSize() == 4)
        glIndexPointer(GL_UNSIGNED_INT, 0, (char*)null);
      else*/
      glIndexPointer(GL_UNSIGNED_SHORT, 0, (char*)null);

      glEnableClientState(GL_INDEX_ARRAY);

      //enable lighting (temporary)
      {
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        float pos[4] = { 4.0, 4.0, 5.0, 1.0 };
        glLightfv(GL_LIGHT0, GL_POSITION, pos);
        glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
        glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0f);
      }

      // draw triangles
      /*NOT SUPPORTED BY OPENGL: if(indexStream->getInternElementSize() == 4)
        glDrawElements(GL_TRIANGLES, indexStream->getLength(), GL_UNSIGNED_INT, null);
      else*/
      glDrawElements(GL_TRIANGLES, indexStream->getLength(), GL_UNSIGNED_SHORT, null);
      //glDrawElements(GL_TRIANGLES, 42, GL_UNSIGNED_SHORT, null);

      // unbind buffers
      if(normAreaStream)
        glDisableVertexAttribArray(occlusionAttributeIndex);
      //todo: glDisableClientState(GL_NORMAL_ARRAY);
      glDisableClientState(GL_INDEX_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
      if(normAreaStream)
        occlusionStream.unbindVertexAttributeBuffer();
      indexStream->unbindIndexBuffer();
      vertexStream->unbindVertexBuffer();

      // unbind shader program
      if(normAreaStream)
      {
        shader.unbind();
        shader.destroy();
      }
    }
  }

  // disable render options
  {
      glEnable(GL_DEPTH_TEST);
  }

  /*Old: GlStream occlusionStream;
  {
    // create occlusion stream (one occlusion value per vertex)
    occlusionStream.create(scene.geometry.vertexStream.getLength(), VEC4_FLOAT16_STREAM, null); //FLOAT16_STREAM?

    // create (& bind) occlusion kernel
    GlKernel kernel;
    kernel.build(fsCalculateDiscOcclusion1Pass, vsTexCoord1);
    kernel.bind();

    // set kernel parameters
    kernel.setUniform1i("vertices", 0);
    kernel.setUniform1i("normDiscs", 1);
    kernel.setUniform1f("nEmitters", (float)scene.geometry.vertexStream.getLength());

    // bind vertices, vertex normals & disc area
    scene.geometry.vertexStream.bindTexture(0);
    scene.geometry.normDiscStream.bindTexture(1);

    // create & bind render target
    GlRenderTarget renderTarget;
    renderTarget.create();
    renderTarget.bind();

    // bind occlusion values as output
    occlusionStream.bindOutput(0);

    // execute kernel
    kernel.execute();

    // unbind occlusion values from output
    occlusionStream.unbindOutput(0);

    // unbind & destroy render target
    renderTarget.unbind();
    renderTarget.destroy();

    // unbind discs and vertices
    scene.geometry.normDiscStream.unbindTexture(1);
    scene.geometry.vertexStream.unbindTexture(0);

    // destroy kernel
    //GlKernel::restoreFixedFunction();
    kernel.unbind();
    kernel.destroy();
  }*/



  // Debug: Render streams
  /*{
    glMatrixMode(GL_PROJECTION);
    glViewport(0, 0, 800, 600);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(-1.0f, -1.0f, 0.0f);
    glScalef(2.0f, 2.0f, 0.0f);

    float vertices[] = { 0.0f, 1.0f,
                         1.0f, 1.0f,
                         1.0f, 0.0f,
                         0.0f, 0.0f };
    float texcoords[] = { 0.0f, 0.0f,
                          1.0f, 0.0f,
                          1.0f, 1.0f,
                          0.0f, 1.0f };

    //scene.geometry.vertexStream.bindTexture(0);
    //scene.geometry.normDiscStream.bindTexture(0);
    occlusionStream.bindTexture(0);
    //temp:
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    glEnableClientState(GL_VERTEX_ARRAY);
    glClientActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glDrawArrays(GL_QUADS, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);

    // restore transform
    glPopMatrix();
    glViewport(0, 0, 800, 600);
    glMatrixMode(GL_MODELVIEW);
  }*/
}
Exemplo n.º 4
0
void GlShaderProgram::addGeometryShaderFromSourceFile(const std::string &geometryShaderSrcFilename, GLenum inputPrimitiveType, GLenum outputPrimitiveType) {
  GlShader *shader = new GlShader(inputPrimitiveType, outputPrimitiveType);
  shader->setAnonymousCreation(true);
  shader->compileFromSourceFile(geometryShaderSrcFilename);
  addShader(shader);
}
Exemplo n.º 5
0
void GlShaderProgram::addGeometryShaderFromSourceCode(const char *geometryShaderSrc, GLenum inputPrimitiveType, GLenum outputPrimitiveType) {
  GlShader *shader = new GlShader(inputPrimitiveType, outputPrimitiveType);
  shader->setAnonymousCreation(true);
  shader->compileFromSourceCode(geometryShaderSrc);
  addShader(shader);
}
Exemplo n.º 6
0
//====================================================== Инициализация шейдеров ===============================================
void initShader()
{
	// Исходник шейдеров
	const GLchar* vsSource =
		"attribute vec3 coord;\n"
		"attribute vec3 color;\n"
		"varying vec3 var_color;\n"
		"uniform mat4 matrix;\n"
		"uniform vec3  camera_position;\n"
		"void main() {\n"
		"  gl_Position = matrix * vec4(coord*1.5, 1.0);\n"
		"  var_color = color;\n"
		"}\n";
		//"attribute  vec3  in_vertex_position;\n"
		//"attribute  vec2  in_vertex_textureCoord;\n"
		//"attribute  vec3  in_vertex_normal;\n"
		//"uniform    mat4  camera_matrix;\n"
		//"uniform    vec3  camera_position;\n"
		//"uniform    vec3  light_position;\n"
		//"varying    vec2  vertex_textureCoord;\n"
		//"varying    vec3  vertex_normal;\n"
		//"varying    vec3  light_direction;\n"
		//"varying    vec3  view_direction;\n"
		//"void main(void) {\n"
		//"	gl_Position = camera_matrix*vec4(in_vertex_position, 1.0);\n"
		//"	vertex_textureCoord = in_vertex_textureCoord;\n"
		//"	vertex_normal = vec3(camera_matrix*vec4(in_vertex_normal, 1.0));\n"
		//"	light_direction = normalize(light_position - in_vertex_position);\n"
		//"	view_direction = normalize(camera_position - in_vertex_position);\n"
		//"}\n";

	const GLchar* fsSource =
		"varying vec3 var_color;\n"
		"void main() {\n"
		"  gl_FragColor = vec4(var_color*2, 1.0);\n"
		"}\n";
		/*"varying    vec2    vertex_textureCoord;\n"
		"varying    vec3    vertex_normal;\n"
		"varying    vec3    light_direction;\n"
		"varying    vec3    view_direction;\n"
		"uniform    sampler2D  Texture;\n"
		"uniform    vec4    light_ambient;\n"
		"uniform    vec4    light_diffuse;\n"
		"uniform    vec4    light_specular;\n"
		"uniform    vec3    light_attenuation;\n"
		"uniform    vec4    material_ambient;\n"
		"uniform    vec4    material_diffuse;\n"
		"uniform    vec4    material_specular;\n"
		"uniform    vec4    material_emission;\n"
		"uniform    float    material_shininess;\n"
		"void main(void) {\n"
		"	gl_FragColor = material_emission + material_ambient*light_ambient*light_attenuation;\n"
		"	float NdotL = max(dot(vertex_normal, light_direction), 0.0);\n"
		"	gl_FragColor += material_diffuse*light_diffuse*NdotL*light_attenuation;\n"
		"	float RdotVpow = max(pow(dot(reflect(-light_direction, vertex_normal), view_direction), material_shininess), 0.0);\n"
		"	gl_FragColor += material_specular*light_specular*RdotVpow*light_attenuation;\n"
		"	gl_FragColor *= texture(Texture, vertex_textureCoord);\n"
		"}\n";*/
	shader.load(vsSource, fsSource);

	Attrib_vertex = shader.getAttribLocation("coord");
	Attrib_color = shader.getAttribLocation("color");
	Unif_matrix = shader.getUniformLocation("matrix");
}