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();
}