码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-23 20:57:29      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

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);

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

标签:

原文地址:http://www.cnblogs.com/YTYMblog/p/5699390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!