Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)



//_______________________________________-
// gam.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "gam.h"

#include
#include
//add dependency OpenGL32.lib,glu32.lib,

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

/* Windows globals, defines, and prototypes */
CHAR szAppName[]="Win OpenGL";
HWND ghWnd;
HDC ghDC;
HGLRC ghRC;

#define SWAPBUFFERS SwapBuffers(ghDC)
#define BLACK_INDEX 0
#define RED_INDEX 13
#define GREEN_INDEX 14
#define BLUE_INDEX 16
#define WIDTH 300
#define HEIGHT 200

LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL bSetupPixelFormat(HDC);

/* OpenGL globals, defines, and prototypes */
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius;

#define GLOBE 1
#define CYLINDER 2
#define CONE 3

GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void polarView( GLdouble, GLdouble, GLdouble, GLdouble);





// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GAM, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GAM));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GAM));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GAM);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_TABSTOP|WS_VISIBLE|WS_OVERLAPPEDWINDOW|BS_DEFPUSHBUTTON,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL,NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


MSG msg;

/* animation loop */
while (1)
{
/*
* Process all pending messages
*/

while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
return TRUE;
}
}
drawScene();
}

return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
#define BUTTON_ID 1001
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

LONG lRet = 1;
RECT rect;


switch (message)
{

case WM_CREATE:
ghDC = GetDC(hWnd);
if (!bSetupPixelFormat(ghDC))
PostQuitMessage (0);

ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
GetClientRect(hWnd, &rect);
initializeGL(rect.right, rect.bottom);
break;

case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...

EndPaint(hWnd, &ps);
break;

case WM_SIZE:

GetClientRect(hWnd, &rect);
resize(rect.right, rect.bottom);
break;

case WM_CLOSE:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
ghRC = 0;
ghDC = 0;

DestroyWindow (hWnd);
break;

case WM_DESTROY:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
PostQuitMessage(0);
break;

case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT:
longinc += 0.5F;
break;
case VK_RIGHT:
longinc -= 0.5F;
break;
case VK_UP:
latinc += 0.5F;
break;
case VK_DOWN:
latinc -= 0.5F;
break;
}

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.

/*dependen
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib*/


INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}





BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;


ppfd = &pfd;

ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 8;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;

pixelformat = ChoosePixelFormat(hdc, ppfd);

if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
{
MessageBox(NULL, L"ChoosePixelFormat failed", L"Error", MB_OK);
return FALSE;
}

if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBox(NULL, L"SetPixelFormat failed", L"Error", MB_OK);
return FALSE;
}

return TRUE;
}

/* OpenGL code */

GLvoid resize( GLsizei width, GLsizei height )
{
GLfloat aspect;

glViewport( 0, 0, width, height );

aspect = (GLfloat) width / height;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
}

GLvoid createObjects()
{
GLUquadricObj *quadObj;

glNewList(GLOBE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_LINE);
gluSphere (quadObj, 1.5, 16, 16);
glEndList();

glNewList(CONE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_LINE);
// gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10);
gluQuadricTexture(quadObj,GL_TRUE);
glEndList();

glNewList(CYLINDER, GL_COMPILE);
glPushMatrix ();
glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);
glPopMatrix ();
glEndList();



}

GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane;

glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 );

glEnable(GL_DEPTH_TEST);

glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );

near_plane = 3.0;
far_plane = 7.0;
maxObjectSize = 3.0F;
radius = near_plane + maxObjectSize/2.0;

latitude = 0.0F;
longitude = 0.0F;
latinc = 6.0F;
longinc = 2.5F;

createObjects();
}

void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,GLdouble longitude)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-latitude, 1.0, 0.0, 0.0);
glRotated(longitude, 0.0, 0.0, 1.0);

}

GLvoid drawScene(GLvoid)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glPushMatrix();

latitude = latinc;
longitude = longinc;

polarView( radius, 0, latitude, longitude );

glIndexi(RED_INDEX);
glCallList(CONE);

glIndexi(BLUE_INDEX);
glCallList(GLOBE);

glIndexi(GREEN_INDEX);
glPushMatrix();
glTranslatef(0.8F, -0.65F, 0.0F);
glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
glCallList(CYLINDER);
glPopMatrix();

glPopMatrix();

SWAPBUFFERS;
}
Posted
Comments
Praveen S 12-Jun-13 7:45am    
how to add surface, texture, animation control etc
BotCar 12-Jun-13 7:56am    
If you want any help here, you need to start by doing three things:
1. Place your code inside a code block.
2. Post only the code necessary for the question - no more and no less.
3. Ask a question. We cannot look at your code and guess what you want to know.
CPallini 12-Jun-13 8:07am    
You did not read the FAQ, did you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900