¼¸ºÎ³ß´çÓ빫²îÂÛ̳

 ÕÒ»ØÃÜÂë
 ×¢²á
²é¿´: 2552|»Ø¸´: 1

¡¾×ªÌû¡¿OpenGLÈëÃÅѧϰ£¨Èý£©

[¸´ÖÆÁ´½Ó]
·¢±íÓÚ 2009-1-20 19:43:34 | ÏÔʾȫ²¿Â¥²ã |ÔĶÁģʽ
µÚÒ»¿Î£¬±àдµÚÒ»¸öOpenGL³ÌÐò
µÚ¶þ¿Î£¬»æÖƼ¸ºÎͼÐÎ
µÚÈý¿Î£¬»æÖƼ¸ºÎͼÐεÄһЩϸ½ÚÎÊÌâ ¡ª¡ª¡ú ±¾´Î¿Î³ÌµÄÄÚÈÝ

ÔÚµÚ¶þ¿ÎÖУ¬ÎÒÃÇѧϰÁËÈçºÎ»æÖƼ¸ºÎͼÐΣ¬µ«´ó¼ÒÈç¹û¶àд¼¸¸ö³ÌÐò£¬¾Í»á·¢ÏÖÆäʵ»¹ÊÇÓÐЩÓôÃÆÖ®´¦¡£ÀýÈ磺µã̫С£¬ÄÑÒÔ¿´Çå³þ£»Ö±ÏßҲ̫ϸ£¬²»Êæ·þ£»»òÕßÏë»­ÐéÏߣ¬µ«²»ÖªµÀ·½·¨Ö»ÄÜÓÃÐí¶à¶ÌÖ±Ïߣ¬ÉõÖÁÓõã×éºÏ¶ø³É¡£
ÕâЩÎÊÌ⽫ÔÚ±¾¿ÎÖб»½â¾ö¡£

ÏÂÃæ¾Íµã¡¢Ö±Ïß¡¢¶à±ßÐηֱðÌÖÂÛ¡£

==========================================

1¡¢¹ØÓÚµã
µãµÄ´óСĬÈÏΪ1¸öÏñËØ£¬µ«Ò²¿ÉÒԸıäÖ®¡£¸Ä±äµÄÃüÁîΪglPointSize£¬Æ亯ÊýÔ­ÐÍÈçÏ£º
void glPointSize(GLfloat size);
size±ØÐë´óÓÚ0.0f£¬Ä¬ÈÏֵΪ1.0f£¬µ¥Î»Îª¡°ÏñËØ¡±¡£
Çë×ÔÐбàд³ÌÐò²¢¹Û²ìЧ¹û¡£

2¡¢¹ØÓÚÖ±Ïß
£¨1£©Ö±Ïß¿ÉÒÔÖ¸¶¨¿í¶È£º
void glLineWidth(GLfloat width);
ÆäÓ÷¨¸úglPointSizeÀàËÆ¡£
£¨2£©»­ÐéÏß¡£
Ê×ÏÈ£¬Ê¹ÓÃglEnable(GL_LINE_STIPPLE);À´Æô¶¯ÐéÏßģʽ£¨Ê¹ÓÃglDisable(GL_LINE_STIPPLE)¿ÉÒԹرÕÖ®£©¡£
È»ºó£¬Ê¹ÓÃglLineStippleÀ´ÉèÖÃÐéÏßµÄÑùʽ¡£
void glLineStipple(GLint factor, GLushort pattern);
patternÊÇÓÉ1ºÍ0×é³ÉµÄ³¤¶ÈΪ16µÄÐòÁУ¬´Ó×îµÍλ¿ªÊ¼¿´£¬Èç¹ûΪ1£¬ÔòÖ±ÏßÉϽÓÏÂÀ´Ó¦¸Ã»­µÄfactor¸öµã½«±»»­ÎªÊµµÄ£»Èç¹ûΪ0£¬ÔòÖ±ÏßÉϽÓÏÂÀ´Ó¦¸Ã»­µÄfactor¸öµã½«±»»­ÎªÐéµÄ¡£
ÒÔÏÂÊÇһЩÀý×Ó£º
screen.width-255) {this.width=screen.width-255;this.alt='±¾Í¼Æ¬Òѱ»ËõС£¬µã»÷²é¿´Ô­´óСͼƬ¡£';}" border=0>screen.width-255) {this.width=screen.width-255;this.alt='±¾Í¼Æ¬Òѱ»ËõС£¬µã»÷²é¿´Ô­´óСͼƬ¡£';}" border=0>
ÉùÃ÷£º¸ÃͼƬÀ´×Ôwww.opengl.org£¬¸ÃͼƬÊÇ¡¶OpenGL ... »Óд¥¼°µ½°æȨÎÊÌâ¡£
Çë×ÔÐбàд³ÌÐò²¢¹Û²ìЧ¹û¡£

