473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Opengl 2D Angle Movement

93 64KB
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
0 1182

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

Similar topics

0
by: Stephen | last post by:
Hi, I'm trying to write a simple A-Life game in Java where objects move around on the screen. I have never done moving objects. I have so far gotten the creatures to find the x & y of food and the...
5
by: Darren Grant | last post by:
Hi there, I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think......
15
by: oracle411 | last post by:
Hi I'm new to OpenGL, but have an OpenGL application written in CPP that I would like to display on a web browser. Was wondering how this could be done? Thank you very much
2
by: George | last post by:
I have marked the areas with comments as to what I would like to do and what the code does. The two problem areas are in the key pressing and mousing clicking areas. thanks for the help. This...
2
by: Rameshika | last post by:
Hi All I am new to 3D modelling in VISUAL .NET C#.I am planning to draw the entire roof (showing the roof material, roof angle, roof shape,ceiling shape, ceiling material) of the house and...
10
by: Bad_Kid | last post by:
which is better for what?
3
by: jg.campbell.ng | last post by:
I'm beginning learning Python and OpenGL in Python. Python fine. But difficulties with OpenGL; presumably with the installation of OpenGL. OS = Linux FC5. Python program gl_test.py: from...
2
by: matt.casella | last post by:
Hi, I've having a *small* problem with a small simulation/game I'm trying to write in C++ using OpenGL. I'm not sure if I need to include code yet, as it might just make things more...
0
by: Code Bug | last post by:
Basically, I want to connect the tip of each plane to the other. So I thought I might try creating a variable specifically for the points I want to connect: The tip of each plane. Then, for each...
1
by: Wilhelm1892 | last post by:
I am having these errors: program6.cpp: In function ‘void handleButton(int, int, int, int)’: program6.cpp:176:40: error: ‘incolormenuindex’ cannot be used as a function program6.cpp:180:42:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.