Skip to content

Commit 9b985e3

Browse files
authored
Merge pull request #26 from Pilzschaf/tutorial30-materials
Tutorial30 materials
2 parents 43bc423 + 0d501b4 commit 9b985e3

4 files changed

Lines changed: 115 additions & 60 deletions

File tree

main.cpp

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
#include <SDL2/SDL.h>
2020
#endif
2121

22-
#include "defines.h"
23-
#include "vertex_buffer.h"
24-
#include "index_buffer.h"
25-
#include "shader.h"
26-
#include "floating_camera.h"
27-
28-
void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
29-
std::cout << "[OpenGL Error] " << message << std::endl;
30-
}
31-
3222
#ifdef _DEBUG
3323

3424
void _GLGetError(const char* file, int line, const char* call) {
@@ -45,6 +35,17 @@ void _GLGetError(const char* file, int line, const char* call) {
4535

4636
#endif
4737

38+
#include "defines.h"
39+
#include "vertex_buffer.h"
40+
#include "index_buffer.h"
41+
#include "shader.h"
42+
#include "floating_camera.h"
43+
#include "mesh.h"
44+
45+
void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
46+
std::cout << "[OpenGL Error] " << message << std::endl;
47+
}
48+
4849
int main(int argc, char** argv) {
4950
SDL_Window* window;
5051
SDL_Init(SDL_INIT_EVERYTHING);
@@ -79,43 +80,13 @@ int main(int argc, char** argv) {
7980
glDebugMessageCallback(openGLDebugCallback, 0);
8081
#endif
8182

82-
std::vector<Vertex> vertices;
83-
uint64 numVertices = 0;
84-
85-
std::vector<uint32> indices;
86-
uint64 numIndices = 0;
87-
88-
std::ifstream input = std::ifstream("models/monkey.bmf", std::ios::in | std::ios::binary);
89-
if(!input.is_open()) {
90-
std::cout << "Error reading model file" << std::endl;
91-
return 1;
92-
}
93-
input.read((char*)&numVertices, sizeof(uint64));
94-
input.read((char*)&numIndices, sizeof(uint64));
95-
96-
for(uint64 i = 0; i < numVertices; i++) {
97-
Vertex vertex;
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));
104-
vertices.push_back(vertex);
105-
}
106-
for(uint64 i = 0; i < numIndices; i++) {
107-
uint32 index;
108-
input.read((char*)&index, sizeof(uint32));
109-
indices.push_back(index);
110-
}
111-
112-
IndexBuffer indexBuffer(indices.data(), numIndices, sizeof(indices[0]));
113-
114-
VertexBuffer vertexBuffer(vertices.data(), numVertices);
115-
vertexBuffer.unbind();
116-
11783
Shader shader("shaders/basic.vs", "shaders/basic.fs");
11884
shader.bind();
85+
Material material = {};
86+
material.diffuse = {0.4f, 0.2f, 0.1f};
87+
//material.specular = material.diffuse;
88+
//material.shininess = 4.0f;
89+
Mesh mesh("models/monkey.bmf", material, &shader);
11990

12091
uint64 perfCounterFrequency = SDL_GetPerformanceFrequency();
12192
uint64 lastCounter = SDL_GetPerformanceCounter();
@@ -236,14 +207,10 @@ int main(int argc, char** argv) {
236207
glm::mat4 modelView = camera.getView() * model;
237208
glm::mat4 invModelView = glm::transpose(glm::inverse(modelView));
238209

239-
vertexBuffer.bind();
240-
indexBuffer.bind();
241210
GLCALL(glUniformMatrix4fv(modelViewProjMatrixLocation, 1, GL_FALSE, &modelViewProj[0][0]));
242211
GLCALL(glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, &modelView[0][0]));
243212
GLCALL(glUniformMatrix4fv(invModelViewLocation, 1, GL_FALSE, &invModelView[0][0]));
244-
GLCALL(glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0));
245-
indexBuffer.unbind();
246-
vertexBuffer.unbind();
213+
mesh.render();
247214

248215
SDL_GL_SwapWindow(window);
249216

mesh.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma once
2+
#include <vector>
3+
#include <fstream>
4+
5+
#include "libs/glm/glm.hpp"
6+
#include "shader.h"
7+
#include "vertex_buffer.h"
8+
#include "index_buffer.h"
9+
10+
struct Material {
11+
glm::vec3 diffuse;
12+
glm::vec3 specular;
13+
glm::vec3 emissive;
14+
float shininess;
15+
};
16+
17+
class Mesh {
18+
public:
19+
Mesh(const char* filename, Material material, Shader* shader) {
20+
this->material = material;
21+
this->shader = shader;
22+
23+
std::vector<Vertex> vertices;
24+
uint64 numVertices = 0;
25+
26+
std::vector<uint32> indices;
27+
numIndices = 0;
28+
29+
std::ifstream input = std::ifstream(filename, std::ios::in | std::ios::binary);
30+
input.read((char*)&numVertices, sizeof(uint64));
31+
input.read((char*)&numIndices, sizeof(uint64));
32+
33+
for(uint64 i = 0; i < numVertices; i++) {
34+
Vertex vertex;
35+
input.read((char*)&vertex.position.x, sizeof(float));
36+
input.read((char*)&vertex.position.y, sizeof(float));
37+
input.read((char*)&vertex.position.z, sizeof(float));
38+
input.read((char*)&vertex.normal.x, sizeof(float));
39+
input.read((char*)&vertex.normal.y, sizeof(float));
40+
input.read((char*)&vertex.normal.z, sizeof(float));
41+
vertices.push_back(vertex);
42+
}
43+
for(uint64 i = 0; i < numIndices; i++) {
44+
uint32 index;
45+
input.read((char*)&index, sizeof(uint32));
46+
indices.push_back(index);
47+
}
48+
49+
vertexBuffer = new VertexBuffer(vertices.data(), numVertices);
50+
indexBuffer = new IndexBuffer(indices.data(), numIndices, sizeof(indices[0]));
51+
52+
diffuseLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_diffuse"));
53+
specularLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_specular"));
54+
emissiveLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_emissive"));
55+
shininessLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_shininess"));
56+
}
57+
~Mesh() {
58+
delete vertexBuffer;
59+
delete indexBuffer;
60+
}
61+
inline void render() {
62+
vertexBuffer->bind();
63+
indexBuffer->bind();
64+
glUniform3fv(diffuseLocation, 1, (float*)&material.diffuse.data);
65+
glUniform3fv(specularLocation, 1, (float*)&material.specular.data);
66+
glUniform3fv(emissiveLocation, 1, (float*)&material.emissive.data);
67+
glUniform1f(shininessLocation, material.shininess);
68+
GLCALL(glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0));
69+
}
70+
private:
71+
VertexBuffer* vertexBuffer;
72+
IndexBuffer* indexBuffer;
73+
Shader* shader;
74+
Material material;
75+
uint64 numIndices = 0;
76+
int diffuseLocation;
77+
int specularLocation;
78+
int emissiveLocation;
79+
int shininessLocation;
80+
};

shaders/basic.fs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ layout(location = 0) out vec4 f_color;
55
in vec3 v_normal;
66
in vec3 v_position;
77

8+
uniform vec3 u_diffuse;
9+
uniform vec3 u_specular;
10+
uniform vec3 u_emissive;
11+
uniform float u_shininess;
12+
813
void main()
914
{
1015
// Vector from fragment to camera (camera always at 0,0,0)
1116
vec3 view = normalize(-v_position);
1217
vec3 light = normalize(vec3(1.0f, 1.0f, 1.0f));
1318
vec3 normal = normalize(v_normal);
14-
vec3 color = vec3(0.4f, 0.2f, 0.1f);
1519
vec3 reflection = reflect(-light, normal);
1620

17-
vec3 ambient = color * 0.2;
18-
vec3 diffuse = max(dot(normal, light), 0.0) * color;
19-
vec3 specular = pow(max(dot(reflection, view), 0.0), 4.0) * color;
21+
vec3 ambient = u_diffuse * 0.2;
22+
vec3 diffuse = max(dot(normal, light), 0.0) * u_diffuse;
23+
vec3 specular = pow(max(dot(reflection, view), 0.000001), u_shininess) * u_specular;
2024

21-
f_color = vec4(ambient + diffuse + specular, 1.0f);
25+
f_color = vec4(ambient + diffuse + specular + u_emissive, 1.0f);
2226
}

shaders_old/basic.fs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
varying vec3 v_normal;
44
varying vec3 v_position;
55

6+
uniform vec3 u_diffuse;
7+
uniform vec3 u_specular;
8+
uniform vec3 u_emissive;
9+
uniform float u_shininess;
10+
611
void main()
712
{
813
vec3 view = normalize(-v_position);
914
vec3 light = normalize(vec3(1.0f, 1.0f, 1.0f));
1015
vec3 normal = normalize(v_normal);
11-
vec3 color = vec3(0.4f, 0.2f, 0.1f);
1216
vec3 reflection = reflect(-light, normal);
1317

14-
vec3 ambient = color * 0.2;
15-
vec3 diffuse = max(dot(normal, light), 0.0) * color;
16-
vec3 specular = pow(max(dot(reflection, view), 0.0), 4.0) * color;
18+
vec3 ambient = u_diffuse * 0.2;
19+
vec3 diffuse = max(dot(normal, light), 0.0) * u_diffuse;
20+
vec3 specular = pow(max(dot(reflection, view), 0.000001), u_shininess) * u_specular;
1721

18-
gl_FragColor = vec4(ambient + diffuse + specular, 1.0f);
22+
gl_FragColor = vec4(ambient + diffuse + specular + u_emissive, 1.0f);
1923
}

0 commit comments

Comments
 (0)