==========================================

3¡¢¹ØÓÚ¶à±ßÐÎ
¶à±ßÐεÄÄÚÈݽ϶࣬ÎÒÃǽ«½²ÊöÒÔÏÂËĸö·½Ãæ¡£
£¨1£©¶à±ßÐεÄÁ½ÃæÒÔ¼°»æÖÆ·½Ê½¡£
ËäÈ»ÎÒÃÇÄ¿Ç°»¹Ã»ÓÐÕæÕýµÄʹÓÃÈýά×ø±êÀ´»­Í¼£¬µ«Êǽ¨Á¢Ò»Ð©ÈýάµÄ¸ÅÄÊDZØÒªµÄ¡£
´ÓÈýάµÄ½Ç¶ÈÀ´¿´£¬Ò»¸ö¶à±ßÐξßÓÐÁ½¸öÃ档ÿһ¸öÃ涼¿ÉÒÔÉèÖò»Í¬µÄ»æÖÆ·½Ê½£ºÌî³ä¡¢Ö»»æÖƱßÔµÂÖÀªÏß¡¢Ö»»æÖƶ¥µã£¬ÆäÖС°Ìî³ä¡±ÊÇĬÈϵķ½Ê½¡£¿ÉÒÔΪÁ½¸öÃæ·Ö±ðÉèÖò»Í¬µÄ·½Ê½¡£
glPolygonMode(GL_FRONT, GL_FILL); // ÉèÖÃÕýÃæΪÌî³ä·½Ê½
glPolygonMode(GL_BACK, GL_LINE); // ÉèÖ÷´ÃæΪ±ßÔµ»æÖÆ·½Ê½
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); // ÉèÖÃÁ½Ãæ¾ùΪ¶¥µã»æÖÆ·½Ê½

==========================================

