473,385 Members | 1,468 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,385 software developers and data experts.

How do I show and control a cube in this opengl example?

SwissProgrammer
220 128KB
How do I show and control a cube in this opengl example?

Hello.

I have the following that works with XP 32 bit and CodeBlocks 17.12 and minGW.

In the following, where it says, "OpenGL animation code goes here", how do I place a cube there that I can zoom in and out and move up, down, left, right via my program code and via the keyboard?

I do not want sound and other stuff. Just a very very simple example.


>>>>> please! No Visual Studio please! <<<<<

Thank you.



This uses gl.h which I did not include since it has 1,476 lines. If you need it, I can post it.




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

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

Similar topics

2
by: Supra | last post by:
in IE explorer......what control is this...it is toolbar? that had round button...similar to netscape and firefox.. does ne 1 knows? regards, supra
0
by: Efkas | last post by:
I have a full custom application with some widged extending Controls like Label and PictureBox. I build a menu with these widgets. When I click on one of them, it calls a function to display...
1
by: Erick T | last post by:
Hello, I am looking for a Web UI Control, either commercial or free. I don't know exactly what to call it, so I will describe what I need it to do. In a nutshell, it's a control to visually...
2
by: Stan | last post by:
I cannot make the link buttons fire ItemCommand from repeater control. Here is the code: <asp:repeater id=rptLetters runat="server"> <itemtemplate> <asp:linkbutton id="lnkLetter"...
19
by: Max | last post by:
Take a look at this screenshot that I've made: http://www.subdir.net/myFiles/line_ss.jpg I've circled the two lines that separate different sections of the form. Does anybody know how to make...
1
by: jm | last post by:
I was reading about the provider model and found that providers have a definition, the contract, that all provider implementations must have. Is this an example of polymorphic behavior because...
4
by: Milsnips | last post by:
Hi there, i have 2 pages: default.aspx and getTime.aspx 1. getTime.aspx has one line: "response.write(datetime.now())" 2. default.aspx has the following html code: "<script...
3
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I need to show a custom control in the DropDown of a Windows.Forms.ToolStripMenuItem (e.g., similar to the Font Color menu item in Word except that the control is specific to my application). I...
23
by: =?Utf-8?B?TWlrZTE5NDI=?= | last post by:
This is an example that is supposed to work in VB http://support.microsoft.com/kb/175512/en-us After spending a couple of hours downloading and installing VB Express 2008 after someone told me it...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.