Code from slides

OpenGL laboratories 1
  • Vertex pipeline configuration
    • Assign identity matrix to matrix variable M
      glm::mat4 M=glm::mat4(1.0f);
      
    • Compute view matrix V
      glm::mat4 V=glm::lookAt(
      glm::vec3(0.0f,0.0f,-5.0f),
      glm::vec3(0.0f,0.0f,0.0f),
      glm::vec3(0.0f,1.0f,0.0f));
      
    • Compute perspective projection matrix
      glm::mat4 P=glm::perspective(50.0f*PI/180.0f, 1.0f, 1.0f, 50.0f);
      
    • Load model, view and projection matrices into the shader program
      spConstant->use();//Activate shader program
      glUniformMatrix4fv(spConstant->u("P"),1,false,glm::value_ptr(P));
      glUniformMatrix4fv(spConstant->u("V"),1,false,glm::value_ptr(V));
      glUniformMatrix4fv(spConstant->u("M"),1,false,glm::value_ptr(M));
      
  • Animation
    • Declaration
      //Global variable declaration
      float speed=360; //360 degrees/s
      
    • drawScene procedure (choose onlyu appropriate fragments)
      void drawScene(GLFWwindow* window,float angle) {
      //……
      mat4 M=mat4(1.0f);
      M=rotate(M,angle,vec3(0.0f,1.0f,0.0f));
      //……
      Models::torus.drawWire();
      }
      
    • Main loop changes in main function
      float angle=0;
      glfwSetTime(0);
      while (!glfwWindowShouldClose(window)) {
      angle+=speed*glfwGetTime();
      glfwSetTime(0);
      drawScene(window,angle);
      glfwPollEvents();
      }
      
  • Load model, view and projection matrices into another shader program
    spLambert->use();//Activate shader program
    glUniform4f(spLambert->u("color"),0,1,0,1);
    glUniformMatrix4fv(spLambert->u("P"),1,false,glm::value_ptr(P));
    glUniformMatrix4fv(spLambert->u("V"),1,false,glm::value_ptr(V));
    glUniformMatrix4fv(spLambert->u("M"),1,false,glm::value_ptr(M));
    
OpenGL laboratories 2
  • Key processing
    void key_callback(GLFWwindow* window, int key,
    int scancode, int action, int mods){
    if (action == GLFW_PRESS) {
    if (key == GLFW_KEY_A) printf("A \n");
    if (key == GLFW_KEY_D) printf("D \n");
    if (key == GLFW_KEY_W && (mods & GLFW_MOD_ALT)!=0) printf("ALT+W\n");
    }
    
    if (action == GLFW_RELEASE) {
    if (key == GLFW_KEY_W) printf("puszczone W\n");
    }
    }
    
  • key_callback procedure registration
    glfwSetKeyCallback(window, key_callback);
    