£¨2£©·´×ª
Ò»°ãÔ¼¶¨Îª¡°¶¥µãÒÔÄæʱÕë˳Ðò³öÏÖÔÚÆÁÄ»ÉϵÄÃ桱Ϊ¡°ÕýÃ桱£¬ÁíÒ»¸öÃæ¼´³ÉΪ¡°·´Ã桱¡£Éú»îÖг£¼ûµÄÎïÌå±íÃ棬ͨ³£¶¼¿ÉÒÔÓÃÕâÑùµÄ¡°ÕýÃ桱ºÍ¡°·´Ã桱£¬¡°ºÏÀíµÄ¡±±»±íÏÖ³öÀ´£¨ÇëÕÒÒ»¸ö±È½Ï͸Ã÷µÄ¿óȪˮƿ×Ó£¬ÔÚÕý¶ÔÄãµÄÒ»ÃæÑØÄæʱÕë»­Ò»¸öÔ²£¬²¢±êÃ÷»­µÄ·½Ïò£¬È»ºó½«±³ÃæתΪÕýÃ棬»­Ò»¸öÀàËƵÄÔ²£¬Ìå»áһϡ°ÕýÃ桱ºÍ¡°·´Ã桱¡£Äã»á·¢ÏÖÕý¶ÔÄãµÄ·½Ïò£¬Æ¿µÄÍâ²àÊÇÕýÃ棬¶ø±³¶ÔÄãµÄ·½Ïò£¬Æ¿µÄÄÚ²à²ÅÊÇÕýÃæ¡£Õý¶ÔÄãµÄÄÚ²àºÍ±³¶ÔÄãµÄÍâ²àÔòÊÇ·´Ãæ¡£ÕâÑùÒ»À´£¬Í¬ÑùÊôÓÚ¡°Æ¿µÄÍâ²à¡±Õâ¸ö±íÃ棬µ«Ä³Ð©µØ·½ËãÊÇÕýÃ棬ijЩµØ·½È´ËãÊÇ·´ÃæÁË£©¡£
µ«Ò²ÓÐһЩ±íÃæ±È½ÏÌØÊâ¡£ÀýÈç¡°Âó±ÈÎÚ˹´ø¡±£¨Çë×Ô¼ºGoogleһϣ©£¬¿ÉÒÔÈ«²¿Ê¹Óá°ÕýÃ桱»òÈ«²¿Ê¹Óá°±³Ã桱À´±íʾ¡£
¿ÉÒÔͨ¹ýglFrontFaceº¯ÊýÀ´½»»»¡°ÕýÃ桱ºÍ¡°·´Ã桱µÄ¸ÅÄî¡£
glFrontFace(GL_CCW); // ÉèÖÃCCW·½ÏòΪ¡°ÕýÃ桱£¬CCW¼´CounterClockWise£¬ÄæʱÕë
glFrontFace(GL_CW); // ÉèÖÃCW·½ÏòΪ¡°ÕýÃ桱£¬CW¼´ClockWise£¬Ë³Ê±Õë
ÏÂÃæÊÇÒ»¸öʾÀý³ÌÐò£¬ÇëÓÃËüÌæ»»µÚÒ»¿ÎÖеÄmyDisplayº¯Êý£¬²¢½«glFrontFace(GL_CCW)ÐÞ¸ÄΪglFrontFace(GL_CW)£¬²¢¹Û²ì½á¹ûµÄ±ä»¯¡£
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT, GL_FILL); // ÉèÖÃÕýÃæΪÌî³äģʽ
glPolygonMode(GL_BACK, GL_LINE); // ÉèÖ÷´ÃæΪÏßÐÎģʽ
glFrontFace(GL_CCW); // ÉèÖÃÄæʱÕë·½ÏòΪÕýÃæ
glBegin(GL_POLYGON); // °´ÄæʱÕë»æÖÆÒ»¸öÕý·½ÐΣ¬ÔÚ×óÏ·½
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, -0.5f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.5f, 0.0f);
glEnd();
glBegin(GL_POLYGON); // °´Ë³Ê±Õë»æÖÆÒ»¸öÕý·½ÐΣ¬ÔÚÓÒÉÏ·½
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f, 0.0f);
glEnd();
glFlush();
}

==========================================

