|
|

楼主 |
发表于 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维的,不知道三维的怎样输出 |
|