Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Drawing an object multiple times using viewports

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    6

    Drawing an object multiple times using viewports

    Basically I am trying to draw a screen that has the same star but in different sizes. I am trying to use different sized viewports to accomplish this, but it is only drawing the star once. With the code I have now I am just trying to tile stars all the same size just as a test...

    Code :
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <windows.h>
    #include <GL/glut.h>
    #include <GL/gl.h>
    #include <fstream>
    using namespace std;
     
     
    void myInit(void)
    {
      glClearColor(1.0, 1.0, 1.0, 0.0);
      glColor3f(0.0, 0.0, 1.0);
      glPointSize(4.0);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.0, 400, 0.0, 400);
     
      glViewport(400,400, 800, 800); 
    }
     
    void setWindow(float left, float right, float bottom, float top)
    {
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluOrtho2D(left, right, bottom, top);
    }
    void drawStar(void)
    {
    	      float x1 = 100;
    		  float y1 = 100;
    		  float x2 = 200;
    		  float y2 = 400;
    		  float x3 = 300;
    		  float y3 = 100;
    		  float x4 = 75;
    		  float y4 = 300;
    		  float x5 = 325;
    		  float y5 = 300;
     
    		  glClear(GL_COLOR_BUFFER_BIT);
    		  glBegin(GL_LINE_LOOP);
    		  glVertex2i(x1,y1);
    		  glVertex2i(x2,y2);
    		  glVertex2i(x3,y3);
    		  glVertex2i(x4,y4);
    		  glVertex2i(x5,y5);
    		  glEnd();
    	      glFlush();
    }
     
    void myDisplay(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT);
    	setWindow(0, 400.0, 0, 500.0);
    	for(int i = 0; i < 9; i++) // for each column
    	for(int j = 0; j < 9; j++) // for each row
    		{
    		glViewport(i * 64, j * 44, 64, 44); // set the next viewport
    		drawStar(); // draw it again
     
    		}	
    	glEnd();
    	glFlush();
     
    }
     
    int main(int argc, char **argv)
    {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(600, 400);
      glutInitWindowPosition(200, 200);
      glutCreateWindow("Swag");
      glutDisplayFunc(myDisplay);
     
      myInit();
      glutMainLoop();
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    Drop the

    Code :
    glClear(GL_COLOR_BUFFER_BIT);

    within the draw function. What do you think happens to the framebuffer if you call this every time before rendering a new star?

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by thokra View Post
    Drop the

    Code :
    glClear(GL_COLOR_BUFFER_BIT);

    within the draw function. What do you think happens to the framebuffer if you call this every time before rendering a new star?
    Thank you, sir.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •