Jump to content
  • Advertisement
Sign in to follow this  
plusnoir

OpenGL How to scale a polygon?

This topic is 2586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(&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, &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
{

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(&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);
}

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!

plusnoir

Share this post


Link to post
Share on other sites
Advertisement

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(&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, &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
{

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(&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);
}

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!

plusnoir


I can't see any code that sets up the projection matrix, you need to do that before you can draw anything. (If you are drawing in 2D you should use orthographic projection) (when drawing in 3D objects get bigger when they are closer to the camera, at a depth of 1.0 you normally have the left edge at -1.0 and the right edge at +1.0

actual scaling can be done using glScale. (In older OpenGL versions, with newer ones you tend to do it in the vertex shader instead)

Share this post


Link to post
Share on other sites
Ok I did it like this.


glScalef(0.1,0.1,0);
glBegin(GL_POLYGON);

glVertex2f(-1.1f,1.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(1.0f,-1.0f);
glVertex2f(-1.0f,-1.0f);
glEnd();



This should be a simple rectangle. But as you can see on the image below, the left side of the rect isn't displayed as you would expect. Why is that?



quadg.jpg


I suppose scaling the coordinates still isn't the projection Matrix you are talking about, or is it?

Share this post


Link to post
Share on other sites
Not sure why your edge is f'd up.

Do glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho() or gluOrtho2D()

//back to modelview
glMatrixMode(GL_MODELVIEW);

Share this post


Link to post
Share on other sites

Ok I did it like this.


glScalef(0.1,0.1,0);
glBegin(GL_POLYGON);

glVertex2f(-1.1f,1.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(1.0f,-1.0f);
glVertex2f(-1.0f,-1.0f);
glEnd();



This should be a simple rectangle. But as you can see on the image below, the left side of the rect isn't displayed as you would expect. Why is that?



quadg.jpg


I suppose scaling the coordinates still isn't the projection Matrix you are talking about, or is it?


glVertex2f(-1.1f,1.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(1.0f,-1.0f);
glVertex2f(-1.0f,-1.0f);


Notice the bolded parts, top left corner is at -1.1f while the bottom left is at -0.1f

(Given your scaling they are actually at -0.11f and -0.10f)

Share this post


Link to post
Share on other sites
Comment about this
glScalef(0.1,0.1,0)

Don't use 0 because that will crush the model. Use 1.0 if you don't want to scale along z-axis.

Share this post


Link to post
Share on other sites
Thanks a lot for your replies.

[color="#1C2837"]glVertex2f(-1.1f[color=#1C2837][size=2],1.0f); The .1 [color=#1C2837][size=2]was actually the reason for the distortion. After changing that I got the normal square as expected. Silly mistake of mine :)

[color="#1C2837"]



[color="#1C2837"]

But here is another question I have. After drawing this square I tried to make it perspective. My idea was to:


[color="#1C2837"]



[color="#1C2837"]

- set the scaling on the Z-axis to 0.1f too.


[color="#1C2837"]

- and then set coordinates of the vertices on the right side of my rect, to -1 (so further afar from the 0bserver)


[color="#1C2837"]



[color="#1C2837"]
glScalef(0.1,0.1,0.1);

glBegin(GL_POLYGON);

glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,-5.0f);
glVertex3f(1.0f,-1.0f,-5.0f);
glVertex3f(-1.0f,-1.0f,0.0f);

glEnd();



[color="#1C2837"]But it still shows the same rectangle with no changes in perspective. Is it because I don't use glOrtho etc. or did i miss something?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!