SharpGL Study Notes (17) three-dimensional text and flat text,

Source: Internet
Author: User

SharpGL Study Notes (17) three-dimensional text and flat text,

 

Before writing the topic about the text, I read the relevant chapters in several books and studied the issues for a few days.

Xu Mingliang's OpenGL game programming book introduces "bitmap fonts", that is, raster the fonts and draw them out. According to the VC code in the book, it was translated as C #. It suddenly got stuck in a windows API, wglUseFontBitmpas (). This function is required to input a DC, which is terrible and I don't know how to do it.

In another book, the sections related to text are similar to the Paster method, loading text texture images. Because its code uses a game framework, I need to study how the entire framework works, it is not easy to understand after a try, so that two days of work will be spent.

Today, I suddenly thought of reading the Demo of SharpGL, And suddenly found that the text output was so simple that I could not understand how complicated the above two books were!

What's going on? Is it because the new version of OpenGL functions provide support for text? Or is SharpGL's unique support?

I feel like the following method is enough to deal with all text-related applications, so I will save some energy and skip the text chapter in the book!

 

However, unfortunately, the following code does not support Chinese characters, which may be a problem. I will continue to write this post if I know how to develop Chinese characters later.

 

Text DEMO code:

 

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using SharpGL;10 using System.Runtime.InteropServices;11 12 namespace SharpGLWinformsApplication113 {14     //15     public partial class SharpGLForm : Form16     {17         private float rotation = 0.0f;18        19         public SharpGLForm()20         {21             InitializeComponent();22         }23 24         private void openGLControl_OpenGLDraw(object sender, PaintEventArgs e)25         {26             OpenGL gl = openGLControl.OpenGL;27             gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);28             gl.LoadIdentity();29 30             gl.Translate(-1f, 0.0f,0f);31             gl.Rotate(rotation, 1f, 0f, 0.0f);32 33             gl.DrawText3D("Arial",32, 0, 0.3f, "Hello!");34 35             rotation -= 3.0f;36 37             gl.DrawText(20, 20, 1,38                 1, 1, "Courier New", 12,39                 "this is a test text.");40             gl.Flush();41         }42 43         private void openGLControl_OpenGLInitialized(object sender, EventArgs e)44         {45             OpenGL gl = openGLControl.OpenGL;46 47             float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };48             float[] light0pos = new float[] { 0.0f, 0.0f, 10.0f, 1.0f };49             float[] light0ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };50             float[] light0diffuse = new float[] { 0.9f, 0.9f, 0.3f, 1.0f };51             float[] light0specular = new float[] { 0.8f, 0.8f, 0.8f, 1.0f };52 53             float[] lmodel_ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };54             gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);55 56             gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient);57             gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos);58             gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient);59             gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse);60             gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular);61             gl.Enable(OpenGL.GL_LIGHTING);62             gl.Enable(OpenGL.GL_LIGHT0);63 64             gl.ShadeModel(OpenGL.GL_SMOOTH);        65             gl.ClearColor(0, 0, 0, 0);66         }67 68         private void openGLControl_Resized(object sender, EventArgs e)69         {70             OpenGL gl = openGLControl.OpenGL;71             gl.MatrixMode(OpenGL.GL_PROJECTION);72             gl.LoadIdentity();73             gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 100.0);74             gl.LookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);75             gl.MatrixMode(OpenGL.GL_MODELVIEW);76         }77 78 79     }80 81 }

 

The effect is as follows:

Three-dimensional text is "Hello !", The flat text is "this is a test text"

Put gl. DrawText3D ("Arial", 32, 0, 0.3f, "Hello! "); In the 0.3 to 0, you can get the depth of the text is 0, equivalent to a thin film.

The flat text is used to output the prompt text, which cannot be rotated and is output with pixels as xy.

 

 

 

The example is very simple, which surprised me to end the text topic so quickly.

 

Download the source code in this section

 

 

Original article, from "blog garden, pig weneng's blog": http://www.cnblogs.com/hackpig/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.