使用GLUT在3D OpenGL世界中显示固定位置2D文本 [英] Displaying fixed location 2D text in a 3D OpenGL world using GLUT

查看:530
本文介绍了使用GLUT在3D OpenGL世界中显示固定位置2D文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 OpenGL 项目,它使用 GLUT (not freeglut)其中我想显示2D文本在固定位置的视口。我的其余对象都在3D世界坐标中。



此答案到一个相关的老问题说,


GLUT附带的位图字体是简单的2D字体,不适合显示在3D环境中。但是,它们非常适合需要在显示窗口上叠加的文本。


我尝试了接受答案中概述的方法,但它没有给我想要的输出。以下是显示函数的代码片段:

  void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(0.0,0.0, - (dis + ddis)); //翻译相机
glRotated(elev + delev,1.0,0.0,0.0); // Rotate the camera
glRotated(azim + dazim,0.0,1.0,0.0); //旋转相机




绘制3D场景




glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0,win_width,0.0,win_height);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRasterPos2i(10,10);
string s =一些文本;
void * font = GLUT_BITMAP_9_BY_15;
for(string :: iterator i = s.begin(); i!= s.end(); ++ i)
{
char c = * i;
glColor3d(1.0,0.0,0.0);
glutBitmapCharacter(font,c);
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glFlush();
glSwapBuffers();
}

A 类似问题似乎被问到,但没有作者接受的答案。



建议使用其他库(主要是操作系统特定的)来实现覆盖。然而,没有明确的指示是否可以单独使用 GLUT 来实现。可以做吗?

解决方案

正如我所说,你发布的代码完全正常。这里是我用来测试它的代码:

  #ifdef __APPLE__ 
#include< GLUT / glut.h>
#else
#include< GL / glut.h>
#endif

#includeGL / glu.h
// glm不需要
//#include< glm.hpp>
//#include< gtc / matrix_transform.hpp>
#include< string>

int win_width = 500,win_height = 500;

void renderScene(void){

static float dis = 0,ddis = 0,elev = 0,delev = 0,azim = 0,dazim = 0;

azim + = 0.5f;
if(azim> = 360.0f){
azim - = 360.0f;
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(0.0f,0.0f, - (dis + ddis));
glRotated(elev + delev,1.0,0.0,0.0);
glRotated(azim + dazim,0.0,1.0,0.0);
glScalef(2.0f,2.0f,2.0f);

glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.5f,0.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.5f,0.0f);
glEnd();


glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
//我喜欢使用glm,因为glu已被弃用
// glm :: mat4 orth = glm :: ortho(0.0f,(float)win_width,0.0f,(float)win_height);
// glMultMatrixf(&(orth [0] [0]));
gluOrtho2D(0.0,win_width,0.0,win_height);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0f,0.0f,0.0f); //需要在RasterPos之前调用
glRasterPos2i(10,10);
std :: string s =Some text;
void * font = GLUT_BITMAP_9_BY_15;

for(std :: string :: iterator i = s.begin(); i!= s.end(); ++ i)
{
char c = *一世;
//这什么都不做,调用glRasterPos时颜色固定为Bitmaps
//glColor3f(1.0,0.0,1.0);
glutBitmapCharacter(font,c);
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROjECTION);
glPopMatrix();
glEnable(GL_TEXTURE_2D);

glutSwapBuffers();
glutPostRedisplay();
}

int main(int argc,char ** argv){

//初始化GLUT并创建窗口
glutInit(& argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(win_width,win_height);
glutCreateWindow(GLUT Test);

//注册回调
glutDisplayFunc(renderScene);

//输入GLUT事件处理周期
glutMainLoop();

return 1;
}

关于已弃用的固定函数管道和其他通常的免责声明适用。

I have an OpenGL project which uses GLUT (not freeglut) wherein I would like to display 2D text on the viewport at a fixed location. The rest of my objects are in 3D world co-ordinates.

This answer to a related old question says that,

the bitmap fonts which ship with GLUT are simple 2D fonts and are not suitable for display inside your 3D environment. However, they're perfect for text that needs to be overlayed on the display window.

I've tried the approach outlined in the accepted answer, but it does not give me the desired output. Following is a code snippet of the display function:

void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();  
  glTranslatef(0.0, 0.0, -(dis+ddis));   //Translate the camera
  glRotated(elev+delev, 1.0, 0.0, 0.0);  //Rotate the camera
  glRotated(azim+dazim, 0.0, 1.0, 0.0);  //Rotate the camera

       .
       .
       .
  draw 3D scene
       .
       .
       .

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  gluOrtho2D(0.0, win_width, 0.0, win_height);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  glRasterPos2i(10, 10);
  string s = "Some text";
  void * font = GLUT_BITMAP_9_BY_15;
  for (string::iterator i = s.begin(); i != s.end(); ++i)
  {
    char c = *i;
    glColor3d(1.0, 0.0, 0.0);
    glutBitmapCharacter(font, c);
  }
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  glFlush();
  glSwapBuffers();
}

A similar question to what I want seems to have been asked, but has no accepted answers by the author.

Answers in general seem to suggest using other libraries (mostly OS specific) to achieve the overlay. However, there is no clear indication to whether this can be achieved with GLUT alone. Can it be done?

解决方案

As I said, the code you posted works perfectly fine. Here's the code I used to test it:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include "GL/glu.h"
//glm not required
//#include <glm.hpp>
//#include <gtc/matrix_transform.hpp>
#include <string>

int win_width = 500, win_height = 500;

void renderScene(void) {

    static float dis=0, ddis=0, elev=0, delev=0, azim=0, dazim=0;

    azim += 0.5f;
    if (azim >= 360.0f){
        azim -= 360.0f;
    }

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -(dis + ddis));
    glRotated(elev + delev, 1.0, 0.0, 0.0);
    glRotated(azim + dazim, 0.0, 1.0, 0.0);
    glScalef(2.0f,2.0f,2.0f);

    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(-0.5f, -0.5f, 0.0f);
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(0.5f, 0.0f, 0.0f);
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);
    glEnd();


    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    //I like to use glm because glu is deprecated
    //glm::mat4 orth= glm::ortho(0.0f, (float)win_width, 0.0f, (float)win_height);
    //glMultMatrixf(&(orth[0][0]));
    gluOrtho2D(0.0, win_width, 0.0, win_height);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glColor3f(1.0f, 0.0f, 0.0f);//needs to be called before RasterPos
    glRasterPos2i(10, 10);
    std::string s = "Some text";
    void * font = GLUT_BITMAP_9_BY_15;

    for (std::string::iterator i = s.begin(); i != s.end(); ++i)
    {
        char c = *i;
        //this does nothing, color is fixed for Bitmaps when calling glRasterPos
        //glColor3f(1.0, 0.0, 1.0); 
        glutBitmapCharacter(font, c);
    }
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glEnable(GL_TEXTURE_2D);

    glutSwapBuffers();
    glutPostRedisplay();
}

int main(int argc, char **argv) {

    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(win_width, win_height);
    glutCreateWindow("GLUT Test");

    // register callbacks
    glutDisplayFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

The usual disclaimers about deprecated fixed function pipeline and others apply.

这篇关于使用GLUT在3D OpenGL世界中显示固定位置2D文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