因为学的是Games development,课程当然要与游戏相关了。这门课是我们的programme leader讲的,毕业论文拿92的大牛,感觉在他的课上收益很多,虽然还是刚去国外的时候听不大懂。。(泪奔)但是硬逼着自己课下学,熬了多少夜好歹最后也拿了个60分。
这门课是从opengl入手讲的计算机图形学,同时配着数学课来上的。这里要说下国外学生的渣数学,625开方还要计算机。
详细介绍下,The introduction to graphics progrmming. 教材是老师自己写的,一共100课,前10课用的glew配合glm写简单的图形,再往后就是用的老师自己写的一个框架,这样省了很多力气,现在还记得当时看见打上贴图的箱子自己那个兴奋感。15课往后就开始学shader了,配合着上课讲颜色,光照等等,学了很多知识,印象最深刻的还是第一课讲24帧与30帧差在哪。到再往后60课讲terrain和skybox,动态模糊之类的,不过这时候也逼近结课。这门课让我第一次对游戏开发有了那么一点认识。
结课作业是要求做一个3d场景,我做的是一个太空场景,有飞船,星球,会自转,公转,陨石,两个星期熬夜完工,很粗糙,扣分项在于光照的种类以及post process种类不多,整个场景可以在chase camera,free camera和 target camera之间切换。
放几个手写的shader记录一下吧。
phong vert shader
#version 400
uniform mat4 model;
uniform mat4 MVP;
uniform mat3 normal_matrix;
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 tex_coord;
out vec3 vertex_position;
out vec3 transformed_normal;
out vec2 vertex_tex_coord;
void main()
{
//Pass through position
vertex_position = (model * vec4(position,1.0)).xyz;
//gl_Position
gl_Position = MVP * vec4 (position, 1.0);
//Transformed normals
transformed_normal = normal_matrix * normal;
//Pass through the coord
vertex_tex_coord = tex_coord;
}
#version 400
//Previous render pass texture
uniform sampler2D tex;
//Previous frame texture
uniform sampler2D previous_frame;
//How much to blend the frames together
uniform float blend_factor;
in vec2 vertex_tex_coord;
out vec4 colour;
void main()
{
//Sample the two textures
vec4 col1 = texture(tex, vertex_tex_coord);
vec4 col2 = texture(previous_frame, vertex_tex_coord);
//Mix the samples by the blend factor
colour = mix(col1,col2,blend_factor);
//Ensure alpha is 1
colour.a =1.0;
}
高斯模糊的fragment shader
#version 400
uniform sampler2D tex;
//1.0f/screen width
uniform float inverse_width;
//1.of/screen height
uniform float inverse_height;
uniform vec2 direction;
//Surrounding pixels to sample and their scale
const vec4 samples[7] = vec4[7]
(
vec4(-3.0,-3.0,0.0,0.015625),
vec4(-2.0,-2.0,0.0,0.09375),
vec4(-1.0,-1.0,0.0,0.234375),
vec4(0.0,0.0,0.0,0.3125),
vec4(1.0,1.0,0.0,0.234375),
vec4(2.0,2.0,0.0,0.09375),
vec4(3.0,3.0,0.0,0.015625)
);
in vec2 vertex_tex_coord;
out vec4 colour;
void main()
{
//Start with black
colour = vec4 (0.0,0.0,0.0,0.0);
for (int i =0; i< 7;i++)
{
//Calculate the texture coordinate to sample
vec2 tex_coord = vertex_tex_coord + vec2(samples[i].x*direction.x*inverse_width,samples [i].y*direction.y*inverse_height);
//Sample the texture and scale accordingly
colour+= samples[i].w*texture(tex, tex_coord);
}
//Ensure alpha is 1
colour.a =1.0;
}
放几个截图,skybox是用Photoshop加HDRshop两个软件配合切出来的,有点小瑕疵在缝合处但不注意看不出来的。贴图都是在deviantArt和找的免费贴图还有最后交作业前放上去的飞船模型配合动态模糊和高斯模糊但是效果不好。。。
放几个星球自转,公转的代码:
void automove(float deltaTime,float a,float b) {
float fast_x = cosf(pi<float>()*2*a);
float fast_y = sinf(pi<float>()*2*a);
float fast_z = sinf(pi<float>()*2*a);
float slow_x = cosf(pi<float>()*2*b);
float slow_y = sinf(pi<float>()*2*b);
float slow_z = sinf(pi<float>()*2*b);
//Autorotation
sphere[2]->trans.rotate(quarter_pi<float>()*deltaTime,vec3(1.0f,1.0f,0.0f));
sphere[0]->trans.rotate(quarter_pi<float>()*deltaTime,vec3(1.0f,1.0f,0.0f));
//Revolution
//small one
sphere[2]->trans.position=vec3((sphere[1]->trans.position.x+57.0f*fast_x),
(sphere[1]->trans.position.y+18.0f*fast_y),
(sphere[1]->trans.position.z+15.0f*fast_z));
//big one
sphere[1]->trans.position=vec3((sphere[3]->trans.position.x-65.0f*slow_x),
(sphere[3]->trans.position.y+45.0f*slow_y),
(sphere[3]->trans.position.z+70.0f*slow_z));
}
飞船后面的能量团用了两个自旋的torus做的哈哈。