|
@ -91,12 +91,13 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
glewInit(); |
|
|
glewInit(); |
|
|
|
|
|
|
|
|
/*创建program*/ |
|
|
/*创建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"); |
|
|
posLocation = glGetAttribLocation(program, "pos"); |
|
|
texcoordLocation = glGetAttribLocation(program, "texcoord"); |
|
|
texcoordLocation = glGetAttribLocation(program, "texcoord"); |
|
|
normalLocation = glGetAttribLocation(program, "normal"); |
|
|
normalLocation = glGetAttribLocation(program, "normal"); |
|
|
|
|
|
offsetLocation = glGetAttribLocation(program, "offset"); |
|
|
MLocation = glGetUniformLocation(program, "M"); |
|
|
MLocation = glGetUniformLocation(program, "M"); |
|
|
VLocation = glGetUniformLocation(program, "V"); |
|
|
VLocation = glGetUniformLocation(program, "V"); |
|
|
PLocation = glGetUniformLocation(program, "P"); |
|
|
PLocation = glGetUniformLocation(program, "P"); |
|
@ -109,7 +110,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
int vertexCount = 0, indexCount = 0; |
|
|
int vertexCount = 0, indexCount = 0; |
|
|
Timer t; |
|
|
Timer t; |
|
|
t.Start(); |
|
|
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()); |
|
|
printf("load model cost %fs %d\n", t.GetPassedTime(), t.GetPassedTickers()); |
|
|
|
|
|
|
|
|
if (vertexes == nullptr) { |
|
|
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 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); |
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); |
|
|
glEnable(GL_DEPTH_TEST); |
|
|
glEnable(GL_DEPTH_TEST); |
|
@ -138,7 +149,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
0,0,0,1 |
|
|
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 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f); |
|
|
glm::mat4 normalMatrix = glm::inverseTranspose(model); |
|
|
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)); |
|
|
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5)); |
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
|
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); |
|
|
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); |
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
|
glUseProgram(0); |
|
|
glUseProgram(0); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Timer t2; |
|
|
while (true) { |
|
|
while (true) { |
|
|
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { |
|
|
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { |
|
|
if (msg.message == WM_QUIT) { |
|
|
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); |
|
|
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(); |
|
|
what(); |
|
|
|
|
|
glFlush(); |
|
|
|
|
|
|
|
|
|
|
|
printf("cost time %f\n",t2.GetPassedTime()*1000.0f); |
|
|
SwapBuffers(dc); |
|
|
SwapBuffers(dc); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|