diff --git a/.gitignore b/.gitignore index 8965e0f..d48a716 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ Release x64 .git ipch +shader.VC.db +shader.VC.VC.opendb diff --git a/.vs/shader/v14/.suo b/.vs/shader/v14/.suo new file mode 100644 index 0000000..64e3f10 Binary files /dev/null and b/.vs/shader/v14/.suo differ diff --git a/scene.cpp b/scene.cpp index 3bf965e..cd7dfa1 100644 --- a/scene.cpp +++ b/scene.cpp @@ -3,63 +3,36 @@ #include "utils.h" #include "ground.h" -GLuint vbo, ebo; -GLuint program; -GLint positionLocation, modelMatrixLocation, viewMatrixLocation, projectionMatrixLocation, colorLocation;//顶点、模型矩阵、视图矩阵、投影矩阵的位置 -GLint texcoordLocation, textureLocation;//顶点坐标插槽位置、贴图插槽位置 GLuint texture; glm::mat4 modelMatrix, viewMatrix, projectionMatrix; Ground ground; +GLint textureLocation; +Shader*shader; +VertexBuffer*vertexbuffer; void Init() { - /*1.初始化vbo并为vbo赋值*/ - float data[] = { - -0.2f,-0.2f,0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f, 0.0f,0.0f, - 0.2f,-0.2f,0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f, 1.0f,0.0f, - 0.0f,0.2f,0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f, 0.5f,1.0f - }; - glGenBuffers(1, &vbo);//创建vbo - glBindBuffer(GL_ARRAY_BUFFER, vbo);//绑定下面要操作的vbo - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 30, data, GL_STATIC_DRAW);//发送cpu数据到GPU - glBindBuffer(GL_ARRAY_BUFFER, 0);//关闭绑定 - - /*1.2使用vbo定义绘画顺序*/ - unsigned short indexes[] = { 0,1,2 }; - glGenBuffers(1, &ebo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * 3, indexes, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + /* 初始化vbo并为vbo赋值 */ + vertexbuffer = new VertexBuffer; + vertexbuffer->SetSize(3); + + vertexbuffer->SetPosition(0, -0.2f, -0.2f, 0.0f); + vertexbuffer->SetTexcoord(0, 0.0f, 0.0f); + vertexbuffer->SetColor(0, 1.0f, 1.0f, 1.0f); - /*2.编译shader*/ - int fileSize = 0; - unsigned char * shaderCode = LoadFileContent("Res/test.vs", fileSize); - GLuint vsShader = CompileShader(GL_VERTEX_SHADER, (char*)shaderCode);//编译 vertex shader - delete shaderCode; + vertexbuffer->SetPosition(1, 0.2f, -0.2f, 0.0f); + vertexbuffer->SetTexcoord(1, 1.0f, 0.0f); + vertexbuffer->SetColor(1, 1.0f, 0.0f, 0.0f); - shaderCode = LoadFileContent("Res/test.fs", fileSize); - GLuint fsShader = CompileShader(GL_FRAGMENT_SHADER, (char*)shaderCode);//编译 fragment shader - delete shaderCode; + vertexbuffer->SetPosition(2, 0.0f, 0.2f, 0.0f); + vertexbuffer->SetTexcoord(2, 0.5f, 1.0f); + vertexbuffer->SetColor(2, 0.0f, 1.0f, 0.0f); - /*3.链接 vertex shader 和 fragment shader*/ - program = CreateProgram(vsShader, fsShader); - glDeleteShader(vsShader); - glDeleteShader(fsShader); + /* 初始化 shader */ + shader = new Shader; + shader->Init("Res/test.vs", "Res/test.fs"); - /*4.获取顶点、模型矩阵、视图矩阵、投影矩阵,在GPU中的位置,有这个位置后面才能对其进行赋值操作*/ - /* - attribute vec4 position; - uniform mat4 ModelMatrix; - uniform mat4 ViewMatrix; - uniform mat4 ProjectionMatrix; - */ - positionLocation = glGetAttribLocation(program, "position"); - colorLocation = glGetAttribLocation(program, "color"); - texcoordLocation = glGetAttribLocation(program, "texcoord"); + textureLocation = glGetUniformLocation(shader->mProgram, "U_Texture"); - modelMatrixLocation = glGetUniformLocation(program, "ModelMatrix"); - viewMatrixLocation = glGetUniformLocation(program, "ViewMatrix"); - projectionMatrixLocation = glGetUniformLocation(program, "ProjectionMatrix"); - textureLocation = glGetUniformLocation(program, "U_Texture"); modelMatrix = glm::translate(0.0f, 0.0f, -0.6f); texture = CreateTexture2DFromBMP("Res/test.bmp"); @@ -67,7 +40,6 @@ void Init() { ground.Init(); } void SetViewPortSize(float width, float height) { - //5.1.设置投影矩阵 projectionMatrix = glm::perspective(60.0f, width / height, 0.1f, 1000.0f); } void Draw() { @@ -77,34 +49,10 @@ void Draw() { ground.Draw(viewMatrix, projectionMatrix); - glUseProgram(program); - - /*5.2.把MVP矩阵赋值到显卡中*/ - glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));//设置模型矩阵 - glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix)); - glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projectionMatrix)); - - - /*6.设置绘制的数据*/ + vertexbuffer->Bind(); + shader->Bind(glm::value_ptr(modelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix)); glBindTexture(GL_TEXTURE_2D, texture); - glUniform1i(textureLocation, 0);//设置贴图 - glBindBuffer(GL_ARRAY_BUFFER, vbo);//绑定vbo,就是1部分设置的几个顶点数据 - glEnableVertexAttribArray(positionLocation);//启用顶点插槽 - glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, 0);//把vbo顶点数据填入顶点插槽 - glEnableVertexAttribArray(colorLocation);//启用颜色插槽 - glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(sizeof(float) * 4));//把vbo颜色数据填入颜色插槽 - glEnableVertexAttribArray(texcoordLocation); - glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(sizeof(float) * 8)); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - - /*7.绘制*/ - //glDrawArrays(GL_TRIANGLES, 0, 3);//顺序绘制 - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);//按照ebo顺序绘制 - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - - /*8.解除绑定*/ - glUseProgram(0); + glUniform1i(textureLocation, 0); + glDrawArrays(GL_TRIANGLES, 0, 3); + vertexbuffer->Unbind(); } \ No newline at end of file diff --git a/shader.VC.db b/shader.VC.db index 9e35511..b887171 100644 Binary files a/shader.VC.db and b/shader.VC.db differ