nigels-com / glew

The OpenGL Extension Wrangler Library
Other
2.58k stars 608 forks source link

Runtime crash when attempting to use glClearTexImage() #370

Closed Zortexxx619 closed 1 year ago

Zortexxx619 commented 1 year ago

(sorry if this isn't the right place or the right format to post something like this, I'm still new to github)

I'm trying to start work on a small project in order to learn working with OpenGL, but I'm running into a major roadblock.

I'm using Eclipse CDT, with MinGW-W64 as my compiler. My GPU supports OpenGL 4.4. I've linked and included all the relevant GLEW & GLUT files as far as I can tell, but something's evidently wrong with it and I cannot tell what: functions and constants that are part of extensions are seemingly undetected by my IDE (other than as definitions). There are no compilation errors, but the program crashes when it gets to the glClearTexImage function; no error message (even the debugger found nothing), the program's window appears, freezes, then the program terminates.

The following is my code. It is modified from a triangle-drawing example whose source I can no longer find (comments are mostly from that):

#include <GL/glew.h>
#include <GL/glut.h>

#include <iostream>

#include <thread>
#include <chrono>

using namespace std;

void mainLoop() {
    while (true) {
        cout << "hilo" << endl;
        this_thread::sleep_for(chrono::seconds(1));
    }
}

// Clears the current window and draws a triangle.
void display() {

  // Set every pixel in the frame buffer to the current clear color.
  glClear(GL_COLOR_BUFFER_BIT);

  // Drawing is done by specifying a sequence of vertices.  The way these
  // vertices are connected (or not connected) depends on the argument to
  // glBegin.  GL_POLYGON constructs a filled polygon. j
  glBegin(GL_POLYGON);
    glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
    glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
    glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
  glEnd();

  // Flush drawing command buffer to make drawing happen as soon as possible.
  glFlush();
}

int main(int argc, char** argv) {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

      // Use a single buffered window in RGB mode (as opposed to a double-buffered
      // window or color-index mode).
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glewInit();

      // Position window and give it a title.
      glutInitWindowPosition(80, 80);
      glutInitWindowSize(1280, 720);
      glutCreateWindow("Not A Simple Triangle");

        cout << glGetString(GL_VERSION) << endl;

      GLuint texture;
      GLubyte colBlack[3] = {0,0,0};
      GLubyte colGray[3] = {64,64,64};

      cout << "0";
      glGenTextures(1, &texture);
      cout << "1";
      glBindTexture(GL_TEXTURE_RECTANGLE, texture);
      cout << "2";
      glTexImage2D(texture, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
      cout << "3";
      glClearTexImage(texture, 0, GL_RGB, GL_UNSIGNED_BYTE, colBlack);
      cout << "4";

      // Tell GLUT that whenever the main window needs to be repainted that it
      // should call the function display().
      glutDisplayFunc(display);

      // Tell GLUT to start reading and processing events.  This function
      // never returns; the program only exits when the user closes the main
      // window or kills the process.
      thread logicLoop(mainLoop);
      glutMainLoop();

    return 0;
}

I've looked all around but I've not been able to find a solution to this issue. The only thing I can think of is that using the pre-built binaries for GLEW instead of building it myself is causing an issue, but that's only a guess. Any help at all would be appreciated.

nigels-com commented 1 year ago

glClearTexImage is available only if the GL version is 4.4 or greater.

It is strongly recommended to check for the expected functionality.

Zortexxx619 commented 1 year ago

as I mentioned, my GPU supports GL 4.4. glGetString(GL_VERSION) in the code even returns 4.4.

After having looked at it and researched some more, it seems the issue actually has to do with Eclipse CDT not being able to parse the function since it's defined as a macro in glew.h, and isn't actually defined until runtime (or something along those lines.) What's happening thereafter is either the function isn't actually getting defined, or I'm using it wrong without the compiler stopping me.

Not sure if this makes it more of an Eclipse issue than a GLEW one, or if there's a way to get Eclipse to parse the functions properly.

Zortexxx619 commented 1 year ago

Okay, I figured out the issue, it was a lot simpler than I thought. I was calling glewInit() before creating a window/context with glut, which was casing it to fail to initialize. Changing the order fixes it.