OpenGL学习——变换——绕x轴来回旋转的木板

学习OpenGL的变换。

Local Space => World Space => View Space => Clip Space => Screen Space

ModelMatrix

ViewMatrix

ProjectionMatrix   :   Orthographic   Perspective

GLM: OpenGL Mathematics

变换效果:

初始化与渲染代码:

RectFall.h

#ifndef RectFall_h
#define RectFall_h

#include "RectBox.h"

class RectFall : public RectBox
{
public:
    RectFall();

    ~RectFall();

protected:
    virtual void init();

    virtual void draw();

private:
    GLuint _textureOne;
    GLuint _textureTwo;

};

#endif // !RectFall_h

RectFall.cpp

#include "RectFall.h"

RectFall::RectFall(){
}

RectFall::~RectFall(){
}

void RectFall::init(){
    // x,y,z  opengl coordinates, the vertex data, three 3d point in normalized device coordinates
    GLfloat vertexs[] = {
        //position            color                texture coordinates
        0.3f, -0.2f, 0.0f,    1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // right up
        0.3f, -0.9f, 0.0f,    0.0f, 0.0f, 1.0f,   1.0f, 0.0f, // right down

        -0.3f, -0.2f, 0.0f,    0.0f, 1.0f, 0.0f,   0.0f, 1.0f, // left up
        -0.3f, -0.9f, 0.0f,    1.0f, 0.0f, 0.0f,   0.0f, 0.0f, // left down
    };
    // vertex draw index array
    GLuint indexs[] = {
        0, 1, 2,
        1, 2, 3,
    };
    //define VAO, vertex array object
    glGenVertexArrays(1, &_VAO);
    glBindVertexArray(_VAO); // bind vertex array object
        //define VBO, vertex buffer object
        GLuint VBO;
        glGenBuffers(1, &VBO);                                                   // gen buffer object
        glBindBuffer(GL_ARRAY_BUFFER, VBO);                                      // bind buffer to the target
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertexs), vertexs, GL_STATIC_DRAW); // copy vertex data to VBO
        //define EBO, element buffer object
        GLuint EBO;
        glGenBuffers(1, &EBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexs), indexs, GL_STATIC_DRAW);
        //set vertex attribute point
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
        glEnableVertexAttribArray(2);
    glBindVertexArray(0);//unbind vertex array object

    //create shader program
    const GLchar* vsPath = "res/rectfall.vs";
    const GLchar* fragPath = "res/rectfall.frag";
    this->_shader = new Shader(vsPath, fragPath);

    //gen texture about
    this->genTexture(_textureOne, "res/container.jpg");
    this->genTexture(_textureTwo, "res/awesomeface.png");
}

void RectFall::draw(){
    //use shader programs
    this->_shader->useProgram();

    //set uniform value
    GLfloat curTime = glfwGetTime();                          // current time
    GLfloat uniMixRate = (cos(2 * curTime) + 1.0f) / 2.0f;
    glUniform1f(_shader->getUniformLocation("uniMixRate"), uniMixRate);

    //set uniform, the transform
    glm::float32 curRotate = glm::radians(-64.0f * curTime);
    curRotate = uniMixRate * glm::radians(360.0f);
    glm::mat4 modelMatrix(1.0f);
    modelMatrix = glm::translate(modelMatrix, glm::vec3(0.0f, -0.55f, 0.0f));
    modelMatrix = glm::rotate(modelMatrix, curRotate, glm::vec3(1.0f, 0.0f, 0.0f));
    modelMatrix = glm::translate(modelMatrix, glm::vec3(0.0f, 0.55f, 0.0f));

    glm::mat4 viewMatrix(1.0f);
    viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, -2.5f));

    glm::mat4 projectionMatrix(1.0f);
    projectionMatrix = glm::perspective(glm::radians(45.0f), 16.0f/9.0f, 0.1f, 100.0f);

    glUniformMatrix4fv(_shader->getUniformLocation("modelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
    glUniformMatrix4fv(_shader->getUniformLocation("viewMatrix"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
    glUniformMatrix4fv(_shader->getUniformLocation("projectionMatrix"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));

    //bind texture
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _textureOne);
    glUniform1i(_shader->getUniformLocation("textureOne"), 0);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, _textureTwo);
    glUniform1i(_shader->getUniformLocation("textureTwo"), 1);

    //draw the Rectangles
    glBindVertexArray(_VAO);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // GL_LINE  GL_FILL   set the wireframe mode
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

加载纹理方法:

void RectBox::genTexture(GLuint &texture, const char *filename){
    std::cout << "filename = " << filename << std::endl;
    //gen texture
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    //set wrapping
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
    //set filter
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //load image
    int width, height;
    unsigned char* image = SOIL_load_image(filename, &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    //free image
    SOIL_free_image_data(image);
    glBindTexture(GL_TEXTURE_2D, 0);
}

着色器代码:

rectfall.vs

#version 330 core
layout (location = 0) in vec3 v_pos;
layout (location = 1) in vec3 v_color;
layout (location = 2) in vec2 v_textureCoord;

out vec4 f_vertexColor;
out vec2 f_textureCoord;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

void main () {
  gl_Position    =  projectionMatrix * viewMatrix * modelMatrix * vec4(v_pos, 1.0);
  f_vertexColor  = vec4(v_color, 1.0f);
  f_textureCoord = v_textureCoord;
}

rectfall.frag

#version 330 core
in vec4 f_vertexColor;
in vec2 f_textureCoord;

out vec4 color;

uniform  float uniMixRate;
uniform  sampler2D textureOne;
uniform  sampler2D textureTwo;

void main () {
  vec4 boxColor  = texture(textureOne, f_textureCoord);
  vec4 faceColor = texture(textureTwo, vec2(f_textureCoord.x, 1.0f - f_textureCoord.y));
  float mixRate = uniMixRate;
  if(mixRate > 0.3f){
    mixRate = 0.3f;
  }
  color = mix(boxColor, faceColor, mixRate);

}

原文地址:https://www.cnblogs.com/xingchong/p/12109244.html

时间: 2024-11-05 19:31:03

OpenGL学习——变换——绕x轴来回旋转的木板的相关文章

OpenGl学习进程(9)在3D空间的绘制实例

    本节将演示在3D空间中绘制图形的几个简单实例:     (1)在3D空间内绘制圆锥体: #include <GL/glut.h> #include <math.h> #pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") #define PI 3.1416 GLfloat xRot = 0; GLfloat yRot =

OpenGL学习脚印: 向量和矩阵要点(math-vector and matrices)

写在前面 前面几节内容环境搭建,绘制三角形,以及使用索引绘制,让我们对现代OpenGL中绘图做了简单了解.要继续后面的部分,需要熟悉OpenGL中涉及的数学知识.因此本节开始介绍OpenGL中的基本数学. 介绍这部分内容的主旨在于对OpenGL涉及的数学有个整体把握,重点把握一些概念在OpenGL中的应用.内容尽量以例子形式说明,仅在必要时会给出数学证明.一个主题往往涉及过多内容,对于文中省略的部分,请参考相应的教材. 通过本节可以了解到 向量基本概念和操作 矩阵的基本概念和操作 GLM数学库

OpenGL学习脚印: 视变换(view transformation)

写在前面 OpenGL中的坐标处理过程包括模型变换.视变换.投影变换.视口变换等内容,这个主题的内容有些多,因此分节学习,主题将分为5节内容来学习.上一节模型变换,本节学习模型变换的下一阶段--视变换.到目前位置,主要在2D下编写程序,学习了视变换后,我们可以看到3D应用的效果了.本节示例程序均可在我的github下载. 通过本节可以了解到 视变换的概念 索引绘制立方体 LookAt矩阵的推导(对数学不感兴趣,可以跳过) 相机位置随时间改变的应用程序 坐标处理的全局过程(了解,另文详述) Ope

绕任意轴旋转的矩阵推导总结

前言 常用的几何变换中旋转是较为复杂的一种,最近看<Physically Based Rendering, Second Edition: From Theory To Implementation>一书涉及绕任意轴旋转的实现,也给出了大体思路,但具体的推导过程及最后的旋转矩阵并未直接地给出,故根据参考Animated CGEM: Rotation About an Arbitrary Axis总结(欢迎指正). (一)基础 1.点乘与叉乘 点乘(dot)亦称作内积或数量积,如图,a·b =

定点绕定轴旋转

通过两个已知点,绕定轴旋转360次,每次旋转一度,获得360个点,一次相连,可以获得一个近似圆.网上找的公式基本都是左手坐标系下的选择公式,故而进行了多次坐标转换 1.3.2版本 public static List<Vector3d> Translate(List<Vector3> list) { List<Vector3d> zuoshou = new List<Vector3d>();//存左手坐标系下的各点坐标 List<Vector3d>

cocos2dx--- Node 绕Y轴旋转

step += 5; float ra = (float) CC_DEGREES_TO_RADIANS(step); float i = sinf(ra) * pNode->getCamera()->getZEye(); float j = cosf(ra) * pNode->getCamera()->getZEye(); pNode->getCamera()->setEyeXYZ(i, 0, j); cocos2dx--- Node 绕Y轴旋转,布布扣,bubuko.

pandas学习(创建多层索引、数据重塑与轴向旋转)

pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组,Series也可以创建多层索引. s = Series(np.random.randint(0,150,size=6),index=[['a','a','b','b','c','c'],['期中','期末','期中','期末','期中','期末']]) # 输出 a 期中 59 期末 4

OpenGl学习之坐标变换(上)

      坐标变换是深入理解三维世界的基础,非常重要.学习这部分首先要清楚几个概念:视点变换.模型变换.投影变换.视口变换. 在现实世界中,所有的物体都具有三维特征,但计算机本身只能处理数字,显示二维的图形,因此我们要将三维物体用二维数据表示出来,这一联系的点就是坐标.在OpenGL三维空间中坐标的形式有两种:世界坐标系和局部坐标系. ①世界坐标系:始终固定不变.举例,以太阳系中心太阳为中心原点,建立世界坐标系的话,地球绕太阳的公转运动是世界坐标的变换. ②局部坐标系:物体本身的中心.地球的自

OpenGL学习之路(二)

1 引子 在上一篇读书笔记中,我们对书本中给出的例子进行详细的分析.首先是搭出一个框架:然后填充初始化函数,在初始化函数中向OpenGL提供顶点信息(缓冲区对象)和顶点属性信息(顶点数组对象),并启用顶点数组对象:最后填充绘制函数,首先清空颜色缓存,然后调用glDrawArray来绘制基本图形.例子中使用的坐标都是二维坐标,所以画出来的图形是二维图形(这里是两个三角形),而我们知道OpenGL最主要是用来进行三维图形的渲染的,所以有必要在学习OpenGL相关API之前对三维变换做一个简要的介绍.