diff --git a/.vs/shader2/v14/.suo b/.vs/shader2/v14/.suo index b099692..5ec0527 100644 Binary files a/.vs/shader2/v14/.suo and b/.vs/shader2/v14/.suo differ diff --git a/main.cpp b/main.cpp index 4dee409..e8e16e2 100644 --- a/main.cpp +++ b/main.cpp @@ -91,12 +91,13 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glewInit(); /*创建program*/ - GLuint program = CreateGPUProgram("res/shader/test.vs", "res/shader/test.fs"); + GLuint program = CreateGPUProgram("res/shader/diffuse.vs", "res/shader/diffuse.fs"); - GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation; + GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation; posLocation = glGetAttribLocation(program, "pos"); texcoordLocation = glGetAttribLocation(program, "texcoord"); normalLocation = glGetAttribLocation(program, "normal"); + offsetLocation = glGetAttribLocation(program, "offset"); MLocation = glGetUniformLocation(program, "M"); VLocation = glGetUniformLocation(program, "V"); PLocation = glGetUniformLocation(program, "P"); @@ -109,7 +110,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine int vertexCount = 0, indexCount = 0; Timer t; t.Start(); - VertexData* vertexes = LoadObjModel("res/model/Sphere.obj", &indexes, vertexCount, indexCount); + VertexData* vertexes = LoadObjModel("res/model/niutou.obj", &indexes, vertexCount, indexCount); printf("load model cost %fs %d\n", t.GetPassedTime(), t.GetPassedTickers()); if (vertexes == nullptr) { @@ -123,7 +124,17 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes); /*创建纹理*/ - GLuint mainTexture = CreateTextureFromFile("res/image/stone.jpg"); + GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp"); + + /* 定义了3个点表示3个模型的位置 */ + float posOffsets[] = { + -1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f + }; + /*创建offset VBO*/ + GLuint offsetVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(float)*9, GL_STATIC_DRAW, posOffsets); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glEnable(GL_DEPTH_TEST); @@ -138,7 +149,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 0,0,0,1 }; - glm::mat4 model = glm::translate(0.0f, 0.0f, -3.0f); + glm::mat4 model = glm::translate(0.0f, -0.5f, -4.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f,0.01f,0.01f); glm::mat4 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f); glm::mat4 normalMatrix = glm::inverseTranspose(model); @@ -171,12 +182,19 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5)); glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, offsetVBO); + glEnableVertexAttribArray(offsetLocation); + glVertexAttribPointer(offsetLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, (void*)0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glVertexAttribDivisor(offsetLocation, 1); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); - glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0,3); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glUseProgram(0); }; + Timer t2; while (true) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { @@ -187,8 +205,12 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + t2.Start(); + //model = glm::translate(-1.0f, -0.5f, -4.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f); what(); + glFlush(); + printf("cost time %f\n",t2.GetPassedTime()*1000.0f); SwapBuffers(dc); } diff --git a/res/image/niutou.bmp b/res/image/niutou.bmp new file mode 100644 index 0000000..3ebf2b5 Binary files /dev/null and b/res/image/niutou.bmp differ diff --git a/res/shader/diffuse.fs b/res/shader/diffuse.fs new file mode 100644 index 0000000..e01b1a4 --- /dev/null +++ b/res/shader/diffuse.fs @@ -0,0 +1,25 @@ +uniform sampler2D U_MainTexture; +varying vec3 V_Normal; +varying vec4 V_WorldPos;//当前的位置 +varying vec2 V_Texcoord; + +void main() { + + vec3 lightPos = vec3(0.0, 10.0, 0.0); + vec3 L=lightPos; + L = normalize(L); + vec3 n = normalize(V_Normal); + + + //ambient + vec4 AmbientLightColor = vec4(0.2, 0.2, 0.2, 1.0); + vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0); + vec4 AmbientColor = AmbientLightColor*AmbientMaterial; + + //diffuse + vec4 DiffuseLightColor = vec4(1.0,1.0,1.0,1.0); + vec4 DiffuseMaterial = vec4(0.8, 0.8, 0.8, 1.0); + vec4 diffuseColor = DiffuseLightColor*DiffuseMaterial*max(0.0, dot(L,n)); + + gl_FragColor = AmbientColor + texture2D(U_MainTexture, V_Texcoord) * diffuseColor; +} \ No newline at end of file diff --git a/res/shader/diffuse.vs b/res/shader/diffuse.vs new file mode 100644 index 0000000..466405d --- /dev/null +++ b/res/shader/diffuse.vs @@ -0,0 +1,20 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; +attribute vec3 offset; + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform mat4 NM; + +varying vec3 V_Normal; +varying vec4 V_WorldPos;//当前的位置 +varying vec2 V_Texcoord; + +void main() { + V_Normal = mat3(NM)*normal; + V_WorldPos = M * vec4(pos, 1.0) + vec4(offset, 1.0f); + V_Texcoord = texcoord; + gl_Position = P*V*V_WorldPos; +} \ No newline at end of file diff --git a/shader2.vcxproj b/shader2.vcxproj index f809b4f..1495ce5 100644 --- a/shader2.vcxproj +++ b/shader2.vcxproj @@ -159,8 +159,8 @@ - - + + diff --git a/shader2.vcxproj.filters b/shader2.vcxproj.filters index 403f9b3..5294a38 100644 --- a/shader2.vcxproj.filters +++ b/shader2.vcxproj.filters @@ -46,10 +46,10 @@ - + 婧愭枃浠 - + 婧愭枃浠