blobt
5 years ago
12 changed files with 230 additions and 81 deletions
-
BIN.vs/shader2/v14/.suo
-
97main.cpp
-
38misc.cpp
-
5misc.h
-
122model.cpp
-
9model.h
-
3res/shader/test.fs
-
5res/shader/test.vs
-
BINshader2.VC.db
-
10shader2.vcxproj
-
18shader2.vcxproj.filters
-
4test.fs
@ -0,0 +1,38 @@ |
|||||
|
#include "misc.h"
|
||||
|
#include <stdio.h>
|
||||
|
|
||||
|
GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void * data) |
||||
|
{ |
||||
|
GLuint object; |
||||
|
glGenBuffers(1, &object); |
||||
|
glBindBuffer(bufferType, object); |
||||
|
glBufferData(bufferType, size, data, usage); |
||||
|
glBindBuffer(bufferType, 0); |
||||
|
|
||||
|
return object; |
||||
|
} |
||||
|
|
||||
|
char* LoadFileContent(const char *path) { |
||||
|
FILE* pFile = fopen(path, "rb"); |
||||
|
if (pFile) { |
||||
|
fseek(pFile, 0, SEEK_END); |
||||
|
int nLen = ftell(pFile); |
||||
|
char* buffer = nullptr; |
||||
|
if (nLen != 0) { |
||||
|
buffer = new char[nLen + 1]; |
||||
|
rewind(pFile); |
||||
|
fread(buffer, nLen, 1, pFile); |
||||
|
buffer[nLen] = '\0'; |
||||
|
} |
||||
|
else { |
||||
|
printf("load file %s fail, content is 0\n", path); |
||||
|
} |
||||
|
fclose(pFile); |
||||
|
return buffer; |
||||
|
} |
||||
|
else { |
||||
|
printf("open file %s fail\n", path); |
||||
|
} |
||||
|
fclose(pFile); |
||||
|
return nullptr; |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
#pragma once |
||||
|
#include "glew.h" |
||||
|
|
||||
|
GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr); |
||||
|
char* LoadFileContent(const char *path); |
@ -0,0 +1,122 @@ |
|||||
|
#include "model.h"
|
||||
|
#include "misc.h"
|
||||
|
#include <stdio.h>
|
||||
|
#include <string>
|
||||
|
#include <sstream>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
VertexData * LoadObjModel(const char * filePath, unsigned int ** indexes, int & vertexCount, int & indexCount) |
||||
|
{ |
||||
|
char* fileContent = LoadFileContent(filePath); |
||||
|
if (fileContent != nullptr) { |
||||
|
|
||||
|
struct VertexInfo { |
||||
|
float v[3]; |
||||
|
}; |
||||
|
|
||||
|
struct VertexDefine { |
||||
|
int positionIndex; |
||||
|
int texcoordIndex; |
||||
|
int normalIndex; |
||||
|
}; |
||||
|
|
||||
|
std::vector<VertexInfo> position; |
||||
|
std::vector<VertexInfo> texcoord; |
||||
|
std::vector<VertexInfo> normal; |
||||
|
|
||||
|
std::vector<unsigned int> objIndexes; //点的索引 对应opengl的indexes
|
||||
|
std::vector<VertexDefine> vertices; //每一个点 对应opengl的vertex
|
||||
|
|
||||
|
std::stringstream ssObjFile(fileContent); |
||||
|
char szOneLine[256]; |
||||
|
std::string temp; |
||||
|
while (!ssObjFile.eof()) { |
||||
|
memset(szOneLine, 0, 256); |
||||
|
ssObjFile.getline(szOneLine, 256); |
||||
|
printf("%s \n", szOneLine); |
||||
|
if (strlen(szOneLine) > 0) { |
||||
|
std::stringstream ssOneLine(szOneLine); |
||||
|
if (szOneLine[0] == 'v') { |
||||
|
if (szOneLine[1] == 't') { |
||||
|
ssOneLine >> temp; |
||||
|
VertexInfo vi; |
||||
|
ssOneLine >> vi.v[0]; |
||||
|
ssOneLine >> vi.v[1]; |
||||
|
texcoord.push_back(vi); |
||||
|
} |
||||
|
else if (szOneLine[1] == 'n') { |
||||
|
ssOneLine >> temp; |
||||
|
VertexInfo vi; |
||||
|
ssOneLine >> vi.v[0]; |
||||
|
ssOneLine >> vi.v[1]; |
||||
|
ssOneLine >> vi.v[2]; |
||||
|
normal.push_back(vi); |
||||
|
} |
||||
|
else { |
||||
|
ssOneLine >> temp; |
||||
|
VertexInfo vi; |
||||
|
ssOneLine >> vi.v[0]; |
||||
|
ssOneLine >> vi.v[1]; |
||||
|
ssOneLine >> vi.v[2]; |
||||
|
|
||||
|
position.push_back(vi); |
||||
|
} |
||||
|
} |
||||
|
else if(szOneLine[0] == 'f'){ |
||||
|
ssOneLine >> temp; |
||||
|
std::string vertexStr; |
||||
|
for (int i = 0; i < 3; i++) { |
||||
|
ssOneLine >> vertexStr; |
||||
|
|
||||
|
size_t pos = vertexStr.find_first_of('/'); |
||||
|
std::string positionIndexStr = vertexStr.substr(0, pos); |
||||
|
size_t pos2 = vertexStr.find_first_of('/', pos + 1); |
||||
|
std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - pos - 1); |
||||
|
std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - pos2 - 1); |
||||
|
|
||||
|
VertexDefine vd; |
||||
|
vd.positionIndex = atoi(positionIndexStr.c_str()) - 1; |
||||
|
vd.texcoordIndex = atoi(texcoordIndexStr.c_str()) - 1; |
||||
|
vd.normalIndex = atoi(normalIndexStr.c_str()) - 1; |
||||
|
|
||||
|
int nCurrentIndex = -1; |
||||
|
size_t nCurrentVerticeCount = vertices.size(); |
||||
|
for (size_t j = 0; j < nCurrentVerticeCount; j++) { |
||||
|
if (vertices[j].positionIndex == vd.positionIndex && |
||||
|
vertices[j].texcoordIndex == vd.texcoordIndex && |
||||
|
vertices[j].normalIndex == vd.normalIndex) { |
||||
|
nCurrentIndex = j; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (nCurrentIndex == -1) { |
||||
|
nCurrentIndex = vertices.size(); |
||||
|
vertices.push_back(vd); |
||||
|
} |
||||
|
|
||||
|
objIndexes.push_back(nCurrentIndex); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
printf("face count %u\n", objIndexes.size() / 3); |
||||
|
|
||||
|
indexCount = (int)objIndexes.size(); |
||||
|
*indexes = new unsigned int[indexCount]; |
||||
|
for (int i = 0; i < indexCount; i++) { |
||||
|
(*indexes)[i] = objIndexes[i]; |
||||
|
} |
||||
|
|
||||
|
vertexCount = (int)vertices.size(); |
||||
|
VertexData *vertexes = new VertexData[vertexCount]; |
||||
|
for (int i = 0; i < vertexCount; i++) { |
||||
|
memcpy(vertexes[i].position, position[vertices[i].positionIndex].v, sizeof(float) * 3); |
||||
|
memcpy(vertexes[i].texcoord, texcoord[vertices[i].texcoordIndex].v, sizeof(float) * 2); |
||||
|
memcpy(vertexes[i].normal, normal[vertices[i].normalIndex].v, sizeof(float) * 3); |
||||
|
} |
||||
|
return vertexes; |
||||
|
} |
||||
|
return nullptr; |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
struct VertexData { |
||||
|
float position[3]; |
||||
|
float texcoord[2]; |
||||
|
float normal[3]; |
||||
|
}; |
||||
|
|
||||
|
VertexData* LoadObjModel(const char* filePath, unsigned int **indexes, int &vertexCount, int &indexCount); |
@ -0,0 +1,3 @@ |
|||||
|
void main() { |
||||
|
gl_FragColor = vec4(1.0); |
||||
|
} |
@ -1,4 +0,0 @@ |
|||||
varying vec4 V_Color; |
|
||||
void main() { |
|
||||
gl_FragColor = V_Color; |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue