OpenGL

OpenGL系列之六:绘制立方体

2022-11-27  本文已影响0人  itfitness

目录

相关文章

OpenGL系列之一:OpenGL第一个程序
OpenGL系列之二:绘制三角形
OpenGL系列之三:三角形顶点增加颜色
OpenGL系列之四:绘制四边形
OpenGL系列之五:绘制点和线

实现效果

实现步骤

1.下载glm

https://github.com/g-truc/glm
下载完如下:

2.引入glm

在项目cpp文件夹下新建3rdparty\libglm\include,然后将上面下载的glm文件夹复制到3rdparty\libglm\include文件夹下。



3.CMakeLists.txt配置

CMakeLists.txt增加如下配置:

#增加glm
SET(GLM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/3rdparty/libglm)
INCLUDE_DIRECTORIES(${GLM_DIR}/include)

完整如下:

cmake_minimum_required(VERSION 3.10)

#增加glm
SET(GLM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/3rdparty/libglm)
INCLUDE_DIRECTORIES(${GLM_DIR}/include)

add_library(CCOpenGLRender SHARED src/main/cpp/CCOpenGLRender.cpp)

target_link_libraries(
        CCOpenGLRender
        -lGLESv1_CM #OpenGL V1
        -lGLESv2 #OpenGL V2
        log
)
4.编写代码

这里是以之前的第五个程序(OpenGL系列之五:绘制点和线
)为基础,主要就是修改了CCOpenGLRender.cpp,完整CCOpenGLRender.cpp代码如下(附注释):

#include <jni.h>
#include <GLES/gl.h>
#include <GLES2/gl2.h>
#include "CCGLPrimitivesDef.h"

//引入glm头文件
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/ext/matrix_clip_space.hpp>

//立方体旋转角度
float m_angle =0.0f;

extern "C"
JNIEXPORT void JNICALL
Java_com_itfitness_opengldemo_GLRender_ndkInitGL(JNIEnv *env, jobject thiz) {
    //清空颜色
    glClearColor(0.0,0,0,1.0);
    //清空深度缓存
    glClearDepthf(1.0);
    //开启深度测试
    glEnable(GL_DEPTH_TEST);
    //什么情况下使用深度测试
    glDepthFunc(GL_LEQUAL);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_itfitness_opengldemo_GLRender_ndkPaintGL(JNIEnv *env, jobject thiz) {
    //清空颜色缓冲区或深度缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //加载模型视图矩阵
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glCullFace(GL_BACK);

    //定义立方体顶点
    CCFloat7 cubeVert[] = {
            {-0.5f, -0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {0.5f, -0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {0.5f,  0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {0.5f,  0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {-0.5f,  0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {-0.5f, -0.5f, -0.5f,  1.0,0.0,0.0,1.0},

            {-0.5f, -0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {0.5f, -0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {0.5f,  0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {0.5f,  0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {-0.5f,  0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {-0.5f, -0.5f,  0.5f,  0.0,1.0,0.0,1.0},

            {-0.5f,  0.5f,  0.5f,  0.0,0.0,1.0,1.0},
            {-0.5f,  0.5f, -0.5f,  0.0,0.0,1.0,1.0},
            {-0.5f, -0.5f, -0.5f,  0.0,0.0,1.0,1.0},
            {-0.5f, -0.5f, -0.5f,  0.0,0.0,1.0,1.0},
            {-0.5f, -0.5f,  0.5f, 0.0,0.0,1.0,1.0},
            {-0.5f,  0.5f,  0.5f,  0.0,0.0,1.0,1.0},

            {0.5f,  0.5f,  0.5f,  0.0,0.0,1.0,1.0},
            {0.5f,  0.5f, -0.5f,  0.0,0.0,1.0,1.0},
            {0.5f, -0.5f, -0.5f,  0.0,0.0,1.0,1.0},
            {0.5f, -0.5f, -0.5f,  0.0,0.0,1.0,1.0},
            {0.5f, -0.5f,  0.5f,  0.0,0.0,1.0,1.0},
            {0.5f,  0.5f,  0.5f,  0.0,0.0,1.0,1.0},

            {-0.5f, -0.5f, -0.5f,  0.0,1.0,0.0,1.0},
            {0.5f, -0.5f, -0.5f,  0.0,1.0,0.0,1.0},
            {0.5f, -0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {0.5f, -0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {-0.5f, -0.5f,  0.5f,  0.0,1.0,0.0,1.0},
            {-0.5f, -0.5f, -0.5f,  0.0,1.0,0.0,1.0},

            {-0.5f,  0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {0.5f,  0.5f, -0.5f,  1.0,0.0,0.0,1.0},
            {0.5f,  0.5f,  0.5f,  1.0,0.0,0.0,1.0},
            {0.5f,  0.5f,  0.5f,  1.0,0.0,0.0,1.0},
            {-0.5f,  0.5f,  0.5f,  1.0,0.0,0.0,1.0},
            {-0.5f,  0.5f, -0.5f,  1.0,0.0,0.0,1.0}
    };

    //启动一组顶点坐标
    glEnableClientState(GL_VERTEX_ARRAY);
    //启动颜色
    glEnableClientState(GL_COLOR_ARRAY);
    // 指定绘制的定点数组
    // 第一个参数:绘制几个点(这里其实就是CCFloat7中的前三个点x,y,z)
    // 第二个参数:类型为float
    // 第三个参数:第一个点到第二个点之间的步长(也就是结构体的长度)
    // 第四个参数:绘制数据的地址
    glVertexPointer(3,GL_FLOAT,sizeof(CCFloat7),cubeVert);
    glColorPointer(4,GL_FLOAT,sizeof(CCFloat7),&cubeVert[0].r);

    //每次绘制角度都增加
    m_angle += 0.05f;

    glm::mat4x4  cubeMat;
    //平移矩阵
    glm::mat4x4  cubeTransMat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -0.5));
    //旋转矩阵
    glm::mat4x4  cubeRotMat = glm::rotate(glm::mat4(1.0f),m_angle,glm::vec3(0.5f, 0.5f, 1.0) );
    //缩放矩阵
    glm::mat4x4  cubeScaleMat = glm::scale(glm::mat4(1.0f),glm::vec3(0.5f, 0.5f, 0.5) );
    cubeMat = cubeTransMat * cubeRotMat * cubeScaleMat;

    //加载矩阵
    glLoadMatrixf(glm::value_ptr(cubeMat));

    // 绘制三角形(立方体由三角形组成)
    glDrawArrays(GL_TRIANGLES,0,36);

    //关闭一组顶点坐标
    glDisableClientState(GL_VERTEX_ARRAY);
    //关闭颜色
    glDisableClientState(GL_COLOR_ARRAY);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_itfitness_opengldemo_GLRender_ndkResizeGL(JNIEnv *env, jobject thiz, jint width,
                                                   jint height) {
    //设置窗口大小
    glViewport(0,0,width,height);
    //投影矩阵
    glMatrixMode(GL_PROJECTION);
    //重置投影矩阵
    glLoadIdentity();
    //摄像机
    glOrthof(-1,1,-1,1,0,1000);

    //添加模型视图矩阵
    glMatrixMode(GL_MODELVIEW);
    //重置当前指定的矩阵为单位矩阵
    glLoadIdentity();
}
上一篇下一篇

猜你喜欢

热点阅读