当前位置: 代码迷 >> Open API >> opengl出现的不惑之年.
  详细解决方案

opengl出现的不惑之年.

热度:1322   发布时间:2013-02-26 00:00:00.0
opengl出现的不惑...
下面是我写的一个小的Opengl程序,原本是打算是在白色的矩形区域内画四个不同颜色的点,但是却出现了一个绿色区域内的四个点,求解释啊。。下面是我的代码:
#include <gl/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON);
   glVertex2f(-2,-1);
   glVertex2f(-2,-2);
   glVertex2f(-1,0);
   glVertex2f(-4,-2);
glEnd();
    glPointSize(2.0);
glBegin(GL_POINTS);
  glColor3f(1.0,1.0,1.0);
  glVertex2f(-0.5,-0.5);
  glColor3f(1.0,0.0,0.0);
  glVertex2f(-0.5,0.5);     
  glColor3f(0.0,0.0,0.0);
  glVertex2f(0.5,0.5);
  glColor3f(0.0,1.0,0.0);
  glVertex2f(0.5,-0.5);
glEnd();
glFlush();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0,1.0,-1.0,1.0);
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
glutCreateWindow("simple");
glutDisplayFunc(display);
init();
glutMainLoop();
}

。。。各位大虾给个解释吧。。。。
------解决方案--------------------------------------------------------
你画的矩形不在屏幕坐标范围内.
可改为

//白色区域
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.75,-0.75);
glVertex2f(0.75,-0.75);
glVertex2f(0.75,0.75);
glVertex2f(-0.75,0.75);
glEnd();

glPointSize(2.0);
glBegin(GL_POINTS);
glColor3f(0.0,0.0,0.0); //黑
glVertex2f(-0.5,-0.5);
glColor3f(1.0,0.0,0.0); //红
glVertex2f(-0.5,0.5);    
glColor3f(0.0,0.0,1.0); //蓝
glVertex2f(0.5,0.5);
glColor3f(0.0,1.0,0.0); //绿
glVertex2f(0.5,-0.5);
glEnd();
glFlush();

多看看红宝书吧
------解决方案--------------------------------------------------------
gluOrtho2D(-1.0,1.0,-1.0,1.0)

有效区域是(-1.0,1.0,-1.0,1.0)