Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 17 additions & 50 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@
#include <SDL2/SDL.h>
#endif

#include "defines.h"
#include "vertex_buffer.h"
#include "index_buffer.h"
#include "shader.h"
#include "floating_camera.h"

void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
std::cout << "[OpenGL Error] " << message << std::endl;
}

#ifdef _DEBUG

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

#endif

#include "defines.h"
#include "vertex_buffer.h"
#include "index_buffer.h"
#include "shader.h"
#include "floating_camera.h"
#include "mesh.h"

void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
std::cout << "[OpenGL Error] " << message << std::endl;
}

int main(int argc, char** argv) {
SDL_Window* window;
SDL_Init(SDL_INIT_EVERYTHING);
Expand Down Expand Up @@ -79,43 +80,13 @@ int main(int argc, char** argv) {
glDebugMessageCallback(openGLDebugCallback, 0);
#endif

std::vector<Vertex> vertices;
uint64 numVertices = 0;

std::vector<uint32> indices;
uint64 numIndices = 0;

std::ifstream input = std::ifstream("models/monkey.bmf", std::ios::in | std::ios::binary);
if(!input.is_open()) {
std::cout << "Error reading model file" << std::endl;
return 1;
}
input.read((char*)&numVertices, sizeof(uint64));
input.read((char*)&numIndices, sizeof(uint64));

for(uint64 i = 0; i < numVertices; i++) {
Vertex vertex;
input.read((char*)&vertex.position.x, sizeof(float));
input.read((char*)&vertex.position.y, sizeof(float));
input.read((char*)&vertex.position.z, sizeof(float));
input.read((char*)&vertex.normal.x, sizeof(float));
input.read((char*)&vertex.normal.y, sizeof(float));
input.read((char*)&vertex.normal.z, sizeof(float));
vertices.push_back(vertex);
}
for(uint64 i = 0; i < numIndices; i++) {
uint32 index;
input.read((char*)&index, sizeof(uint32));
indices.push_back(index);
}

IndexBuffer indexBuffer(indices.data(), numIndices, sizeof(indices[0]));

VertexBuffer vertexBuffer(vertices.data(), numVertices);
vertexBuffer.unbind();

Shader shader("shaders/basic.vs", "shaders/basic.fs");
shader.bind();
Material material = {};
material.diffuse = {0.4f, 0.2f, 0.1f};
//material.specular = material.diffuse;
//material.shininess = 4.0f;
Mesh mesh("models/monkey.bmf", material, &shader);

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

vertexBuffer.bind();
indexBuffer.bind();
GLCALL(glUniformMatrix4fv(modelViewProjMatrixLocation, 1, GL_FALSE, &modelViewProj[0][0]));
GLCALL(glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, &modelView[0][0]));
GLCALL(glUniformMatrix4fv(invModelViewLocation, 1, GL_FALSE, &invModelView[0][0]));
GLCALL(glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0));
indexBuffer.unbind();
vertexBuffer.unbind();
mesh.render();

SDL_GL_SwapWindow(window);

Expand Down
80 changes: 80 additions & 0 deletions mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once
#include <vector>
#include <fstream>

#include "libs/glm/glm.hpp"
#include "shader.h"
#include "vertex_buffer.h"
#include "index_buffer.h"

struct Material {
glm::vec3 diffuse;
glm::vec3 specular;
glm::vec3 emissive;
float shininess;
};

class Mesh {
public:
Mesh(const char* filename, Material material, Shader* shader) {
this->material = material;
this->shader = shader;

std::vector<Vertex> vertices;
uint64 numVertices = 0;

std::vector<uint32> indices;
numIndices = 0;

std::ifstream input = std::ifstream(filename, std::ios::in | std::ios::binary);
input.read((char*)&numVertices, sizeof(uint64));
input.read((char*)&numIndices, sizeof(uint64));

for(uint64 i = 0; i < numVertices; i++) {
Vertex vertex;
input.read((char*)&vertex.position.x, sizeof(float));
input.read((char*)&vertex.position.y, sizeof(float));
input.read((char*)&vertex.position.z, sizeof(float));
input.read((char*)&vertex.normal.x, sizeof(float));
input.read((char*)&vertex.normal.y, sizeof(float));
input.read((char*)&vertex.normal.z, sizeof(float));
vertices.push_back(vertex);
}
for(uint64 i = 0; i < numIndices; i++) {
uint32 index;
input.read((char*)&index, sizeof(uint32));
indices.push_back(index);
}

vertexBuffer = new VertexBuffer(vertices.data(), numVertices);
indexBuffer = new IndexBuffer(indices.data(), numIndices, sizeof(indices[0]));

diffuseLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_diffuse"));
specularLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_specular"));
emissiveLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_emissive"));
shininessLocation = GLCALL(glGetUniformLocation(shader->getShaderId(), "u_shininess"));
}
~Mesh() {
delete vertexBuffer;
delete indexBuffer;
}
inline void render() {
vertexBuffer->bind();
indexBuffer->bind();
glUniform3fv(diffuseLocation, 1, (float*)&material.diffuse.data);
glUniform3fv(specularLocation, 1, (float*)&material.specular.data);
glUniform3fv(emissiveLocation, 1, (float*)&material.emissive.data);
glUniform1f(shininessLocation, material.shininess);
GLCALL(glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0));
}
private:
VertexBuffer* vertexBuffer;
IndexBuffer* indexBuffer;
Shader* shader;
Material material;
uint64 numIndices = 0;
int diffuseLocation;
int specularLocation;
int emissiveLocation;
int shininessLocation;
};
14 changes: 9 additions & 5 deletions shaders/basic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ layout(location = 0) out vec4 f_color;
in vec3 v_normal;
in vec3 v_position;

uniform vec3 u_diffuse;
uniform vec3 u_specular;
uniform vec3 u_emissive;
uniform float u_shininess;

void main()
{
// Vector from fragment to camera (camera always at 0,0,0)
vec3 view = normalize(-v_position);
vec3 light = normalize(vec3(1.0f, 1.0f, 1.0f));
vec3 normal = normalize(v_normal);
vec3 color = vec3(0.4f, 0.2f, 0.1f);
vec3 reflection = reflect(-light, normal);

vec3 ambient = color * 0.2;
vec3 diffuse = max(dot(normal, light), 0.0) * color;
vec3 specular = pow(max(dot(reflection, view), 0.0), 4.0) * color;
vec3 ambient = u_diffuse * 0.2;
vec3 diffuse = max(dot(normal, light), 0.0) * u_diffuse;
vec3 specular = pow(max(dot(reflection, view), 0.000001), u_shininess) * u_specular;

f_color = vec4(ambient + diffuse + specular, 1.0f);
f_color = vec4(ambient + diffuse + specular + u_emissive, 1.0f);
}
14 changes: 9 additions & 5 deletions shaders_old/basic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
varying vec3 v_normal;
varying vec3 v_position;

uniform vec3 u_diffuse;
uniform vec3 u_specular;
uniform vec3 u_emissive;
uniform float u_shininess;

void main()
{
vec3 view = normalize(-v_position);
vec3 light = normalize(vec3(1.0f, 1.0f, 1.0f));
vec3 normal = normalize(v_normal);
vec3 color = vec3(0.4f, 0.2f, 0.1f);
vec3 reflection = reflect(-light, normal);

vec3 ambient = color * 0.2;
vec3 diffuse = max(dot(normal, light), 0.0) * color;
vec3 specular = pow(max(dot(reflection, view), 0.0), 4.0) * color;
vec3 ambient = u_diffuse * 0.2;
vec3 diffuse = max(dot(normal, light), 0.0) * u_diffuse;
vec3 specular = pow(max(dot(reflection, view), 0.000001), u_shininess) * u_specular;

gl_FragColor = vec4(ambient + diffuse + specular, 1.0f);
gl_FragColor = vec4(ambient + diffuse + specular + u_emissive, 1.0f);
}