OpenGL线图

时间:2012-03-06 11:56:55

标签: opengl line glut

我正在练习教科书中的练习,但是我无法得到我应该得到的输出。

这就是我所拥有的:

#include <math.h>
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>

//Initialize OpenGL 
void init(void) {
    glClearColor(0.0,0.0,0.0,0.0); 
    glMatrixMode(GL_PROJECTION); 
    gluOrtho2D(0.0,300.0,0.0,300.0);    
} 

void drawLines(void) {
    glClear(GL_COLOR_BUFFER_BIT);  
    glColor3f(0.0,0.4,0.2); 
    glPointSize(3.0);  

    glBegin(GL_LINES);
    glVertex2d(180, 15);
    glVertex2d(10, 145);
    glEnd();
} 

int main(int argc, char**argv) {
    glutInit(&argc, argv);  
    glutInitWindowPosition(10,10); 
    glutInitWindowSize(500,500); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 

    glutCreateWindow("Example"); 
    init(); 
    glutDisplayFunc(drawLines); 
    glutMainLoop();
}

当我运行这段代码时,我的白屏完全空白。

2 个答案:

答案 0 :(得分:8)

我也不是OpenGL的专家,但问题是你没有将视口设置到应该投影场景的位置。你的init应该看起来像这样:

glClearColor(0, 0, 0, 0);

glViewport(0, 0, 500, 500);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 500, 0, 500, 1, -1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

你还需要放一个glFlush();画完后。

void drawLines(void) {
    ...
    glFlush();
}  

答案 1 :(得分:0)

确定并绘制以下OpenGL子函数的输出:

  1. glColor3f(0.0,0.0,0.0,0.0);
    glBegin(Gl_LINES);
    glVertex2i(200,200);
    glVertex2i(70,20);
    glVertex2i(120,150);
    glVertex2i(200,20);
    glVertex2i(60,100);
    glEnd();
    (5分)

  2. int p1 [] = {200,100};
    int p2 [] = {70,20};
    int p3 [] = {120,150};
    int p4 [] = {200,20};
    int p5 [] = {60,100};
    glBegin(GL_LINE_STRIP);
    glVertex2iv(p1);
    glVertex2iv(p2);
    glVertex2iv(p3);
    glVertex2iv(p4);
    glVertex2iv(p5);
    glEnd();