在OpenGL中,我想使用一些像“\ t”或“\ n”这样的字符文字。但是,在显示文本时,它们完全是igonre。我该怎么办?
char buffer[100];
sprintf(buffer, "First float is: %f.\n\tThe second one is: %f", 2.225,3.141592);
glCallLists(strlen(buffer), GL_UNSIGNED_BYTE, buffer);
// This will display all the text on the same line and ignoring the "\t"
示例(我正在一个大项目中工作。我试图复制下面的opengl绘图例程的基本结构):
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
int centreX = 100;
int centreY = 200;
GLvoid Print(const char *fmt)
{
char text[256];
if (fmt == NULL)
return;
sprintf(buffer,fmt);
glListBase(256);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
}
void DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0f,1.0f,0.5f);
glRasterPos2i((GLint)centreX, (GLint)centreY);
Print("First float is: %f.\n\tThe second one is: %f", 2.225,3.141592);
}
void main()
{
while (true)
{
DrawScene();
}
}
答案 0 :(得分:4)
首先:OpenGL不知道&#34; text&#34;是!
您使用的方法依赖于显示列表。对于每个字符代码,创建一个显示列表。这些列表包含绘制单个字母并将变换应用于下一个字母位置的命令。然而,诸如换行符,制表符等控制字符取决于屏幕的位置,即前后的绘图状态。但是在编制显示列表时,我们不知道你将如何处理它们。
你想要的是一个知道如何定位文本的正确的文本布局引擎(如Pango)。此外,您不想使用显示列表(它们已经过时超过15年)并绘制文本字形想要使用类似 freetype-gl 的内容。
答案 1 :(得分:3)
datenwolf 100%正确。字符文字如&#39; \ t&#39;或者&#39; \ n&#39;尚未由OpenGL / GLUT开发人员实现。
OpenGL / GLUT中的字体往往是位图类型,通常是8x8到16x16像素的维度,并使用x和y坐标绘制到屏幕上,就像任何其他位图一样。
相反,您必须计算x y位置以模拟&#39; \ t&#39;或者&#39; \ n&#39;你自己的代码。
有用的链接http://www.codersource.net/2011/01/27/displaying-text-opengl-tutorial-5/
GLUT位图&amp; stroke font演示代码:
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <GL/glut.h>
void bitmap_output(int x, int y, char *string, void *font){
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
void stroke_output(GLfloat x, GLfloat y, char *format,...){
va_list args;
char buffer[200], *p;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(0.005, 0.005, 0.005);
for (p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
bitmap_output(40, 35, "This is written in a GLUT bitmap font.",
GLUT_BITMAP_TIMES_ROMAN_24);
bitmap_output(30, 210, "More bitmap text is a fixed 9 by 15 font.",
GLUT_BITMAP_9_BY_15);
bitmap_output(70, 240, " Helvetica is yet another bitmap font.",
GLUT_BITMAP_HELVETICA_18);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(40.0, 1.0, 0.1, 20.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(0.0, 0.0, 4.0, /* eye is at (0,0,30) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in postivie Y direction */
glPushMatrix();
glTranslatef(0, 0, -4);
glRotatef(50, 0, 1, 0);
stroke_output(-2.5, 1.1, " This is written in a");
stroke_output(-2.5, 0, " GLUT stroke font.");
stroke_output(-2.5, -1.1, "using 3D perspective.");
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();
}
void reshape(int w, int h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(465, 250);
glutCreateWindow("GLUT bitmap & stroke font example");
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0, 0, 0);
glLineWidth(3.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
starwars滚动条演示字体:
// *********************************************************************
// * o p e n g l / G L U T s t a r w a r s s c r o l l e r *
// *********************************************************************
#include <windows.h>
#include <string.h>
#include <GL\glut.h>
#include <iostream.h>
#define MAX_LINES_OF_STARWARS_QUOTES 32
#define MAX_CHARACTERS_IN_A_LINE 80
GLfloat UpwardsScrollVelocity = -10.0;
float view=20.0;
char starWarsQuotes[MAX_LINES_OF_STARWARS_QUOTES][MAX_CHARACTERS_IN_A_LINE]; // up to 32 lines of quotes 80 characters wide
int numberOfstarWarsQuotes=0,i,ic=0;
//*********************************************
//* glutIdleFunc(timeTick); *
//*********************************************
void timeTick(void){
if (UpwardsScrollVelocity< -600) view-=0.000011;
if(view < 0) {view=20; UpwardsScrollVelocity = -10.0;}
UpwardsScrollVelocity -= 0.015;
glutPostRedisplay();
}
//*********************************************
//* RenderToDisplay() *
//*********************************************
void RenderToDisplay(){
int l,lenghOfstarWarsQuotes, i;
glTranslatef(0.0, -100, UpwardsScrollVelocity);
glRotatef(-20, 1.0, 0.0, 0.0);
glScalef(0.1, 0.1, 0.1);
for( l=0;l<numberOfstarWarsQuotes;l++){
lenghOfstarWarsQuotes = (int)strlen(starWarsQuotes[l]);
glPushMatrix();
glTranslatef(-(lenghOfstarWarsQuotes*37), -(l*200), 0.0);
for (i = 0; i < lenghOfstarWarsQuotes; i++) {
glColor3f((UpwardsScrollVelocity/10)+300+(l*10),(UpwardsScrollVelocity/10)+300+(l*10),0.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, starWarsQuotes[l][i]);
}
glPopMatrix();
}
}
//*********************************************
//* glutDisplayFunc(myDisplayFunction); *
//*********************************************
void myDisplayFunction(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 30.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
RenderToDisplay();
glutSwapBuffers();
}
//*********************************************
//* glutReshapeFunc(reshape); *
//*********************************************
void reshape(int w, int h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1.0, 1.0, 3200);
glMatrixMode(GL_MODELVIEW);
}
//*********************************************
//* int main() *
//*********************************************
int main(){
strcpy(starWarsQuotes[0],"S t a r w a r s q u o t e s ");
strcpy(starWarsQuotes[1],"Character literals test : \t \n \r \0 ......");
strcpy(starWarsQuotes[2],"I’m Luke Skywalker, I’m here to rescue you.");
strcpy(starWarsQuotes[3],"Luke, I am your father!");
strcpy(starWarsQuotes[4],"Obi-Wan has taught you well");
strcpy(starWarsQuotes[5],"The force is strong with this one");
strcpy(starWarsQuotes[6],"I find your lack of faith disturbing");
strcpy(starWarsQuotes[7],"Great, kid. Don’t get cocky");
numberOfstarWarsQuotes=8;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 400);
glutCreateWindow("StarWars scroller");
glClearColor(0.0, 0.0, 0.0, 1.0);
glLineWidth(3);
glutDisplayFunc(myDisplayFunction);
glutReshapeFunc(reshape);
glutIdleFunc(timeTick);
glutMainLoop();
return 0;
}