-1

The code is too long to put on Stack Overflow, so I put it in a Gist. To use the code, you need to use only one of the main.cpp files.

I am learning from learnopengl.com and I am at the point where I draw the triangle. However, my Object Oriented code does not draw the triangle. When I expanded my code (like macros, e.g. expanding ADD(x, y) to x + y when #define ADD(x, y) x + y), the code worked. In fact, every time I tried to object orient OpenGL, it always failed. What am I doing wrong?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
BlueStaggo
  • 171
  • 1
  • 12
  • Log every GL call. Then compare logs before and after OOP-fication. – HolyBlackCat Aug 09 '21 at 12:45
  • 4
    `vertices` decays to pointer to float when you pass it as `float vertices[]` to function. So `sizeof(vertices)` returns a size of pointer to float instead the whole size of data for your triangle. Pass additional parameter which stores the actual size of passed data. – rafix07 Aug 09 '21 at 12:47
  • Can you tell if the OOP code opens the window and compiles shaders correctly? So to narrow down the problem. My suggestion here is to check the GL calls order. – yurand Aug 09 '21 at 12:56
  • Does `glfwCreateWindow` return an `unsigned int` (as in `id` in globj.h)? Becuase it seems to me that it returns a `GLFWwindow*` as in `window` in main-exp.cpp, Seems strange to me that you wrote `id = glfwCreateWindow(...)` I'm surprised your compiler didn't complain. – Wyck Aug 09 '21 at 13:27
  • Surprising to me that you would call `glfwCreateWindow` before calling `glfwInit`, especially since `glfwCreateWindow` lists `GLFW_NOT_INITIALIZED` as one of its potential error codes ([see docs](https://www.glfw.org/docs/3.3/group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344)) – Wyck Aug 09 '21 at 13:33

1 Answers1

2

Not sure about other problems, but this is definitely wrong:

    inline static void data(float vertices[],
                            GLenum usage = GL_STATIC_DRAW)
    { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, usage); }

    inline static void data(const float vertices[],
                            GLenum usage = GL_STATIC_DRAW)
    { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, usage); }

sizeof(vertices) will always be sizeof(float*) because that's how C arrays work – they don't carry information about their length when passed as arguments. In main-exp.cpp you're doing sizeof on the constexpr, which does count the items. Fix this by any of these methods which should replace sizeof(vertices):

  • use std:vector, std:array or (C++20) std:span.
  • explicitly pass the length alongside of the C array.
flyx
  • 35,506
  • 7
  • 89
  • 126
  • the sizeof seems definitely the error here, since your OpenGL will be loading the wrong data. the sizeof in the non OOP code instead works correctly because the sizeof of a fixed size array at compiletime will be as you expected it. passing instead it to a function will be seen as a normal pointer. – yurand Aug 09 '21 at 13:02