blobt
5 years ago
8 changed files with 164 additions and 32 deletions
-
8Res/model.fs
-
12Res/model.vs
-
116model.cpp
-
14model.h
-
38scene.cpp
-
BINshader.VC.db
-
2shader.vcxproj
-
6shader.vcxproj.filters
@ -0,0 +1,8 @@ |
|||
#ifdef GL_ES |
|||
precision mediump float; |
|||
#endif |
|||
varying vec4 V_Color; |
|||
void main() |
|||
{ |
|||
gl_FragColor=V_Color; |
|||
} |
@ -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; |
|||
} |
@ -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); |
|||
} |
@ -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); |
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue