OpenGl学习使用矩阵变换和摄像机(投影矩阵)

xiaoxiao2021-02-28  39

// Include standard headers #include <stdio.h> #include <stdlib.h> // Include GLEW #include <GL/glew.h> // Include GLFW #include <GLFW/glfw3.h> GLFWwindow* window; // Include GLM #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> using namespace glm; #include <common/shader.hpp> int main( void ) { // 初始化GLFW if( !glfwInit() ) { fprintf( stderr, "Failed to initialize GLFW\n" ); getchar(); return -1; } glfwWindowHint(GLFW_SAMPLES, 4);//4*抗锯齿 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//使用OpenGL的版本 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 适配Mac glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用新版本的OpenGl文件  // 创建窗口并命名 window = glfwCreateWindow( 1024, 768, "Tutorial 03 - Matrices", 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" ); getchar(); glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // 初始化 GLEW glewExperimental = true; //核心类需要这个 if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); getchar(); glfwTerminate(); return -1; } // 确保能接受esc键 glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); // 设置背景 glClearColor(0.0f, 0.0f, 0.4f, 0.0f); GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); //使用shader GLuint programID = LoadShaders( "SimpleTransform.vertexshader", "SingleColor.fragmentshader" ); // Get a handle for our "MVP" uniform GLuint MatrixID = glGetUniformLocation(programID, "MVP");//用glGetUniformLocation请求uniform ourColor的地址。我们为请求函数提供着色器程序和uniform的名字(这是我们希望获得的地址的来源)。如果glGetUniformLocation返回-1就代表没有找到这个地址。 // 投影矩阵,视角弧度为45,比例为4:3,显示范围为0.1到100 glm::mat4 Projection = glm::perspective(glm::radians(30.0f), 4.0f / 3.0f, 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(10,3,3), //摄像机在(4,3,3),世界坐标 glm::vec3(0,0,0), // 摄像机照向的点 glm::vec3(0,1,0)  // 向上抬(上下的范围是-1~1)   ); // 模型的矩阵,它在原点 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 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, };//定义三个点 GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); do{ //清空屏幕 glClear( GL_COLOR_BUFFER_BIT ); // 使用shader glUseProgram(programID); // 把我们的转换发送到当前绑定的着色器 // in the "MVP" uniform glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); //  第一个属性缓冲区:顶点缓冲区 glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( 0,                  //要和shader在一个层次. 3,                  // 几个点 GL_FLOAT,           // 类型 GL_FALSE,           // 是否标准化 0,                  // 指定连续顶点属性之间的偏移量。如果为0,那么顶点属性会被理解为:它们是紧密排列在一起的。初始值为0。 (void*)0            // 指定一个指针,指向数组中第一个顶点属性的第一个组件。初始值为0。 ); // 画出三角形 glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(0); // 清除缓存 glfwSwapBuffers(window); glfwPollEvents(); } // 检查esc是否被按下 while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&   glfwWindowShouldClose(window) == 0 ); // 清除顶点缓冲对象(Vertex Buffer Objects,VBO)和shader glDeleteBuffers(1, &vertexbuffer); glDeleteProgram(programID); glDeleteVertexArrays(1, &VertexArrayID); // 关闭窗口停止glfw glfwTerminate(); return 0; }
转载请注明原文地址: https://www.6miu.com/read-2632311.html

最新回复(0)