-1

I'm trying to learn OpenGL but I've not yet got the hang of it, as I encountered a problem at the first hurdle where I try to display a bright red square, but the image comes out as a maroon coloured square. (I apologize but I cannot post pictures due to not having enough reputation :( )

I've been using the SOIL library (http://www.lonesock.net/soil.html) to make the task of loading textures simpler, and I am fairly sure that this is where the problem lies.

I understand the most obvious answer is to not use SOIL, and to learn raw OGL first before I try using extensions, and I do intend to do this. However I would still like this problem solving for peace of mind.

My personal assumption is I have probably enabled some sort of shading somewhere, or there is some quirk of OGL or SOIL that forces the shade of the texture to change, however I am not experienced enough to solve this.

Below is what I believe to be the relevant code.

void displayBackground()
{
    GetTexture("resources/red.png");
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2f(0, 0);
        glTexCoord2f(480, 0); glVertex2f( 480, 0);
        glTexCoord2f(480, 480); glVertex2f( 480,  480);
        glTexCoord2f(0, 480); glVertex2f(0,  480);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}

And below is the SOIL-specific code which as far as I can tell should load a solid red texture into the active OGL texture

GLuint GetTexture(std::string Filename)
{
    GLuint tex_ID;

    tex_ID = SOIL_load_OGL_texture(
                Filename.c_str(),
                SOIL_LOAD_AUTO,
                SOIL_CREATE_NEW_ID,
                SOIL_FLAG_POWER_OF_TWO
                | SOIL_FLAG_MIPMAPS
                | SOIL_FLAG_COMPRESS_TO_DXT
                | SOIL_FLAG_DDS_LOAD_DIRECT
                );

        if( tex_ID > 0 )
        {
            glEnable( GL_TEXTURE_2D );
            glBindTexture( GL_TEXTURE_2D, tex_ID );

            return tex_ID;
        }
        else
            return 0;
}

Thank you in advance for anyone insight into where I have possibly gone wrong.

@Nazar554 I'm assuming this is what you mean by the view port? Sorry, I'm aware this is very basic OGL stuff and I probably sound rather stupid, but you've got to start somewhere right? :P

/** OpenGL Initial Setup**/
    //pixel format descriptor to describe pixel layout of a given surface
    PIXELFORMATDESCRIPTOR pfd;
    std::memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER_DONTCARE;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;

    HDC hdc = GetDC(hwnd); //gets device context of hwnd. Device context is a set of graphics objects that define how to draw to the given device
    int format = ChoosePixelFormat(hdc, &pfd); //chooses best pixel format for device context given the pfd to be used
    SetPixelFormat(hdc, format, &pfd);
    HGLRC hglrc;
    hglrc = wglCreateContext(hdc); //creates OGL rendering context suitable for drawing on the device specified by hdc
    wglMakeCurrent(hdc, hglrc); //makes hglrc the thread's current context. subsequent OGL calls made on hdc

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  // Red, Green, Blue, Alpha. (Additive color) Does not need to be updated every cycle
    glOrtho(0, 900, 600, 1.0, -1.0, 1.0); //sets co-ordinates system
Sammdahamm
  • 11
  • 10

1 Answers1

0

Put glOrtho before glClearColor. Also you need to select projection matrix before calling glOrtho. Use this:

glMatrixMode(GL_PROJECTION); // select projection matrix
glLoadIdentity(); // clear it
glOrtho(0, w, h, 0, 0, 1); // compute projection matrix, and multiply identity matrix by it
// w, h is your window size if you are doing 2D
glMatrixMode(GL_MODELVIEW); // select model matrix

Also, if you are studying OpenGL better begin with modern versions (3.3+, or 2.1 without old stuff), not 1.2. They have a lot of differences and it will be hard to forget everything you studied before. For beginners freeglut or GLFW is more simple and portable than pure Win32.

Nazar554
  • 4,105
  • 3
  • 27
  • 38
  • `Put glOrtho before glClearColor.` The order of these doesn't matter. – Colonel Thirty Two Sep 22 '14 at 15:15
  • @ColonelThirtyTwo looks cleaner for me that way. keeps matrix stuff separated – Nazar554 Sep 22 '14 at 15:17
  • I added the code you provided and it all compiled successfully, but unfortunately the image still comes out brown/maroon instead of bright red :( – Sammdahamm Sep 22 '14 at 15:17
  • Also thank you for the advice on trying modern OGL, that seems like a much better idea! I wasn't actually aware that I was using out of date OGL :P This problem isn't particularly important, because I intend to start over, learning raw OGL without the dependency on SOIL, but I'd rather solve it just in case I happen across a similar problem in the future – Sammdahamm Sep 22 '14 at 15:19