Draw a 3D cylinder using openGL.


 
Thread Tools Search this Thread
Top Forums Programming Draw a 3D cylinder using openGL.
# 1  
Old 11-30-2009
Error Draw a 3D cylinder using openGL.

Hi,

please give me, how to code to draw 3D cylinder in openGL, that should be rotated in x-direction.
waiting for your reply ..
# 2  
Old 11-30-2009
Is this a homework question? Please post it in the appropriate forums
Please show us what have you tried so far.
# 3  
Old 11-30-2009
Error

Hi Sir,

Sorry for asking code..
I made a code to draw cylinder is given below :

code :

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>

#ifndef M_PI
#define M_PI 3.14159265
#endif

int win, win2;
int needsLightUpdate = GL_TRUE;
int useLighting = GL_TRUE;
int useSpecularTexture = GL_FALSE;
int useTexture = GL_FALSE;
int useHighRes = GL_FALSE;
int usePattern = GL_FALSE;
int moveLight = GL_FALSE;
int moveObject = GL_FALSE;

int drawObj = 0, maxObj = 2;
GLfloat lightRotX, lightRotY;
GLfloat objectRotX, objectRotY;
int curx, cury, width, height;

void
drawCylinder(int numMajor, int numMinor, float height, float radius)
{
double majorStep = height / numMajor;
double minorStep = 2.0 * M_PI / numMinor;
int i, j;

for (i = 0; i < numMajor; ++i) {
GLfloat z0 = 0.5 * height - i * majorStep;
GLfloat z1 = z0 - majorStep;

glBegin(GL_TRIANGLE_STRIP);
for (j = 0; j <= numMinor; ++j) {
double a = j * minorStep;
GLfloat x = radius * cos(a);
GLfloat y = radius * sin(a);
glNormal3f(x / radius, y / radius, 0.0);
glTexCoord2f(j / (GLfloat) numMinor, i / (GLfloat) numMajor);
glVertex3f(x, y, z0);

glNormal3f(x / radius, y / radius, 0.0);
glTexCoord2f(j / (GLfloat) numMinor, (i + 1) / (GLfloat) numMajor);
glVertex3f(x, y, z1);
}
glEnd();
}
}
void
initialize(void)
{
glMatrixMode(GL_PROJECTION);
glFrustum(-0.50, 0.50, -0.50, 0.50, 1.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -2.0);

glDepthFunc(GL_LEQUAL);

glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
}

void
redraw(void)
{
glClearColor(0.1, 0.1, 0.1, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


 glBegin(GL_QUADS);
glColor3f(0.0, 0.3, 7.0);
glVertex3f(-2.0, -2.0, -1.0);
glColor3f(0.0, 0.3, 7.0);
glVertex3f(2.0, -2.0, -1.0);
glColor3f(0.0, 0.3, 0.7);
glVertex3f(2.0, 2.0, -1.0);
glColor3f(0.0, 0.3, 0.7);
glVertex3f(-2.0, 2.0, -1.0);
glEnd();
glEnable(GL_DEPTH_TEST);


  if (useLighting) {
glEnable(GL_LIGHTING);
glPushMatrix();
glRotatef(lightRotY, 0, 1, 0);
glRotatef(lightRotX, 1, 0, 0);
glPopMatrix();
} else {
glColor3f(0.2, 0.2, 0.2);
}
if (useTexture || useSpecularTexture) {
glEnable(GL_TEXTURE_2D);
}

if (useSpecularTexture) {
/* pass one */
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);


}

glPushMatrix();
glRotatef(objectRotY, 0, 1, 0);
glRotatef(objectRotX, 1, 0, 0);
drawCylinder(6, 16, 1.0, 0.4);

glPopMatrix();

if (useSpecularTexture) {
/* pass two */
if (!useLighting) {
glColor3f(1.0, 1.0, 1.0);
}
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glPushMatrix();
glRotatef(objectRotY, 0, 1, 0);
glRotatef(objectRotX, 1, 0, 0);
drawCylinder(6, 16, 1.0, 0.4);
glPopMatrix();
}
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
}

void
motion(int x, int y)
{
if (moveLight || moveObject) {
GLfloat dx = (y - cury) * 360.0 / height;
GLfloat dy = (x - curx) * 360.0 / width;
if (moveLight) {
lightRotX += dx;
if (lightRotX > 360)
lightRotX -= 360;
if (lightRotX < 0)
lightRotX += 360;
lightRotY += dy;
if (lightRotY > 360)
lightRotY -= 360;
if (lightRotY < 0)
lightRotY += 360;
needsLightUpdate = GL_TRUE;
} else if (moveObject) {
objectRotX += dx;
if (objectRotX > 360)
objectRotX -= 360;
if (objectRotX < 0)
objectRotX += 360;
objectRotY += dy;
if (objectRotY > 360)
objectRotY -= 360;
if (objectRotY < 0)
objectRotY += 360;
}
curx = x;
cury = y;
}
glutPostRedisplay();
}

