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