Kadaj13
Kadaj13

Reputation: 1541

using of glviewport() in OpenGL

I read this tutorial and I execute it correctly.

However, I wanted to apply some changes. The first change was to see a different view of that circle, for example showing just the 1/4 of the circle. I know this is done by glViewPort(parameters). However, after changing the parameters, nothing happens. I have checked many forums to figure out why this problem occurs, but I couldn't figure out anything.

Can someone explain me more on this and how to use it? (To understand where the problem happens)

This is the code (Which is originally written on that tutorial)

Code:

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void init(void)
{
  glClearColor(1.0,1.0,1.0,0.0);
  glMatrixMode(GL_PROJECTION);
  //glLoadIdentity();
  gluOrtho2D(0.0,200.0,0.0,200.0);
  //glViewport(0, 0, 250, 250);
}


void setPixel(GLint x,GLint y)
{
  glBegin(GL_POINTS);
  glVertex2i(x,y);
  glEnd();
}

void Circle(){

  int xCenter=100,yCenter=100,r=50;
  int x=0,y=r;
  int p = 3/2 - r;
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f( 1 ,0, 0);
  while(x<=y){
    setPixel(xCenter+x,yCenter+y);
    setPixel(xCenter+y,yCenter+x);
    setPixel(xCenter-x,yCenter+y);
    setPixel(xCenter+y,yCenter-x);
    setPixel(xCenter-x,yCenter-y);
    setPixel(xCenter-y,yCenter-x);
    setPixel(xCenter+x,yCenter-y);
    setPixel(xCenter-y,yCenter+x);

    if (p<0)
  p += (2*x)+3;
    else {
 p += (2*(x-y))+5;
 y -= 1;
    }
    x++;
  }

  glFlush();
}

int main(int argc,char **argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(500,500);


    glutCreateWindow("My Circl2e");
    init();

    glViewport(0,0,250,250);
    //glLoadIdentity();

    glutDisplayFunc(Circle);

    glutMainLoop();
    return 0;
}

p.s: In some examples I see they use setWindow(parameters) before using glViewPort(parameters). but setWindow() needs library which is not available for ubuntu.

Upvotes: 0

Views: 10378

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54572

The default GLUT reshape function calls glViewport() with the window size. From the documentation:

If a reshape callback is not registered for a window or NULL is passed to glutReshapeFunc (to deregister a previously registered callback), the default reshape callback is used. This default callback will simply call glViewport(0,0,width,height) on the normal plane (and on the overlay if one exists).

Since you call glViewport() so early, the window will be shaped after you make your call, overriding the viewport you specified.

You either need to register your own reshape function, and call glViewport() with your desired viewport parameters there, or call glViewport() at the start of your Circle() function.

Upvotes: 3

Related Questions