0
\$\begingroup\$

I'm trying to simply draw a triangle but for some reason I can't see any.

I made sure that every buffer was bound before use. Added the shader/program error chec. Added some logs using glDebugMessageCallback also but had no luck on this, the debug infos weren't relevent.

Does someone has any idea on this ?

Here is the glfw context:

GLFWwindow* createContext( unsigned int width, unsigned int height, const char* name ) {
  if ( !glfwInit() )
    throw "Couldn't open the window";

  glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
  glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );

  glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, true );
  glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
  glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

  auto window = glfwCreateWindow( width, height, name, NULL, NULL );
  glfwMakeContextCurrent( window );

  return window;
}

Here is the init in main:

  auto window = createContext( 1280, 960, "Simple Triangle" );
  if ( !window ) {
    std::cout << "Couldn't open window" << std::endl;
    glfwTerminate();
    return -1;
  }

  glfwSetErrorCallback( errorCallback );

  glewExperimental = true;
  glewInit();

  glViewport( 0, 0, 1280, 960 );
  glEnable( GL_DEPTH_TEST );
  glDepthFunc( GL_LESS );

  glEnable( GL_DEBUG_OUTPUT );
  glDebugMessageCallback( errorCallback, NULL );

Here is how I set up the shaders and the program:

  float vertices[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f
  };

  float color[] = {
    1.0f, 0.0f, 0.0f,
    0.0f, 0.5f, 0.0f,
    0.0f, 0.2f, 0.0f
  };

  GLuint vbo;
  glGenBuffers( 1, &vbo );
  glBindBuffer( GL_ARRAY_BUFFER, vbo );
  glBufferData( GL_ARRAY_BUFFER, sizeof ( vertices ), vertices, GL_STATIC_DRAW );

  GLuint colorVbo;
  glGenBuffers( 1, &colorVbo );
  glBindBuffer( GL_ARRAY_BUFFER, colorVbo );
  glBufferData( GL_ARRAY_BUFFER, sizeof ( color ), color, GL_STATIC_DRAW );

  GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
  auto fileContent = IoHelper::readFile( "shaders/simple.vert.glsl" );
  auto source = fileContent.c_str();
  std::cout << "Vertex Shader:\n" << fileContent << std::endl;
  glShaderSource( vertexShader, 1, &source, NULL );
  glCompileShader( vertexShader );
  getShaderCompileError( vertexShader );

  std::cout << "-----------------------------------------" << std::endl;

  GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
  fileContent = IoHelper::readFile( "shaders/simple.frag.glsl" );
  source = fileContent.c_str();
  std::cout << "Fragment Shader:\n" << fileContent << std::endl;
  glShaderSource( fragmentShader, 1, &source, NULL );
  glCompileShader( fragmentShader );
  getShaderCompileError( fragmentShader );

  GLuint program;
  program = glCreateProgram();
  glAttachShader( program, vertexShader );
  glAttachShader( program, fragmentShader );
  glLinkProgram( program );
  getProgramCompileError( program );

  GLuint vao;
  glGenVertexArrays( 1, &vao );
  glBindVertexArray( vao );

  glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof ( vertices ), NULL );
  glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof ( vertices ), NULL );
  glEnableVertexAttribArray( 0 );
  glEnableVertexAttribArray( 1 );

Here is my main loop:

  while ( !glfwWindowShouldClose( window ) ) {
    glClearColor( 0.2f, 0.3f, 0.3f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT );

    glUseProgram( program );
    glDrawArrays( GL_TRIANGLES, 0, 3 );

    glfwPollEvents();
    glfwSwapBuffers( window );
  }

And finally my shaders:

Vertex Shader:
#version 430

layout ( location = 0 ) in vec3 iPos;
layout ( location = 1 ) in vec3 iColor;

out vec4 oColor;

void main()
{
    gl_Position = vec4( iPos, 1.0 );
    oColor = vec4( iColor, 1.0 );
}

-----------------------------------------
Fragment Shader:
#version 430

in vec4 oColor;
out vec4 fragColor;

void main()
{
    fragColor = oColor;
}

Thank you

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

With some research and some tries, I finally found where the problem came from.

I tried using

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

instead of

glClear( GL_COLOR_BUFFER_BIT );

Does someone have an explanation on this ?

Thanks

\$\endgroup\$
0
\$\begingroup\$

have you tried changing the glDrawArrays( GL_TRIANGLES, 0, 3 ); to glDrawArrays( GL_TRIANGLES, 0, 9 );?

\$\endgroup\$
1
  • \$\begingroup\$ When answering like this, please make sure to include why this change would solve the issue. \$\endgroup\$
    – Vaillancourt
    Mar 15, 2022 at 12:02
0
\$\begingroup\$

As you've already found out in your own comment you have to clear the depth buffer. You've enabled depth testing with GL_LESS while rendering the same fragment over and over again, so 'new' one's depth is not less than the 'old' one's, thus it is discarded. https://www.khronos.org/opengl/wiki/Depth_Test#Depth_test

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .