|
|
@ -91,9 +91,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
|
glewInit(); |
|
|
|
|
|
|
|
/*创建program*/ |
|
|
|
GLuint program = CreateGPUProgram("res/shader/MixLight.vs", "res/shader/MixLight.fs"); |
|
|
|
GLuint program = CreateGPUProgram("res/shader/test.vs", "res/shader/test.fs"); |
|
|
|
|
|
|
|
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation, surfaceColorLocation; |
|
|
|
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation; |
|
|
|
posLocation = glGetAttribLocation(program, "pos"); |
|
|
|
texcoordLocation = glGetAttribLocation(program, "texcoord"); |
|
|
|
normalLocation = glGetAttribLocation(program, "normal"); |
|
|
@ -104,12 +104,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
|
NMLocation = glGetUniformLocation(program, "NM"); |
|
|
|
textureLocation = glGetUniformLocation(program, "U_MainTexture"); |
|
|
|
|
|
|
|
/*使用subroutine*/ |
|
|
|
surfaceColorLocation = glGetSubroutineUniformLocation(program, GL_FRAGMENT_SHADER, "U_SurfaceColor"); |
|
|
|
GLuint ambientLightIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "Ambient"); |
|
|
|
GLuint diffuseLightIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "Diffuse"); |
|
|
|
GLuint specularLightIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "Specular"); |
|
|
|
|
|
|
|
/*load model*/ |
|
|
|
unsigned int *indexes = nullptr; |
|
|
|
int vertexCount = 0, indexCount = 0; |
|
|
@ -122,21 +116,16 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
|
printf("load obj model fail\n"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*创建vao*/ |
|
|
|
GLuint vao = CreateVAOWithVBOSettings([&]()->void { |
|
|
|
/*创建vbo*/ |
|
|
|
GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo); |
|
|
|
glEnableVertexAttribArray(posLocation); |
|
|
|
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); |
|
|
|
glEnableVertexAttribArray(texcoordLocation); |
|
|
|
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); |
|
|
|
glEnableVertexAttribArray(normalLocation); |
|
|
|
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5)); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
|
|
}); |
|
|
|
/*创建vbo*/ |
|
|
|
GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo); |
|
|
|
glEnableVertexAttribArray(posLocation); |
|
|
|
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); |
|
|
|
glEnableVertexAttribArray(texcoordLocation); |
|
|
|
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); |
|
|
|
glEnableVertexAttribArray(normalLocation); |
|
|
|
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5)); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
|
|
|
|
|
|
/*创建IBO*/ |
|
|
|
GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes); |
|
|
@ -181,29 +170,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
|
glBindTexture(GL_TEXTURE_2D, mainTexture); |
|
|
|
glUniform1i(textureLocation, 0); |
|
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(vao); |
|
|
|
|
|
|
|
model = glm::translate(-1.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f); |
|
|
|
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &ambientLightIndex);/*选的你要使用的subroutine*/ |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); |
|
|
|
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT,0); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
|
|
|
|
|
|
model = glm::translate(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f); |
|
|
|
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &diffuseLightIndex);/*选的你要使用的subroutine*/ |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); |
|
|
|
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
|
|
|
|
|
|
model = glm::translate(1.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f); |
|
|
|
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &specularLightIndex);/*选的你要使用的subroutine*/ |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); |
|
|
|
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
|
|
|
|
|
|
glBindVertexArray(0); |
|
|
|
glUseProgram(0); |
|
|
|