OpenGL laboratories 3
  • glDrawArrays example 1
       //Model description
       //vertex coordinate array
       float verts[]={ 
          0, 1,0,1,
         -1,-1,0,1,
          1,-1,0,1
       };
       int vertexCount=3; //Number of vertices
    
       //Drawing code
       spConstant->use();
       glUniformMatrix4fv(spConstant->u("P"),1,false,glm::value_ptr(P));
       glUniformMatrix4fv(spConstant->u("V"),1,false,glm::value_ptr(V));
       glUniformMatrix4fv(spConstant->u("M"),1,false,glm::value_ptr(M));
       glUniform4f(spConstant->u("color"),1,0,0,1);
    
       glEnableVertexAttribArray(spConstant->a("vertex"));
       glVertexAttribPointer(spConstant->a("vertex"),4,GL_FLOAT,false,0,verts);
    
       glDrawArrays( GL_TRIANGLES, 0, vertexCount );
    
       glDisableVertexAttribArray(spConstant->a("vertex"));
      
  • glDrawArrays example 2
    //Model description
    //Vertex coordinate array
    float verts[]={
      0,4.08,0,1,     0,0,2.88,1,
      0,4.08,0,1,     2.5,0,-1.44,1,
      0,4.08,0,1,    -2.5,0,-1.44,1,
      0,0,2.88,1,    -2.5,0,-1.44,1,
      0,0,2.88,1,     2.5,0,-1.44,1,
      -2.5,0,-1.44,1, 2.5,0,-1.44,1
    };
    int vertexCount=12;
    
    ///Drawing code
    spConstant->use();
    glUniformMatrix4fv(spConstant->u("P"),1,false,glm::value_ptr(P));
    glUniformMatrix4fv(spConstant->u("V"),1,false,glm::value_ptr(V));
    glUniformMatrix4fv(spConstant->u("M"),1,false,glm::value_ptr(M));
    glUniform4f(spConstant->u("color"),0,1,0,1);
    
    glEnableVertexAttribArray(spConstant->a("vertex"));
    glVertexAttribPointer(spConstant->a("vertex"),4,GL_FLOAT,false,0,verts);
    
    glDrawArrays( GL_LINES, 0, vertexCount );
    
    glDisableVertexAttribArray(spConstant->a("vertex"));
      
  • glDrawArrays example 3
    //Model description
    //Vertex coordinate array
    float verts[]={
      0,4.08,0,1,     0,0,2.88,1,    -2.5,0,-1.44,1,
      0,4.08,0,1,     0,0,2.88,1,     2.5,0,-1.44,1,
      0,4.08,0,1,     2.5,0,-1.44,1, -2.5,0,-1.44,1,
      2.5,0,-1.44,1, -2.5,0,-1.44,1,  0,0,2.88,1};
    //Vertex color array
    float colors[]={
      1,0,0,1,  1,0,0,1,  1,0,0,1,
      0,1,0,1,  0,1,0,1,  0,1,0,1,
      0,0,1,1,  0,0,1,1,  0,0,1,1,
      1,1,0,1,  1,1,0,1,  1,1,0,1};
    int vertexCount=12;
    
    //Drawing code
    spColored->use();
    glUniformMatrix4fv(spColored->u("P"),1,false,glm::value_ptr(P));
    glUniformMatrix4fv(spColored->u("V"),1,false,glm::value_ptr(V));
    glUniformMatrix4fv(spColored->u("M"),1,false,glm::value_ptr(M));
    
    glEnableVertexAttribArray(spColored->a("vertex"));
    glVertexAttribPointer(spColored->a("vertex"),4,GL_FLOAT,false,0,verts);
    
    glEnableVertexAttribArray(spColored->a("color"));
    glVertexAttribPointer(spColored->a("color"),4,GL_FLOAT,false,0,colors);
    
    glDrawArrays( GL_TRIANGLES, 0, vertexCount );
    
    glDisableVertexAttribArray(spColored->a("vertex"));
    glDisableVertexAttribArray(spColored->a("color"));
      
  • glDrawElements example
    //Model description
    //Vertex coordinate array
    float verts[]={
      0,4.08,0,1,     0,0,2.88,1,   -2.5,0,-1.44,1,    2.5,0,-1.44,1};
    //Vertex color array
    float colors[]={
      1,0,0,1,  0,1,0,1,  0,0,1,1,  1,1,0,1};
    //Index array
    unsigned int indexes[] ={
      0,1,2,  0,1,3,  0,2,3,  3,2,1};
    int indexCount=12;
    int vertexCount=4;
    
    
    //Drawing code
    spColored->use();
    glUniformMatrix4fv(spColored->u("P"),1,false,glm::value_ptr(P));
    glUniformMatrix4fv(spColored->u("V"),1,false,glm::value_ptr(V));
    glUniformMatrix4fv(spColored->u("M"),1,false,glm::value_ptr(M));
    
    glEnableVertexAttribArray(spColored->a("vertex"));
    glVertexAttribPointer(spColored->a("vertex"),4,GL_FLOAT,false,0,verts);
    
    glEnableVertexAttribArray(spColored->a("color"));
    glVertexAttribPointer(spColored->a("color"),4,GL_FLOAT,false,0,colors);
    
    glDrawElements(GL_TRIANGLES,indexCount, GL_UNSIGNED_INT,indexes);
    
    glDisableVertexAttribArray(spColored->a("vertex"));
    glDisableVertexAttribArray(spColored->a("color"));
      
  • Reading texture
    GLuint tex; //Handle – global declaration
    
    //Function for reading texture files
    GLuint readTexture(char* filename) {	
      GLuint tex;	
      glActiveTexture(GL_TEXTURE0); 	
    
      //Read into computers memory
      std::vector<unsigned char> image;   //Allocate data structure for the image
      unsigned width, height;   //Variables, which will be set to image dimensions
      //Read image
      unsigned error = lodepng::decode(image, width, height, filename);
     
      //Import to graphics card memory
      glGenTextures(1,&tex); //Initialize a handle
      glBindTexture(GL_TEXTURE_2D, tex); //Activate the handle
      //Copy image from computers memory to graphics cards memory
      glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*) image.data());	
    
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
    
      return tex;
    } 
    
    //Reading and import of a concrete image, include in initOpenGLProgram
    tex=readTexture("bricks.png");
    
    //Remove texture from graphics card memory, include in freeOpenGLProgram
    glDeleteTextures(1,&tex);
      
  • Drawing of a textured model
    //Model description
    //Vertex coordinate array
    float verts[]={
      1.0f,-1.0f,0.0f,1.0f, //A
     -1.0f, 1.0f,0.0f,1.0f, //B
     -1.0f,-1.0f,0.0f,1.0f, //C
    
      1.0f,-1.0f,0.0f,1.0f, //A
      1.0f, 1.0f,0.0f,1.0f, //D
     -1.0f, 1.0f,0.0f,1.0f, //B
    };
    
    //TExturing coordinate array
    float texCoords[]={
      1.0f, 0.0f,	//A
      0.0f, 1.0f,    //B
      0.0f, 0.0f,    //C
    
      1.0f, 0.0f,    //A
      1.0f, 1.0f,    //D
      0.0f, 1.0f,    //B
    };
    
    //Number of vertices
    int vertexCount=6;
    
    //Drawing code
    spTextured->use();
    glUniformMatrix4fv(spTextured->u("P"),1,false,glm::value_ptr(P));
    glUniformMatrix4fv(spTextured->u("V"),1,false,glm::value_ptr(V));
    glUniformMatrix4fv(spTextured->u("M"),1,false,glm::value_ptr(M));
    
    glEnableVertexAttribArray(spTextured->a("vertex"));
    glVertexAttribPointer(spTextured->a("vertex"),4,GL_FLOAT,false,0,verts);
    
    glEnableVertexAttribArray(spTextured->a("texCoord"));
    glVertexAttribPointer(spTextured->a("texCoord"),2,GL_FLOAT,false,0,texCoords);
    
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,tex);
    glUniform1i(spLambertTextured->u("tex"),0);
    
    glDrawArrays( GL_TRIANGLES, 0, vertexCount );
    
    glDisableVertexAttribArray(spTextured->a("vertex"));
    glDisableVertexAttribArray(spTextured->a("texCoord"));
      
  • Drawing of textured and shaded model
    //Model description
    //Vertex coordinate array
    float verts[]={
      1.0f,-1.0f,0.0f,1.0f, //A
     -1.0f, 1.0f,0.0f,1.0f, //B
     -1.0f,-1.0f,0.0f,1.0f, //C
    
      1.0f,-1.0f,0.0f,1.0f, //A
      1.0f, 1.0f,0.0f,1.0f, //D
     -1.0f, 1.0f,0.0f,1.0f}; //B
    //Texturing coordinate array
    float texCoords[]={
      1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, //ABC
      1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, //ADB
    };
    //Normal vector array
    float normals[]={
      0.0f,0.0f,1.0f,0.0f, //A
      0.0f,0.0f,1.0f,0.0f, //B
      0.0f,0.0f,1.0f,0.0f, //C
    
      0.0f,0.0f,1.0f,0.0f, //A
      0.0f,0.0f,1.0f,0.0f, //D
      0.0f,0.0f,1.0f,0.0f, //B
    };
    
    //Number of vertices
    int vertexCount=6;
    
    //Drawing code
    glEnableVertexAttribArray(spLambertTextured->a("vertex"));
    glVertexAttribPointer(spLambertTextured->a("vertex"),4,GL_FLOAT,false,0,verts);
    
    glEnableVertexAttribArray(spLambertTextured->a("normal"));
    glVertexAttribPointer(spLambertTextured->a("normal"),4,GL_FLOAT,false,0,normals);
    
    glEnableVertexAttribArray(spLambertTextured->a("texCoord"));
    glVertexAttribPointer(spLambertTextured->a("texCoord"),2,GL_FLOAT,false,0,texCoords);
    
    glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,tex);
    glUniform1i(spLambertTextured->u("tex"),0);
    
    glDrawArrays( GL_TRIANGLES, 0, vertexCount );
    glDisableVertexAttribArray(spLambertTextured->a("vertex"));
    glDisableVertexAttribArray(spLambertTextured->a("normal"));
    glDisableVertexAttribArray(spLambertTextured->a("texCoord"));
    
      
