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

  • Home
  • SEARCH
  • 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 3288024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:33:36+00:00 2026-05-17T20:33:36+00:00

So I am trying to learn how to use glViewport(), MULTIPLE TIMES. Below is

  • 0

So I am trying to learn how to use glViewport(), “MULTIPLE TIMES”. Below is my code, I have tried to follow others examples, but they are either so convoluted with other things not relevant to what I am doing or, I do not understand what they are doing. By the way I am using glut to manage my windowing system. So the best way for me to learn is to get a simple example running for myself, then extrapolate from there. Below is my code for a simple program that divides the screen in two and draws two identical spheres, I can’t figure out why the sphere on the right is getting stretched when gluPerspective() is identical for both view ports. Please, if you could just explain to me what I am doing wrong in my code it would greatly help. Outside resources are great, but i need simple, simple examples (not Nate Robinson examples).

GLfloat width = 800, height = 600, x_0 = 470, y_0 = 0;
int mainWindow;


    void resize(int w, int h){
         width=w;
         height=h;
    }

    void draw(void){
         glEnable(GL_SCISSOR_TEST);
         glClearDepth(1.0);
         glClearColor(0.0, 0.0, 0.0, 1.0);
         glEnable(GL_DEPTH_TEST);
         glEnable(GL_LIGHT0);
         glEnable(GL_LIGHTING);
         GLfloat color[4] = {1.0, 0.0, 0.0, 1.0};

         /*
          * LEFT VIEW PORT
          */
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glViewport(0, 0, (float)width/2 , height);
         gluPerspective(45, ((float)width/2)/(float)height, 1, 2000);
         glMatrixMode(GL_MODELVIEW);
         glScissor(0, 0, (float)width , height);
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();
         gluLookAt( 0.0, 0.0, 50.0,
                    0.0, 0.0, 0.0,
                    0.0, 1.0, 0.0 );

         glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); 
         glPushMatrix();
         glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
         glutSolidSphere(10.0, 20, 40);
         glPopMatrix();
         glPopAttrib();

         /*
          * THE SECOND VIEW PORT
          */
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glViewport((float)width/2, 0, width , height);
         gluPerspective(45, ((float)width)/2/((float)height), 1, 2000);
         glMatrixMode(GL_MODELVIEW);
         glScissor(((float)width/2), 0, width , height);
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();
         gluLookAt( 0.0, 0.0, 50.0,
                    0.0, 0.0, 0.0,
                    0.0, 1.0, 0.0 );
         glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); 
         glPushMatrix();
         glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
         glutSolidSphere(10.0, 20, 40);
         glPopMatrix();
         glPopAttrib();

         glDisable(GL_SCISSOR_TEST);
         glFlush();
         glutSwapBuffers();
    }

    void glutAppInitialize(void){
         GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0};
         GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
         GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
         GLfloat light_position[] = {1.0, 1.0, 0.0, 0.0};
         glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
         glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
         glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    }

    int main(int argc, char* argv[]){
         glutInit(&argc, argv);
         glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
         glutInitWindowPosition(x_0, y_0);
         glutInitWindowSize(width ,height);
         glutAppInitialize();
         mainWindow = glutCreateWindow("Tori App");
         glutDisplayFunc(draw);  
         glutReshapeFunc(resize);
         glutMainLoop();
         return(0);
    }

Note that I am playing around while I wait on a response, I got it to do what I wanted, but the solution makes no since, please see the lines of code, basically I moved the view ports start position to 1/4 of the screen instead of on half and I am no longer dividing by two on gluperspective.

/*
 *  THE SECOND VIEW PORT
 */

    glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(width/4, 0, width , height);
gluPerspective(45, ((float)width)/(((float)height)), 1, 2000);
  • 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-17T20:33:37+00:00Added an answer on May 17, 2026 at 8:33 pm

    The second pair of arguments to glViewport are width and height, not the ending x/y position. Your second glViewport should be glViewport(width/2, 0, width/2, height)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn and use html5 and have a basic layout but
I am trying to learn CodeIgniter to use for a shopping site, but I
I am trying to learn and use Blocks effectively. On the web, I have
We're trying to learn to use T4 Templates. I have a desire to use
I am trying to learn to use mercurial by pushing onto Google code. I
I'm trying to learn to use SDL for a little game I'm writing ,
I am trying to learn how to use MSBuild so we can use it
I'm trying to learn advanced sql and how to use system queries (sql server).
In trying to learn how to create objects in ActionScript, I have had no
I am trying to learn and use an SDK for a vendor's product. Unfortunately,

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.