OpenGL自行车(一):车子框架

目的:构造轮子自动旋转的自行车

主要使用了:

  • 矩阵构造(平移、旋转)
  • 模型视图矩阵
  • 三角形批次类(创建轮子)
  • 投影矩阵(透视投影)
/*
bicycle.cpp  框架完成
by GaoLei 2016/05/30
*/

#include <GLTools.h>
#include <GLShaderManager.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLFrame.h>
#include <GLMatrixStack.h>
#include <GLGeometryTransform.h>
#include <StopWatch.h>//计时以算旋转
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

GLShaderManager     shaderManager;          // 着色器管理器
GLMatrixStack       modelViewMatrix;        // 模型视图矩阵
GLMatrixStack       projectionMatrix;       // 投影矩阵
GLFrame             cameraFrame;
GLFrustum           viewFrustum;            // 视景体

GLTriangleBatch             front_wheel;
GLBatch             bike_body;
GLTriangleBatch             back_wheel;

GLGeometryTransform transformPipeline;



void SetupRC()
{
    shaderManager.InitializeStockShaders();
    glEnable(GL_DEPTH_TEST);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glClearColor(1.0F, 1.0F, 1.0F, 1.0F);
    //建立几何图形,圆盘也可以使用批次用三角形扇画出
    gltMakeDisk(front_wheel,0.1,0.2,30,2);
    gltMakeDisk(back_wheel, 0.1, 0.2, 30, 2);

    bike_body.Begin(GL_LINES, 6, 0);
    bike_body.Vertex3f(-0.22f, -0.1f, 0);
    bike_body.Vertex3f(0.22f, -0.1f, 0);
    bike_body.Vertex3f(0.0f, 0.1f, 0);
    bike_body.Vertex3f(0.0f,- 0.1f, 0);
    bike_body.Vertex3f(-0.05f, 0.1f, 0);
    bike_body.Vertex3f(0.05f, 0.1f, 0);
    bike_body.End();

    cameraFrame.MoveForward(-2.0f);
}
//改变视景体和视口,在改变窗口大小时调用
void ChangeSize(int w, int h)
{
    GLfloat  fAspect;
    //防止对0进行除法操作
    if (h == 0)
        h = 1;
    //将视口设置为窗口大小
    glViewport(0, 0, w, h);
    fAspect = (GLfloat)w / (GLfloat)h;
    //生成透视投影
    viewFrustum.SetPerspective(50.0f, fAspect, 1.0, 100.0);
    projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
    transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
}

//进行调用以绘制场景
void RenderScene(void)
{
    static GLfloat vBODYColor[] = { 0.0f, 1.0f, 0.0f, 1.0f };
    static GLfloat vFWColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    static GLfloat vBWColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };

    static CStopWatch rotTimer;
    float zRot = rotTimer.GetElapsedSeconds() * 60.0f;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//用当前清除色清除窗口

    //保存当前模型视图矩阵
    modelViewMatrix.PushMatrix();

    M3DMatrix44f mCamera;
    cameraFrame.GetCameraMatrix(mCamera);
    modelViewMatrix.MultMatrix(mCamera);
    
    shaderManager.UseStockShader(GLT_SHADER_FLAT,
        transformPipeline.GetModelViewProjectionMatrix(),
        vBODYColor);
    bike_body.Draw();//车身

    modelViewMatrix.PushMatrix();
    modelViewMatrix.Translate(-0.35, -0.25, 0.0);
    modelViewMatrix.Rotate(zRot, 0.0f, 0.0f, 1.0f);
    shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
        vFWColor);
    front_wheel.Draw();
    modelViewMatrix.PopMatrix();
    modelViewMatrix.PushMatrix();
    modelViewMatrix.Translate(0.35f, -0.25f, 0.0f);
    modelViewMatrix.Rotate(zRot, 0.0f, 0.0f, 1.0f);
    shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
        vBWColor);
    back_wheel.Draw();
    modelViewMatrix.PopMatrix();
    modelViewMatrix.PopMatrix();
    glutSwapBuffers();
    glutPostRedisplay();
}
int main(int argc, char *argv[])
{
    gltSetWorkingDirectory(argv[0]);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Bike");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);

    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
    }

    SetupRC();
    glutMainLoop();
    return 0;
}

轮子在转
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1 前言 OpenGL渲染3D模型离不开空间几何的数学理论知识,而本篇文章的目的就是对空间几何进行简单的介绍,并对...
    RichardJieChen阅读 7,631评论 1 11
  • 引言 请不要质疑你的眼睛,文章的题目就是“3D图形学基础理论”。可能有人要疑惑了,作为一个 iOS 开发者为什么要...
    ZhengYaWei阅读 8,968评论 5 36
  • 1 前言 一直想沿着图像处理这条线建立一套完整的理论知识体系,同时积累实际应用经验。因此有了从使用AVFounda...
    RichardJieChen阅读 5,988评论 5 12
  • 云朵 白云悠悠 像一个下午那样晃荡 呼吸由此变慢 呼吸是一个村庄的云 人们有时在云下 有时在云上 想起遥远的祖先 ...
    目成1987阅读 281评论 1 5
  • 铺落了一地的退了绿装的叶, 枯枝上还挂着的摇曳, 仿佛在梦里的抽噎。 南来北去的轮回, 是对习惯,还是季节的追随?...
    弯月别枝半夜鸣阅读 418评论 4 6

友情链接更多精彩内容