0

In fact, what I want to do is to show an image with identical size to the window I created. But I found the rectangle with image texture will be slightly enlarged so that the edge of the image is invisible.

Thus I did the following experiment: For my window size 500x500, I draw a rectangle of size 500x500 in red first, and draw a second one with (500- 2) * (500- 2) in black. What I get is only a black window. By increasing value of d in the code to 8, the red lines show at left/right and bottom, and the top one shows until d = 33.

I thought it is because of depth or projection, but I don't know how to fix it. Is there any suggestion ??

The following is my sample code:

#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/

void rectangle(float x, float y, float x1, double y1, float r, float g, float b)
{
  glColor3f(r, g, b);
  glBegin(GL_QUADS);
  glVertex2f(x, y);
  glVertex2f(x, y1);
  glVertex2f(x1, y1);
  glVertex2f(x1, y);
  glEnd();
}

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

  int d = 33;

  // draw rectangle in red
  rectangle(d, d, 500 - d, 500 - d, 1, 0, 0);

  // draw rectangle in black
  rectangle(d + 1, d + 1, 500 - d - 1, 500 - d - 1, 0, 0, 0);

  /* flush GL buffers */
  glFlush();
}
void init()
{
  /* set clear color to black */
  glClearColor(0.0, 0.0, 0.0, 0.0);
  /* set fill color to white */

  /* set up standard orthogonal view with clipping */
  /* box as cube of side 2 centered at origin */
  /* This is default view and these statements could be removed */

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  gluOrtho2D(0, 500, 0, 500);
}

int main(int argc, char** argv)
{
  /* Initialize mode and open a window in upper left corner of
  /* screen */
  /* Window title is name of program (arg[0]) */
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(0, 0);
  glutCreateWindow("simple");
  glutDisplayFunc(display);
  init();
  glutMainLoop();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

0

I am not sure if this is a bug of glut or not, but I found that the offset values are just equal to the thickness of window border. That is, for the left/right/bottom edges, offset = 9 pixels, and offset = 32 for the top.

It looks that though glutInitWindowSize(500, 500) initializes the black region of the window with 500x500, it also assumes the whole window size which includes the borders to be 500x500. Then when I draw a rectangle of 500x500, it will stretch the rectangle to fit the outline of the created window, but not fit the inner black region.

My workaround is by setting gluOrtho2D to project a region with (-window_left, 500 + window_right, 0 - window_bottom, 500 + window_top), but still drawing rectangle at (0, 0, 500, 500):

gluOrtho2D(0 - 9, 500 + 9, 0 - 9, 500 + 40);  // 40 is an empirical value for showing the red rectangle completely