游戏开发论坛

 找回密码
 立即注册
搜索
楼主: xjj210130

如何在图形上加载数据(含有图形)

[复制链接]

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-7-12 20:32:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

如果是3D空间
根据各个标签的位置
转换到屏幕位置
然后显示对应的数据就好了

这就像是游戏中头顶上顶的人名一样

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-14 10:00:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

使用如下的代码:
/*
* OpenGL Font Support Functions from Chapter 7.
*
* Written by Michael Sweet.
*/

/*
* Include necessary headers.
*/

#include "font.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


/*
* Limits...
*/

#define MAX_STRING        1024


/*
* 'FontCreate()' - Load Windows font bitmaps into OpenGL display lists.
*/

GLFONT *                         /* O - Font data */
FontCreate(HDC        hdc,       /* I - Device Context */
           const char *typeface, /* I - Font specification */
           int        height,    /* I - Font height/size in pixels */
           int        weight,    /* I - Weight of font (bold, etc) */
           DWORD      italic)    /* I - Text is italic */
    {
    GLFONT *font;                /* Font data pointer */
    HFONT  fontid;               /* Windows font ID */
    int    charset;              /* Character set code */

    /* Allocate memory */
    if ((font = calloc(1, sizeof(GLFONT))) == (GLFONT *)0)
        return ((GLFONT *)0);

    /* Allocate display lists */
    if ((font->base = glGenLists(256)) == 0)
        {
        free(font);
        return (0);
        }

    /* Select a character set */
    if (stricmp(typeface, "symbol") == 0)
        charset = SYMBOL_CHARSET;
    else
        charset = ANSI_CHARSET;

    /* Load the font */
    fontid = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,
                        charset, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
                        DRAFT_QUALITY, DEFAULT_PITCH, typeface);

    SelectObject(hdc, fontid);

    /* Create bitmaps for each character */
    wglUseFontBitmaps(hdc, 0, 256, font->base);

    /* Get the width and height information for each character */
    GetCharWidth(hdc, 0, 255, font->widths);
    font->height = height;

    return (font);
    }

/*
* 'FontDestroy()' - Delete the specified font.
*/

void
FontDelete(GLFONT *font) /* I - Font to delete */
    {
    if (font == (GLFONT *)0)
        return;

    glDeleteLists(font->base, 256);
    free(font);
    }


/*
* 'FontPuts()' - Display a string using the specified font.
*/

void
FontPuts(GLFONT     *font, /* I - Font to use */
         const char *s)    /* I - String to display */
    {
    if (font == (GLFONT *)0 || s == NULL)
        return;

    glPushAttrib(GL_LIST_BIT);
    glListBase(font->base);
    glCallLists(strlen(s), GL_UNSIGNED_BYTE, s);
    glPopAttrib();
    }


/*
* 'FontPrintf()' - Display a formatted string using the specified font.
*/

void
FontPrintf(GLFONT     *font,   /* I - Font to use */
           int        align,   /* I - Alignment to use */
           const char *format, /* I - printf() style format string */
           ...)                /* I - Other arguments as necessary */
    {
    va_list       ap;          /* Argument pointer */
    unsigned char s[1024],     /* Output string */
                  *ptr;        /* Pointer into string */
    int           width;       /* Width of string in pixels */

    if (font == (GLFONT *)0 || format == (char *)0)
        return;

    /* Format the string */
    va_start(ap, format);
    vsprintf((char *)s, format, ap);
    va_end(ap);

    /* Figure out the width of the string in pixels... */
    for (ptr = s, width = 0; *ptr; ptr ++)
        width += font->widths[*ptr];

    /* Adjust the bitmap position as needed to justify the text */
    if (align < 0)
        glBitmap(0, 0, 0, 0, -width, 0, NULL);
    else if (align == 0)
        glBitmap(0, 0, 0, 0, -width / 2, 0, NULL);

    /* Draw the string */
    FontPuts(font, s);
    }

/*
* OpenGL Font Support Definitions from Chapter 7.
*
* Written by Michael Sweet.
*/

#ifndef _FONT_H_
#  define _FONT_H_

/*
* Include necessary headers.
*/

#  include <windows.h>
#  include <GL/gl.h>


/*
* Make this header file work with C and C++ source code...
*/

#  ifdef __cplusplus
extern "C" {
#  endif /* __cplusplus */


/*
* Font data structure...
*/

typedef struct
    {
    GLuint base;        /* Display list number of first character */
    int    widths[256]; /* Width of each character in pixels */
    int    height;      /* Height of characters */
    } GLFONT;


/*
* Prototypes...
*/

extern GLFONT        *FontCreate(HDC hdc, const char *typeface, int height,
                            int weight, DWORD italic);
extern void        FontDestroy(GLFONT *font);
extern void        FontPrintf(GLFONT *font, int align, const char *format, ...);
extern void        FontPuts(GLFONT *font, const char *s);

#  ifdef __cplusplus
}
#  endif /* __cplusplus */
#endif /* !_FONT_H_ */


/*
* Functions...
*/
#include "gl/glut.h"
#include "font.h"
float Width,Height;
GLFONT *Font;  /* Font data */


void Redraw(void);
void Resize(int width, int height);


/*
* 'main()' - Open a window and display some text.
*/

int                /* O - Exit status */
main(int  argc,    /* I - Number of command-line arguments */
     char *argv[]) /* I - Command-line arguments */
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(792, 573);
    glutCreateWindow("Bitmap Font Example");
    glutReshapeFunc(Resize);
    glutDisplayFunc(Redraw);
    Font = FontCreate(wglGetCurrentDC(), "Times", 32, 0, 1);
    glutMainLoop();
    return (0);
    }


/*
* 'Redraw()' - Redraw the window...
*/

void
Redraw(void)
    {
    /* Clear the window to black */
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    /* Draw a vertical line down the center of the window */
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f(10.,0., 0.);
    glVertex3f(100., 0.,100.);
    glEnd();

    /* Draw text left justified... */
    glColor3f(1.0, 0.0, 0.0);
    glRasterPos3f(10.0, 0.0,10.0);
    FontPrintf(Font, 1, "Left Justified Text");

   
    /* Draw right-justified text... */
    glColor3f(0.0, 0.1, 1.0);
    glRasterPos3f(0.0, 0.0,10.0);
    FontPrintf(Font, -1, "Right Justified Text");

    glFinish();
    }


/*
* 'Resize()' - Resize the window...
*/

void
Resize(int width,  /* I - Width of window */
       int height) /* I - Height of window */
    {
    /* Save the new width and height */
    Width  = width;
    Height = height;

    /* Reset the viewport... */
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, (GLfloat)width, 0.0, (GLfloat)height, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    }

用于输出,只能输出2维的,不知道三维的怎样输出

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-15 11:37:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

哎,看来是要被训啊

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-15 11:54:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

哎,看来是要被训啊

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-16 10:55:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

不要沉哦

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-18 11:37:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

??

32

主题

1259

帖子

1351

积分

金牌会员

Rank: 6Rank: 6

积分
1351
发表于 2006-7-18 19:47:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

你还不如直接问,如何在刻度上写上文字。

glProject()

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-19 10:58:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

也是没有人回答啊

1

主题

12

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2006-7-24 16:33:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

不知道对不对,可能是比较笨的做法.
每个刻度都有三维坐标吧,把三维坐标转换到屏幕坐标,然后用glRasterPos()和glutBitmapCharacter(),就行了.

7

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
 楼主| 发表于 2006-7-24 19:06:00 | 显示全部楼层

Re:如何在图形上加载数据(含有图形)

哈哈多谢ursular的回答,你这种方法我试过了,对于小型的数据处理可以做得还可以的,大型数据就很难啊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 01:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表