在 OpenGL 中绘制多个形状

Posted

技术标签:

【中文标题】在 OpenGL 中绘制多个形状【英文标题】:Drawing multiple shapes in OpenGL 【发布时间】:2013-10-15 16:40:40 【问题描述】:
#include <Windows.h>
#include <GL\glut.h>

#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

bool init(void)

    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glMatrixMode(GL_PROJECTION);
    // Set grid to be from 0 to 1
    gluOrtho2D(0.0, 3.0, 0.0, 3.0);
    return true;


void drawline(float from_x, float from_y, float to_x, float to_y)

    // From coordinate position
    glVertex2f(from_x, from_y);

    // To coordinate position
    glVertex2f(to_x, to_y);


void render(void)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
    glLineWidth(2.0); // Set line width to 2.0
    glLoadIdentity();

    // Draw line
    glBegin(GL_LINES);
    drawline(0.25, 0.5, 0.4, 0.5);
    drawline(0.4, 0.6, 0.4, 0.5);
    drawline(0.4, 0.4, 0.4, 0.5);
    drawline(0.6, 0.5, 0.75, 0.5);
    glEnd();

    // Draw triangle
    glBegin(GL_TRIANGLES);
    glVertex2f(0.4, 0.5);
    glVertex2f(0.6, 0.6);
    glVertex2f(0.6, 0.4);
    glEnd();

    glutSwapBuffers();


void reshape(int w, int h)

    glViewport(0, 0, w, h);
    glMatrixMode(GL_MODELVIEW);


int main(int argc, char** argv)

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My OpenGL program");

    init();

    // Draw shape one
    glPushMatrix();
    glTranslatef(1.5, 1.5, 0.0);
    glutDisplayFunc(render);
    glPopMatrix();

    // Draw shape two
    glPushMatrix();
    glTranslatef(2.5, 2.5, 0.0);
    glutDisplayFunc(render);
    glPopMatrix();

    glutReshapeFunc(reshape);

    glutMainLoop();
    return 0;

我正在画这样的东西:http://i.imgur.com/JJpbJ7M.png

我不需要整个东西,我只想能够在我想要的位置绘制两个形状。但是,这不起作用,谁能指出我正确的方向?

【问题讨论】:

【参考方案1】:

你几乎拥有它。

render()而不是main()中绘制你的形状:

#include <GL/glut.h>

void drawline(float from_x, float from_y, float to_x, float to_y)

    // From coordinate position
    glVertex2f(from_x, from_y);

    // To coordinate position
    glVertex2f(to_x, to_y);


void drawShape()

    glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
    glLineWidth(2.0); // Set line width to 2.0

    // Draw line
    glBegin(GL_LINES);
    drawline(0.25, 0.5, 0.4, 0.5);
    drawline(0.4, 0.6, 0.4, 0.5);
    drawline(0.4, 0.4, 0.4, 0.5);
    drawline(0.6, 0.5, 0.75, 0.5);
    glEnd();

    // Draw triangle
    glBegin(GL_TRIANGLES);
    glVertex2f(0.4, 0.5);
    glVertex2f(0.6, 0.6);
    glVertex2f(0.6, 0.4);
    glEnd();


void render(void)

    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0.0, 4.0, 0.0, 4.0, -1, 1 );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // Draw shape one
    glPushMatrix();
    glTranslatef(1.5, 1.5, 0.0);
    drawShape();
    glPopMatrix();

    // Draw shape two
    glPushMatrix();
    glTranslatef(2.5, 2.5, 0.0);
    drawShape();
    glPopMatrix();

    glutSwapBuffers();


int main(int argc, char** argv)

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My OpenGL program");
    glutDisplayFunc(render);
    glutMainLoop();
    return 0;

我删除了reshape()init(),因为默认的glutReshapeFunc() 已经调用了glViewport(),并且无论如何您都应该在每一帧重置投影和模型视图矩阵。

【讨论】:

以上是关于在 OpenGL 中绘制多个形状的主要内容,如果未能解决你的问题,请参考以下文章

无法在opengl中绘制多个对象

在 OpenGL 中绘制 2D 形状的轮廓,同时更改线条粗细

如何在 OpenGL 3.2+ 中渲染多个形状和对象?

在 OpenGL 中绘制形状的标准(常用)方法是啥?

C++ OpenGL,绘制形状返回错误

在opengl中绘制几何图形