简体   繁体   中英

OpenGL rendering black square

I am attempting to render a colored rectangle, but I just end up with a black one.

My shaders and shader program do not produce any errors when compiling or linking, so I am sure there is something going wrong when I am attempting to buffer the data. But, everything looks correct to me.

在此处输入图片说明

Shader sources

std::string const fragment_shader_source = "#version 330 core\n"
                                           ""
                                           "in vec4 fColor;\n"
                                           "\n"
                                           "out vec4 finalColor;\n"
                                           "\n"
                                           "void main() {\n"
                                           "    finalColor = fColor;\n"
                                           "}";

std::string const vertex_shader_source = "#version 330 core\n"
                                         ""
                                         "layout (location = 0) in vec2 vPos;\n"
                                         "layout (location = 1) in vec4 vColor;\n"
                                         "\n"
                                         "out vec4 fColor;\n"
                                         "void main() {\n"
                                         "  gl_Position = vec4(vPos.xy, 1.0, 1.0);\n"
                                         "  fColor = vColor;\n"
                                         "}";

Vertex data

    static GLfloat const vertex_data[] = {
    //            //Position                  //Color                 //UV Coords
                0.5f, 0.5f,             1.0f, 0.0f, 1.0f, 1.0f,
                -0.5f, 0.5f,            1.0f, 0.0f, 0.0f, 1.0f,
                0.5f, -0.5f,            1.0f, 1.0f, 0.0f, 1.0f,

                -0.5f, -0.5f,           0.0f, 1.0f, 1.0f, 1.0f,
                -0.5f, 0.5f,            0.0f, 0.0f, 1.0f, 1.0f,
                0.5f, -0.5f,            0.0f, 1.0f, 0.0f, 1.0f
    };

Here is my main loop:

int SDLApp::AppMain(int argc, char** argv) {

  if(sdl_window_ == nullptr) {
    acorn::Log(acorn::LoggingLevel::Fatal, "Could not initialize the window.");
    return -1;
  }

  if(sdl_gl_context_ == nullptr) {
    acorn::Log(acorn::LoggingLevel::Fatal, "Could not create an OpenGL context.");
    return -1;
  }

  acorn::shader::FragmentShader f(fragment_shader_source);
  acorn::shader::VertexShader v(vertex_shader_source);
  acorn::shader::Program shader_program(f, v);
  shader_program.Link();
  shader_program.Use();


  GLuint vao_;
  glGenVertexArrays(1, &vao_);

  GLuint vbo_;
  glGenBuffers(1, &vbo_);

  glBindVertexArray(vao_);
  glBindBuffer(GL_ARRAY_BUFFER, vbo_);

  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)2);

  glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat) * 6, vertex_data, GL_STATIC_DRAW);


  glClearColor(1.0f, 0.0f, 0.0f,1.0f);
  SDL_Event event;
  while (true) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        return 0;
      }
    }
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLES, 0, 6);
    SDL_GL_SwapWindow(sdl_window_);
  }
}

The last parameter of glVertexAttribPointer is treated as a byte offset into the buffer object's data store.
So the offset has to be 8, 2*sizeof(float) :

glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat),
    (void*)(2*sizeof(GLfloat)));

After changing that, your code works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM