drawing cylinders from scratch

Hi,

I’m trying to draw a cylinder from scratch. I can’t seem to find anywhere on the net that will tell me howto do this. Can anybody point me to a good tutorial on howto build cylinders and other round objects from scratch??

Thanks!

Use a triangle strip and assign vertices based on rsin(theta), rcos(theta), +/- (height/2).

/*
*A P P L I C A T I O N I D E N T I F I C A T I O N
*

  •                       OPEN GL 
    
  •                 CYLINDER VER 0.00001 
    

*/

/I N C L U D E S/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

/* W I N D O W S G L O B A L S, D E F I N E S A N D P R O T O T Y P E S */
/Windows Globals/
/Make the class name into a global variable/
CHAR szAppName[]=“Win OpenGL”;

HWND ghWnd; /* handle for our window /
HDC ghDC; /
handle for device context /
HGLRC ghRC; /
handle for render context */

/Defines/
#define SWAPBUFFERS SwapBuffers(ghDC) /makes scene visible under current device context/
#define BLACK_INDEX 0 /black for background/
#define RED_INDEX 13 /green for wireframe globe object/
#define WIDTH 640 /starting window pixel width/
#define HEIGHT 480 /starting window pixel height/

/Prototypes or procedures for window and pixel format/
LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL bSetupPixelFormat(HDC);

/* O P E N GL G L O B A L S, D E F I N E S A N D P R O T O T Y P E S */
/OpenGL globals/
GLfloat cylinderDiam, cylinderHeight, diaminc, heightinc;
GLdouble radius;

/OpenGL defines/
#define CYLINDER 1 /The object to be rendered/

/OpenGL prototypes or functions/
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void endCylinderView( GLdouble, GLdouble, GLdouble, GLdouble);

/* R E G I S T E R F R A M E C L A S S A N D C R E A T E A S W I N D O W */

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;

/* Register the frame class */ 
wndclass.style         = 0; 
wndclass.lpfnWndProc   = (WNDPROC)MainWndProc; 
wndclass.cbClsExtra    = 0; 
wndclass.cbWndExtra    = 0; 
wndclass.hInstance     = hInstance; 
wndclass.hIcon         = LoadIcon (hInstance, szAppName); 
wndclass.hCursor       = LoadCursor (NULL,IDC_ARROW); 
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
wndclass.lpszMenuName  = szAppName; 
wndclass.lpszClassName = szAppName; 

if (!RegisterClass (&wndclass) ) 
    return FALSE; 

/* Create the frame */ 
ghWnd = CreateWindow (szAppName, 
         "CYLINDER VER 0.0001", 
     WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
         CW_USEDEFAULT, 
         CW_USEDEFAULT, 
         WIDTH, 
         HEIGHT, 
         NULL, 
         NULL, 
         hInstance, 
         NULL); 

/* make sure window was created */ 
if (!ghWnd) 
    return FALSE; 

/* show and update main window */ 
ShowWindow (ghWnd, nCmdShow); 

UpdateWindow (ghWnd); 

/* 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(); 
} 

}

/* W I N 32 A P I W I N D O W´ s G E T D E V I C E C O N T E X T /
/
I T´ S R E N D E R I N G C O N T E X T /
/
A N D U S E R K E Y B O A R D R O U T I N E S /
/
main window procedure */
LONG WINAPI MainWndProc (
HWND hWnd, /*handle for our window */
UINT uMsg, /messages/
WPARAM wParam,
LPARAM lParam)
{
LONG lRet = 1;
PAINTSTRUCT ps;
RECT rect;

switch (uMsg) { 

/Win procedural call to get device context with window´s handle/
/Win procedural call to get render context concurrent with device context/
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; 

/Win32 Api Paint/
case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
/Win32 Api window´s size/
case WM_SIZE:
GetClientRect(hWnd, &rect);
resize(rect.right, rect.bottom);
break;

/Win32 Api to close window and reset render and device context to zero/
case WM_CLOSE:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
ghRC = 0;
ghDC = 0;

    DestroyWindow (hWnd); 
    break;

/Win32 Api to destroy window and delete render and device context/
case WM_DESTROY:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
PostQuitMessage (0);
break;

/User controlled Kyboard routines/
case WM_KEYDOWN:
switch (wParam) {
case VK_LEFT:
diaminc += 0.2F;
break;
case VK_RIGHT:
diaminc -= 0.2F;
break;
case VK_UP:
heightinc += 0.2F;
break;
case VK_DOWN:
heightinc -= 0.2F;
break;
}
/* Windows procedure for messages*/
default:
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}

return lRet; 

}

/* D E T E R M I N A T I O N O F D E P T H A N D P I X E L F O R M A T*/

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

ppfd = &pfd; 

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

pixelformat = ChoosePixelFormat(hdc, ppfd); 

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

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

return TRUE; 

}

/* O P E N GL R E S I Z E A N D P R O J E C T I O N T R A N S F O R M S*/
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 ); 

}

/SCENE SETUP/

/* Object creation */
GLvoid createObjects()
{
GLUquadricObj *quadObj;

glNewList(CYLINDER, GL_COMPILE);             
    quadObj = gluNewQuadric (); 
    gluQuadricDrawStyle (quadObj, GLU_FILL); 
    gluQuadricNormals (quadObj, GLU_SMOOTH); 
    gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);      
glEndList();     

}

/* Function Initialize Scene objects */
GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane;

/* Initial depth and background */
glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 );

/* Initial depth test */
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 = 12.0; 
maxObjectSize = 3.0F; 
radius = near_plane + maxObjectSize/2.0; 

cylinderDiam = 0.0F; 
cylinderHeight = 0.0F; 
diaminc = 3.0F; 
heightinc = 0.0F; 

createObjects(); 

}

/Set model transform for object (i) using glTranslatef, glScalef, glRotatef, and/or equivalent./
void endCylinderView(GLdouble radius, GLdouble twist, GLdouble cylinderDiam,
GLdouble cylinderHeight)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-cylinderDiam, 1.0, 0.0, 0.0);
glRotated(cylinderHeight, 0.0, 0.0, 1.0);

}

/* O P E N GL R E F R E S H */

/* Final Function to Draw current scene objects, ect */

GLvoid drawScene(GLvoid)
{
/Clear previous frame depth and color buffer enabling next frame/
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

/access OpenGl´s matrix stack to save transformations/
glPushMatrix();

 /*User´s Kayboard values*/
    cylinderDiam += diaminc*.5; 
    cylinderHeight += heightinc*.5;
     
 /*Call to object´s function with current values*/
    endCylinderView(radius, 0, cylinderDiam, cylinderHeight);         
     
/* Final Call for Objects to be rendered*/
    glIndexi(RED_INDEX); 
    glCallList(CYLINDER);     

/restore OpenGl´s matrix stack transformations to continue to translatef/
glPopMatrix();

/* Make scene visible */
SWAPBUFFERS;
}

/*
*
*

  •                       OPEN GL 
    
  •                      CYLINDER  
    

*/

/I N C L U D E S/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

/* W I N D O W S G L O B A L S, D E F I N E S A N D P R O T O T Y P E S */
/Windows Globals/
/Make the class name into a global variable/
CHAR szAppName[]=“Win OpenGL”;

HWND ghWnd; /* handle for our window /
HDC ghDC; /
handle for device context /
HGLRC ghRC; /
handle for render context */

/Defines/
#define SWAPBUFFERS SwapBuffers(ghDC) /makes scene visible under current device context/
#define BLACK_INDEX 0 /black for background/
#define RED_INDEX 13 /green for wireframe globe object/
#define WIDTH 640 /starting window pixel width/
#define HEIGHT 480 /starting window pixel height/

/Prototypes or procedures for window and pixel format/
LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL bSetupPixelFormat(HDC);

/* O P E N GL G L O B A L S, D E F I N E S A N D P R O T O T Y P E S */
/OpenGL globals/
GLfloat cylinderDiam, cylinderHeight, diaminc, heightinc;
GLdouble radius;

/OpenGL defines/
#define CYLINDER 1 /The object to be rendered/

/OpenGL prototypes or functions/
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void endCylinderView( GLdouble, GLdouble, GLdouble, GLdouble);

/* R E G I S T E R F R A M E C L A S S A N D C R E A T E A S W I N D O W */

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;

/* Register the frame class */ 
wndclass.style         = 0; 
wndclass.lpfnWndProc   = (WNDPROC)MainWndProc; 
wndclass.cbClsExtra    = 0; 
wndclass.cbWndExtra    = 0; 
wndclass.hInstance     = hInstance; 
wndclass.hIcon         = LoadIcon (hInstance, szAppName); 
wndclass.hCursor       = LoadCursor (NULL,IDC_ARROW); 
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
wndclass.lpszMenuName  = szAppName; 
wndclass.lpszClassName = szAppName; 

if (!RegisterClass (&wndclass) ) 
    return FALSE; 

