Skip to content

Commit 311839b

Browse files
committed
Vector normals
1 parent 9220c62 commit 311839b

7 files changed

Lines changed: 33 additions & 25 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ tools/modelexporter
3535

3636
# Dolphin
3737
.directory
38+
39+
# VSCode
40+
.vscode/ipch

defines.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <cstdint>
3+
#include "libs/glm/glm.hpp"
34

45
typedef int8_t int8;
56
typedef int16_t int16;
@@ -15,12 +16,6 @@ typedef float float32;
1516
typedef double float64;
1617

1718
struct Vertex {
18-
float32 x;
19-
float32 y;
20-
float32 z;
21-
22-
float32 r;
23-
float32 g;
24-
float32 b;
25-
float32 a;
19+
glm::vec3 position;
20+
glm::vec3 normal;
2621
};

main.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ int main(int argc, char** argv) {
9595

9696
for(uint64 i = 0; i < numVertices; i++) {
9797
Vertex vertex;
98-
input.read((char*)&vertex.x, sizeof(float));
99-
input.read((char*)&vertex.y, sizeof(float));
100-
input.read((char*)&vertex.z, sizeof(float));
101-
vertex.r = 1.0f;
102-
vertex.g = 1.0f;
103-
vertex.b = 1.0f;
104-
vertex.a = 1.0f;
98+
input.read((char*)&vertex.position.x, sizeof(float));
99+
input.read((char*)&vertex.position.y, sizeof(float));
100+
input.read((char*)&vertex.position.z, sizeof(float));
101+
input.read((char*)&vertex.normal.x, sizeof(float));
102+
input.read((char*)&vertex.normal.y, sizeof(float));
103+
input.read((char*)&vertex.normal.z, sizeof(float));
105104
vertices.push_back(vertex);
106105
}
107106
for(uint64 i = 0; i < numIndices; i++) {

models/monkey.bmf

33.6 KB
Binary file not shown.

shaders/basic.vs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#version 330 core
22

33
layout(location = 0) in vec3 a_position;
4-
layout(location = 1) in vec4 a_color;
4+
layout(location = 1) in vec3 a_normal;
55

66
out vec4 v_color;
77

@@ -10,5 +10,5 @@ uniform mat4 u_modelViewProj;
1010
void main()
1111
{
1212
gl_Position = u_modelViewProj * vec4(a_position, 1.0f);
13-
v_color = a_color;
13+
v_color = vec4(a_normal, 1.0f);
1414
}

tools/modelexporter.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ struct Position {
1212
};
1313

1414
std::vector<Position> positions;
15+
std::vector<Position> normals;
1516
std::vector<uint32_t> indices;
1617

1718
void processMesh(aiMesh* mesh, const aiScene* scene) {
1819
for(unsigned int i = 0; i < mesh->mNumVertices; i++) {
19-
Position vertex;
20-
vertex.x = mesh->mVertices[i].x;
21-
vertex.y = mesh->mVertices[i].y;
22-
vertex.z = mesh->mVertices[i].z;
23-
positions.push_back(vertex);
20+
Position position;
21+
position.x = mesh->mVertices[i].x;
22+
position.y = mesh->mVertices[i].y;
23+
position.z = mesh->mVertices[i].z;
24+
positions.push_back(position);
25+
26+
Position normal;
27+
normal.x = mesh->mNormals[i].x;
28+
normal.y = mesh->mNormals[i].y;
29+
normal.z = mesh->mNormals[i].z;
30+
normals.push_back(normal);
2431
}
2532

2633
for(unsigned int i = 0; i < mesh->mNumFaces; i++) {
@@ -65,7 +72,7 @@ int main(int argc, char** argv) {
6572
}
6673

6774
Assimp::Importer importer;
68-
const aiScene* scene = importer.ReadFile(argv[argc-1], aiProcess_Triangulate | aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph | aiProcess_JoinIdenticalVertices | aiProcess_ImproveCacheLocality);
75+
const aiScene* scene = importer.ReadFile(argv[argc-1], aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph | aiProcess_JoinIdenticalVertices | aiProcess_ImproveCacheLocality);
6976
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE, !scene->mRootNode) {
7077
std::cout << "Error while loading model with assimp: " << importer.GetErrorString() << std::endl;
7178
return 1;
@@ -87,6 +94,10 @@ int main(int argc, char** argv) {
8794
output.write((char*)&positions[i].x, sizeof(float));
8895
output.write((char*)&positions[i].y, sizeof(float));
8996
output.write((char*)&positions[i].z, sizeof(float));
97+
98+
output.write((char*)&normals[i].x, sizeof(float));
99+
output.write((char*)&normals[i].y, sizeof(float));
100+
output.write((char*)&normals[i].z, sizeof(float));
90101
}
91102
for(uint64_t i = 0; i < numIndices; i++) {
92103
output.write((char*)&indices[i], sizeof(uint32_t));

vertex_buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ struct VertexBuffer {
1313
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), data, GL_STATIC_DRAW);
1414

1515
glEnableVertexAttribArray(0);
16-
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex,x));
16+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex,position));
1717
glEnableVertexAttribArray(1);
18-
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex,r));
18+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex,normal));
1919

2020
glBindVertexArray(0);
2121
}

0 commit comments

Comments
 (0)