Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3910
  • 博文数量: 2
  • 博客积分: 203
  • 博客等级: 入伍新兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-02 09:29
文章分类

全部博文(2)

文章存档

2011年(2)

最近访客

分类: LINUX

2011-08-19 18:19:09

#include
#include
#include

void setLight(void);
void myDisplay(void);
void handlerMenu(int val);
void handlerMouse(int button, int state, int x, int y);
void handlerKeyboard(unsigned char key, int x, int y);
void handlerPassiveMotion(int x, int y);
void handlerMotion(int x, int y);
void setLight(void);
void handlerSpecialKeys(int key, int x, int y);
void myIdle(void);
void myDrawObject();
void setVertexs();
void drawAxis();

int gMenuId = 0;
int gScreenW = 1000;
int gScreenH = 800;

#define PRISMS 4
#define LAYERS 3
#define COLUMNS 3

#define HEIGHT 2.0f
#define RADIUS 1.5f

#define PI (3.1415926535897931)

GLfloat gVertexts[LAYERS+1][PRISMS][COLUMNS][3];
#define POINT_CNT ((LAYERS+1) * PRISMS * COLUMNS)

void myDisplay(void)
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LINE_SMOOTH);

    glMatrixMode(GL_PROJECTION);
    gluPerspective(40.0f, 1.0f, 1.0f, 10.0f);

    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.5f, 3.5f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    glTranslatef(-0.5f, -0.5f, -1.5f);

    glClearColor(0.5f, 0.5f,0.5f, 0.5f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//  drawAxis();

    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    //glRectf(0.0f, 0.0f, 0.5f, 0.5f);
    glLineWidth(3.0f);
    setVertexs();
    for(int i = 0; i <= LAYERS; i++){
        myDrawObject(GL_LINE_LOOP, (GLfloat*)gVertexts[i], POINT_CNT/(LAYERS + 1), 3);
    }
    glTranslatef(0.0f, 0.0f, -3.0f);
    //glutSolidSphere(0.5, 100, 100);
    glColor4f(0.8f, 0.6f, 0.3f, 0.2f);
    //glutSolidCube(2.0);


    glFlush();
}

void drawAxis(){
    GLfloat size = 10.0f;
    glBegin(GL_LINES);
        glLineWidth(2.0f);

        glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        glVertex3f(-size, 0.0f, 0.0f);
        glVertex3f(size, 0.0f, 0.0f);

        glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
        glVertex3f(0.0f, -size, 0.0f);
        glVertex3f(0.0f, size, 0.0f);

        glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
        glVertex3f(0.0f, 0.0f, -size);
        glVertex3f(0.0f, 0.0f, size);
    glEnd();
}
void myDrawObject(GLenum mode, GLfloat* pv, int pc, int dimension){
    GLfloat* start = pv;
    glBegin(mode);
        for(int i = 0; i < pc; i++){
            if(dimension == 3){
                glVertex3f(*start++, *start++, *start++);
            }else if(dimension == 2){
                glVertex2f(*start++, *start++); }
        }
    glEnd();
}


void setVertexs(){
    int layer = 0;
    int surface = 0;
    int index = 0;
    double angleStep = 2 * PI / PRISMS;

    GLfloat sx = RADIUS;
    GLfloat sy = -HEIGHT / 2;
    GLfloat sz = 0.0f;
    GLfloat x = 0.0f, y = 0.0f, z = 0.0f;
    double angle = 0.0;
    for(layer = 0; layer <= LAYERS; layer++){
        for(surface = 0; surface < PRISMS; surface++){
            angle = -(1 + surface) * angleStep;
            x = (float)RADIUS * cos(angle);
            z = (float)RADIUS * sin(angle);
            for(index = 0; index < COLUMNS; index++){
                gVertexts[layer][surface][index][0] = sx + index * (x - sx) / COLUMNS;
                gVertexts[layer][surface][index][1] = sy;
                gVertexts[layer][surface][index][2] = sz + index * (z - sz) / COLUMNS;

            }
            sx = x;
          sz = z;
        }
        sx = RADIUS;
        sy += layer * HEIGHT / LAYERS;
        sz = 0.0f;
    }
}

void handlerMenu(int val){

}

void handlerMouse(int button, int state, int x, int y){

    //printf("%d state = %d x = %d y = %d\n", button, state, x, y);
    if(button == GLUT_LEFT_BUTTON){
        if(state == GLUT_DOWN){
        }else{
        }
    }
}

void handlerKeyboard(unsigned char key, int x, int y){
    //printf("%c x = %d y = %d\n", key, x, y);
    exit(0);
}

void handlerPassiveMotion(int x, int y){
    printf("Passive Motion: x = %d, y = %d\n", x, y);
}

void handlerMotion(int x, int y){
    //printf("Motion: x = %d, y = %d\n", x, y);
}

void setLight(void)
{
    static const GLfloat light_position[] = {1.0f, 1.0f, -1.0f, 1.0f};
    static const GLfloat light_ambient[]  = {0.2f, 0.2f, 0.2f, 1.0f};
    static const GLfloat light_diffuse[]  = {1.0f, 1.0f, 1.0f, 1.0f};
    static const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

    glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
}

void handlerSpecialKeys(int key, int x, int y){
    printf("keycode = %d x = %d, y = %d\n", key, x, y);
}


void myIdle(void){
    /*
    ++angle;
    if(angle >= 360.0f)
        angle = 0.0f;
    if(++day > 360)
        day = 0;
    sleep(1);
    */
    myDisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowPosition(60, 60);
    glutInitWindowSize(gScreenW, gScreenH);

    glutCreateWindow("hello opengl");

    glutDisplayFunc(&myDisplay);

    printf("windows: w =%d h=%d\n", glutGet(GLUT_SCREEN_WIDTH) * 4 / 5, glutGet(GLUT_SCREEN_HEIGHT) * 4 / 5);
#if 0  
    glutIdleFunc(&myIdle);

    glutMouseFunc(handlerMouse);
    glutMotionFunc(handlerMotion);
    glutPassiveMotionFunc(handlerPassiveMotion);
    glutSpecialFunc(handlerSpecialKeys);
#endif
  glutKeyboardFunc(handlerKeyboard);

#if 0
    gMenuId = glutCreateMenu(handlerMenu);
    glutAddMenuEntry("hello", 23);
    glutSetMenu(gMenuId);
#endif

    glutMainLoop();

    return 0;
}


阅读(424) | 评论(0) | 转发(0) |
0

上一篇:博客已升级,请注意变更地址

下一篇:没有了

给主人留下些什么吧!~~