£¨3£©ÌÞ³ý¶à±ßÐαíÃæ
ÔÚÈýά¿Õ¼äÖУ¬Ò»¸ö¶à±ßÐÎËäÈ»ÓÐÁ½¸öÃ棬µ«ÎÒÃÇÎÞ·¨¿´¼û±³ÃæµÄÄÇЩ¶à±ßÐΣ¬¶øһЩ¶à±ßÐÎËäÈ»ÊÇÕýÃæµÄ£¬µ«±»ÆäËû¶à±ßÐÎËùÕÚµ²¡£Èç¹û½«ÎÞ·¨¿´¼ûµÄ¶à±ßÐκͿɼûµÄ¶à±ßÐÎͬµÈ¶Ô´ý£¬ÎÞÒɻήµÍÎÒÃÇ´¦ÀíͼÐεÄЧÂÊ¡£ÔÚÕâÖÖʱºò£¬¿ÉÒÔ½«²»±ØÒªµÄÃæÌÞ³ý¡£
Ê×ÏÈ£¬Ê¹ÓÃglEnable(GL_CULL_FACE);À´Æô¶¯ÌÞ³ý¹¦ÄÜ£¨Ê¹ÓÃglDisable(GL_CULL_FACE)¿ÉÒԹرÕÖ®£©
È»ºó£¬Ê¹ÓÃglCullFaceÀ´½øÐÐÌÞ³ý¡£
glCullFaceµÄ²ÎÊý¿ÉÒÔÊÇGL_FRONT£¬GL_BACK»òÕßGL_FRONT_AND_BACK£¬·Ö±ð±íʾÌÞ³ýÕýÃæ¡¢ÌÞ³ý·´Ãæ¡¢ÌÞ³ýÕý·´Á½ÃæµÄ¶à±ßÐΡ£
×¢Ò⣺ÌÞ³ý¹¦ÄÜÖ»Ó°Ïì¶à±ßÐΣ¬¶ø¶ÔµãºÍÖ±ÏßÎÞÓ°Ïì¡£ÀýÈ磬ʹÓÃglCullFace(GL_FRONT_AND_BACK)ºó£¬ËùÓеĶà±ßÐζ¼½«±»ÌÞ³ý£¬ËùÒÔ¿´¼ûµÄ¾ÍÖ»ÓеãºÍÖ±Ïß¡£

£¨4£©ïοնà±ßÐÎ
Ö±Ïß¿ÉÒÔ±»»­³ÉÐéÏߣ¬¶ø¶à±ßÐÎÔò¿ÉÒÔ½øÐÐïοա£
Ê×ÏÈ£¬Ê¹ÓÃglEnable(GL_POLYGON_STIPPLE);À´Æô¶¯ïοÕģʽ£¨Ê¹ÓÃglDisable(GL_POLYGON_STIPPLE)¿ÉÒԹرÕÖ®£©¡£
È»ºó£¬Ê¹ÓÃglPolygonStippleÀ´ÉèÖÃïοյÄÑùʽ¡£
void glPolygonStipple(const GLubyte *mask);
ÆäÖеIJÎÊýmaskÖ¸ÏòÒ»¸ö³¤¶ÈΪ128×ֽڵĿռ䣬Ëü±íʾÁËÒ»¸ö32*32µÄ¾ØÐÎÓ¦¸ÃÈçºÎïοա£ÆäÖУºµÚÒ»¸ö×Ö½Ú±íʾÁË×î×óÏ·½µÄ´Ó×óµ½ÓÒ£¨Ò²¿ÉÒÔÊÇ´ÓÓÒµ½×ó£¬Õâ¸ö¿ÉÒÔÐ޸ģ©8¸öÏñËØÊÇ·ñïοգ¨1±íʾ²»ïοգ¬ÏÔʾ¸ÃÏñËØ£»0±íʾïοգ¬ÏÔʾÆäºóÃæµÄÑÕÉ«£©£¬×îºóÒ»¸ö×Ö½Ú±íʾÁË×îÓÒÉÏ·½µÄ8¸öÏñËØÊÇ·ñïοա£
µ«ÊÇ£¬Èç¹ûÎÒÃÇÖ±½Ó¶¨ÒåÕâ¸ömaskÊý×飬ÏñÕâÑù£º
static GLubyte Mask[128] =
{
0x00, 0x00, 0x00, 0x00, // ÕâÊÇ×îÏÂÃæµÄÒ»ÐÐ
0x00, 0x00, 0x00, 0x00,
0x03, 0x80, 0x01, 0xC0, // Âé
0x06, 0xC0, 0x03, 0x60, // ·³
0x04, 0x60, 0x06, 0x20, // µÄ
0x04, 0x30, 0x0C, 0x20, // ³õ
0x04, 0x18, 0x18, 0x20, // ʼ
0x04, 0x0C, 0x30, 0x20, // »¯
0x04, 0x06, 0x60, 0x20, // £¬
0x44, 0x03, 0xC0, 0x22, // ²»
0x44, 0x01, 0x80, 0x22, // ½¨
0x44, 0x01, 0x80, 0x22, // Òé
0x44, 0x01, 0x80, 0x22, // ʹ
0x44, 0x01, 0x80, 0x22, // ÓÃ
0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22,
0x66, 0x01, 0x80, 0x66,
0x33, 0x01, 0x80, 0xCC,
0x19, 0x81, 0x81, 0x98,
0x0C, 0xC1, 0x83, 0x30,
0x07, 0xE1, 0x87, 0xE0,
0x03, 0x3F, 0xFC, 0xC0,
0x03, 0x31, 0x8C, 0xC0,
0x03, 0x3F, 0xFC, 0xC0,
0x06, 0x64, 0x26, 0x60,
0x0C, 0xCC, 0x33, 0x30,
0x18, 0xCC, 0x33, 0x18,
0x10, 0xC4, 0x23, 0x08,
0x10, 0x63, 0xC6, 0x08,
0x10, 0x30, 0x0C, 0x08,
0x10, 0x18, 0x18, 0x08,
0x10, 0x00, 0x00, 0x08 // ÕâÊÇ×îÉÏÃæµÄÒ»ÐÐ
};
ÕâÑùÒ»¶ÑÊý¾Ý·Ç³£È±·¦Ö±¹ÛÐÔ£¬ÎÒÃÇÐèÒªºÜ·Ñ¾¢µÄÈ¥·ÖÎö£¬²Å»á·¢ÏÖËü±íʾµÄ¾¹È»ÊÇÒ»Ö»²ÔÓ¬¡£
Èç¹û½«ÕâÑùµÄÊý¾Ý±£´æ³ÉͼƬ£¬²¢ÓÃרÃŵŤ¾ß½øÐб༭£¬ÏÔÈ»»á·½±ãºÜ¶à¡£ÏÂÃæ½éÉÜÈçºÎ×öµ½ÕâÒ»µã¡£

==========================================

Ê×ÏÈ£¬ÓÃWindows×Ô´øµÄ»­±Ê³ÌÐòн¨Ò»¸±Í¼Æ¬£¬È¡ÃûΪmask.bmp£¬×¢Òâ±£´æʱ£¬Ó¦¸ÃÑ¡Ôñ¡°µ¥É«Î»Í¼¡±¡£ÔÚ¡°Í¼Ïó¡±->¡°ÊôÐÔ¡±¶Ô»°¿òÖУ¬ÉèÖÃͼƬµÄ¸ß¶ÈºÍ¿í¶È¾ùΪ32¡£
Ó÷Ŵ󾵹۲ìͼƬ£¬²¢±à¼­Ö®¡£ºÚÉ«¶ÔÓ¦¶þ½øÖÆÁ㣨ïοգ©£¬°×É«¶ÔÓ¦¶þ½øÖÆÒ»£¨²»ïοգ©£¬±à¼­Íê±Ïºó±£´æ¡£
È»ºó£¬¾Í¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´»ñµÃÕâ¸öMaskÊý×éÁË¡£
static GLubyte Mask[128];
FILE *fp;
fp = fopen("mask.bmp", "rb");
if( !fp )
exit(0);
// Òƶ¯ÎļþÖ¸Õëµ½Õâ¸öλÖã¬Ê¹µÃÔÙ¶Ásizeof(Mask)¸ö×ֽھͻáÓöµ½Îļþ½áÊø
// ×¢Òâ-(int)sizeof(Mask)ËäÈ»²»ÊÇʲôºÃµÄд·¨£¬µ«ÕâÀïËüȷʵÊÇÕýÈ·ÓÐЧµÄ
// Èç¹ûÖ±½Óд-sizeof(Mask)µÄ»°£¬ÒòΪsizeofÈ¡µÃµÄÊÇÒ»¸öÎÞ·ûºÅÊý£¬È¡¸ººÅ»áÓÐÎÊÌâ
if( fseek(fp, -(int)sizeof(Mask), SEEK_END) )
exit(0);
// ¶ÁÈ¡sizeof(Mask)¸ö×Ö½Úµ½Mask
if( !fread(Mask, sizeof(Mask), 1, fp) )
exit(0);
fclose(fp);

