绘制图形与3D增强技巧(四)----多边形图元及其点画模式

1.四边形图元

glBegin(GL_QUADS);

glend();

2.通用多边形

glBegin(GL_POLYGONS);

glend();

3.多边形点画模式

glenable(GL_POLYGON_STIPPLE);

glPolygonStipple(pBitmp);

其中pBitmap为一块指定了数据区域的指针,

#include "stdafx.h"

// PStipple.cpp
// OpenGL SuperBible
// Demonstrates OpenGL Polygon Stippling
// Program by Richard S. Wright Jr.
#include<GL\glut.h>
#include <math.h>

// Define a constant for the value of PI
#define GL_PI 3.1415f

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;

// Bitmap of camp fire
GLubyte fire[128] = { 0x00, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x00, 0xc0,
                   0x00, 0x00, 0x01, 0xf0,
                   0x00, 0x00, 0x07, 0xf0,
                   0x0f, 0x00, 0x1f, 0xe0,
                   0x1f, 0x80, 0x1f, 0xc0,
                   0x0f, 0xc0, 0x3f, 0x80,
                   0x07, 0xe0, 0x7e, 0x00,
                   0x03, 0xf0, 0xff, 0x80,
                   0x03, 0xf5, 0xff, 0xe0,
                   0x07, 0xfd, 0xff, 0xf8,
                   0x1f, 0xfc, 0xff, 0xe8,
                   0xff, 0xe3, 0xbf, 0x70,
                   0xde, 0x80, 0xb7, 0x00,
                   0x71, 0x10, 0x4a, 0x80,
                   0x03, 0x10, 0x4e, 0x40,
                   0x02, 0x88, 0x8c, 0x20,
                   0x05, 0x05, 0x04, 0x40,
                   0x02, 0x82, 0x14, 0x40,
                   0x02, 0x40, 0x10, 0x80,
                   0x02, 0x64, 0x1a, 0x80,
                   0x00, 0x92, 0x29, 0x00,
                   0x00, 0xb0, 0x48, 0x00,
                   0x00, 0xc8, 0x90, 0x00,
                   0x00, 0x85, 0x10, 0x00,
                   0x00, 0x03, 0x00, 0x00,
                   0x00, 0x00, 0x10, 0x00 };

// Called to draw scene
void RenderScene(void)
    {
    // Clear the window
    glClear(GL_COLOR_BUFFER_BIT);

    // Save matrix state and do the rotation
    glPushMatrix();
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);

    // Begin the stop sign shape,
    // use a standard polygon for simplicity
    glBegin(GL_POLYGON);
        glVertex2f(-20.0f, 50.0f);
        glVertex2f(20.0f, 50.0f);
        glVertex2f(50.0f, 20.0f);
        glVertex2f(50.0f, -20.0f);
        glVertex2f(20.0f, -50.0f);
        glVertex2f(-20.0f, -50.0f);
        glVertex2f(-50.0f, -20.0f);
        glVertex2f(-50.0f, 20.0f);
    glEnd();

    // Restore transformations
    glPopMatrix();

    // Flush drawing commands
    glutSwapBuffers();
    }

// This function does any needed initialization on the rendering
// context.
void SetupRC()
    {
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    // Set drawing color to red
    glColor3f(1.0f, 0.0f, 0.0f);

    // Enable polygon stippling
    glEnable(GL_POLYGON_STIPPLE);

    // Specify a specific stipple pattern
    glPolygonStipple(fire);
    }

void SpecialKeys(int key, int x, int y)
    {
    if(key == GLUT_KEY_UP)
        xRot-= 5.0f;

    if(key == GLUT_KEY_DOWN)
        xRot += 5.0f;

    if(key == GLUT_KEY_LEFT)
        yRot -= 5.0f;

    if(key == GLUT_KEY_RIGHT)
        yRot += 5.0f;

    if(key > 356.0f)
        xRot = 0.0f;

    if(key < -1.0f)
        xRot = 355.0f;

    if(key > 356.0f)
        yRot = 0.0f;

    if(key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
    }

void ChangeSize(int w, int h)
    {
    GLfloat nRange = 100.0f;

    // Prevent a divide by zero
    if(h == 0)
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset projection matrix stack
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Establish clipping volume (left, right, bottom, top, near, far)
    if (w <= h)
        glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
    else
        glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

    // Reset Model view matrix stack
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

int main(int argc, char* argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Polygon Stippling");
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(SpecialKeys);
    glutDisplayFunc(RenderScene);
    SetupRC();
    glutMainLoop();

    return 0;
}
时间: 2024-08-08 01:18:25

绘制图形与3D增强技巧(四)----多边形图元及其点画模式的相关文章

绘制图形与3D增强技巧(五)----多边形图元的使用及其他

1.注意多边形图元中的多边形只能是平面的,而且必须为凸多边形,且多边形的边不能弯曲 2.细分和边界,可以人为设置边界边和非边界边 glEdgeFlag(true)//接下来所有点均为边界边起点 glEdgeFlag(false)//接下来所有点均为非边界边起点 3.操纵深度缓冲区 glutinitdispalymode(GLUT_DEPTH);//开辟深度缓冲区 glenable(GL_DEPTH_TEST);//启用深度测试:即使未启用,深度值仍然写入深度缓冲区,只不过不进行测试 glDept

绘制图形与3D增强技巧(二)----直线图元之点画

一.直线的点画模式:即并不完全填充所有像素来画一条直线,而是用点画的形式,间隔地画一条直线 首先启用点画模式: glEnable(GL_LINE_STIPPLE); 然后自定义创建自己的点画模式 glLineStipple(GLlint factor,GLushort pattern); 其中第一个参数factor为乘法因子,第二个参数pattern为自定义的点画模式:一个16位的值,每个位表示线段的一部分状态为打开或者关闭,采用16进制,例如:0x5555 直线点画的实例程序: // STIP

绘制图形与3D增强技巧(三)----三角形图元TRANGLE

1. glBegin(GL_TRANGLES); ........ glend(); 2.多边形的环绕方向:逆时针和顺时针的正反面 GLFront(GL_CCW)和GLFront(GL_CW); 3.三角形带 glBegin(GL_TRANGLE_STRIP); ....... glend(); 4.三角形扇 glBegin(GL_TRANGLE_FAN); ....... glend(); 例子: // TRANGLE.cpp : 定义控制台应用程序的入口点. // #include "std

绘制图形与3D增强技巧(二)----直线图元

一. glBegin(GL_LINES); glend(); 二.线带和线环 glBegin(GL_LINE_STRIP); glend(); glBegin(GL_LINE_LOOP); glend(); 三.设置直线宽度 glLineWidth(GLfloat width); 四.获得直线宽度范围和增量 GLfloat sizes[2]; GLfloat add; glGetFloatv(GL_LINE_WIDTH_RANGE,sizes); glGetFloatv(GL_LINE_WIDT

绘制图形与3D增强技巧(一)----点图元

1.图元 1.点图元 glBegin(GL_POINTS); glend(); 程序:点图元的应用 #include "stdafx.h" #include<stdio.h> #include<GL\glut.h> #include<math.h> #define PI 3.14 GLfloat xRot=0.0f; GLfloat yRot=0.0f; void Init() { glClearColor(0.0,0.0,0.0,0.0); } v

【UWP通用应用开发】编辑文本、绘制图形、3D透视效果及绘制时钟实战

编辑文本及键盘输入 相信大家都会使用TextBox,但如果要让文本在TextBox中换行该怎么做呢?将TextWrapping属性设置为Wrap,将AcceptsReturn属性设置为True就好咯. PasswordBox很明显就是一个密码框了,和其他的控件相比其有2个特殊之处,一个是其可以用MaxLength来控制最大的长度,一个是用PasswordChanged来捕捉密码的改名.显然比如QQ密码的MaxLength就是16位了,而PasswordChanged可以用来监测比如用户设置的密码

OpenGl学习进程(8)第六课:点、边和图形(三)绘制图形

本节是OpenGL学习的第六个课时,下面介绍OpenGL图形的相关知识:     (1)多边形的概念: 多边形是由多条线段首尾相连而形成的闭合区域.OpenGL规定,一个多边形必须是一个“凸多边形”.通过点.直线和多边形,就可以组合成各种几何图形.一段弧可以看成是是很多短的直线段相连,这些直线段足够短,以至于其长度小于一个像素的宽度.通过位于不同平面的相连的小多边形,还可以组成一个“曲面”. 什么是凸边形: 凸边形:多边形内任意两点所确定的线段都在多边形内,由此也可以推导出,凸多边形不能是空心的

OpenGL学习05_点画模式(点画多边形)

在默认情况下,填充多边形是用实心模式绘制的.此外,它们还可以使用一种32位×32位的窗口对齐的点画模式.glPolygonStipple()函数用于指定多边形的点画模式. void glPolygonStipple(const GLubyte *mask); 定义填充多边形的当前点画模式.mask参数是一个指向32×32位图的指针,后者被解释为0和1的掩码.如果模式中出现的是1,那么多边形中对应的像素就被绘制:如果出现的是0,多边形中对应的像素就不被绘制. 下面通过一个绘制小强的Demo演示一下

OpenGL学习进程(4)第二课:绘制图形

本节是OpenGL学习的第二个课时,下面介绍如何用点和线来绘制图形:     (1)用点的坐标来绘制矩形: #include <GL/glut.h> void display(void) { // clear all pixels glClear(GL_COLOR_BUFFER_BIT); // draw yellow polygon (rectangle) with corners at glColor3f(1.0, 1.0, 0.0); glBegin(GL_POLYGON); //绘制开