title: Having issues rendering a 2D rectangle on a 3D plane in OpenGL
body: <p>I'm having issues rendering a 2D object in the 3D space in OpenGL. Whenever I compile my code I get a purple background, which is what I set it to. However, I do not see the object that I should be able to see. I think it's something wrong with the vertex shader, however, I am not completely sure.</p>
  <p>Here is my code:</p>
  <pre><code>#pragma once
  
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;glm/glm.hpp&gt;
  #include &lt;glm/gtc/matrix_transform.hpp&gt;
  #include &lt;glm/gtc/type_ptr.hpp&gt;
  
  #ifndef STBI_INCLUDE_STB_IMAGE_H
  #define STB_IMAGE_IMPLEMENTATION
  #include &quot;stb_image.h&quot;
  #endif
  #include &quot;Shader.h&quot;
  
  class Render3D {
  private:
      GLFWwindow* window;
      const int windowWidth = 1920;
      const int windowHeigth = 1080;
  
      Render3D(Render3D&amp; render){}
      Render3D operator=(Render3D&amp; render) {}
      
      void processInput(GLFWwindow* window) {
  
          
  
      }
  
      void privateInit() {
          if (!glfwInit()) {
              throw(-1);
              std::cout &lt;&lt; &quot;Error: GLFW Init Failed&quot; &lt;&lt; std::endl;
          }
  
          glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
          glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
          glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  
          window = glfwCreateWindow(windowWidth, windowHeigth, &quot;HAHAH BRRR&quot;, NULL, NULL);
          if (window == NULL) {
              std::cout &lt;&lt; &quot;Failed to create GLFW Window \n&quot;;
              glfwTerminate();
              throw (-1);
          }
  
          glfwMakeContextCurrent(window);
  
          glewExperimental = true;
          if (glewInit() != GLEW_OK) {
              std::cout &lt;&lt; &quot;GLEW INIT FAILED\n&quot;;
              exit(1);
          }
  
          //The first two parameters set the position of the lower left corner
          glViewport(0, 0, windowWidth, windowHeigth);
      }
      
  
      
  
  
      void render() {
  
          float positions[] = {
  
              // positions          // colors           // texture 
                   0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
                   0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
                  -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
                  -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // top left 
  
          };
  
          unsigned int indices[] = {
              0, 1, 3,
              1, 2, 3
  
          };
  
          const char* vertexShader =
              &quot;#version 330 core\n&quot;
              &quot;\n&quot;
              &quot;layout (location = 0) vec3 aPos;\n&quot;
              &quot;layout (location = 1) vec3 aColors;\n&quot;
              &quot;layout (location = 2) vec2 aTex;\n&quot;
              &quot;\n&quot;
              &quot;uniform mat4 model;\n&quot;
              &quot;uniform mat4 view;\n&quot;
              &quot;uniform mat4 projection;\n&quot;
              &quot;\n&quot;
              &quot;out vec3 Colors;\n&quot;
              &quot;out vec2 TextureCoords;\n&quot;
              &quot;\n&quot;
              &quot;void main(){\n&quot;
              &quot;\n&quot;
              &quot;gl_Position = projection * view * model * vec4(aPos, 1.0f);\n&quot;
              &quot;Colors = aColors;\n&quot;
              &quot;TextureCoords = aTex;\n&quot;
              &quot;\n&quot;
              &quot;}\0&quot;;
  
          const char* fragmentShader =
              &quot;#version 330 core\n&quot;
              &quot;\n&quot;
              &quot;out vec4 ourFrag;\n&quot;
              &quot;\n&quot;
              &quot;in vec3 Colors;\n&quot;
              &quot;in vec2 TextureCoords;\n&quot;
              &quot;\n&quot;
              &quot;uniform sampler2D texture1;\n&quot;
              &quot;uniform sampler2D texture2;\n&quot;
              &quot;\n&quot;
              &quot;void main(){\n&quot;
              &quot;\n&quot;
              &quot;ourFrag = mix(texture(texture1, TextureCoords), texture(texture2, TextureCoords), 0.2) * vec4(Colors, 1.0f);;\n&quot;
              &quot;\n&quot;
              &quot;\n&quot;
              &quot;}\0&quot;;
  
          unsigned int VAO, VBO, EBO;
          glGenBuffers(1, &amp;VBO);
          glGenBuffers(1, &amp;EBO);
  
          glGenVertexArrays(1, &amp;VAO);
          glBindVertexArray(VAO);
  
          glBindBuffer(GL_ARRAY_BUFFER, VBO);
          glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
  
          glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
          glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  
          glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(0));
          glEnableVertexAttribArray(0);
          
          glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
          glEnableVertexAttribArray(1);
  
          glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
          glEnableVertexAttribArray(2);
  
          glBindVertexArray(0);
  
          unsigned int Texture1, Texture2;
          glGenTextures(1, &amp;Texture1);
          glGenTextures(1, &amp;Texture2);
  
          glBindTexture(GL_TEXTURE_2D, Texture1);
  
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
          int width, height, nrChannels;
          unsigned char* data = stbi_load(&quot;src/Images/woodblock.jpg&quot;, &amp;width, &amp;height, &amp;nrChannels, 4);
          if (data) {
              glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
              glGenerateMipmap(GL_TEXTURE_2D);
          }
          else {
              std::cout &lt;&lt; &quot;TEXTURE FAILED:&quot;;
              std::cout &lt;&lt; stbi_failure_reason() &lt;&lt; std::endl;
          }
  
          glBindTexture(GL_TEXTURE_2D, Texture2);
  
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
          stbi_set_flip_vertically_on_load(1);
          data = stbi_load(&quot;src/Images/awesomeface.png&quot;, &amp;width, &amp;height, &amp;nrChannels, 4);
          if (data) {
              glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
              glGenerateMipmap(GL_TEXTURE_2D);
          }
          else {
              std::cout &lt;&lt; &quot;TEXTURE FAILED:&quot;;
              std::cout &lt;&lt; stbi_failure_reason() &lt;&lt; std::endl;
          }
  
  
          Shader render3D_Program(vertexShader, fragmentShader, 1);
          render3D_Program.use();
  
          glUniform1i(glGetUniformLocation(render3D_Program.ID, &quot;texture1&quot;), 0);
          glUniform1i(glGetUniformLocation(render3D_Program.ID, &quot;texture2&quot;), 1);
  
  
          while (!glfwWindowShouldClose(window)) {
              processInput(window);
  
              glClearColor(0.5f, 0.2f, 0.6f, 1.0f);
              glClear(GL_COLOR_BUFFER_BIT);
  
              glActiveTexture(GL_TEXTURE0);
              glBindTexture(GL_TEXTURE_2D, Texture1);
              glActiveTexture(GL_TEXTURE1);
              glBindTexture(GL_TEXTURE_2D, Texture2);
  
              render3D_Program.use();
              glm::mat4 model = glm::mat4(1.0f);
              glm::mat4 view1 = glm::mat4(1.0f);
              glm::mat4 projection = glm::mat4(1.0f);
  
              model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
              view1 = glm::translate(view1, glm::vec3(0.0f, 0.0f, -3.0f));
              projection = glm::perspective(glm::radians(45.0f), (float)windowWidth / (float)windowHeigth, 0.1f, 100.0f);
  
  
              glUniformMatrix4fv(glGetUniformLocation(render3D_Program.ID, &quot;model&quot;), 1, GL_FALSE, value_ptr(model));
              glUniformMatrix4fv(glGetUniformLocation(render3D_Program.ID, &quot;view&quot;), 1, GL_FALSE, value_ptr(view1));
              glUniformMatrix4fv(glGetUniformLocation(render3D_Program.ID, &quot;projection&quot;), 1, GL_FALSE, value_ptr(projection));
  
  
              glBindVertexArray(VAO);
              glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
              glfwSwapBuffers(window);
              glfwPollEvents();
          }
  
          glDeleteVertexArrays(1, &amp;VAO);
          glDeleteBuffers(1, &amp;VBO);
          glDeleteBuffers(1, &amp;VBO);
  
          glfwTerminate();
      }
  
  public:
  
      Render3D(){}
  
      void Init() {
          privateInit();
      }
  
      void Run() {
          render();
      }
  
  };
  </code></pre>
  <p>Additionally, here is the Shader class:</p>
  <pre><code>#ifndef SHADER_H
  #define SHADER_H
  
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;iostream&gt;
  
  class Shader
  {
  public:
      unsigned int ID;
      // constructor generates the shader on the fly
      // ------------------------------------------------------------------------
      Shader(const char* vertexPath, const char* fragmentPath)
      {
          // 1. retrieve the vertex/fragment source code from filePath
          std::string vertexCode;
          std::string fragmentCode;
          std::ifstream vShaderFile;
          std::ifstream fShaderFile;
          // ensure ifstream objects can throw exceptions:
          vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
          fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
          try
          {
              // open files
              vShaderFile.open(vertexPath);
              fShaderFile.open(fragmentPath);
              std::stringstream vShaderStream, fShaderStream;
              // read file's buffer contents into streams
              vShaderStream &lt;&lt; vShaderFile.rdbuf();
              fShaderStream &lt;&lt; fShaderFile.rdbuf();
              // close file handlers
              vShaderFile.close();
              fShaderFile.close();
              // convert stream into string
              vertexCode = vShaderStream.str();
              fragmentCode = fShaderStream.str();
          }
          catch (std::ifstream::failure&amp; e)
          {
              std::cout &lt;&lt; &quot;ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ&quot; &lt;&lt; std::endl;
          }
          const char* vShaderCode = vertexCode.c_str();
          const char* fShaderCode = fragmentCode.c_str();
          // 2. compile shaders
          unsigned int vertex, fragment;
          // vertex shader
          vertex = glCreateShader(GL_VERTEX_SHADER);
          glShaderSource(vertex, 1, &amp;vShaderCode, NULL);
          glCompileShader(vertex);
          checkCompileErrors(vertex, &quot;VERTEX&quot;);
          // fragment Shader
          fragment = glCreateShader(GL_FRAGMENT_SHADER);
          glShaderSource(fragment, 1, &amp;fShaderCode, NULL);
          glCompileShader(fragment);
          checkCompileErrors(fragment, &quot;FRAGMENT&quot;);
          // shader Program
          ID = glCreateProgram();
          glAttachShader(ID, vertex);
          glAttachShader(ID, fragment);
          glLinkProgram(ID);
          checkCompileErrors(ID, &quot;PROGRAM&quot;);
          // delete the shaders as they're linked into our program now and no longer necessary
          glDeleteShader(vertex);
          glDeleteShader(fragment);
      }
  
      Shader(const char* VertexShader, const char* FragmentShader, bool Specifier) {
         unsigned int vertex = glCreateShader(GL_VERTEX_SHADER);
         unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER);
         glShaderSource(vertex, 1, &amp;VertexShader, NULL);
         glShaderSource(fragment, 1, &amp;FragmentShader, NULL);
         glCompileShader(vertex);
  
         int successVertex;
         char infoLogVertex[512];
         glGetShaderiv(vertex, GL_COMPILE_STATUS, &amp;successVertex);
         if (!successVertex) {
             glGetShaderInfoLog(vertex, 512, NULL, infoLogVertex);
             std::cout &lt;&lt; &quot;ERROR::SHADER::VERTEX::COMPILATION_FAILED\n&quot; &lt;&lt; infoLogVertex &lt;&lt; std::endl;
         }
  
         glCompileShader(fragment);
         int successFragment;
         glGetShaderiv(fragment, GL_COMPILE_STATUS, &amp;successFragment);
         if (!successFragment){
             glGetShaderInfoLog(fragment, 512, NULL, infoLogVertex);
             std::cout &lt;&lt; &quot;ERROR::SHADER:FRAGMENT::COMPILATION_FAILED\n&quot; &lt;&lt; infoLogVertex &lt;&lt; std::endl;
         }
  
         ID = glCreateProgram();
         glAttachShader(ID, vertex);
         glAttachShader(ID, fragment);
         glLinkProgram(ID);
         checkCompileErrors(ID, &quot;PROGRAM&quot;);
  
         glDeleteShader(vertex);
         glDeleteShader(fragment);
  
      }
  
      // activate the shader
      // ------------------------------------------------------------------------
      void use()
      {
          glUseProgram(ID);
      }
      // utility uniform functions
      // ------------------------------------------------------------------------
      void setBool(const std::string&amp; name, bool value) const
      {
          glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
      }
      // ------------------------------------------------------------------------
      void setInt(const std::string&amp; name, int value) const
      {
          glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
      }
      // ------------------------------------------------------------------------
      void setFloat(const std::string&amp; name, float value) const
      {
          glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
      }
  
  private:
      // utility function for checking shader compilation/linking errors.
      // ------------------------------------------------------------------------
      void checkCompileErrors(unsigned int shader, std::string type)
      {
          int success;
          char infoLog[1024];
          if (type != &quot;PROGRAM&quot;)
          {
              glGetShaderiv(shader, GL_COMPILE_STATUS, &amp;success);
              if (!success)
              {
                  glGetShaderInfoLog(shader, 1024, NULL, infoLog);
                  std::cout &lt;&lt; &quot;ERROR::SHADER_COMPILATION_ERROR of type: &quot; &lt;&lt; type &lt;&lt; &quot;\n&quot; &lt;&lt; infoLog &lt;&lt; &quot;\n -- --------------------------------------------------- -- &quot; &lt;&lt; std::endl;
              }
          }
          else
          {
              glGetProgramiv(shader, GL_LINK_STATUS, &amp;success);
              if (!success)
              {
                  glGetProgramInfoLog(shader, 1024, NULL, infoLog);
                  std::cout &lt;&lt; &quot;ERROR::PROGRAM_LINKING_ERROR of type: &quot; &lt;&lt; type &lt;&lt; &quot;\n&quot; &lt;&lt; infoLog &lt;&lt; &quot;\n -- --------------------------------------------------- -- &quot; &lt;&lt; std::endl;
              }
          }
      }
  };
  #endif
  </code></pre>