void
mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
switch (button) {
case GLUT_LEFT_BUTTON:
moveObject = GL_TRUE;
motion(curx = x, cury = y);
break;
case GLUT_MIDDLE_BUTTON:
moveLight = GL_TRUE;
motion(curx = x, cury = y);
break;
}
} else if (state == GLUT_UP) {
switch (button) {
case GLUT_LEFT_BUTTON:
moveObject = GL_FALSE;
break;
case GLUT_MIDDLE_BUTTON:
moveLight = GL_FALSE;
break;
}
}
}

void
display(void)
{
redraw();
glFlush();
glutSwapBuffers();

}


int main (int argc, char **argv)
{
int i;

glutInit(&argc, argv);
glutInitWindowSize(width = 500, height = 500);
                              
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

for (i = 1; i < argc; ++i) {

exit(1);
}
win = glutCreateWindow("envphong");
glutDisplayFunc(display);

glutMouseFunc(mouse);
glutMotionFunc(motion);
initialize();
glutMainLoop();
return 0;
}


This is working good, please guide me how to place multiple images on the surface of the cylinder ..

Please guide me Sir.. waiting for reply..

Last edited by pludi; 11-30-2009 at 09:42 AM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Sector,cylinder,track information

Hi all, I am trying to get below information from linux machine, could you please assist me to get this information using a command(s) or small script: SectorsPerTrack TotalCylinders TotalHeads TotalTracks TracksPerCylinder i am aware of fdisk -l command but the information provided by... (1 Reply)
Discussion started by: omkar.jadhav
1 Replies

2. Red Hat

Warning: extended partition does not start at a cylinder boundary.

Can you please help me to remove this error. Disk /dev/sda: 64.4 GB, 64424509440 bytes 255 heads, 63 sectors/track, 7832 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk... (4 Replies)
Discussion started by: karthik9358
4 Replies

3. Programming

Draw multicolor line

Hello. I need to draw many lines with multicolor (color is set by some pixmap (xpm)) on C++ with standart libs. Horizontal lines are drawing succsessfully with GC and XSetTile. But non-horizontal - no good. I rotate pixmap for all lines and then XSetTile for each line with own pixmap, but it's... (1 Reply)
Discussion started by: Yuriy
1 Replies

4. Solaris

Solaris 8 and openGL

Hi everybody, Sorry to bother you one more time, but I have a problem with my Solaris 8 installation. Actually, Solaris works well, I can start X and access my desktop (CDE), but I'm having troubles using Cadds (a CAO software). Cadds works, but the 3D visualization is very slow compare to... (4 Replies)
Discussion started by: firstpost
4 Replies

5. Solaris

Partition 0 not aligned on cylinder boundary: "

hi Guys .. user want mirror disk c3t9d0 (running ) to c2t9d0 (fresh hdd). when i tried to bash : prtvtoc /dev/rdsk/c3t9d0s2 | fmthard -s- /dev/rdsk/c2t9d0s2 it showing following error Partition 0 not aligned on cylinder boundary: " 0 4 222 ..... unable to mirror .... plz... (1 Reply)
Discussion started by: coolboys
1 Replies

6. Solaris

Increase root partition by moving starting cylinder

I am trying to expand the root partition on Solaris 10. I can expand root partition using format/partition command, but usually increasing cylinders on partition is done on back end. In this case I would have to expand from the front end following the table below, meaning I would have to move the... (12 Replies)
Discussion started by: mjaminal
12 Replies

7. What is on Your Mind?

Do you draw flowcharts

Do you draw flowcharts while you do the programming , design new network or simmilar ? (8 Replies)
Discussion started by: solaris_user
8 Replies

8. Ubuntu

How to draw cylinder using openGL

Hi Sir, i am new to openGL, i want to know how to draw cylinder using openGL code in C or C++.. And i have to insert bitmap images on cylinder.. How to do this .. please guide me ... Thanking You in advance .. (0 Replies)
Discussion started by: Ravikishore
0 Replies

9. Ubuntu

i am new to opengl , how to work opengl in ubuntu

Hi, i am new to opengl, how to work openGL in ubuntu ,, i am not getting which packages as to be install and how to install those packages. and how to work with small programs.. Please guide me ,,, it will appriceated ... (7 Replies)
Discussion started by: Ravikishore
7 Replies

10. Solaris

Cylinder Group Size reduces in UFS.

Hi all, I am working on Solaris 8 . Its default file system is UFS. My problem is regarding to the file system. I create two partition 1) / 1.2 Gb and 2) /home 4.5 GB In root partition there were 76 Cylinder groups ,when I was traversing the Cylinder Groups I found that the length of one cylinder... (2 Replies)
Discussion started by: sundeep saini
2 Replies
Login or Register to Ask a Question