Opengl in Codeblocks

Started by
4 comments, last by musicon 11 years, 10 months ago
I am trying to get familiar with Opengl using the Codeblocks template and the gnu compiler (as opposed to Glut in Visual C++). I am trying to draw a stars and stripes flag, partly using xoax sample code and partly the opengl template given by Codeblocks. So far:


#include <windows.h>
#include <gl/gl.h>

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);


int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
HWND hwnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;

/* register window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "GLSample";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;


if (!RegisterClassEx(&wcex))
return 0;

/* create main window */
hwnd = CreateWindowEx(0,
"GLSample",
"OpenGL Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
950,
700,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hwnd, nCmdShow);

/* enable OpenGL for the window */
EnableOpenGL(hwnd, &hDC, &hRC);

/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
int iStripeIndex;
for(iStripeIndex=0;iStripeIndex<13;iStripeIndex++){
/* OpenGL animation code goes here */
glClearColor(0.0f, 0.0f, 0.7f, 0.0f);
//glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();

if(iStripeIndex%2==0){
glColor3f(204.0/255.0,0.0,0.0);
}else{
glColor3f(1.0,1.0,1.0);
}

float fStartX=0.0;
float fEndX=1.0;
float fStartY=iStripeIndex*(1.0/13.0);
float fEndY=(iStripeIndex+1)*(1.0/13.0);

glBegin(GL_QUADS);
glVertex3f(fStartX, fStartY,0.0);
glVertex3f(fEndX, fStartY,0.0);
glVertex3f(fEndX, fEndY,0.0);
glVertex3f(fStartX, fEndY,0.0);
glEnd();

//glPopMatrix();

SwapBuffers(hDC);
}
}
}

/* shutdown OpenGL */
DisableOpenGL(hwnd, hDC, hRC);

/* destroy the window explicitly */
DestroyWindow(hwnd);

return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;

case WM_DESTROY:
return 0;

case WM_KEYDOWN:
{
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}
break;

default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
PIXELFORMATDESCRIPTOR pfd;

int iFormat;

/* get the device context (DC) */
*hDC = GetDC(hwnd);

/* set the pixel format for the DC */
ZeroMemory(&pfd, sizeof(pfd));

pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;

iFormat = ChoosePixelFormat(*hDC, &pfd);

SetPixelFormat(*hDC, iFormat, &pfd);

/* create and enable the render context (RC) */
*hRC = wglCreateContext(*hDC);

wglMakeCurrent(*hDC, *hRC);
}

void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hwnd, hDC);
}



The main issue is the lines are showing but in altogether the wrong part of the window (top right quarter basically instead of the whole screen). I am having problems with the background blue as well.
Advertisement
1. Why is glClear() commented out?
2.You have not set up a Projection matrix. Im not sure what the default is, but you should probably set one up to make sure youve got it correct
3. You may be running into problems because iStripeIndex is an int. Im not sure off the top of my head what will happen when you multiply an int by a float. It may be casting your 1.0/13.0 to an int (0). Try casting iStripeIndex to a float before doing your calculations
4. Try making the fourth parameter of glClearColor a 1.0
Thanks for advice.

I commented out the Clear line as it was clearing the stripes previously drawn, like clearing the background in an animation program.
Projection Matrix is probably the crux of the problem so am looking at that, if I can find example that isn't Einstein maths.
I still think iStripeIndex has to be an int as making this float gives conversion problems.
It should be running in a continuous loop, so that the stripes would be drawn on top every frame, correct?
Projection matrix should be as simple as
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0,1,0,1,-1,1);
glMatrixMode(GL_MODELVIEW);
(that may be slightly off though, just look up the functions to make sure the parameters are correct)
How are you doing the converson?
As already said, uncomment the glClear(), it should be done every frame.
Also, set the 4th parameter of glClearColor() to 1.0 (ex: glClearColor(0.1, 0.3, 0.5, 1.0), will give a blueish background)

And you should also set the viewport on the window resizing.
On the window procedure, add

case WM_SIZE:
//The LOWORD(lparam) is the new width of the window
// and the HIWORD(lparam) is the new height of the window
glViewport(LOWORD(lparam), HIWORD(lparam));
break;

This will set the viewport (the rawable area of the window) every time the window is resized.

You should also use a projection (as it's been said), or an orthogonal view:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

or

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and you can use gluLookAt() with the gluPerspective() if you want.

Also, i think the float * int gives a float, not an int (it does give an int when 2 integers are multiplied).

Hope it helped

EDIT: Make sure the height used in gluPerspective() is not 0, else you will be dividing by 0, which is not good.
Thanks for your input. It is all working. The original example was using GLUT and it did use glOrtho. Its the only projection I know but it worked here.

This topic is closed to new replies.

Advertisement