Fixed linux compat
This commit is contained in:
parent
dcc5871929
commit
b47c5e27d8
9 changed files with 49 additions and 32 deletions
66
engine/mesh.h
Normal file
66
engine/mesh.h
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <vector>
|
||||
#include <glad/glad.h>
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z; // position
|
||||
float r, g, b; // color
|
||||
float u, v; // texcoord
|
||||
// = 8 floats = 32 bytes
|
||||
};
|
||||
|
||||
class Mesh
|
||||
{
|
||||
public:
|
||||
unsigned int VBO, VAO, EBO;
|
||||
std::size_t indexCount = 0;
|
||||
|
||||
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices) : indexCount(indices.size())
|
||||
{
|
||||
if (vertices.empty() || indices.empty()) {
|
||||
throw std::runtime_error("Mesh created with empty vertices or indices");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
std::cout << VAO << std::endl;
|
||||
std::cout << VBO << std::endl;
|
||||
std::cout << EBO << std::endl;
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
|
||||
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x));
|
||||
glEnableVertexAttribArray(0);
|
||||
// color attribute
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, r));
|
||||
glEnableVertexAttribArray(1);
|
||||
// texture coord attribute
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, u));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cout << "Failed to load mesh" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Draw() const
|
||||
{
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount),
|
||||
GL_UNSIGNED_INT, nullptr);
|
||||
}
|
||||
};
|
||||
133
engine/shader.h
Normal file
133
engine/shader.h
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
#ifndef SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
// Compiled Shader Program ID
|
||||
unsigned int ID;
|
||||
|
||||
Shader(const char* vertexPath, const char* fragmentPath)
|
||||
{
|
||||
std::string vertexCode, fragmentCode;
|
||||
std::ifstream vertexFile, fragmentFile;
|
||||
|
||||
std::cout << vertexPath << std::endl;
|
||||
|
||||
vertexFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
fragmentFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try
|
||||
{
|
||||
// Open the files
|
||||
vertexFile.open(vertexPath);
|
||||
fragmentFile.open(fragmentPath);
|
||||
std::stringstream vertexStream, fragmentStream;
|
||||
|
||||
// Save the contents to our streams
|
||||
vertexStream << vertexFile.rdbuf();
|
||||
fragmentStream << fragmentFile.rdbuf();
|
||||
|
||||
// Close files
|
||||
vertexFile.close();
|
||||
fragmentFile.close();
|
||||
|
||||
// Save the contents to our strings
|
||||
vertexCode = vertexStream.str();
|
||||
fragmentCode = fragmentStream.str();
|
||||
}
|
||||
catch (std::ifstream::failure& e)
|
||||
{
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
const char* vertexShaderSource = vertexCode.c_str();
|
||||
const char* fragmentShaderSource = fragmentCode.c_str();
|
||||
|
||||
unsigned int vertexShader;
|
||||
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
checkCompileErrors(vertexShader, "VERTEX");
|
||||
|
||||
unsigned int fragmentShader;
|
||||
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
checkCompileErrors(fragmentShader, "FRAGMENT");
|
||||
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertexShader);
|
||||
glAttachShader(ID, fragmentShader);
|
||||
glLinkProgram(ID);
|
||||
checkCompileErrors(ID, "PROGRAM");
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
|
||||
void Use()
|
||||
{
|
||||
glUseProgram(ID);
|
||||
};
|
||||
|
||||
// Setters
|
||||
void setBool(const std::string &name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
void setInt(const std::string &name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
void setFloat(const std::string &name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
void setVec2(const std::string &name, float v0, float v1) const
|
||||
{
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), v0, v1);
|
||||
}
|
||||
void setVec3(const std::string &name, float v0, float v1, float v2) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), v0, v1, v2);
|
||||
}
|
||||
void setVec4(const std::string &name, float v0, float v1, float v2, float v3) const
|
||||
{
|
||||
glUniform4f(glGetUniformLocation(ID, name.c_str()), v0, v1, v2, v3);
|
||||
}
|
||||
|
||||
private:
|
||||
void checkCompileErrors(unsigned int shader, std::string type)
|
||||
{
|
||||
int success;
|
||||
char infoLog[1024];
|
||||
if (type != "PROGRAM")
|
||||
{
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
||||
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
54
engine/texture.h
Normal file
54
engine/texture.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image/stb_image.h>
|
||||
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
unsigned int ID;
|
||||
int width, height, nrChannels;
|
||||
|
||||
Texture(const char* texturePath, bool flip = false)
|
||||
{
|
||||
// Create the Texture and set our ID
|
||||
glGenTextures(1, &ID);
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
|
||||
// set the texture wrapping/filtering options (on the currently bound texture object)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
// Grab the texture from our path
|
||||
if (flip)
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB;
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Failed to load texture" << std::endl;
|
||||
}
|
||||
|
||||
if (flip)
|
||||
stbi_set_flip_vertically_on_load(false);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
void Draw() const
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE);
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue