1.3 Visual Studio-OpenGL矩阵变换

主要节介绍Visual Studio-OpenGL矩阵变换,以及模型、观察、投影矩阵的创建及坐标变换,以及代码实现。

参考视频

1. https://b23.tv/rpak2Ht

2. https://b23.tv/9tlsJh0

3.  https://b23.tv/8yhEDQE

参考网址:OpenGL教程: http://www.opengl-tutorial.org/    Tutorial3

主程序代码为:

​
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> 
#include <glm/gtx/transform.hpp>
#include <common/shader.hpp>
using namespace glm;

// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
   -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f,  1.0f, 0.0f,
};

int main() {
    // Initialise GLFW
    glewExperimental = true; // Needed for core profile
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL 

    // Open a window and create its OpenGL context
    GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
    if (window == NULL) {
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        return -1;
    }

     glfwMakeContextCurrent(window); // Initialize GLEW
    glewExperimental = true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

	// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
	glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float)4 / (float)3, 0.1f, 100.0f);
	// Or, for an ortho camera :
	//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates

	// Camera matrix
	glm::mat4 View = glm::lookAt(
		glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
		glm::vec3(0, 0, 0), // and looks at the origin
		glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
	);

	// Model matrix : an identity matrix (model will be at the origin)
	glm::mat4 Model = glm::mat4(1.0f);
	// Our ModelViewProjection : multiplication of our 3 matrices
	glm::mat4 mvp = Projection * View * Model; // Remember, matrix multiplication is the other way around


    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // This will identify our vertex buffer
    GLuint vertexbuffer;
    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);
    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

	// Create and compile our GLSL program from the shaders
	GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");
	GLuint MatrixID = glGetUniformLocation(programID, "MVP");

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do {
		
        // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		// Use our shader
		glUseProgram(programID);
        // Draw nothing, see you in tutorial 2 !
        // 1st attribute buffer : vertices
        glEnableVertexAttribArray(0);
        //glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );
		// Send our transformation to the currently bound shader, in the "MVP" uniform
        // This is done in the main loop since each model will have a different MVP matrix (At least for the M part)
		glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);
        // Draw the triangle !
		
        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
        glDisableVertexAttribArray(0);

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents(); 

    } // Check if the ESC key was pressed or the window was closed
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
        glfwWindowShouldClose(window) == 0);
}

​

顶点着色器SimpleVertexShader.vertexshader 文件中GLSL代码为:

#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
void main(){
   // Output position of the vertex, in clip space : MVP * position
  gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
}


片段着色器SimpleFragmentShader.fragmentshader 文件、shader.hpp 文件中的代码同博客<1.2.2 Visual Studio OpenGL着色器语言文件读取>,链接为:Visual Studio OpenGL着色器语言文件读取-CSDN博客

运行结果为

24966e7e995b47ff9dee0b91b284661f.png

相关推荐

  1. OpenGL 变换(Transformations)编程

    2024-01-08 00:08:01       55 阅读
  2. openGL 三:矩阵和向量

    2024-01-08 00:08:01       55 阅读
  3. 矩阵的初等变换

    2024-01-08 00:08:01       51 阅读
  4. 齐次变换矩阵

    2024-01-08 00:08:01       43 阅读
  5. <Halcon> 变换矩阵求解

    2024-01-08 00:08:01       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-08 00:08:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-08 00:08:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-08 00:08:01       82 阅读
  4. Python语言-面向对象

    2024-01-08 00:08:01       91 阅读

热门阅读

  1. leetcode-字母异位词分组

    2024-01-08 00:08:01       56 阅读
  2. 智能寻迹避障机器人设计(第一章)

    2024-01-08 00:08:01       43 阅读
  3. 吉林大学 分布计算系统 ppt文字版

    2024-01-08 00:08:01       39 阅读
  4. 【LeetCode周赛】第379场周赛

    2024-01-08 00:08:01       57 阅读
  5. elk日志分析系统

    2024-01-08 00:08:01       66 阅读
  6. yolov8 PTQ和QAT量化实战(源码详解)

    2024-01-08 00:08:01       83 阅读
  7. linux 终端获取键值

    2024-01-08 00:08:01       60 阅读
  8. R语言-受限三样次条回归RCS-基于logistic

    2024-01-08 00:08:01       57 阅读
  9. Linux 定时任务管理

    2024-01-08 00:08:01       61 阅读
  10. 后台管理项目的多数据源方案

    2024-01-08 00:08:01       111 阅读
  11. 十二、Seata的⾼可⽤

    2024-01-08 00:08:01       56 阅读
  12. Android 车联网——多屏多用户(十五)

    2024-01-08 00:08:01       57 阅读
  13. bash 双hash算法sha256的写法

    2024-01-08 00:08:01       62 阅读