发信人: yxy (田丝丝#冬眠的小肥鹰), 信区: GAME_Designer 
标  题: OPENGL(12) 
发信站: BBS 水木清华站 (Sun Feb 15 15:41:17 1998) 
 
此次讲解OPENGL复杂建模方式,将分几个部分完成,这篇先介绍图原扩展: 
如何利用专用函数精确绘制平面图形。下次会讲解如何利用法向量生成曲面。 
1.点和线 
void glPointSize(GLfloat size); 
设置点的宽度,size必须>0,缺省1 
 
void glLineWidth(GLfoat width); 
设置线宽,width>0,缺省为1 
 
void glLineStipple(GLint factor,GLushort pattern); 
设置线的模式,factor用于对模式进行拉伸的比例因子,pattern是线的模式 
例如11001100是虚线(1绘制,0不绘制) 
 
必须要启用glEnable(GL_LINE_STIPPLE)才能使用以上函数,不再使用时调用 
glDisable(GL_LINE_STIPPLE)关闭,这与以前的glEnable();glDisable();的 
用法都是类似的。请看下面例子: 
/////////////////////////////////////////////////////////////////// 
//sample.cpp 
#include "glos.h" 
#include <GL/gl.h> 
#include <GL/glaux.h> 
#include "windows.h" 
void myinit(void); 
void CALLBACK  display(void); 
void CALLBACK  reshape(GLsizei w,GLsizei h); 
 
 
void myinit(void) 

        auxInitDisplayMode(AUX_SINGLE|AUX_RGBA); 
        auxInitPosition(0,0,600,500); 
        auxInitWindow("sample1"); 
        glClearColor(0.0,0.0,0.0,0.0); 
        glClear(GL_COLOR_BUFFER_BIT); 
 
        glShadeModel(GL_FLAT); 

/* 
void CALLBACK reshape(GLsizei w,GLsizei h) 

 
glViewport(0,0,w,h); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
 
if(w<=h) 
 glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w, 
         4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0); 
else 
  glOrtho(-4.0*(GLfloat)h/(GLfloat)w, 
         4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0,-4.0,4.0); 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

*/ 
 
//自定义的绘制直线的函数,参数为起始点和终止点坐标 
void line2i(GLint x1,GLint y1,GLint x2,GLint y2) 

glBegin(GL_LINES); 
glVertex2f(x1,y1); 
glVertex2f(x2,y2); 
glEnd(); 

 
void CALLBACK display(void) 

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
 
//首先绘制一系列点,点的大小不断增加 
  int i; 
  glColor3f(0.8,0.6,0.4); 
  for(i=1;i<=10;i++) 
  { 
          glPointSize(i*2); 
          glBegin(GL_POINTS); 
          glVertex2f(30.0+((GLfloat)i*50.0),330.0); 
          glEnd(); 
  } 
//再绘制两条虚线,第二条比第一条松散一些,由pattern参数即可看出  
  glEnable(GL_LINE_STIPPLE); 
  glLineStipple(1,0x0101);//间隔1位 
  glColor3f(1.0,0.0,0.0); 
  line2i(20,250,250,250); 
  glLineStipple(1,0x00ff);//间隔2位 
  glColor3f(0.0,0.0,1.0); 
  line2i(300,250,550,250); 
 
//改变线的绘制宽度的效果--加宽 
//重新画出上面两条虚线 
  glLineWidth(5.0); 
  glEnable(GL_LINE_STIPPLE); 
  glLineStipple(1,0x0101); 
  glColor3f(1.0,0.0,0.0); 
  line2i(50,150,250,150); 
  glLineStipple(1,0x00ff); 
  glColor3f(0.0,0.0,1.0); 
  line2i(300,150,550,150); 
 
  glFlush(); 

void main(void) 

        myinit(); 
     
//    auxReshapeFunc(reshape); 
    auxMainLoop(display); 

//end of sample 
////////////////////////////////////////////// 
 
2.多边形 
void glPolygonMode(GLenum face,GLenum mode); 
控制多边形指定面的绘图模式, 
face为:GL_FRONT GL_BACK或GL_FRONT_AND BACK 
mode为:GL_POINT GL_LINE或GL_FILL表示多边型的轮廓点、轮廓线和填充模式 
的绘制方式。缺省是填充方式。 
 
void glPolygonStipple(const GLubyte *mask); 
其中mask必须是指向32*32的位图指针,1是绘制、0不绘制 
 
使用上述函数也要调用: 
glEnable(GL_POLYGON-STIPPLE); 
glDisable(GL_POLYGON_STIPPLE); 
请看下面例子: 
///////////////////////////////////////////// 
//sample.cpp 
#include "glos.h" 
#include <GL/gl.h> 
#include <GL/glaux.h> 
#include "windows.h" 
void myinit(void); 
void CALLBACK  display(void); 
void CALLBACK  reshape(GLsizei w,GLsizei h); 
 
//定义填充模式32*32点阵 
GLubyte pattern[]={ 
        0x00,0x01,0x80,0x00, 
        0x00,0x03,0xc0,0x00, 
        0x00,0x07,0xe0,0x00, 
        0x00,0x0f,0xf0,0x00, 
        0x00,0x1f,0xf8,0x00, 
        0x00,0x3f,0xfc,0x00, 
        0x00,0x7f,0xfe,0x00, 
        0x00,0xff,0xff,0x00, 
        0x01,0xff,0xff,0x80, 
        0x03,0xff,0xff,0xc0, 
        0x07,0xff,0xff,0xe0, 
        0x0f,0xff,0xff,0xf0, 
        0x1f,0xff,0xff,0xf8, 
        0x3f,0xff,0xff,0xfc, 
        0x7f,0xff,0xff,0xfe, 
        0xff,0xff,0xff,0xff, 
     
        0xff,0xff,0xff,0xff, 
        0x7f,0xff,0xff,0xfe, 
        0x3f,0xff,0xff,0xfc, 
        0x1f,0xff,0xff,0xf8, 
        0x0f,0xff,0xff,0xf0, 
        0x07,0xff,0xff,0xe0, 
        0x03,0xff,0xff,0xc0, 
        0x01,0xff,0xff,0x80, 
        0x00,0xff,0xff,0x00, 
        0x00,0x7f,0xfe,0x00, 
        0x00,0x3f,0xfc,0x00, 
        0x00,0x1f,0xf8,0x00, 
        0x00,0x0f,0xf0,0x00, 
        0x00,0x07,0xe0,0x00, 
        0x00,0x03,0xc0,0x00, 
        0x00,0x01,0x80,0x00 
}; 
 
void myinit(void) 

    auxInitDisplayMode(AUX_SINGLE|AUX_RGBA); 
        auxInitPosition(0,0,400,400); 
        auxInitWindow("sample1"); 
        glClearColor(0.0,0.0,0.0,0.0); 
        glClear(GL_COLOR_BUFFER_BIT); 
 
        glShadeModel(GL_FLAT); 

/* 
void CALLBACK reshape(GLsizei w,GLsizei h) 

 
glViewport(0,0,w,h); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
 
if(w<=h) 
 glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w, 
         4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0); 
else 
  glOrtho(-4.0*(GLfloat)h/(GLfloat)w, 
         4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0,-4.0,4.0); 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

*/ 
 
void CALLBACK display(void) 

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
//选用兰色作为填充色 
  glColor3f(0.0,0.0,1.0); 
//启用多边形绘制模式 
  glEnable(GL_POLYGON_STIPPLE);   
//利用定义好的填充模式绘制多边形 
  glPolygonStipple(pattern); 
//绘制长方形 
  glRectf(48.0,80.0,210.0,305.0); 
 
  glFlush(); 

void main(void) 

        myinit(); 
     
//    auxReshapeFunc(reshape); 
    auxMainLoop(display); 

//end of sample 
例子中的运行结果是给出一个表面有定义图样的长方形 
 
-- 
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.74.90]