0
\$\begingroup\$

I just recently started using C++ and OpenGL and I am running into an issue. I am not totally sure what I am doing so could you please tell me what I am doing wrong and what I could improve?

Here is the problem:
I can't see to get a textured quad to render at all. I've tried a bunch of different ways and even asked people who claimed to know C++ and they said they didn't see any obvious issues.

I am using the following libraries:

  • GLFW (For input/window callbacks)
  • GLUT (For OpenGL)

Here is my code:

///In class "Game"

    virtual void render() {

        int ww, hh;
        glfwGetWindowSize(gameWindow, &ww, &hh);

        glLoadIdentity();
        gluOrtho2D(0, ww, hh, 0);

        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        gameRoom->render();

        glfwSwapBuffers(gameWindow);

        glPopMatrix();

    }

///In class "Sprite"

    void render(int index, int x, int y, float xscale, float yscale)
    {
        float tcx1, tcx2, tcy1, tcy2;

        tcx1 = (float)frames[index].x / base_width;
        tcx2 = (float)(frames[index].x + frame_width) / base_width;

        tcy1 = (float)frames[index].y / base_height;
        tcy2 = (float)(frames[index].y + frame_height) / base_height;

        float vx1, vx2, vy1, vy2;

        vx1 = (x - Game::gameView->x) / Game::gameView->width;
        vx2 = (x + frame_width * xscale - Game::gameView->x) / Game::gameView->width;

        vy1 = (y - Game::gameView->y) / Game::gameView->height;
        vy2 = (y + frame_height * yscale - Game::gameView->y) / Game::gameView->height;

        glPushMatrix();

        glBindTexture(GL_TEXTURE_2D, texture);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_TRIANGLES);

        glTexCoord2f(tcx1, tcy1); glVertex2f(vx1, vy1);
        glTexCoord2f(tcx2, tcy1); glVertex2f(vx2, vy1);
        glTexCoord2f(tcx1, tcy2); glVertex2f(vx1, vy2);

        glTexCoord2f(tcx2, tcy1); glVertex2f(vx2, vy1);
        glTexCoord2f(tcx1, tcy2); glVertex2f(vx1, vy2);
        glTexCoord2f(tcx2, tcy2); glVertex2f(vx2, vy2);

        glEnd();
        glDisable(GL_TEXTURE_2D);

        glPopMatrix();

    }

When I run the game all I get is a black screen... Please help!

\$\endgroup\$
  • 3
    \$\begingroup\$ "My OpenGL program just draws a black screen" is one of the most common symptoms that beginners will encounter, and it can be caused by a huge diversity of factors. You'll tend to get answers faster if you can do some debugging legwork to narrow down the cause. For instance, have you tried reading the OpenGL error queue to see if it has any information about what went wrong? Can you try a simpler scenario like drawing just a clear colour to see if your video output is visible at all? etc. Every possibility you can eliminate is one step closer \$\endgroup\$ – DMGregory Apr 2 '19 at 19:49
  • \$\begingroup\$ +1 to what @DMGregory. I've started several OpenGL projects, and I've learnt the hard way that starting from scratch is a long and hard process. It's much easier to start from existing code. Find some code that does something similar to what you want online, run it to make sure it works, and only then try to edit it - that way you can continuously edit and test, and you'll know where things went wrong. \$\endgroup\$ – nihohit Apr 2 '19 at 20:14
  • \$\begingroup\$ I would add that using OpenGL 1 is a really bad idea, it's not supported everywhere anymore (e.g. the driver of my GPU dropped it a couple of years ago), it's very limited and you won't gain anything from learning it. Try using a more modern version, preferably 3+. Or if you want to actually make games, learn a framework or engine, they stop you from wasting 90% of your time on things that have been done before and better. \$\endgroup\$ – Bálint Apr 3 '19 at 0:24
  • \$\begingroup\$ @Bálint I don't want to be branded/limited by an engine. Also where would I get OpenGL 3? Also almost every documentation/tutorial/learning material uses glut so thats why I decided to use glut, before I was using glew, is that any better> \$\endgroup\$ – WolfHybrid23 Apr 3 '19 at 1:15
  • \$\begingroup\$ You wouldn't be limited by an engine (at least by a good one). You are however limited by OpenGL 1. Try looking for a tutorial that isn't 20+ years old. OpenGL 3 can run on most hardware nowadays \$\endgroup\$ – Bálint Apr 3 '19 at 7:42
2
\$\begingroup\$

I fixed the issue.

The problem was this:

I programmed a converter in CSharp that took the image and outputted all of its pixels as raw RGBA data which my program read from, in the converter's for loop to read the pixels I forgot to offset the write position by 4 times the x and y value which caused only the red values to be written and not even in a format that's readable by my engine.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.