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

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

时间:2016-07-23 20:56:00      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

一.

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_WIDTH_GRANULARITY,&add);

 

直线示范例程序

 

/

#include "stdafx.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=0.0f;
    GLfloat sizes[2];
    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; angle+=0.1f)
    {
        
            glBegin(GL_LINES);
            x=50.0f*cos(angle);
            y=50.0f*sin(angle);
            glVertex3f(x,y,z);
            x=50.0f*cos(angle+PI);
            y=50.0f*sin(angle+PI);
            glVertex3f(x,y,z);
            glEnd();
    }
    

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


}

 

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

标签:

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

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