Browse Source

使用instance绘制多个模型

master
blobt 4 years ago
parent
commit
fe5dc7cc05
  1. BIN
      .vs/shader2/v14/.suo
  2. 34
      main.cpp
  3. BIN
      res/image/niutou.bmp
  4. 25
      res/shader/diffuse.fs
  5. 20
      res/shader/diffuse.vs
  6. 4
      shader2.vcxproj
  7. 4
      shader2.vcxproj.filters

BIN
.vs/shader2/v14/.suo

34
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);
}

BIN
res/image/niutou.bmp

25
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;
}

20
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;
}

4
shader2.vcxproj

@ -159,8 +159,8 @@
<ClInclude Include="timer.h" />
</ItemGroup>
<ItemGroup>
<None Include="res\shader\test.fs" />
<None Include="res\shader\test.vs" />
<None Include="res\shader\diffuse.fs" />
<None Include="res\shader\diffuse.vs" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

4
shader2.vcxproj.filters

@ -46,10 +46,10 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="res\shader\test.fs">
<None Include="res\shader\diffuse.fs">
<Filter>源文件</Filter>
</None>
<None Include="res\shader\test.vs">
<None Include="res\shader\diffuse.vs">
<Filter>源文件</Filter>
</None>
</ItemGroup>
Loading…
Cancel
Save