OpenGL.Tutorial03_Matrices_测试
1、
2、
// ZC: 工程-->右键-->属性--> 配置属性:
// ZC: C/C++ -->常规-->附加包含目录,里面添加:
// ZC: E:\OpenGL_something\glfw-3.2.1.bin.WIN32\include
// ZC: E:\OpenGL_something\glm-0.9.8.5
// ZC: E:\OpenGL_something\glew-2.1.0\include
// ZC: 链接器-->输入-->附加依赖项,里面添加:
// ZC: E:\OpenGL_something\glfw-3.2.1.bin.WIN32\lib-vc2010\glfw3.lib 这个应该是静态链接的lib(动态的貌似是glfw3dll.lib[ZC:我是看文件大小判断的...])
// ZC: E:\OpenGL_something\glew-2.1.0\lib\Release\Win32\glew32.lib
// ZC: opengl32.lib
// ZC: glu32.lib
// ZC: kernel32.lib
// ZC: user32.lib
// ZC: gdi32.lib
// ZC: winspool.lib
// ZC: shell32.lib
// ZC: ole32.lib
// ZC: oleaut32.lib
// ZC: uuid.lib
// ZC: comdlg32.lib
// ZC: advapi32.lib
// ZC: 关于使用glew32.lib还是glew32s.lib:
// ZC: 看文件大小判断 静态链接的lib应该是glew32s.lib,但是我在编译的时候,发现无法定位函数grewInit(...)... 于是只能使用动态的glew32s.lib
// ZC: 于是还要将"E:\OpenGL_something\glew-2.1.0\bin\Release\Win32\glew32.dll"复制到项目的"E:\Project_VS10\OpenGL_Console_zz\Test\Test"中 // Include standard headers
#include <stdio.h>
#include <stdlib.h> // Include GLEW
#include <GL/glew.h> // Include GLFW
#include <GLFW/glfw3.h>
extern GLFWwindow* window; // Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm; #include <common/shader.hpp> int mainTutorial03Matrices( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -;
} glfwWindowHint(GLFW_SAMPLES, );
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, );
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, );
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
/*window = glfwCreateWindow( 1024, 768, "Tutorial 03 - Matrices", NULL, NULL);*/
window = glfwCreateWindow( , , "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 -;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -;
} // Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); // Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f); GLuint VertexArrayID;
glGenVertexArrays(, &VertexArrayID);
glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "../Test/shaders/SimpleTransform_03.vertexshader", "../Test/shaders/SingleColor_03.fragmentshader" ); // Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP"); // 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), 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 Projection = glm::perspective(glm::radians(45.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(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)
// );
glm::mat4 View = glm::lookAt(
glm::vec3(,,),// Camera is at (4,3,3), in World Space
glm::vec3(,,), // and looks at the origin
glm::vec3(,,) // Head is up (set to 0,-1,0 to look upside-down)
);
// ZC: 上面的参数的理解:
// ZC: 第一个参数:摄像机所在的位置(世界坐标系)
// ZC: 第二个参数:摄像机往哪个点看过去(摄像机往哪个点的方向看去) (ZC: 这个应该是参与计算的)
// ZC: 第三个参数:头的位置(这里的"头" 实际就是指"摄像机"): (ZC: 这个稍微测试了一下,发现 应该就是指方向[即 其次坐标的"w"是0])
// ZC: (0,1,0):就是正常的人类头顶朝上的样子,
// ZC: (1,0,0):就是头往X轴正方向倒90°,
// ZC: (0,0,1):应该就是头朝Z轴正方向倒90°(也就是往后倒,稍微验证了一下,应该就是这样子的) // Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
//glm::mat4 Model = glm::mat4(
// 2.0f, 0.0f, 0.0f, 0.0f,
// 0.0f, 2.0f, 0.0f, 0.0f,
// 0.0f, 0.0f, 2.0f, 0.0f,
// 0.0f, 0.0f, 0.0f, 1.0f);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("\n"); // Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("\n"); 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,
};
//static const GLfloat g_vertex_buffer_data[] = {
// -2.0f, -2.0f, 0.0f,
// 2.0f, -2.0f, 0.0f,
// 0.0f, 2.0f, 0.0f,
//}; GLuint vertexbuffer;
glGenBuffers(, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); do{ // Clear the screen
glClear( GL_COLOR_BUFFER_BIT ); // Use our shader
glUseProgram(programID); // Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, , GL_FALSE, &MVP[][]); // 1rst attribute buffer : vertices
glEnableVertexAttribArray();
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
, // attribute. No particular reason for 0, but must match the layout in the shader.
, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
, // stride
(void*) // array buffer offset
); // Draw the triangle !
glDrawArrays(GL_TRIANGLES, , ); // 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(); // 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) == ); // Cleanup VBO and shader
glDeleteBuffers(, &vertexbuffer);
glDeleteProgram(programID);
glDeleteVertexArrays(, &VertexArrayID); // Close OpenGL window and terminate GLFW
glfwTerminate(); return ;
}
3、改变中间的一段代码 用于测试:
3.1、上面的代码执行的现象:

