Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create cube using opengl in qt
Forum Updated to NodeBB v4.3 + New Features

How to create cube using opengl in qt

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 6.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahesh
    wrote on 14 Mar 2012, 15:23 last edited by
    #1

    hi, some one please help me. i want to create a cube(3D Object) with this following code but i am getting only single rectItem(backside).this is my glwidget item code is ther any wrong in the code?
    @
    #include "glwidget.h"

    GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
    {
    }

    void GLWidget::initializeGL()
    {
    glClearColor(1,1,1,1);
    }

    void GLWidget::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,0.0f);

    glBegin(GL_QUADS);
    
       glColor3f(0.0f,1.0f,0.0f);          // Set The Color To Green
       glVertex3f( 1.0f, 1.0f,-1.0f);          // Top Right Of The Quad (Top)
       glVertex3f(-1.0f, 1.0f,-1.0f);          // Top Left Of The Quad (Top)
       glVertex3f(-1.0f, 1.0f, 1.0f);          // Bottom Left Of The Quad (Top)
       glVertex3f( 1.0f, 1.0f, 1.0f);          // Bottom Right Of The Quad (Top)
    
    
       glColor3f(1.0f,0.5f,0.0f);          // Set The Color To Orange
       glVertex3f( 1.0f,-1.0f, 1.0f);          // Top Right Of The Quad (Bottom)
       glVertex3f(-1.0f,-1.0f, 1.0f);          // Top Left Of The Quad (Bottom)
       glVertex3f(-1.0f,-1.0f,-1.0f);          // Bottom Left Of The Quad (Bottom)
       glVertex3f( 1.0f,-1.0f,-1.0f);          // Bottom Right Of The Quad (Bottom)
    
       glColor3f(1.0f,0.0f,0.0f);          // Set The Color To Red
       glVertex3f( 1.0f, 1.0f, 1.0f);          // Top Right Of The Quad (Front)
       glVertex3f(-1.0f, 1.0f, 1.0f);          // Top Left Of The Quad (Front)
       glVertex3f(-1.0f,-1.0f, 1.0f);          // Bottom Left Of The Quad (Front)
       glVertex3f( 1.0f,-1.0f, 1.0f);          // Bottom Right Of The Quad (Front)
    
       glColor3f (1.0f,1.0f,0.0f);          // Set The Color To Yellow
       glVertex3f( 1.0f,-1.0f,-1.0f);          // Bottom Left Of The Quad (Back)
       glVertex3f(-1.0f,-1.0f,-1.0f);          // Bottom Right Of The Quad (Back)
       glVertex3f(-1.0f, 1.0f,-1.0f);          // Top Right Of The Quad (Back)
       glVertex3f( 1.0f, 1.0f,-1.0f);          // Top Left Of The Quad (Back)
    
       glColor3f(0.0f,0.0f,1.0f);          // Set The Color To Blue
       glVertex3f(-1.0f, 1.0f, 1.0f);          // Top Right Of The Quad (Left)
       glVertex3f(-1.0f, 1.0f,-1.0f);          // Top Left Of The Quad (Left)
       glVertex3f(-1.0f,-1.0f,-1.0f);          // Bottom Left Of The Quad (Left)
       glVertex3f(-1.0f,-1.0f, 1.0f);          // Bottom Right Of The Quad (Left)
    
       glColor3f(1.0f,0.0f,1.0f);          // Set The Color To Violet
       glVertex3f( 1.0f, 1.0f,-1.0f);          // Top Right Of The Quad (Right)
       glVertex3f( 1.0f, 1.0f, 1.0f);          // Top Left Of The Quad (Right)
       glVertex3f( 1.0f,-1.0f, 1.0f);          // Bottom Left Of The Quad (Right)
       glVertex3f( 1.0f,-1.0f,-1.0f);          // Bottom Right Of The Quad (Right)
    
    
     glEnd();
    

    }

    void GLWidget::resizeGL(int w, int h)
    {
    }
    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      john_god
      wrote on 14 Mar 2012, 15:38 last edited by
      #2

      You have to define a viewport in the resizeGL() method

      one example

      @
      void GLWidget::resizeGL(int w, int h)
      {
      double Aspect;

      // Prevent a divide by zero, when window is too short
      // (you cant make a window of zero width)
      if(h == 0)//altura
      h = 1;

          // Set the viewport to be the entire window
      glViewport(0, 0, w, h);
      
      
      Aspect=(double)w/(double)h;
      
      //Reset projection matrix stack
      glMatrixMode(GL_PROJECTION);
      // Reset the coordinate system before modifying
      glLoadIdentity();
      

      // gluPerspective(45.0f,Aspect, 1.0, 425.0);

       double Range = 10;
      
      if (w <= h)
          glOrtho(-(Range),Range,-Range/Aspect,Range/Aspect,-(Range*2),Range*2);
      else
          glOrtho(-(Range*Aspect),Range*Aspect,-Range,Range,-(Range*2),Range*2);
      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      

      }@

      Note you are using the deprecate OpenGL fixed pipeline. There's a example of a cube with programable shaders for symbian, somewhere in Nokia forum I believe, can“t remember right now.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mahesh
        wrote on 19 Mar 2012, 08:15 last edited by
        #3

        hi some one please tell me i just created a cube in 3d.
        For small values of x,y co ordinates it is working fine for bigger values it is not showing cube. it is adding to the scene but i am unable to move my view to those co ordinates."graphicsview having fitInView .is there any similar funtion for QGLWidget"
        @
        GLWidget::GLWidget(QWidget *parent) :
        QGLWidget(parent)
        {
        }
        void GLWidget::initializeGL()
        {
        glClearColor(1,1,1,1);
        glShadeModel(GL_FLAT);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        }

        void GLWidget::paintGL()
        {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        

        glBegin(GL_QUADS);



         glEnd();
        

        }
        void GLWidget::resizeGL(int w, int h)
        {

           double Aspect;
           if(h == 0)
                h = 1;
            glViewport(0, 0, w, h);
            Aspect=(double)w/(double)h;
                    glMatrixMode(GL_PROJECTION);
                   glLoadIdentity();
                   double Range = 10;
            if (w <= h)
                glOrtho(-(Range),Range,-Range/Aspect,Range/Aspect,-(Range*2),Range*2);
            else
                glOrtho(-(Range*Aspect),Range*Aspect,-Range,Range,-(Range*2),Range*2);
        
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
        

        }

        @

        1 Reply Last reply
        0

        3/3

        19 Mar 2012, 08:15

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved