title: Why my cylinder model cannot be rotated in the Z-axis?
body: <p>I attempted to write the following code to draw a cylinder. The cylinder was drawn in C++ with OpenGL. And I'm given a little tool by the school that I could compile with my own model's cpp file and then able to rotate the model with the mouse. The tool doesn't affect the rotation of my model because it works for the other demo models. However, for some reason which I do not understand, I cannot rotate the cylinder in the Z-axis to see it in its horizontal view. So, the cylinder can only be rotated and seen in these directions:</p>
  
  <p><img src="https://i.stack.imgur.com/ikhsU.png" alt="enter image description here"></p>
  
  <p>Why I cannot rotate the cylinder to see it in this direction? The following image was rotated manually in Photoshop to illustrate the direction of view that the model <em>couldn't</em> rotate to:</p>
  
  <p><img src="https://i.stack.imgur.com/nHIlD.png" alt="enter image description here"></p>
  
  <p>I don't understand what is the reason for not being to rotate in that direction because the demo shown for other models(written by others) could be rotated freely in all directions.</p>
  
  <p>This is the code that I have written to try in <code>mymodel.cpp</code>:</p>
  
  <pre><code>void drawCylinderObject() {
      float topRadius = 5;
      float bottomRadius = 5;
      float height = 10;
      int numOfPolygons = 50;
      float basisvec1[3] = {1, 0, 0};
      float basisvec2[3] = {0, 0, -1};
      float topPosition[3] = {0, height/2.0, 0};
      float bottomPosition[3] = {0, -height/2.0, 0};
  
  
      for(int i=0; i&lt;numOfPolygons; i++) {
          float angle1 = (float)i/(float)numOfPolygons*2.0*M_PI;
          float angle2 = ((float)i+1.0)/(float)numOfPolygons*2.0*M_PI;
          vector&lt;float&gt; point1(3), point2(3), point3(3), point4(3);
          for(int j=0; j&lt;3; j++) {
              point1[j] = topPosition[j] + topRadius * cos(angle1) * basisvec1[j] + topRadius * sin(angle1) * basisvec2[j];
          }
  
  
          for(int j=0; j&lt;3; j++) {
              point2[j] = bottomPosition[j] + bottomRadius * cos(angle1) * basisvec1[j] + bottomRadius * sin(angle1) * basisvec2[j];
  
          }
  
          for(int j=0; j&lt;3; j++) {
              point3[j] = bottomPosition[j] + bottomRadius * cos(angle2) * basisvec1[j] + bottomRadius * sin(angle2) * basisvec2[j];
          }
  
          for(int j=0; j&lt;3; j++) {
              point4[j] = topPosition[j] + topRadius * cos(angle2) * basisvec1[j] + topRadius * sin(angle2) * basisvec2[j];
          }
  
          float crossvec1[3] = {point4[0]-point1[0], point4[1]-point1[1], point4[2]-point1[2]};
          float crossvec2[3] = {point2[0]-point1[0], point2[1]-point1[1], point2[2]-point1[2]};
          float normalVector1[3];
  
          crossProduct(crossvec2, crossvec1, normalVector1);
  
          glBegin(GL_POLYGON);
  
          glNormal3fv(normalVector1);
  
          glVertex3f(point1[0], point1[1], point1[2]);
          glVertex3f(point2[0], point2[1], point2[2]);
          glVertex3f(point3[0], point3[1], point3[2]);
          glVertex3f(point4[0], point4[1], point4[2]);
  
          glEnd();
      }
  }
  </code></pre>
  
  <p>And the overwritten function I have is like this, also in <code>mymodel.cpp</code>:</p>
  
  <pre><code>void CRenderView::drawScene()
  {
      //calls the above function
      drawCylinderObject();
  }
  </code></pre>
  
  <p>What I have done basically is just to define 2 perpendicular basis unit vectors and then extend them outwards with a magnitude value. And I loop through this 360 degrees to draw the polygons to form the cylinder. But what is wrong that this way of drawing doesn't allow me to rotate the model freely?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The following is part of the code of how the tool draws the scene. Somehow, the tool has a huge chunk of classes. Most of its classes merely sets up the GUI of the tool and then the only part that draws it is the one below in <code>CRenderView.cpp</code>:</p>
  
  <pre><code>void CRenderView::OnPaint() 
  {
      // Device context for painting
      CPaintDC dc(this); 
  
      // Model is stored in Document
      CToolDoc *pDoc = (CToolDoc *)GetDocument();
      ASSERT_VALID(pDoc);
  
      // Useful in multidoc templates
      HWND hWnd = GetSafeHwnd();
      HDC hDC = ::GetDC(hWnd);
      wglMakeCurrent(hDC,m_hGLContext);
  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      glClearColor(m_ClearColorRed,m_ClearColorGreen,m_ClearColorBlue,1.0f);
  
      glPushMatrix();
  
      // Position / translation / scale
      glTranslated(m_xTranslation,m_yTranslation,m_zTranslation);
      glRotatef(m_xRotation, 1.0, 0.0, 0.0);
      glRotatef(m_yRotation, 0.0, 1.0, 0.0);
      glScalef(m_xScaling,m_yScaling,m_zScaling);
  
      // Start rendering...
      drawScene();
  
      glPopMatrix();
  
      // Double buffer
      SwapBuffers(dc.m_ps.hdc);
  }
  
  protected:
      void drawScene();
  
  void CRenderView::OnMouseMove(UINT nFlags, 
                                                              CPoint point) 
  {
      if(m_LeftButtonDown)
      {
          m_yRotation -= (float)(m_LeftDownPos.x - point.x)/3.0f;
          m_xRotation -= (float)(m_LeftDownPos.y - point.y)/3.0f;
          m_LeftDownPos = point;
          InvalidateRect(NULL,FALSE);
      }
      CView::OnMouseMove(nFlags, point);
  }
  </code></pre>