Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:59:17+00:00 2026-05-26T01:59:17+00:00

I attempted to write the following code to draw a cylinder. The cylinder was

  • 0

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:

enter image description here

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 couldn’t rotate to:

enter image description here

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.

This is the code that I have written to try in mymodel.cpp:

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<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<float> point1(3), point2(3), point3(3), point4(3);
        for(int j=0; j<3; j++) {
            point1[j] = topPosition[j] + topRadius * cos(angle1) * basisvec1[j] + topRadius * sin(angle1) * basisvec2[j];
        }


        for(int j=0; j<3; j++) {
            point2[j] = bottomPosition[j] + bottomRadius * cos(angle1) * basisvec1[j] + bottomRadius * sin(angle1) * basisvec2[j];

        }

        for(int j=0; j<3; j++) {
            point3[j] = bottomPosition[j] + bottomRadius * cos(angle2) * basisvec1[j] + bottomRadius * sin(angle2) * basisvec2[j];
        }

        for(int j=0; j<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();
    }
}

And the overwritten function I have is like this, also in mymodel.cpp:

void CRenderView::drawScene()
{
    //calls the above function
    drawCylinderObject();
}

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?

Edit:

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 CRenderView.cpp:

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:59:18+00:00Added an answer on May 26, 2026 at 1:59 am

    May I ask, why you rotate the cylinder by recalculating its vertices? Just generate a cylinder model once, then perform any following transformations on the modelview matrix. Also I think you’ll want to rotate about Z, not Y.

    Update

    That “tool” seems to be a slightly extended version of the “MFC OpenGL CView” tutorial. *yuck*

    The biggest problem I see, that the CRenderView::OnPaint function has been written by someone, who doesn’t know how to properly use OpenGL.

    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);
    

    glClearColor must be called before glClear, as it sets the value that will be applied upon the glClear call. This way round it will work on the glClear call of the next frame or not at all.

        glPushMatrix();
    

    Which matrix is pushed here? This lacks a call to glMatrixMode.

    Starting from some arbitrary matrix. And BTW where is the projection set (let me guess, in the OnSize handler, right?).

        // 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);
    

    You confirmed you want to rotate about the Z axis. So why won’t you do it here? There only rotation about X and Y here. Problem is: You’re using euler angles here, which have some nasty properties and are frowned upon by 3D graphics people. Better use Quaternions for representing rotations (just a suggestion).

        glScalef(m_xScaling,m_yScaling,m_zScaling);
    
        // Start rendering...
        drawScene();
    
        glPopMatrix();
    
        // Double buffer
        SwapBuffers(dc.m_ps.hdc);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

take a look at the following code I attempted to write inside a constructor:
This code causes the following exception, sometimes : Attempted to read or write protected
The following code is an attempt to write a variadic function that acts like
I have the following code that defines a getParts method to find a given
I have code which will draw a graph that scales if the user attempts
I am using the following code This error occurs : Attempted to read or
The following code compile without errors. Basically, the C#2005 Console application calls VC++2005 class
I have the following code in this jsFiddle. The problem I'm having is that
the following code is a handler that takes in a Percent (percent of the
Following is the code sample taken from Qt application. I want to write following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.