bacis stuff

hello,
i have to display a small cube on the screen. i have to do the world co-ordintes to the screen co-ordinates…eg: i have a cube of 3cm on 10X10cm paper which is about 5 cm from the left and 4 cm from the top. i am supposed to display this cube on a screen of 250x250. i have no idea about how to do this.can someone please help me how to do this…
thanx a lot

Get the OpenGL Programming Guide. This is a pretty basic question. At least read through the first couple of chapters.

If you can’t afford the book at least look through examples on this site or look at the book examples at ftp:://ftp.sgi.com/opengl.


glViewport(0, 0, 250, 250);

gluOrtho2D(-5, 5, -5, 5);

thanx a lot…
i started studying opengl today…i’m toatlly confused with the projections and camera positioning…
In case of perspective projection is the object placed inside the frustum (ie between near and far clipping planes) or between the eye and near clipping plane?
is there any way to generalize the values of the clipping planes?
thanx

Begginner

I´m not sure if this is what you´re trying to do but, here´s a 3d cube sppining in 3d space. Did´nt have time to put in a proper perspective, though. If you can shoehorn one in, repost it.

But, it did compile without a single error or warning in my DEV C++ compiler. Put in your libs and compiler parameters for your particular C++ compiler.

Hope this helps.

BeginOpenGL

/***************************************

  •        OpenGL CUBE
    
  •        BeginOpenGL
    

***************************************/
/includes/
#include <windows.h>
#include <gl/gl.h>

/**************************

  • Function Declarations

**************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);

/**************************

  • WinMain

**************************/

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
/´declare the var cubeAngleY to be used as an angle in rotations/
float cubeAngleY = 0.0f;

/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);

/* create main window */
hWnd = CreateWindow (
  "GLSample", "OpenGL Cube Ver. 0.00001", 
  WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  0, 0, 1024, 768,
  NULL, NULL, hInstance, NULL);

/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);

/* program main loop */
while (!bQuit)
{
    /* check for messages */
    if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
        /* handle or dispatch messages */
        if (msg.message == WM_QUIT)
        {
            bQuit = TRUE;
        }
        else
        {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    else
    {
        /* OPEN GL ANIMATION CODE FOR PRIMITIVES GOES BELOW */
        
        /*Set the Background color to black*/
        glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
        /*Clear previous frame color buffer to enable drawing of next frame*/
        /*Clear previous frame depth buffer to enable drawing of next frame*/
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);           
        /*Clear existing internal tansformations by loading identity matrix*/
        glLoadIdentity ();
        /*move all vertices(the cube) 1 unit forward on z axis*/
        glTranslatef (0, 0, -1);
        /*rotate cube around it´s vertical Y  axis*/             
        glRotatef(cubeAngleY, 0.0f, 1.0f, 0.0f);
        /*access OpenGl´s matrix stack to save transformations*/
        glPushMatrix ();
        
      /*assign color and position in 3d space to each vertex thru its 3 coords*/ 
       void drawCube(void);       
      
       glBegin(GL_QUADS);  //front 
       glColor3f(1.0f, 0.0f, 0.0f);glVertex3f(-.5f, -.5f,  .5f);
       glColor3f(0.0f, 1.0f, 0.0f);glVertex3f( .5f, -.5f,  .5f);
       glColor3f(0.0f, 0.0f, 1.0f);glVertex3f( .5f,  .5f,  .5f);
       glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-.5f,  .5f,  .5f);
       glEnd();
      
       glBegin(GL_QUADS);  //back 
       glColor3f(0.0f, 1.0f, 0.0f);glVertex3f( .5f, -.5f, -.5f);
       glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-.5f, -.5f, -.5f);
       glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-.5f,  .5f, -.5f);
       glColor3f(0.0f, 1.0f, 1.0f);glVertex3f( .5f,  .5f, -.5f);
       glEnd();    
       
        glBegin(GL_QUADS);  //left 
        glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-.5f, -.5f, -.5f);
        glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-.5f, -.5f,  .5f);
        glColor3f(0.0f, 1.0f, 1.0f);glVertex3f(-.5f,  .5f,  .5f);
        glColor3f(1.0f, 0.0f, 1.0f);glVertex3f(-.5f,  .5f, -.5f);
        glEnd();    
       
        glBegin(GL_QUADS); //right 
        glColor3f(1.0f, 1.0f, 0.0f);glVertex3f( .5f, -.5f, .5f);
        glColor3f(0.0f, 1.0f, 1.0f);glVertex3f( .5f, -.5f, -.5f);
        glColor3f(1.0f, 0.0f, 1.0f);glVertex3f( .5f, .5f, -.5f);
        glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( .5f, .5f, .5f);
        glEnd();
       
        glBegin(GL_QUADS);  //top 
        glColor3f(0.0f, 1.0f, 1.0f);glVertex3f(-.5f,  .5f,  .5f);
        glColor3f(1.0f, 0.0f, 1.0f);glVertex3f( .5f,  .5f,  .5f);
        glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( .5f,  .5f, -.5f);
        glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(-.5f,  .5f, -.5f);
        glEnd(); 
       
         glBegin(GL_QUADS);  //bottom 
         glColor3f(1.0f, 0.0f, 1.0f);glVertex3f( .5f, -.5f, -.5f);
         glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( .5f, -.5f,  .5f);
         glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(-.5f, -.5f,  .5f);
         glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-.5f, -.5f, -.5f);
         glEnd();               

        /*restore OpenGl´s matrix stack transformations to continue to translatef*/
        glPopMatrix ();
        /*Make the cube visible*/
        SwapBuffers (hDC);
        
       /*increment cube´s y angle*/
        cubeAngleY+=0.9f; 
        
        Sleep (1);
    }
}

/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);

/* destroy the window explicitly */
DestroyWindow (hWnd);

return msg.wParam;

}

/********************

  • Window Procedure

********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_CREATE:
    return 0;
case WM_CLOSE:
    PostQuitMessage (0);
    return 0;

case WM_DESTROY:
    return 0;

case WM_KEYDOWN:
    switch (wParam)
    {
    case VK_ESCAPE:
        PostQuitMessage(0);
        return 0;
    }
    return 0;

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

}

/*******************

  • Enable OpenGL

*******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

/* get the device context (DC) */
*hDC = GetDC (hWnd);

/* set the pixel format for the DC */
ZeroMemory (&pfd, 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 = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);

/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}

/******************

  • Disable OpenGL

******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}