Triangles not getting rendered with color with color array added along with vertices

35 views Asked by At
#include <GLFW/glfw3.h>
#include <GLES2/gl2.h>
#include <unistd.h>
#include <stdio.h>
  

      GLfloat vertices[] = {
          0.0f, 0.0f, 0.0f,
          1.0f, 0.0f, 0.0f,
          0.0f, 1.0f, 0.0f
        };
        GLfloat colors[] = {
            1.0f, 1.0f, 0.0f,
          1.0f, 0.0f, 0.0f,
          0.0f, 0.0f, 1.0f
        };
    
    void renderloop(GLuint vao, GLuint vbo, GLuint colorBuffer)
        {
        
            glBindBuffer(GL_ARRAY_BUFFER, vbo);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        
            glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
            glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
        
             // Render loop
            // Clear the screen
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
            // Draw the triangle
            glDrawArrays(GL_TRIANGLES, 0, 3);
            glFinish();
        
        }
        
        void render()
        {
          static int framecount = 0;
          printf("%d framecount\n", framecount);
          GLuint vao;
          GLuint vbo;
          GLuint colorBuffer;
          //can put an if based to initalize based on first allocation
            if (framecount == 0)
            {
                // Vertex array object
                glGenVertexArrays(1, &vao);
                glBindVertexArray(vao);
        
                // Vertex buffer object
                glGenBuffers(1, &vbo);
                glBindBuffer(GL_ARRAY_BUFFER, vbo);
                // Vertex attribute pointers (positions)
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
                // Vertex attribute pointers (colors)
        
                glGenBuffers(1, &colorBuffer);
                glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
                glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
        
                glEnableVertexAttribArray(0); // Vertex attribute location 0 (positions)
                glEnableVertexAttribArray(1); // Vertex attribute location 1 (colors)
        
                glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
                glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
        
            // Enable the vertex attribute array for color
            // Unbind VAO to prevent accidental changes
                //glBindVertexArray(0);
            
          }
          framecount+=1;
          renderloop(vao,vbo,colorBuffer);
            framecount++;
        }
        
        
        // Main function
        void main(void) {
        // Initialize GLFW
          glfwInit();
        
          // Create a window
          GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL);
          if (window == NULL) {
            glfwTerminate();
            return;
          }
        
          // Make the window the current context
          glfwMakeContextCurrent(window);
          for(int i=0;i<10000;i++)
          {
            render();
            glfwSwapBuffers(window);
            glfwPollEvents();
        
          }
          // Terminate GLFW
          glfwTerminate();
        
        }

The intention is to change color and vertices for each frame and then issue glDrawArrays call. I'm facing an issue where the triangle is getting drawn, but color on triangle is still white. What has to be done for the color values also to be considered as per the color array ? Tried different ways of binding and unbinding the buffer before and after the glDrawArray call, but nothing seems to work.

I don't have any shaders in this code. I only have these. Also, the code was working fine with just the vertices array, if I add the color array then it stops working.

0

There are 0 answers