| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 777 人关注过本帖
标题:[OGL][初级]2D的旋转齿轮 (两个齿轮咬合)
只看楼主 加入收藏
if_exist
Rank: 2
等 级:论坛游民
帖 子:86
专家分:41
注 册:2009-4-20
结帖率:100%
收藏
 问题点数:0 回复次数:4 
[OGL][初级]2D的旋转齿轮 (两个齿轮咬合)
来自 www.
" border="0" />

:D 代码没有优化就送上来了  

" border="0" />

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <time.h>
#include <math.h>

int winid;

float angx=-2.25,angxb=-13.5;
//float angx=0.0,angxb=-9.0;

void gears(float length,float lenadd,float parts) {
    float x,y,z,tmplen,i;
    float maxi=parts;
    glBegin(GL_LINE_LOOP);
    for (i=0.0;i<maxi;i+=1.0) {
        if ((int) i%2==0) {
            if ((int) (i/2)%2==0) {
                tmplen=length+8.0;
            } else {
                tmplen=length;
            }

        }
        x=(tmplen)*cos(6.28*i/maxi);
        y=(tmplen)*sin(6.28*i/maxi);
        glVertex3f(x,y,0.0);
    }
    glEnd();
}

void display(void) {
    float x,y,z;
    static float i,ti,tmplen;
    float maxi=40.0,length;
    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_BLEND);
    glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glLineWidth(2.0);


    glPushMatrix();
    glRotatef(angx,0.0,0.0,1.0);
    gears(50.0,8.0,80.0);
    glPopMatrix();


    glPushMatrix();

    glTranslatef(-80.0,0.0,0.0);
    glRotatef(-angxb,0.0,0.0,1.0);
    gears(20.0,8.0,40.0);
    glPopMatrix();

    glFlush();
}

void idle(void) {
    angx+=2.0;
    angxb+=4.0;
    usleep(36000);
    glutPostRedisplay();
}

void reshape(int Width,int Height) {
    float far=200.0;
    glViewport(0,0,500,500);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-100,100,-100,100,0,500);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0,0.0,far, 0.0,0.0,0.0, 0.0,1.0,far);
}

void init(void) {
    glClearColor(0.0,0.0,0.0,0.0);
}

void keypress(unsigned char key,int mousex,int mousey) {
    switch (key) {
    case 'q':
        glutDestroyWindow(winid);
        exit(0);
        break;
    case 'a':

        glutPostRedisplay();
        break;

    }
}

void keyup(unsigned char key,int mousex,int mousey) {
    switch (key) {
    case 'a':
        break;
    }
}

int main(int argc,char *argv[]) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    winid=glutCreateWindow("Press x");
    init();
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutKeyboardFunc(keypress);
    glutKeyboardUpFunc(keyup);
    glutMainLoop();
    return 0;
}



[ 本帖最后由 if_exist 于 2012-4-2 10:43 编辑 ]
搜索更多相关主题的帖子: 齿轮 旋转 
2012-04-02 09:17
smallmoon521
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:517
专家分:1373
注 册:2008-4-21
收藏
得分:0 
没人顶啊,我顶一个,谢谢分享

为游戏狂~~!!    大家努力编哈!
2012-04-05 21:39
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
好看,我也顶顶。

我们都在路上。。。。。
2012-04-06 15:20
OoDreamParty
Rank: 2
等 级:论坛游民
帖 子:10
专家分:20
注 册:2012-3-1
收藏
得分:0 
c 语言咋编图形化界面啊
2012-04-09 23:41
快乐小混
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-10-24
收藏
得分:0 
顶!!!!
2012-06-03 10:09
快速回复:[OGL][初级]2D的旋转齿轮 (两个齿轮咬合)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030326 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved