PDA

View Full Version : cylinder in qt using OpenGL.



kk_1
7th April 2016, 21:18
Hey there,
i have to crate a cylinder with GL_QUADS but its not working, it shows a rectangle only..
can someone pleas help me .. (i am very very new in qt and OpenGL)


void OGLWidget::initializeGL()
{
initializeOpenGLFunctions();

glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);

}




void OGLWidget::paintGL()
{
const double PI = 3.14159;
glColor4f(1, 0, 0, 1.0);

/* top triangle */
double i, result = 0.1;
double height = 1, radius = 0.5;
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, height, 0); /* center */
for (i = 0; i <= 2 * PI; i += result)
glVertex3f(radius * cos(i), height, radius * sin(i));
glEnd();

// bottom triangle
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, 0); /* center */
for (i = 2 * PI; i >= 0; i -= result)
glVertex3f(radius * cos(i), 0, radius * sin(i));
glVertex3f(radius, height, 0);
glEnd();

/* middle tube */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= 2 * PI; i += result)
{
glVertex3f(radius * cos(i), 0, radius * sin(i));
glVertex3f(radius * cos(i), height, radius * sin(i));
}
/*loop back to zero degrees */
glVertex3f(radius, 0, 0);
glVertex3f(radius, height, 0);
glEnd();
glPopMatrix();
}

void OGLWidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

ChrisW67
8th April 2016, 13:32
A cylinder looks like a rectangle when viewed from the side. Where is the camera (viewpoint) in your setup?

Radek
8th April 2016, 17:15
Also, where are lights? If you want a 3D picture, you need some "3D tools", that means lights, which will show what is near and what is far. Also, you need to specify the projection: glOrhto() or glFrustum(). At any rate, I recommend to simplify your life using GLUT (in OpenGL 2.0). It contains higher level primitives, like the cylinder which you want to draw, setting the viewpoint and other. In my experience, GLUT is compatible with Qt.