By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,337 Members | 2,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,337 IT Pros & Developers. It's quick & easy.

Opengl 2D Angle Movement

P: 88
Hey, I made a program for 2D Angle Movement.

Now the user sets the degrees the triangle should move and it does, but it doesn't go correctly.

0 degrees is to the right of the window.

It started in the center of the window and went down left... as far as I know that isn't 180 degrees. It's more like 225 degrees.

why does it do this?

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. #include "math.h"
  4.  
  5. int wWidth = 1080;
  6. int wHeight = 720;
  7.  
  8. LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  9. void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
  10. void DisableOpenGL(HWND, HDC, HGLRC);
  11.  
  12.  
  13. int WINAPI WinMain(HINSTANCE hInstance,
  14.                    HINSTANCE hPrevInstance,
  15.                    LPSTR lpCmdLine,
  16.                    int nCmdShow)
  17. {
  18.     WNDCLASSEX wcex;
  19.     HWND hwnd;
  20.     HDC hDC;
  21.     HGLRC hRC;
  22.     MSG msg;
  23.     BOOL bQuit = FALSE;
  24.     float theta = 0.0f;
  25.  
  26.     /* register window class */
  27.     wcex.cbSize = sizeof(WNDCLASSEX);
  28.     wcex.style = CS_OWNDC;
  29.     wcex.lpfnWndProc = WindowProc;
  30.     wcex.cbClsExtra = 0;
  31.     wcex.cbWndExtra = 0;
  32.     wcex.hInstance = hInstance;
  33.     wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  34.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  35.     wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  36.     wcex.lpszMenuName = NULL;
  37.     wcex.lpszClassName = "GLSample";
  38.     wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
  39.  
  40.  
  41.     if (!RegisterClassEx(&wcex))
  42.         return 0;
  43.  
  44.     /* create main window */
  45.     hwnd = CreateWindowEx(0,
  46.                           "GLSample",
  47.                           "OpenGL Sample",
  48.                           WS_OVERLAPPEDWINDOW,
  49.                           GetSystemMetrics(SM_CXSCREEN)/2-wWidth/2,
  50.                           GetSystemMetrics(SM_CYSCREEN)/2-wHeight/2,
  51.                           wWidth,
  52.                           wHeight,
  53.                           NULL,
  54.                           NULL,
  55.                           hInstance,
  56.                           NULL);
  57.  
  58.     ShowWindow(hwnd, nCmdShow);
  59.  
  60.     /* enable OpenGL for the window */
  61.     EnableOpenGL(hwnd, &hDC, &hRC);
  62.  
  63.     /* program main loop */
  64.     while (!bQuit)
  65.     {
  66.         /* check for messages */
  67.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  68.         {
  69.             /* handle or dispatch messages */
  70.             if (msg.message == WM_QUIT)
  71.             {
  72.                 bQuit = TRUE;
  73.             }
  74.             else
  75.             {
  76.                 TranslateMessage(&msg);
  77.                 DispatchMessage(&msg);
  78.             }
  79.         }
  80.         else
  81.         {
  82.             /* OpenGL animation code goes here */
  83.  
  84.             glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  85.             glClear(GL_COLOR_BUFFER_BIT);
  86.  
  87.             //GLint con = 10;
  88.             int moveDeg = 180.0 - 90.0;
  89.  
  90.             if (moveDeg == 0)
  91.             {
  92.                 moveDeg = 360.0;
  93.             }
  94.             //GLfloat amountX = cos(moveDeg)/100.0;
  95.             //GLfloat amountY = sin(moveDeg)/100.0;
  96.  
  97.             Sleep(100);
  98.  
  99.             glPushMatrix();
  100.                 glBegin(GL_TRIANGLES);
  101.                     glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f( 0.0,    0.05f);
  102.                     glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f( 0.05f, -0.05f);
  103.                     glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(-0.05f, -0.05f);
  104.                 glEnd();
  105.             glPopMatrix();
  106.             //Move
  107.             glTranslatef(cos(moveDeg)/100, sin(moveDeg)/100, 0.0f);
  108.             SwapBuffers(hDC);
  109.         }
  110.     }
  111.  
  112.     /* shutdown OpenGL */
  113.     DisableOpenGL(hwnd, hDC, hRC);
  114.  
  115.     /* destroy the window explicitly */
  116.     DestroyWindow(hwnd);
  117.  
  118.     return msg.wParam;
  119. }
  120.  
  121. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  122. {
  123.     switch (uMsg)
  124.     {
  125.         case WM_CLOSE:
  126.             PostQuitMessage(0);
  127.         break;
  128.  
  129.         case WM_DESTROY:
  130.             return 0;
  131.  
  132.         case WM_KEYDOWN:
  133.         {
  134.             switch (wParam)
  135.             {
  136.                 case VK_ESCAPE:
  137.                     PostQuitMessage(0);
  138.                 break;
  139.             }
  140.         }
  141.         break;
  142.  
  143.         default:
  144.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  145.     }
  146.  
  147.     return 0;
  148. }
  149.  
  150. void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
  151. {
  152.     PIXELFORMATDESCRIPTOR pfd;
  153.  
  154.     int iFormat;
  155.  
  156.     /* get the device context (DC) */
  157.     *hDC = GetDC(hwnd);
  158.  
  159.     /* set the pixel format for the DC */
  160.     ZeroMemory(&pfd, sizeof(pfd));
  161.  
  162.     pfd.nSize = sizeof(pfd);
  163.     pfd.nVersion = 1;
  164.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  165.                   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  166.     pfd.iPixelType = PFD_TYPE_RGBA;
  167.     pfd.cColorBits = 24;
  168.     pfd.cDepthBits = 16;
  169.     pfd.iLayerType = PFD_MAIN_PLANE;
  170.  
  171.     iFormat = ChoosePixelFormat(*hDC, &pfd);
  172.  
  173.     SetPixelFormat(*hDC, iFormat, &pfd);
  174.  
  175.     /* create and enable the render context (RC) */
  176.     *hRC = wglCreateContext(*hDC);
  177.  
  178.     wglMakeCurrent(*hDC, *hRC);
  179. }
  180.  
  181. void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
  182. {
  183.     wglMakeCurrent(NULL, NULL);
  184.     wglDeleteContext(hRC);
  185.     ReleaseDC(hwnd, hDC);
  186. }
  187.  
  188.  
Jul 9 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.