PDA

View Full Version : How to scale a polygon?



bodyCount++
11-10-2011, 04:00 AM
I started learning openGL recently. I am using code::blocks (because VS with oGL was a pain in the ***). This is what my initial program looks like:


#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(&amp;wcex))
return 0;

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

ShowWindow(hwnd, nCmdShow);

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

/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
}
else
{

DrawGLScene();
SwapBuffers(hDC);
Sleep (1);
}
}

/* 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(&amp;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, &amp;pfd);

SetPixelFormat(*hDC, iFormat, &amp;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);
}

int DrawGLScene(GLvoid) //Here's where we do all the drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen and thedepth buffer
glLoadIdentity(); //Reset the view




glColor3f(0,1,0);
glBegin(GL_POLYGON);

glVertex2f(0.0f,0.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(1.0f,2.0f);
glVertex2f(3.0f,2.0f);
glVertex2f(2.0f,0.0f);


glEnd();

return TRUE;

}

As you can see I am trying to draw a polygon. But somehow it is always out of bounds of the window
I am drawing to. Though i picked small values (3.0f, is the highest).

Why is it because of the window size or do I simply have to scale my polygon?

Thanks in advance!

BC++

tksuoran
11-10-2011, 04:28 AM
Your projection and modelview matrices are identity matrices. As result your window effectively has range -1 to 1 in both x and y.
See http://www.opengl.org/resources/faq/technical/transformations.htm#tran0011

You will need to setup a suitable projection matrix to get something else, see http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml for example.

bodyCount++
11-11-2011, 03:20 AM
Are there any tutorials you could recommend that deal with that subject? NeHe perhaps?

Dinesh
11-11-2011, 06:15 AM
you can use scale3f(GLfloat x,GLfloat y,GLfloat z) function.It takes the scaling factor as a parameter.If you want to scale whole polygon then put all x,y,z parameter same.To increase the size scaling factor is more then 1.0f and to decrease the size scaling factor is less then 1.0f.If you don't want to scale any co-ordinates pass 1.0f value as parameter.
example
scale3f(0.5f,1.0f,0.5f) here it scale the x and z co-ordinates into half but y co-ordinates remain same.

blackzero
11-11-2011, 11:31 PM
you are using 2D primitive
for that you have to define your 2D projection (in other words 2D window area).

use gluOrtho2D(Left , Right , Bottom , Top) glLoadIdentity();

example:

if you want that your window's size should 640 x 480 and the origin will at left-bottom corner

then set as:

gluOrtho2D ( 0 , 640 , 0 , 480);

hope this will help you

blackzero
11-11-2011, 11:35 PM
or use the values of vertices in between -1 and 1

which will work by default..:)