绘制图形与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);
    

}
void ChangeSize(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat aspect=w/h;
    if (w<=h)
    {
        glOrtho(-100.0,100.0,-100.0/aspect,100.0/aspect,-100.0,100.0);
    }
    else
    {
        glOrtho(-100.0*aspect,100.0*aspect,-100.0,100.0,-100.0,100.0);
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


}

void Mydisplay()
{
    GLfloat x,y,angle,z=-50.0f;
    GLfloat sizes[2];
    GLfloat step;
    GLfloat CurPointSize;
    glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
    glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step);
    CurPointSize=sizes[0];
    glColor3f(0.0f,1.0f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(xRot,1.0,0.0,0.0);
    glRotatef(yRot,0.0,1.0,0.0);
    
    for (angle = 0.0f; angle <2*PI*3; angle+=0.1f)
    {
        x=50.0f*cos(angle);
        y=50.0f*sin(angle);
        glBegin(GL_POINTS);
        glVertex3f(x,y,z);
        glEnd();
        z+=0.5f;
        CurPointSize+=step;
    }
    

    glPopMatrix();
    glutSwapBuffers();


}
void Mykeys(int key,int x,int y)
{
    if (key==GLUT_KEY_LEFT)
    {
        yRot-=5.0f;
    }
    if (key==GLUT_KEY_RIGHT)
    {
        yRot+=5.0f;
    }
    if (key==GLUT_KEY_UP)
    {
        xRot-=5.0f;
    }
    if (key==GLUT_KEY_DOWN)
    {
        xRot+=5.0f;
    }
    if (xRot>355.0f)
    {
        xRot=0.0f;
    }
    if (xRot<=-5.0f)
    {
        xRot=355.0f;
    }
    if (yRot>355.0f)
    {
        yRot=0.0f;
    }
    if (yRot<=-5.0f)
    {
        yRot=355.0f;
    }


    glutPostRedisplay();

}
int main(int argc,char ** argv)
{
    
    
    
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);

    glutInitWindowPosition(500,500);
    glutInitWindowSize(800,800);
    glutCreateWindow("Points");

    Init();
    glutDisplayFunc(Mydisplay);
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(Mykeys);
    glutMainLoop();


}

 

知识点:

1.键盘响应函数

2.矩阵堆栈

3.矩阵初始化

4.点图元的大小:glpointsize();

5.获得点图元的大小范围和增量大小

  GLfloat sizes[2];

  GLfloat step;

  glGetFloatv(GL_POINT_SIZE_RANGE,sizes);

  glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step);

posted @ 2016-07-23 19:44  LT.C#  阅读(297)  评论(0编辑  收藏  举报