OpenGL laboratories 6
  • Reading texture
    GLuint tex0; //Handle – global declaration
    
    //Function for reading texture files
    GLuint readTexture(const char* filename) {	
      GLuint tex;	
      glActiveTexture(GL_TEXTURE0); 	
    
      //Read into computers memory
      std::vector<unsigned char> image;   //Allocate data structure for the image
      unsigned width, height;   //Variables, which will be set to image dimensions
      //Read image
      unsigned error = lodepng::decode(image, width, height, filename);
     
      //Import to graphics card memory
      glGenTextures(1,&tex); //Initialize a handle
      glBindTexture(GL_TEXTURE_2D, tex); //Activate the handle
      //Copy image from computers memory to graphics cards memory
      glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*) image.data());	
    
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
    
      return tex;
    } 
    
    //Reading and import of a concrete image, include in initOpenGLProgram
    tex0=readTexture("metal.png");
    
    //Remove texture from graphics card memory, include in freeOpenGLProgram
    glDeleteTextures(1,&tex0);
      
  • Setting up the attribute texCoord0
    
    //In drawScene
    glEnableVertexAttribArray(sp->a("texCoord0"));
    …
    glVertexAttribPointer(sp->a("texCoord0"),
    	2,GL_FLOAT,false,0,texCoords);//an appropriate array
    …
    glDisableVertexAttribArray(sp->a("texCoord0"));
    
  • Configure sampler variable
    glUniform1i(sp->u("textureMap0"),0); //drawScene
    
  • Configure texturing unit
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,tex0);