Placing a rectangle on top of a gluCylinder?

I have trouble drawing a rectangle, on top of a gluCylinder, and rotating the newly created object (both the rectangle and the cylinder around the same axis). Can anyone explain to me how to manipulate this? I am struggling to translate them together somehow but it doesn’t work. Thanks a lot.

I have trouble drawing a rectangle, on top of a gluCylinder, and rotating the newly created object (both the rectangle and the cylinder around the same axis).[…] I am struggling to translate them together somehow but it doesn’t work. Thanks a lot.

And finally is your problem about translation or rotation?

Something like this should work:


do your transformations

drawCylinder();
drawRectangle();


you need to clearly tell to opengl that you are youfing depth buffer bit
if some where you call
glClear(GL_BLABLA_BUFFER_BIT )
you replace with
glClear(GL_BLABLA | GL_DEPTH_BUFFER_BIT);

and also if you use glut library
you need to replace
glutInitDisplayMode(GLUT_BLABLA);
with
glutInitDisplayMode(GLUT_BLABLA | GLUT_ DEPTH);
and also enable GL_DEPTH_TEST

Here is a sample perhapes you looking for
try to compile it (if Linux thats better cause makefile)

main.cpp:

#include <stdlib.h>
#include <GL/glut.h>

int WinWidth = 640,WinHeight = 480;
void Cylinder(GLdouble base, GLdouble top, GLdouble height);
void Disk(GLdouble inner, GLdouble outer);
void pyram(GLfloat rad,GLfloat Height)
{// every wall with different color so i can identify its Pyramidical

GLUquadric *Cy;
Cy = gluNewQuadric ();
gluQuadricDrawStyle ( Cy, GLU_FILL);
gluQuadricNormals ( Cy, GLU_SMOOTH);
gluQuadricOrientation ( Cy, GLU_INSIDE);
glColor3f(1.0f,0.0f,0.0f);
gluCylinder (Cy, 0.0f, rad, Height, 32,24);
glColor3f(0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,Height);
gluDisk ( Cy, 0.0f, rad,32,24);
}
GLfloat zr = 0.5f;
void RScene()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glDepthMask(GL_TRUE);
glEnable(GL_CCW);
glEnable(GL_DEPTH_TEST);
glRotatef(zr,1.0f,0.0f,0.0f);

glPushMatrix();
pyram(0.5,0.5);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CCW);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

zr+= 0.5f;
glutSwapBuffers();
glutPostRedisplay();
}

void WinSize(GLsizei w,GLsizei h)
{
GLfloat Ratio = (GLdouble)w/(GLdouble)h;
if(h==0)h=1;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,Ratio,-100.0f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);

}
void Idle(){glutPostRedisplay();}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WinWidth,WinHeight);
glutCreateWindow(“Thats Perhaps you need”);
glutDisplayFunc(RScene);
glutReshapeFunc(WinSize);

glutMainLoop();

}

void Cylinder(GLdouble base, GLdouble top, GLdouble height)
{
}
void Disk(GLdouble inner, GLdouble outer)
{GLUquadric *Dsk;
Dsk = gluNewQuadric ();
gluQuadricDrawStyle ( Dsk, GLU_FILL);
gluQuadricNormals ( Dsk, GLU_SMOOTH);
gluQuadricOrientation ( Dsk, GLU_INSIDE);

}
At Linux condition try make with below file

makefile:

LIB = -I/usr/lib
INC = -I/usr/include
SRC = main.cpp
COMP = gcc
LINK = -lglut -lGL
1st:
$(COMP) -o cam1 $(SRC) $(LIB) $(INC) $(LINK)

you need to clearly tell to opengl that you are using depth buffer bit
if some where you call
glClear(GL_BLABLA_BUFFER_BIT )
you replace with
glClear(GL_BLABLA | GL_DEPTH_BUFFER_BIT);

and also if you use glut library
you need to replace
glutInitDisplayMode(GLUT_BLABLA);
with
glutInitDisplayMode(GLUT_BLABLA | GLUT_ DEPTH);
and also enable GL_DEPTH_TEST

Here is a sample perhapes you looking for
try to compile it (if Linux thats better cause makefile)

main.cpp:

#include <stdlib.h>
#include <GL/glut.h>

int WinWidth = 640,WinHeight = 480;
void Cylinder(GLdouble base, GLdouble top, GLdouble height);
void Disk(GLdouble inner, GLdouble outer);
void pyram(GLfloat rad,GLfloat Height)
{// every wall with different color so i can identify its Pyramidical

GLUquadric *Cy;
Cy = gluNewQuadric ();
gluQuadricDrawStyle ( Cy, GLU_FILL);
gluQuadricNormals ( Cy, GLU_SMOOTH);
gluQuadricOrientation ( Cy, GLU_INSIDE);
glColor3f(1.0f,0.0f,0.0f);
gluCylinder (Cy, 0.0f, rad, Height, 32,24);
glColor3f(0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,Height);
gluDisk ( Cy, 0.0f, rad,32,24);
}
GLfloat zr = 0.5f;
void RScene()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glDepthMask(GL_TRUE);
glEnable(GL_CCW);
glEnable(GL_DEPTH_TEST);
glRotatef(zr,1.0f,0.0f,0.0f);

glPushMatrix();
pyram(0.5,0.5);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CCW);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

zr+= 0.5f;
glutSwapBuffers();
glutPostRedisplay();
}

void WinSize(GLsizei w,GLsizei h)
{
GLfloat Ratio = (GLdouble)w/(GLdouble)h;
if(h==0)h=1;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,Ratio,-100.0f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);

}
void Idle(){glutPostRedisplay();}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WinWidth,WinHeight);
glutCreateWindow(“Thats Perhaps you need”);
glutDisplayFunc(RScene);
glutReshapeFunc(WinSize);

glutMainLoop();

}

void Cylinder(GLdouble base, GLdouble top, GLdouble height)
{
}
void Disk(GLdouble inner, GLdouble outer)
{GLUquadric *Dsk;
Dsk = gluNewQuadric ();
gluQuadricDrawStyle ( Dsk, GLU_FILL);
gluQuadricNormals ( Dsk, GLU_SMOOTH);
gluQuadricOrientation ( Dsk, GLU_INSIDE);

}
At Linux condition try make with below file

makefile:

LIB = -I/usr/lib
INC = -I/usr/include
SRC = main.cpp
COMP = gcc
LINK = -lglut -lGL
1st:
$(COMP) -o cam1 $(SRC) $(LIB) $(INC) $(LINK)