/* Create the frame */ 
ghWnd = CreateWindow (szAppName, 
         "CYLINDER VER 0.0001", 
     WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
         CW_USEDEFAULT, 
         CW_USEDEFAULT, 
         WIDTH, 
         HEIGHT, 
         NULL, 
         NULL, 
         hInstance, 
         NULL); 

/* make sure window was created */ 
if (!ghWnd) 
    return FALSE; 

/* show and update main window */ 
ShowWindow (ghWnd, nCmdShow); 

UpdateWindow (ghWnd); 

/* 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(); 
} 

}

/* W I N 32 A P I W I N D O W´ s G E T D E V I C E C O N T E X T /
/
I T´ S R E N D E R I N G C O N T E X T /
/
A N D U S E R K E Y B O A R D R O U T I N E S /
/
main window procedure */
LONG WINAPI MainWndProc (
HWND hWnd, /*handle for our window */
UINT uMsg, /messages/
WPARAM wParam,
LPARAM lParam)
{
LONG lRet = 1;
PAINTSTRUCT ps;
RECT rect;

switch (uMsg) { 

/Win procedural call to get device context with window´s handle/
/Win procedural call to get render context concurrent with device context/
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; 

/Win32 Api Paint/
case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
/Win32 Api window´s size/
case WM_SIZE:
GetClientRect(hWnd, &rect);
resize(rect.right, rect.bottom);
break;

/Win32 Api to close window and reset render and device context to zero/
case WM_CLOSE:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
ghRC = 0;
ghDC = 0;

    DestroyWindow (hWnd); 
    break;

/Win32 Api to destroy window and delete render and device context/
case WM_DESTROY:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
PostQuitMessage (0);
break;

/User controlled Kyboard routines/
case WM_KEYDOWN:
switch (wParam) {
case VK_LEFT:
diaminc += 0.2F;
break;
case VK_RIGHT:
diaminc -= 0.2F;
break;
case VK_UP:
heightinc += 0.2F;
break;
case VK_DOWN:
heightinc -= 0.2F;
break;
}
/* Windows procedure for messages*/
default:
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}

return lRet; 

}

/* D E T E R M I N A T I O N O F D E P T H A N D P I X E L F O R M A T*/

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

ppfd = &pfd; 

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

pixelformat = ChoosePixelFormat(hdc, ppfd); 

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

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

return TRUE; 

}

/* O P E N GL R E S I Z E A N D P R O J E C T I O N T R A N S F O R M S*/
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 ); 

}

/SCENE SETUP/

/* Object creation */
GLvoid createObjects()
{
GLUquadricObj *quadObj;

glNewList(CYLINDER, GL_COMPILE);             
    quadObj = gluNewQuadric (); 
    gluQuadricDrawStyle (quadObj, GLU_FILL); 
    gluQuadricNormals (quadObj, GLU_SMOOTH); 
    gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);      
glEndList();     

}

/* Function Initialize Scene objects */
GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane;

/* Initial depth and background */
glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 );

/* Initial depth test */
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 = 12.0; 
maxObjectSize = 3.0F; 
radius = near_plane + maxObjectSize/2.0; 

cylinderDiam = 0.0F; 
cylinderHeight = 0.0F; 
diaminc = 3.0F; 
heightinc = 0.0F; 

createObjects(); 

}

/Set model transform for object (i) using glTranslatef, glScalef, glRotatef, and/or equivalent./
void endCylinderView(GLdouble radius, GLdouble twist, GLdouble cylinderDiam,
GLdouble cylinderHeight)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-cylinderDiam, 1.0, 0.0, 0.0);
glRotated(cylinderHeight, 0.0, 0.0, 1.0);

}

/* O P E N GL R E F R E S H */

/* Final Function to Draw current scene objects, ect */

GLvoid drawScene(GLvoid)
{
/Clear previous frame depth and color buffer enabling next frame/
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

/access OpenGl´s matrix stack to save transformations/
glPushMatrix();

 /*User´s Kayboard values*/
    cylinderDiam += diaminc*.5; 
    cylinderHeight += heightinc*.5;
     
 /*Call to object´s function with current values*/
    endCylinderView(radius, 0, cylinderDiam, cylinderHeight);         
     
/* Final Call for Objects to be rendered*/
    glIndexi(RED_INDEX); 
    glCallList(CYLINDER);     

/restore OpenGl´s matrix stack transformations to continue to translatef/
glPopMatrix();

/* Make scene visible */
SWAPBUFFERS;
}

oops double post!

cylinder from scratch ? what’s this ? Can someone explain me ? :slight_smile:

If you want to make a cylinder, you can use the quadratic solution, check http://nehe.gamedev.net