以文本方式查看主题 - 计算机科学论坛 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- [推荐]NeHe OpenGL教程(中英文版附带VC++源码)Lesson 15-lesson 16 (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=53881) |
-- 作者:一分之千 -- 发布时间:10/16/2007 12:15:00 PM -- [推荐]NeHe OpenGL教程(中英文版附带VC++源码)Lesson 15-lesson 16
这一课,我们将在上一课的基础上创建带有纹理的字体,它真的很简单。 我们将使用第14课的代码来创作纹理字体的演示。如果程序中哪部分的代码有变化,我会重写那部分的所有代码以便看出我做的改动。 GLvoid BuildFont(GLvoid) // 创建位图字体 base = glGenLists(256); // 创建256个显示列表 SYMBOL_CHARSET, // 设置字符集 OUT_TT_PRECIS, // 输出精度 "Wingdings"); // 字体名称 SelectObject(hDC, font); // 选择创建的字体 wglUseFontOutlines( hDC, // 设置当前窗口设备描述表的句柄 0.1f, // 字体的光滑度,越小越光滑,0.0为最光滑的状态 0.2f, // 在z方向突出的距离 if (TextureImage[0]=LoadBMP("Data/Lights.bmp")) // 载入位图 GL_EYE_LINEAR - 纹理会固定在屏幕上。它永远不会移动。物体将被赋予处于它通过的地区的那一块纹理。 GL_OBJECT_LINEAR - 这种就是我们使用的模式。纹理被固定于在屏幕上运动的物体上。 // 设置纹理映射模式 int InitGL(GLvoid) // 此处开始对OpenGL进行所有设置 glShadeModel(GL_SMOOTH); // 启用阴影平滑 glEnable(GL_TEXTURE_2D); // 使用二维纹理 // 设置字体的位置 glRotatef(rot,1.0f,0.0f,0.0f); // 沿X轴旋转 glTranslatef(-0.35f,-0.35f,0.1f); // 移动到可以显示的位置 glPrint("N"); // 绘制海盗旗符号
|
-- 作者:一分之千 -- 发布时间:10/16/2007 12:15:00 PM -- Lesson 15 After posting the last two tutorials on bitmap and outlined fonts, I received quite a few emails from people wondering how they could texture map the fonts. You can use autotexture coordinate generation. This will generate texture coordinates for each of the polygons on the font. A small note, this code is Windows specific. It uses the wgl functions of Windows to build the font. Apparently Apple has agl support that should do the same thing, and X has glx. Unfortunately I can't guarantee this code is portable. If anyone has platform independant code to draw fonts to the screen, send it my way and I'll write another font tutorial. We'll build our Texture Font demo using the code from lesson 14. If any of the code has changed in a particular section of the program, I'll rewrite the entire section of code so that it's easier to see the changes that I have made. The following section of code is similar to the code in lesson 14, but this time we're not going to include the stdarg.h file. #include <windows.h> // Header File For Windows HDC hDC=NULL; // Private GDI Device Context bool keys[256]; // Array Used For The Keyboard Routine GLuint texture[1]; // One Texture Map ( NEW ) GLfloat rot; // Used To Rotate The Text LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc A few of you were wondering how to use the wingdings font, which is another reason I'm not using a standard font. Wingdings is a SYMBOL font, and requires a few changes to make it work. It's not as easy as telling Windows to use the wingdings font. If you change the font name to wingdings, you'll notice that the font doesn't get selected. You have to tell Windows that the font is a symbol font and not a standard character font. More on this later. GLvoid BuildFont(GLvoid) // Build Our Bitmap Font base = glGenLists(256); // Storage For 256 Characters SYMBOL_CHARSET, // Character Set Identifier ( Modified ) OUT_TT_PRECIS, // Output Precision "Wingdings"); // Font Name ( Modified ) SelectObject(hDC, font); // Selects The Font We Created wglUseFontOutlines( hDC, // Select The Current DC 0.1f, // Deviation From The True Outlines 0.2f, // Font Thickness In The Z Direction I'm creating a mipmapped texture only because it looks better. The name of the texture is lights.bmp. AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image if (!Filename) // Make Sure A Filename Was Given File=fopen(Filename,"r"); // Check To See If The File Exists if (File) // Does The File Exist? return NULL; // If Load Failed Return NULL int LoadGLTextures() // Load Bitmaps And Convert To Textures AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL if (TextureImage[0]=LoadBMP("Data/Lights.bmp")) // Load The Bitmap glGenTextures(1, &texture[0]); // Create The Texture // Build Linear Mipmapped Texture GL_TEXTURE_GEN_MODE lets us select the mode of texture mapping we want to use on the S and T texture coordinates. You have 3 choices: GL_EYE_LINEAR - The texture is fixed to the screen. It never moves. The object is mapped with whatever section of the texture it is passing over. GL_OBJECT_LINEAR - This is the mode we are using. The texture is fixed to the object moving around the screen. GL_SPHERE_MAP - Everyones favorite. Creates a metalic reflective type object. It's important to note that I'm leaving out alot of code. We should be setting the GL_OBJECT_PLANE as well, but by default it's set to the parameters we want. Buy a good book if you're interested in learning more, or check out the MSDN help CD / DVD. // Texturing Contour Anchored To The Object if (TextureImage[0]) // If Texture Exists free(TextureImage[0]); // Free The Image Structure return Status; // Return The Status int InitGL(GLvoid) // All Setup For OpenGL Goes Here glShadeModel(GL_SMOOTH); // Enable Smooth Shading glEnable(GL_TEXTURE_2D); // Enable Texture Mapping int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing // Position The Text glRotatef(rot,1.0f,0.0f,0.0f); // Rotate On The X Axis glTranslatef(-0.35f,-0.35f,0.1f); // Center On X, Y, Z Axis glPrint("N"); // Draw A Skull And Crossbones Symbol if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class KillFont(); // Destroy The Font Jeff Molofee (NeHe) |
-- 作者:一分之千 -- 发布时间:10/16/2007 12:16:00 PM -- 第十六课 这一课是基于第7课的代码的,你将学会三种不同的雾的计算方法,以及怎样设置雾的颜色和雾的范围。 Data Setup: 我们将从设置保存雾的信息的变量开始。变量fogMode用来保存三种类型的雾:GL_EXP,GL_EXP2和GL_LINEAR。我将在稍后解释这三种类型的区别。这些变量位于程序开头GLuint texture[3]这行后。变量fogfilter将用来表示我们使用的是哪种雾类型。变量fogColor保存雾的颜色。在程序的顶部我还加了一个布尔类型的变量gp用来记录'g'键是否被按下。 bool gp; // G健是否按下 glClearColor(0.5f,0.5f,0.5f,1.0f); // 设置背景的颜色为雾气的颜色 glFogi(GL_FOG_MODE, fogMode[fogfilter]); // 设置雾气的模式 第二行glFogi(GL_FOG_MODE, fogMode[fogfilter]);建立雾的过滤模式。之前我们声明了数组fogMode,它保存了值GL_EXP, GL_EXP2, and GL_LINEAR。现在是使用他们的时候了。我来解释它们(具体见红皮书,其实这是计算雾效混合因子的三种方式): 第三行glFogfv(GL_FOG_COLOR, fogcolor);设置雾的颜色。之前我们已将变量fogcolor设为(0.5f,0.5f,0.5f,1.0f),这是一个很棒的灰色。 接下来我们看看最后的四行。glFogf(GL_FOG_DENSITY, 0.35f);这行设置雾的密度。增加数字会让雾更密,减少它则雾更稀。 glHint (GL_FOG_HINT, GL_DONT_CARE); 设置修正。我使用了GL_DONT_CARE因为我不关心它的值。 Eric Desrosiers Adds:关于glHint(GL_FOG_HINT, hintval);的解释 gl_dont_care - 由OpenGL决定使用何种雾效(对每个顶点还是每个像素执行雾效计算)和一个未知的公式(?) 下一行glFogf(GL_FOG_START, 1.0f);设定雾效距屏幕多近开始。你可以根据你的需要随意改变这个值。下一行类似,glFogf(GL_FOG_END, 5.0f);告诉OpenGL程序雾效持续到距屏幕多远。 现在我们建立了绘制雾的代码,我们将加入键盘指令在不同的雾效模式间循环。这段代码及其它的按键处理代码在程序的最后。 if (keys['G'] && !gp) // G键是否 按下 |
-- 作者:一分之千 -- 发布时间:10/16/2007 12:17:00 PM -- Lesson 16 This tutorial brought to you by Chris Aliotta... So you want to add fog to your OpenGL program? Well in this tutorial I will show you how to do exactly that. This is my first time writing a tutorial, and I am still relatively new to OpenGL/C++ programming, so please, if you find anything that's wrong let me know and don't jump all over me. This code is based on the code from lesson 7. Data Setup: We'll start by setting up all our variables needed to hold the information for fog. The variable fogMode will be used to hold three types of fog: GL_EXP, GL_EXP2, and GL_LINEAR. I will explain the differences between these three later on. The variables will start at the beginning of the code after the line GLuint texture[3]. The variable fogfilter will be used to keep track of which fog type we will be using. The variable fogColor will hold the color we wish the fog to be. I have also added the boolean variable gp at the top of the code so we can tell if the 'g' key is being pressed later on in this tutorial. bool gp; // G Pressed? ( New ) Now that we have established our variables we will move down to InitGL. The glClearColor() line has been modified to clear the screen to the same same color as the fog for a better effect. There isn't much code involved to make fog work. In all you will find this to be very simplistic. glClearColor(0.5f,0.5f,0.5f,1.0f); // We'll Clear To The Color Of The Fog ( Modified ) glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode The second line, glFogi(GL_FOG_MODE, fogMode[fogfilter]); establishes the fog filter mode. Now earlier we declared the array fogMode. It held GL_EXP, GL_EXP2, and GL_LINEAR. Here is when these variables come into play. Let me explain each one: Next lets look at the last four lines of this code. The line glFogf(GL_FOG_DENSITY, 0.35f); establishes how dense the fog will be. Increase the number and the fog becomes more dense, decrease it and it becomes less dense. The line glHint (GL_FOG_HINT, GL_DONT_CARE); establishes the hint. I used GL_DONT_CARE, because I didn't care about the hint value. Eric Desrosiers Adds: Little explanation of glHint(GL_FOG_HINT, hintval); hintval can be : GL_DONT_CARE, GL_NICEST or GL_FASTEST gl_dont_care - Lets opengl choose the kind of fog (per vertex of per pixel) and an unknown formula. The next line glFogf(GL_FOG_START, 1.0f); will establish how close to the screen the fog should start. You can change the number to whatever you want depending on where you want the fog to start. The next line is similar, glFogf(GL_FOG_END, 5.0f);. This tells the OpenGL program how far into the screen the fog should go. Keypress Events Now that we've setup the fog drawing code we will add the keyboard commands to cycle through the different fog modes. This code goes down at the end of the program with all the other key handling code. if (keys['G'] && !gp) // Is The G Key Being Pressed? Christopher Aliotta (Precursor) |
-- 作者:hwfchina -- 发布时间:3/18/2008 10:23:00 PM -- 顶~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
-- 作者:ririyeye -- 发布时间:1/8/2010 10:36:00 AM -- 初次学习必须看 |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
109.375ms |