0
votes

I am trying to write program that displays a window with simulated "tv static". I have it mostly working but when I expand the window grid lines form. I have no idea what could be causing this as this is my first OpenGL (glut) program. Any suggestions? thanks in advanceenter image description here

#include <GLUT/glut.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

void display(void){
    /* clear window */
    glClear(GL_COLOR_BUFFER_BIT);

    int maxy = glutGet(GLUT_WINDOW_HEIGHT);
    int maxx = glutGet(GLUT_WINDOW_WIDTH);

    glBegin(GL_POINTS);

    for (int y = 0; y <= maxy; ++y) {
        for (int x = 0; x <= maxx; ++x) {

        glColor3d(rand() / (float) RAND_MAX,rand() / (float) RAND_MAX,rand() / (float) RAND_MAX);

        glVertex2i(x, y);
    }
}
            glEnd();


/* flush GL buffers */

glFlush();

}


void init(){
/* set clear color to black */
glClearColor (0.0, 0.0, 0.0, 1.0);

/* set fill  color to white */
glColor3f(1.0, 1.0, 1.0);

/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

int main(int argc, char** argv){
srand(time(NULL));
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
glutInit(&argc,argv);

//You can try the following to set the size and position of the window

 glutInitWindowSize(500,500);
 glutInitWindowPosition(0,0);

glutCreateWindow("simple");

glutDisplayFunc(display);
init();
glutIdleFunc(display);
glutMainLoop();
}

Edit: I can remove the lines by using glRecti; however the pixels get bigger the larger the window gets.

3
I ended up modifying kvark's code in order to fill in the spaces(incrementing by .3) - romejoe

3 Answers

3
votes

Use

void glutReshapeFunc(void (*func)(int width, int height));

to reset the projection, glOrtho, when the window size changes.

In your case this should do the trick:

void resize(int width,int height)
{
  glOrtho(0, width, height, 0, 0, 1);
}

int main(int argc, char** argv){
  //...
  glutReshapeFunc(resize);

  glutDisplayFunc(display);
  init();
  glutIdleFunc(display);
  glutMainLoop();
}
2
votes

Your screen is width*height size, but you are actually drawing (width+1)*(height+1) points. Also, your boundary pixels are drawn right on the boundary lines, so I'm not sure they'll be visible.

Solution:

for (int y = 0; y < maxy; ++y) {
    for (int x = 0; x < maxx; ++x) {
        glColor3d(rand() / (float) RAND_MAX,rand() / (float) RAND_MAX,rand() / (float) RAND_MAX);
        glVertex2f(x+0.5f, y+0.5f);
    }
}

Notice the change in loop conditions and type of glVertex call.

1
votes

It looks like glut's internal idea of window size isn't getting updated when you resize the window. You may need a window resize handler.