C语言 OpenGL -三角形不能用颜色和顶点的动态数组生成

jdzmm42g  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(117)
#include <GLFW/glfw3.h>
#include <GLES2/gl2.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// Vertex Shader source code
const char* vertexShaderSource = R"(
    #version 330 core

    layout(location = 0) in vec3 inPosition; // Vertex positions
    layout(location = 1) in vec3 inColor;    // Vertex colors

    out vec3 fragColor; // Output color to the fragment shader

  void main()
  {
      gl_Position = vec4(inPosition, 1.0);
      fragColor = inColor; // Pass color to the fragment shader
  }
)";

// Fragment Shader source code
const char* fragmentShaderSource = R"(
    #version 330 core

    in vec3 fragColor; // Input color from the vertex shader

    out vec4 outColor; // Output color

    void main()
    {
        outColor = vec4(fragColor, 1.0); // Set the fragment color using the input color
    }

)";

void sleep_for_microseconds(unsigned int microseconds) {
  struct timespec ts;
  ts.tv_sec = 0;
  ts.tv_nsec = microseconds * 1000;
  nanosleep(&ts, NULL);
}

GLfloat vertices[9];
GLfloat colors[9];

void renderloop(GLuint vao, GLuint vbo, GLuint colorBuffer)
{
    define_vertices();
    define_colors();

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    // Update the VBO with new vertex data
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_DYNAMIC_DRAW);

    //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

     // Render loop
    // Clear the screen
    glClearColor(0.35f, 0.15f, 0.25f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Draw the triangle
    glDrawArrays(GL_TRIANGLES, 0, 3*100);
    glFinish();
}

void render()
{
  static int framecount = 0;
  printf("%d framecount\n", framecount);
  GLuint vao;
  GLuint vbo;
  GLuint colorBuffer;
    if (framecount == 0)
    {
      // Compile and link shaders
      GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
      glCompileShader(vertexShader);

      GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
      glCompileShader(fragmentShader);

      GLuint shaderProgram = glCreateProgram();
      glAttachShader(shaderProgram, vertexShader);
      glAttachShader(shaderProgram, fragmentShader);
      glLinkProgram(shaderProgram);

      // Use the shader program for rendering
      glUseProgram(shaderProgram);
      // 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_DYNAMIC_DRAW);
        glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_DYNAMIC_DRAW);

  }
  framecount+=1;
  renderloop(vao,vbo,colorBuffer);
    framecount++;
}

float randomFloat(float min, float max) {
    return min + ((float)rand() / RAND_MAX) * (max - min);
}

// Main function
void main(void) {
// Initialize GLFW
  glfwInit();
  define_vertices();
  define_colors();
  // 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();
    sleep_for_microseconds(40000);
  }
  // Terminate GLFW
  glfwTerminate();

}

define_colors和define_vertices是两个函数,用于在每个帧之后更新colors和vertices全局变量。我已经验证了一帧的代码,工作正常,(当有顶点和颜色的静态数组时)。但是,当我有动态变量的颜色和顶点如下,没有三角形被渲染。有什么问题吗。我通过在生成的顶点中添加打印来验证,它似乎正在正确生成。

j2cgzkjk

j2cgzkjk1#

我将你的代码移植到Visual Studio 2019中,并做了一些修改,使其绘制一个简单的旋转三角形并改变颜色。编译器抱怨局部变量 VAO 、vbo和colorBuffer在没有初始化的情况下使用。您应该立即初始化(在第0帧),并在接下来的帧中使用。所以你不应该使用局部变量。或者将它们移出函数作用域(例如,全局变量)或使它们成为静态变量,如:

void render()
{
    static GLuint vao;
    static GLuint vbo;
    static GLuint colorBuffer;

    static int framecount = 0;
    printf("%d framecount\n", framecount);

    if (framecount == 0)
    {
        // Compile and link shaders
    ...
    }
    framecount += 1;
    renderloop(vao, vbo, colorBuffer);
    framecount++;
}

在这些更新之后,我可以毫无问题地渲染我的旋转三角形。

相关问题