3.2、放大2倍(方式1):
// Model matrix : an identity matrix (model will be at the origin)
//glm::mat4 Model = glm::mat4(1.0f); // ZC: 变化的是这里,把X/Y/Z坐标都扩大了2倍
glm::mat4 Model = glm::mat4(
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 2.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("\n"); // Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("\n"); 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,
};
现象:

3.3、放大2倍(方式2):
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f); // ZC: 这里没改变
//glm::mat4 Model = glm::mat4(
// 2.0f, 0.0f, 0.0f, 0.0f,
// 0.0f, 2.0f, 0.0f, 0.0f,
// 0.0f, 0.0f, 2.0f, 0.0f,
// 0.0f, 0.0f, 0.0f, 1.0f);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("\n"); // Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("\n");//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,
//};
static const GLfloat g_vertex_buffer_data[] = { // ZC: 这里改变了,手动的将 三角形的3个点的坐标放大了
-2.0f, -2.0f, 0.0f,
2.0f, -2.0f, 0.0f,
0.0f, 2.0f, 0.0f,
};
现象:

3.4、测试:刚开始 放大 是使用的 “glm::mat4 Model = glm::mat4(2.0f);”,实际这是没有 放大效果的(视觉上 大小没变化),原因寻找:Model的矩阵信息为:
2.0f 0.0f 0.0f 0.0f
0.0f 2.0f 0.0f 0.0f
0.0f 0.0f 2.0f 0.0f
0.0f 0.0f 0.0f 2.0f
∵ 它把 X/Y/Z轴的数据放大了,但是 它的最后一行数据“0.0f 0.0f 0.0f 2.0f” 实现了类似这样的功能:把摄像机拉远了一倍距离。这个的数学解释 我暂时还不太明白,只是做了一个实验 验证了一下
3.4.1、
glm::mat4 View = glm::lookAt(
glm::vec3(,,),// Camera is at (4,3,3), in World Space
glm::vec3(,,), // and looks at the origin
glm::vec3(,,) // 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);
glm::mat4 Model = glm::mat4(2.0f); // ZC: 改了这里
//glm::mat4 Model = glm::mat4(
// 2.0f, 0.0f, 0.0f, 0.0f,
// 0.0f, 2.0f, 0.0f, 0.0f,
// 0.0f, 0.0f, 2.0f, 0.0f,
// 0.0f, 0.0f, 0.0f, 1.0f);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("\n"); // Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("\n");
现象:

3.4.2、
glm::mat4 View = glm::lookAt(
glm::vec3(,,),// ZC: 这里,手动将摄像机 拉远了一倍距离
glm::vec3(,,), // and looks at the origin
glm::vec3(,,) // 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);
//glm::mat4 Model = glm::mat4(2.0f);
glm::mat4 Model = glm::mat4( // ZC: 这里,将 X/Y/Z轴数据放大1倍
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 2.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("%f, %f, %f, %f\n", Model[].x, Model[].y, Model[].z, Model[].w);
printf("\n"); // Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("%f, %f, %f, %f\n", MVP[].x, MVP[].y, MVP[].z, MVP[].w);
printf("\n");
现象:

ZC:可以看到,虽然 矩阵Model 的信息不同,但是 最后的 矩阵MVP 的信息是一样的,∴ 肉眼看起来 效果一样...
4、尝试 C++中计算好位置,直接传入 GLSL(不要再在 GLSL中去做乘法计算)
ZC:貌似 这个例子中使用的 "glDrawArrays(GL_TRIANGLES, 0, 3);"的方式,暂时不支持实现 这个尝试,看后面会不会 学习到别的方式,或者 后面水平高了再来尝试吧...
5、
OpenGL.Tutorial03_Matrices_测试的更多相关文章
- OpenGL光照测试
OpenGL光照测试 花了大概半个月,研究了OpenGL的光照.请注意是固定管线渲染的光照,如果使用着色器的高手们请飘过.这个程序是通过光照对模型的照射,来研究OpenGL光照的性质.以后可以通过这个 ...
- Qt OpenGL裁剪测试
剪裁测试(Scissor Test)用于限制绘制区域. 我们可以指定一个矩形的剪裁窗口,当启用剪裁测试后,只有在这个窗口之内的像素才能被绘制,其它像素则会被丢弃. 换句话说,无论怎么绘制,剪裁窗口以外 ...
- opengl 模板测试 glStencilOp glStencilFunc
下面来设置蒙板缓存和蒙板测试. 首先我们启用蒙板测试,这样就可以修改蒙板缓存中的值. 下面我们来解释蒙板测试函数的含义: 当你使用glEnable(GL_STENCIL_TEST)启用蒙板测试之后,蒙 ...
- OpenGL(十四) 模板测试
启用模板测试时,OpenGL会在内存中开辟一块空间作为模板缓冲区,里边保存了每个像素的"模板值",模板测试的过程就是把每一个像素的模板值与一个设定的模板参考值进行比较,符合设定条件 ...
- opengl学习-利用模板测试勾画物体轮廓中出现的一个问题
我在学习OpenGL模板测试勾画物体轮廓的时候,出现了这个问题: 这个出现的原因就是,改变摄像机的时候,每次绘制,上次绘制中模板缓冲区的数据没有清除的原因.也就是在while循环开始的时候,glCle ...
- opengl入门学习
OpenGL入门学习 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640 ...
- OpenGL入门学习(转)
OpenGL入门学习 http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html 说起编程作图,大概还有很多人想起TC的#includ ...
- Android OpenGL ES .介绍
引自:http://blog.csdn.net/hgl868/article/details/6971624 1. OpenGL ES 简介 Android 3D引擎采用的是OpenGL ES. ...
- OpenGL理解
说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色 ...
随机推荐
- stm32高级定时器的应用——spwm
用过stm32定时器的朋友都知道,定时器的CCR寄存器,可以用来配置PWM的输出,但同样也可以用来配置spwm.废话不多说,直接上代码. 首先,你得考虑一下几个因素: 1.同步调制还是异步调制. 2 ...
- C#的抽象类和接口,区别与相似
一.抽象类:抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们 ...
- Go 初体验 - 反射
首先,反射虽强大, 但不可乱用,会有性能损耗 直接上代码: 输出: 代码并不难理解,不做解释
- Vue渐进式JavaScript 框架
1. Vue简介 1.1 初步了解Vue.js框架 Vue.js (读音 /vjuː/,类似于 view) 是一种轻量级前端MVVM框架.同时吸收了React(组件化)和Angular(灵活指令页面 ...
- CentOS7 源码编译安装Tengine
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.它的目的是打造一个高效.安全的Web平台. 发展 Tengine的性能和 ...
- ASP.NET MVC案例教程(七)
前言 写这篇文章的目的,是想总结一些东西,以帮助朋友们更好的使用这个框架.但是,我又不像把官方列举的哪些优势.功能翻译过来列举在这里.所以,我想干脆我就纯从个人观点上对这个框架评论一下吧.说的不好 ...
- conn.encoders[SafeBytes] = conn.encoders[bytes] KeyError: <class 'bytes'>
问题描述:Django连接mysql数据库,修改了setting.py文件后,启动服务器报错 错误截图如下: 解决方法: 1.pip install pymsql 2.在setting.py同目录下的 ...
- 使用Spring的@Async创建异步方法
使用Spring的@Async创建异步方法 在开发系统的过程中,通常会考虑到系统的性能问题,提升系统性能的一个重要思想就是“串行”改“并行”.说起“并行”自然离不开“异步”,今天我们就来聊聊如何使用S ...
- 煎蛋ooxx
pipeline.py class Jiandanline(FilesPipeline): def get_media_requests(self, item, info): for file_url ...
- Let'sEncrypt 免费通配符/泛域名SSL证书添加使用教程
Let'sEncrypt 免费通配符/泛域名SSL证书添加使用教程 通配符证书一般还是比较贵的一般最便宜的通配符证书5.60美元一年,只不过Let'sEncrypt的有效期是3个月,对于一般用户来说基 ...