Browse Source

绘制model

master
blobt 4 years ago
parent
commit
dc46ea52cd
  1. 8
      Res/model.fs
  2. 12
      Res/model.vs
  3. 116
      model.cpp
  4. 14
      model.h
  5. 38
      scene.cpp
  6. BIN
      shader.VC.db
  7. 2
      shader.vcxproj
  8. 6
      shader.vcxproj.filters

8
Res/model.fs

@ -0,0 +1,8 @@
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 V_Color;
void main()
{
gl_FragColor=V_Color;
}

12
Res/model.vs

@ -0,0 +1,12 @@
attribute vec4 position;
attribute vec4 color;
attribute vec2 texcoord;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
varying vec4 V_Color;
void main()
{
V_Color=color;
gl_Position=ProjectionMatrix*ViewMatrix*ModelMatrix*position;
}

116
model.cpp

@ -0,0 +1,116 @@
#include "model.h"
#include "utils.h"
Model::Model() {
}
void Model::Init(const char*modelPath) {
struct FloatData {
float v[3];
};
struct VertexDefine {
int posIndex;
int texcoordIndex;
int normalIndex;
};
int nFileSize = 0;
unsigned char*fileContent = LoadFileContent(modelPath, nFileSize);
if (fileContent == nullptr) {
return;
}
std::vector<FloatData> positions, texcoords, normals;
std::vector<VertexDefine> vertexes;
std::string temp;
std::stringstream ssFileContent((char*)fileContent);
char szOneLine[256];
while (!ssFileContent.eof()) {
memset(szOneLine, 0, 256);
ssFileContent.getline(szOneLine, 256);
if (strlen(szOneLine) > 0) {
if (szOneLine[0] == 'v') {
std::stringstream ssOneLine(szOneLine);
if (szOneLine[1] == 't') {
ssOneLine >> temp;
FloatData floatData;
ssOneLine >> floatData.v[0];
ssOneLine >> floatData.v[1];
texcoords.push_back(floatData);
}
else if (szOneLine[1] == 'n') {
ssOneLine >> temp;
FloatData floatData;
ssOneLine >> floatData.v[0];
ssOneLine >> floatData.v[1];
ssOneLine >> floatData.v[2];
normals.push_back(floatData);
}
else {
ssOneLine >> temp;
FloatData floatData;
ssOneLine >> floatData.v[0];
ssOneLine >> floatData.v[1];
ssOneLine >> floatData.v[2];
positions.push_back(floatData);
}
}
else if (szOneLine[0] == 'f') {
std::stringstream ssOneLine(szOneLine);
ssOneLine >> temp;
std::string vertexStr;
for (int i = 0; i < 3; i++) {
ssOneLine >> vertexStr;
size_t pos = vertexStr.find_first_of('/');
std::string posIndexStr = vertexStr.substr(0, pos);
size_t pos2 = vertexStr.find_first_of('/', pos + 1);
std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - 1 - pos);
std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - 1 - pos2);
VertexDefine vd;
vd.posIndex = atoi(posIndexStr.c_str());
vd.texcoordIndex = atoi(texcoordIndexStr.c_str());
vd.normalIndex = atoi(normalIndexStr.c_str());
vertexes.push_back(vd);
}
}
}
}
delete fileContent;
int vertexCount = (int)vertexes.size();
mVertexBuffer = new VertexBuffer;
mVertexBuffer->SetSize(vertexCount);
for (int i = 0; i < vertexCount; ++i) {
float *temp = positions[vertexes[i].posIndex - 1].v;
mVertexBuffer->SetPosition(i, temp[0], temp[1], temp[2]);
temp = texcoords[vertexes[i].texcoordIndex - 1].v;
mVertexBuffer->SetTexcoord(i, temp[0], temp[1]);
temp = normals[vertexes[i].normalIndex - 1].v;
mVertexBuffer->SetNormal(i, temp[0], temp[1], temp[2]);
}
mShader = new Shader;
mShader->Init("Res/model.vs", "Res/model.fs");
}
void Model::Draw(glm::mat4 & viewMatrix, glm::mat4 projectionMatrix) {
glEnable(GL_DEPTH_TEST);
mVertexBuffer->Bind();
mShader->Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix));
glDrawArrays(GL_TRIANGLES, 0, mVertexBuffer->mVertexCount);
mVertexBuffer->Unbind();
}
void Model::SetPosition(float x, float y, float z) {
mModelMatrix = glm::translate(x, y, z);
}

14
model.h

@ -0,0 +1,14 @@
#pragma once
#include "ggl.h"
#include "vertexbuffer.h"
#include "shader.h"
class Model {
VertexBuffer* mVertexBuffer;
Shader*mShader;
public:
glm::mat4 mModelMatrix;
Model();
void Init(const char*modelPath);
void Draw(glm::mat4 & viewMatrix, glm::mat4 projectionMatrix);
void SetPosition(float x, float y, float z);
};

38
scene.cpp

@ -2,39 +2,17 @@
#include "ggl.h"
#include "utils.h"
#include "ground.h"
#include "model.h"
glm::mat4 modelMatrix, viewMatrix, projectionMatrix;
Ground ground;
GLint textureLocation;
Shader*shader;
VertexBuffer*vertexbuffer;
Model model;
void Init() {
/* ³õʼ»¯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);
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);
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);
/* ³õʼ»¯ shader */
shader = new Shader;
shader->Init("Res/test.vs", "Res/test.fs");
shader->SetTexture("U_Texture", "Res/test.bmp");
shader->SetTexture("U_Texture2", "Res/test2.bmp");
modelMatrix = glm::translate(0.0f, 0.0f, -0.6f);
ground.Init();
model.Init("Res/Sphere.obj");
model.SetPosition(0.0f, 0.0f, -5.0f);
}
void SetViewPortSize(float width, float height) {
projectionMatrix = glm::perspective(60.0f, width / height, 0.1f, 1000.0f);
@ -45,9 +23,5 @@ void Draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ground.Draw(viewMatrix, projectionMatrix);
vertexbuffer->Bind();
shader->Bind(glm::value_ptr(modelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix));
glDrawArrays(GL_TRIANGLES, 0, 3);
vertexbuffer->Unbind();
model.Draw(viewMatrix, projectionMatrix);
}

BIN
shader.VC.db

2
shader.vcxproj

@ -145,6 +145,7 @@
<ItemGroup>
<ClCompile Include="ground.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="model.cpp" />
<ClCompile Include="scene.cpp" />
<ClCompile Include="shader.cpp" />
<ClCompile Include="utils.cpp" />
@ -153,6 +154,7 @@
<ItemGroup>
<ClInclude Include="ggl.h" />
<ClInclude Include="ground.h" />
<ClInclude Include="model.h" />
<ClInclude Include="scene.h" />
<ClInclude Include="shader.h" />
<ClInclude Include="utils.h" />

6
shader.vcxproj.filters

@ -33,6 +33,9 @@
<ClCompile Include="shader.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="model.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ggl.h">
@ -53,6 +56,9 @@
<ClInclude Include="shader.h">
<Filter>源文件</Filter>
</ClInclude>
<ClInclude Include="model.h">
<Filter>源文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Res\test.fs">

Loading…
Cancel
Save