ºÃµÄ£¬ÏÖÔÚÇë×Ô¼º±à¼­Ò»¸öͼƬ×÷Ϊmask£¬²¢ÓÃÉÏÊö·½·¨È¡µÃMaskÊý×飬ÔËÐкó¹Û²ìЧ¹û¡£
˵Ã÷£º»æÖÆÐéÏßʱ¿ÉÒÔÉèÖÃfactorÒò×Ó£¬µ«¶à±ßÐεÄïοÕÎÞ·¨ÉèÖÃfactorÒò×Ó¡£ÇëÓÃÊó±ê¸Ä±ä´°¿ÚµÄ´óС£¬¹Û²ìïοÕЧ¹ûµÄ±ä»¯Çé¿ö¡£
#include <stdio.h>
#include <stdlib.h>
void myDisplay(void)
{
static GLubyte Mask[128];
FILE *fp;
fp = fopen("mask.bmp", "rb");
if( !fp )
exit(0);
if( fseek(fp, -(int)sizeof(Mask), SEEK_END) )
exit(0);
if( !fread(Mask, sizeof(Mask), 1, fp) )
exit(0);
fclose(fp);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 1.0f, 0.0f);
glEnable(GL_POLYGON_STIPPLE);
glPolygonStipple(Mask);
glRectf(-0.5f, -0.5f, 0.0f, 0.0f); // ÔÚ×óÏ·½»æÖÆÒ»¸öÓÐïοÕЧ¹ûµÄÕý·½ÐÎ
glDisable(GL_POLYGON_STIPPLE);
glRectf(0.0f, 0.0f, 0.5f, 0.5f); // ÔÚÓÒÉÏ·½»æÖÆÒ»¸öÎÞïοÕЧ¹ûµÄÕý·½ÐÎ
glFlush();
}

==========================================

С½á
±¾¿ÎѧϰÁË»æÖƼ¸ºÎͼÐεÄһЩϸ½Ú¡£
µã¿ÉÒÔÉèÖôóС¡£
Ö±Ïß¿ÉÒÔÉèÖÿí¶È£»¿ÉÒÔ½«Ö±Ïß»­³ÉÐéÏß¡£
¶à±ßÐεÄÁ½¸öÃæµÄ»æÖÆ·½·¨¿ÉÒÔ·Ö±ðÉèÖã»ÔÚÈýά¿Õ¼äÖУ¬²»¿É¼ûµÄ¶à±ßÐοÉÒÔ±»ÌÞ³ý£»¿ÉÒÔ½«Ìî³ä¶à±ßÐλæÖƳÉïοյÄÑùʽ¡£
Á˽âÕâЩϸ½Ú»áʹÎÒÃÇÔÚһЩͼÏó»æÖÆÖиü¼ÓµÃÐÄÓ¦ÊÖ¡£
ÁíÍ⣬°ÑһЩÊý¾Ýдµ½³ÌÐòÖ®ÍâµÄÎļþÖУ¬ÓÐʱ¿ÉÒÔÏԵøü·½±ã¡£

ÈëÃÅѧϰµÚÈý¿Î£¬µ½´Ë½áÊø¡£

TO BE CONTINUED
 Â¥Ö÷| ·¢±íÓÚ 2009-1-20 19:50:33 | ÏÔʾȫ²¿Â¥²ã

»Ø¸´: ¡¾×ªÌû¡¿OpenGLÈëÃÅѧϰ£¨Èý£©

glFrontFace(GL_CW); // ÉèÖÃCW·½ÏòΪ¡°ÕýÃ桱£¬CW¼´ClockWise
ÄúÐèÒªµÇ¼ºó²Å¿ÉÒÔ»ØÌû µÇ¼ | ×¢²á

±¾°æ»ý·Ö¹æÔò

QQ|Archiver|СºÚÎÝ|¼¸ºÎ³ß´çÓ빫²îÂÛ̳

GMT+8, 2024-12-22 12:52 , Processed in 0.037898 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí