PDA

View Full Version : New to OpenGL. How to I install it?



fandecine
06-28-2002, 02:48 PM
Hi guys!
Sorry but I am a beginner. I just bought the book "OpenGL superbible". I should get it within a few days. I have only one question. I have Windows XP with VC++ 6.0. What do I need to do in order to start coding? Do I have to download something else or I am already set and just have to type the codes?
I will really appreciate your help.
Thanks.
LD

BlackJack
06-28-2002, 10:58 PM
For starting up, the stuff shipped with VC++ is enough.

#include "gl/gl.h"

and add opengl32.lib to your project. Check the internet for tutorials and there you go. You had not needed to buy a book. Too many books are always better than too less on the one hand, hehe, but they unfortunately also cost money and in the internet there are far more far better informations about GL for free.

This should work for you without downloading any additional things: (draws a simple quad, one light is switched on and the normals are rotated, was just an experimental thing):





#include "stdafx.h"
#include "gl/gl.h"
#include "math.h"

HINSTANCE hInst;
HWND hWnd; //window handle
ATOM WinClass;

//handled needed by GL
HDC WinDC;
HGLRC RC;
int PixelFormat;
PIXELFORMATDESCRIPTOR Pfd;

//forwarded functions
ATOM RegisterClass( HINSTANCE hInstance );
BOOL InitInstance( HINSTANCE, int );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
MSG msg;
WinClass=RegisterClass(hInstance);
if( !InitInstance( hInstance, nCmdShow ) )
return FALSE;

while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage( &msg ); DispatchMessage( &msg );
}
return msg.wParam;
}

ATOM RegisterClass( HINSTANCE hInstance )
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = "Mesh3Test";
wcex.hIconSm = 0;

return RegisterClassEx(&wcex);
}

BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
{
hInst = hInstance; // Instanzzugriffsnummer in unserer globalen Variable speichern
hWnd = CreateWindow((char*)WinClass, "lp", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if( !hWnd )
{
return FALSE;
}

SetWindowPos(hWnd,NULL,0,0,640,480,0);

// -------------------- set up OpenGL -------------------------

WinDC=GetDC(hWnd);
memset(&Pfd,0,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=16; Pfd.cDepthBits=16; Pfd.iLayerType=PFD_MAIN_PLANE;
PixelFormat = ChoosePixelFormat(WinDC, &Pfd);
SetPixelFormat(WinDC, PixelFormat, &Pfd);

RC = wglCreateContext(WinDC);
wglMakeCurrent(WinDC,RC);
glClearColor(0.f,0.f,0.f,1.f);
glDepthFunc(GL_LEQUAL);
glDepthMask(true);
glViewport(0,0,640,480);
glEnable(GL_DEPTH_TEST);

// --------------------------------------------------------------

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

return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch( message )
{
case WM_PAINT:

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

float mfFrTop,mfFrRight,mfFrBottom,mfFrLeft;
#define pi 3.141592653f

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//calculate and set the viewcone
mfFrTop = 10/*nearclip*/*(float)tan((60/*v. viewa.*//2.f)*(pi/180.f));
mfFrRight = mfFrTop*(90/*h. viewa.*//60/*v. viewa.*/);
mfFrBottom = -mfFrTop;
mfFrLeft = -mfFrRight;
glFrustum(mfFrLeft,mfFrRight,mfFrBottom,mfFrTop,10/*nearclip*/,1000/*farclip*/);

glEnable(GL_LIGHTING); //enable lighting
glEnable(GL_LIGHT0); //enable light 0
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE ); //local viewer shall not matter
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);

//setup infinite directional light
float lightpos[4] = {0.f,0.f,1.f,0.f};
glLightfv(GL_LIGHT0,GL_POSITION,lightpos);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

int deg = (GetTickCount()/5) % 360;

glColor3f(1,1,1); //shared color for all vertices
glNormal3f((float)cos(deg*pi/180.f),0,(float)sin(deg*pi/180.f)); //shared normal for all vertices
glBegin(GL_QUADS);
glVertex3f(-20,-20,-100);
glVertex3f(20,-20,-100);
glVertex3f(20,20,-100);
glVertex3f(-20,20,-100);
glEnd();

SwapBuffers(WinDC);
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
return 0;
}