added the other files
This commit is contained in:
parent
deff78ed58
commit
11d4aac4ce
18
CMakeLists.txt
Normal file
18
CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
project(CoursOpenGl)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SRC src/*)
|
||||||
|
file(GLOB_RECURSE HEADERS include/*)
|
||||||
|
|
||||||
|
find_package(GLEW REQUIRED)
|
||||||
|
include_directories(${GLEW_INCLUDE_DIRS})
|
||||||
|
add_executable(CoursOpenGl ${LIB_HEADERS} ${HEADERS} ${SRC})
|
||||||
|
|
||||||
|
find_package(OpenGL REQUIRED)
|
||||||
|
find_package(glfw3 3.4 REQUIRED)
|
||||||
|
|
||||||
|
|
||||||
|
target_include_directories(CoursOpenGl PUBLIC ${CMAKE_SOURCE_DIR}/include)
|
||||||
|
target_link_libraries(CoursOpenGl glfw OpenGL::GL -lGLU ${GLEW_LIBRARIES})
|
||||||
|
target_link_directories(CoursOpenGl PUBLIC .)
|
||||||
|
|
||||||
11
assets/shaders/fragment.glsl
Normal file
11
assets/shaders/fragment.glsl
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D ourTexture;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
//FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||||
|
FragColor = texture(ourTexture, TexCoord);
|
||||||
|
}
|
||||||
13
assets/shaders/vertex.glsl
Normal file
13
assets/shaders/vertex.glsl
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
BIN
assets/textures/prof.png
Normal file
BIN
assets/textures/prof.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 353 KiB |
35
include/ShaderProgram.h
Normal file
35
include/ShaderProgram.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#ifndef SHADERPROGRAM_H
|
||||||
|
#define SHADERPROGRAM_H
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <string>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
namespace djalim {
|
||||||
|
class ShaderProgram {
|
||||||
|
public:
|
||||||
|
ShaderProgram();
|
||||||
|
~ShaderProgram();
|
||||||
|
|
||||||
|
bool loadShaders(const char* vertexShaderFile, const char* fragmentShaderFile);
|
||||||
|
void use();
|
||||||
|
|
||||||
|
void setUniform(const std::string& name, float value);
|
||||||
|
void setUniform(const std::string& name, int value);
|
||||||
|
void setUniform(const std::string& name, const glm::vec2& value);
|
||||||
|
void setUniform(const std::string& name, const glm::vec3& value);
|
||||||
|
void setUniform(const std::string& name, const glm::vec4& value);
|
||||||
|
void setUniform(const std::string& name, const glm::mat4& value);
|
||||||
|
|
||||||
|
GLuint getProgram() const { return mHandle; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string fileToString(const std::string& filename);
|
||||||
|
void checkCompileErrors(GLuint shader, std::string type);
|
||||||
|
|
||||||
|
GLuint mHandle;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SHADERPROGRAM_H
|
||||||
20
include/Texture2D.h
Normal file
20
include/Texture2D.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef TEXTURE2D_H
|
||||||
|
#define TEXTURE2D_H
|
||||||
|
#include <stb_image/stb_image.h>
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Texture2D {
|
||||||
|
public:
|
||||||
|
Texture2D();
|
||||||
|
~Texture2D();
|
||||||
|
|
||||||
|
bool loadTexture(const std::string& filename, bool generateMipmaps = true);
|
||||||
|
void bind(GLuint texUnit = 0);
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLuint mTexture;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TEXTURE2D_H
|
||||||
60
include/engine.h
Normal file
60
include/engine.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#ifndef ENGINE_H
|
||||||
|
#define ENGINE_H
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include "ShaderProgram.h"
|
||||||
|
#include "Texture2D.h"
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#define NBCALLBACKS 1
|
||||||
|
|
||||||
|
typedef void (*KeyCallback)(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||||
|
|
||||||
|
struct Object3D {
|
||||||
|
Texture2D texture;
|
||||||
|
GLuint VBO;
|
||||||
|
GLuint VAO;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::map<std::string,Object3D> Objects3D;
|
||||||
|
|
||||||
|
static bool gWireframe = false;
|
||||||
|
|
||||||
|
namespace djalim {
|
||||||
|
|
||||||
|
class OpenGlEngine{
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void start();
|
||||||
|
OpenGlEngine( const char* title, int width, int height);
|
||||||
|
~OpenGlEngine();
|
||||||
|
|
||||||
|
GLFWwindow* window;
|
||||||
|
double previousSeconds = 0.0;
|
||||||
|
double elapsedSeconds;
|
||||||
|
int frameCount = 0;
|
||||||
|
std::map<std::string,Texture2D> textures;
|
||||||
|
ShaderProgram shaderProgram;
|
||||||
|
|
||||||
|
Objects3D objects;
|
||||||
|
|
||||||
|
unsigned int VBO;
|
||||||
|
unsigned int VAO;
|
||||||
|
|
||||||
|
bool gWireframe = false;
|
||||||
|
|
||||||
|
void onUpdate();
|
||||||
|
void loadHints();
|
||||||
|
void createObject(std::string name, std::string textures, std::vector<float>& vertices);
|
||||||
|
void loadCallbacks();
|
||||||
|
void showFps();
|
||||||
|
void onCreate();
|
||||||
|
void onDestroy();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // ENGINE_H
|
||||||
15
include/loops.h
Normal file
15
include/loops.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef LOOPS_H
|
||||||
|
#define LOOPS_H
|
||||||
|
#include "ShaderProgram.h"
|
||||||
|
#include "Texture2D.h"
|
||||||
|
#include "engine.h"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace djalim {
|
||||||
|
void rainbowWindow(double elapsedSeconds);
|
||||||
|
void rgbTriangle(djalim::ShaderProgram shaderProgram, unsigned int VAO);
|
||||||
|
//void cube(GLFWwindow* window, djalim::ShaderProgram shader, GLuint VAO, std::map<std::string, Texture2D>& textures);
|
||||||
|
void cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // LOOPS_H
|
||||||
7988
include/stb_image/stb_image.h
Normal file
7988
include/stb_image/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
95
src/core/ShaderProgram.cpp
Normal file
95
src/core/ShaderProgram.cpp
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
djalim::ShaderProgram::ShaderProgram() : mHandle(0) {}
|
||||||
|
|
||||||
|
djalim::ShaderProgram::~ShaderProgram() {
|
||||||
|
if (mHandle > 0) {
|
||||||
|
glDeleteProgram(mHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::use() {
|
||||||
|
if (mHandle > 0) {
|
||||||
|
glUseProgram(mHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool djalim::ShaderProgram::loadShaders(const char* vertexShaderFile, const char* fragmentShaderFile) {
|
||||||
|
std::string vertSourceStr = fileToString(vertexShaderFile);
|
||||||
|
std::string fragSourceStr = fileToString(fragmentShaderFile);
|
||||||
|
const char* vertSource = vertSourceStr.c_str();
|
||||||
|
const char* fragSource = fragSourceStr.c_str();
|
||||||
|
|
||||||
|
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
glShaderSource(vertexShader, 1, &vertSource, NULL);
|
||||||
|
glCompileShader(vertexShader);
|
||||||
|
checkCompileErrors(vertexShader, "VERTEX");
|
||||||
|
|
||||||
|
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
glShaderSource(fragmentShader, 1, &fragSource, NULL);
|
||||||
|
glCompileShader(fragmentShader);
|
||||||
|
checkCompileErrors(fragmentShader, "FRAGMENT");
|
||||||
|
|
||||||
|
mHandle = glCreateProgram();
|
||||||
|
glAttachShader(mHandle, vertexShader);
|
||||||
|
glAttachShader(mHandle, fragmentShader);
|
||||||
|
glLinkProgram(mHandle);
|
||||||
|
checkCompileErrors(mHandle, "PROGRAM");
|
||||||
|
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string djalim::ShaderProgram::fileToString(const std::string& filename) {
|
||||||
|
std::ifstream fileStream(filename);
|
||||||
|
if (!fileStream.is_open()) {
|
||||||
|
std::cerr << "Erreur : Impossible d'ouvrir le fichier shader : " << filename << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << fileStream.rdbuf();
|
||||||
|
return buffer.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::checkCompileErrors(GLuint shader, std::string type) {
|
||||||
|
GLint success;
|
||||||
|
GLchar infoLog[1024];
|
||||||
|
if (type != "PROGRAM") {
|
||||||
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||||
|
std::cerr << "ERREUR::SHADER_COMPILATION_ERROR de type: " << type << "\n" << infoLog << std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
||||||
|
std::cerr << "ERREUR::PROGRAM_LINKING_ERROR de type: " << type << "\n" << infoLog << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::setUniform(const std::string& name, const glm::vec4& value) {
|
||||||
|
glUniform4f(glGetUniformLocation(mHandle, name.c_str()), value.x, value.y, value.z, value.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::setUniform(const std::string& name, const glm::mat4& value) {
|
||||||
|
glUniformMatrix4fv(glGetUniformLocation(mHandle, name.c_str()), 1, GL_FALSE, &value[0][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::setUniform(const std::string& name, int value) {
|
||||||
|
glUniform1i(glGetUniformLocation(mHandle, name.c_str()), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::setUniform(const std::string& name, float value) {
|
||||||
|
glUniform1f(glGetUniformLocation(mHandle, name.c_str()), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::ShaderProgram::setUniform(const std::string& name, const glm::vec3& value) {
|
||||||
|
glUniform3f(glGetUniformLocation(mHandle, name.c_str()), value.x, value.y, value.z);
|
||||||
|
}
|
||||||
52
src/core/Texture2D.cpp
Normal file
52
src/core/Texture2D.cpp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include "Texture2D.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include <stb_image/stb_image.h>
|
||||||
|
|
||||||
|
Texture2D::Texture2D() : mTexture(0) {}
|
||||||
|
|
||||||
|
Texture2D::~Texture2D() {
|
||||||
|
if (mTexture > 0) {
|
||||||
|
glDeleteTextures(1, &mTexture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Texture2D::loadTexture(const std::string& filename, bool generateMipmaps) {
|
||||||
|
int width, height, components;
|
||||||
|
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &components, STBI_rgb);
|
||||||
|
if (data == NULL) {
|
||||||
|
std::cerr << "Erreur: Impossible de charger la texture : " << filename << std::endl;
|
||||||
|
std::cerr << "STBI Reason: " << stbi_failure_reason() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Texture loaded successfully: " << filename << std::endl;
|
||||||
|
std::cout << "Width: " << width << ", Height: " << height << ", Components: " << components << std::endl;
|
||||||
|
|
||||||
|
glGenTextures(1, &mTexture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, mTexture);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||||
|
|
||||||
|
if (generateMipmaps) {
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
std::cout << "Mipmaps generated" << std::endl;
|
||||||
|
|
||||||
|
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_LINEAR_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Texture2D::bind(GLuint texUnit) {
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, mTexture);
|
||||||
|
}
|
||||||
29
src/core/callbacks.cpp
Normal file
29
src/core/callbacks.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
|
||||||
|
extern bool gWireframe;
|
||||||
|
|
||||||
|
// TODO Try to make callbacks more flexible
|
||||||
|
// there should not be a single function with 100+ if elses 💀
|
||||||
|
void mainCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||||
|
{
|
||||||
|
//TODO Extract this in a exitCallBack Function
|
||||||
|
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS) {
|
||||||
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||||
|
}
|
||||||
|
// end todo
|
||||||
|
|
||||||
|
//TODO Extract this in a wireframeCallBack Function
|
||||||
|
if (key == GLFW_KEY_W && action == GLFW_PRESS)
|
||||||
|
gWireframe = !gWireframe;
|
||||||
|
if (gWireframe)
|
||||||
|
|
||||||
|
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
else
|
||||||
|
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
// end todo
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::loadCallbacks(){
|
||||||
|
glfwSetKeyCallback(window, mainCallback);
|
||||||
|
}
|
||||||
106
src/core/engine.cpp
Normal file
106
src/core/engine.cpp
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) {
|
||||||
|
// initialize GLFW
|
||||||
|
if (!glfwInit()) {
|
||||||
|
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadHints();
|
||||||
|
|
||||||
|
this->window = glfwCreateWindow(width, height, title, NULL, NULL);
|
||||||
|
if (!this->window) {
|
||||||
|
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||||
|
glfwTerminate();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwMakeContextCurrent(this->window);
|
||||||
|
|
||||||
|
// initialize GLEW
|
||||||
|
glewExperimental = GL_TRUE;
|
||||||
|
if (glewInit() != GLEW_OK) {
|
||||||
|
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadCallbacks();
|
||||||
|
|
||||||
|
bool loaded = shaderProgram.loadShaders("../assets/shaders/vertex.glsl", "../assets/shaders/fragment.glsl");
|
||||||
|
if (!loaded) {
|
||||||
|
std::cerr << "Failed to load shaders correctly" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
onCreate();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::vector<float>& vertices){
|
||||||
|
|
||||||
|
objects[name];
|
||||||
|
|
||||||
|
bool textureLoaded = objects[name].texture.loadTexture(textures);
|
||||||
|
|
||||||
|
if (!textureLoaded) {
|
||||||
|
std::cerr << "Failed to load " << name << " texture!" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//glGenVertexArrays(1, &VAO);
|
||||||
|
glGenVertexArrays(1, &(objects[name].VAO));
|
||||||
|
|
||||||
|
//glGenBuffers(1, &VBO);
|
||||||
|
glGenBuffers(1, &(objects[name].VBO));
|
||||||
|
|
||||||
|
//glBindVertexArray(VAO);
|
||||||
|
glBindVertexArray(objects[name].VAO);
|
||||||
|
|
||||||
|
|
||||||
|
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, objects[name].VBO);
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << "Loading 3D objects with "<< vertices.size() << " vertices"<< std::endl;
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), vertices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// Attribut de position
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
// Attribut de coordonnée de texture
|
||||||
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
djalim::OpenGlEngine::~OpenGlEngine() {
|
||||||
|
onDestroy();
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::start(){
|
||||||
|
// Main loop
|
||||||
|
|
||||||
|
double currentSeconds;
|
||||||
|
while (!glfwWindowShouldClose(this->window)) {
|
||||||
|
|
||||||
|
currentSeconds = glfwGetTime();
|
||||||
|
elapsedSeconds = currentSeconds-previousSeconds;
|
||||||
|
previousSeconds = currentSeconds;
|
||||||
|
|
||||||
|
glfwPollEvents();
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
//glClearColor(0, 0.5, 0.5, 1.0f);
|
||||||
|
|
||||||
|
onUpdate();
|
||||||
|
|
||||||
|
glfwSwapBuffers(this->window);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/core/hints.cpp
Normal file
9
src/core/hints.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::loadHints(){
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
|
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||||
|
}
|
||||||
68
src/loops/cube.cpp
Normal file
68
src/loops/cube.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include "ShaderProgram.h"
|
||||||
|
#include "Texture2D.h"
|
||||||
|
#include "engine.h"
|
||||||
|
#include "loops.h"
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, GLuint VAO, std::map<std::string, Texture2D>& textures){
|
||||||
|
//
|
||||||
|
// shader.use();
|
||||||
|
// shader.setUniform("ourTexture", 0);
|
||||||
|
// textures["cube"].bind();
|
||||||
|
//
|
||||||
|
// // Matrice de Projection
|
||||||
|
// glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
|
||||||
|
// shader.setUniform("projection", projection); // Assurez-vous que setUniform pour mat4 est implémenté
|
||||||
|
//
|
||||||
|
// // Matrice de Vue
|
||||||
|
// glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), // Position caméra
|
||||||
|
// glm::vec3(0.0f, 0.0f, 0.0f), // Cible
|
||||||
|
// glm::vec3(0.0f, 1.0f, 0.0f)); // Axe Haut
|
||||||
|
// shader.setUniform("view", view);
|
||||||
|
//
|
||||||
|
// // Matrice Modèle
|
||||||
|
// glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
// model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
|
||||||
|
// shader.setUniform("model", model);
|
||||||
|
//
|
||||||
|
// // Dessiner le cube
|
||||||
|
// glBindVertexArray(VAO);
|
||||||
|
// glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& objects){
|
||||||
|
|
||||||
|
shader.use();
|
||||||
|
shader.setUniform("ourTexture", 0);
|
||||||
|
|
||||||
|
objects["cube"].texture.bind();
|
||||||
|
//textures["cube"].bind();
|
||||||
|
|
||||||
|
// Matrice de Projection
|
||||||
|
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
|
||||||
|
shader.setUniform("projection", projection); // Assurez-vous que setUniform pour mat4 est implémenté
|
||||||
|
|
||||||
|
// Matrice de Vue
|
||||||
|
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), // Position caméra
|
||||||
|
glm::vec3(0.0f, 0.0f, 0.0f), // Cible
|
||||||
|
glm::vec3(0.0f, 1.0f, 0.0f)); // Axe Haut
|
||||||
|
shader.setUniform("view", view);
|
||||||
|
|
||||||
|
// Matrice Modèle
|
||||||
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
|
||||||
|
shader.setUniform("model", model);
|
||||||
|
|
||||||
|
// Dessiner le cube
|
||||||
|
glBindVertexArray(objects["cube"].VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
|
||||||
|
//std::cout << objects["cube"].VAO << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
32
src/loops/rainbowWindow.cpp
Normal file
32
src/loops/rainbowWindow.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
#include "loops.h"
|
||||||
|
#include "iostream"
|
||||||
|
#include <GL/gl.h>
|
||||||
|
|
||||||
|
|
||||||
|
void djalim::rainbowWindow(double elapsedSeconds){
|
||||||
|
static double red = 0.0;
|
||||||
|
static double green= 0.0;
|
||||||
|
static double blue = 0.0;
|
||||||
|
static double thresh = 0.0;
|
||||||
|
|
||||||
|
thresh += elapsedSeconds;
|
||||||
|
|
||||||
|
if (thresh >0.1){
|
||||||
|
thresh = 0.0;
|
||||||
|
red += 0.1;
|
||||||
|
if( red > 1.0 ) {
|
||||||
|
red = 0.0;
|
||||||
|
green += 0.1;
|
||||||
|
if( green > 1.0 ) {
|
||||||
|
green = 0.0;
|
||||||
|
blue += 0.1;
|
||||||
|
if( blue > 1.0 ) {
|
||||||
|
blue = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "red: " << red << ", green: " << green << ", blue: " << blue << " \r";
|
||||||
|
glClearColor(red, green, blue, 1.0f);
|
||||||
|
}
|
||||||
12
src/loops/rbgTriangle.cpp
Normal file
12
src/loops/rbgTriangle.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
#include "loops.h"
|
||||||
|
|
||||||
|
void djalim::rgbTriangle(djalim::ShaderProgram shaderProgram, unsigned int VAO){
|
||||||
|
// Dessiner le triangle
|
||||||
|
shaderProgram.use();
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
|
glClearColor(0, 0.5, 0.5, 1.0f);
|
||||||
|
|
||||||
|
}
|
||||||
7
src/main.cpp
Normal file
7
src/main.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600);
|
||||||
|
engine.start();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
76
src/onCreate.cpp
Normal file
76
src/onCreate.cpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
#include "Texture2D.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::onCreate(){
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
std::vector<float> vertices {
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
createObject("cube", "../assets/textures/prof.png", vertices);
|
||||||
|
|
||||||
|
// textures["cube"] = Texture2D();
|
||||||
|
// bool textureLoaded = textures["cube"].loadTexture("../assets/textures/prof.png");
|
||||||
|
// if (!textureLoaded) {
|
||||||
|
// std::cerr << "Failed to load cube texture!" << std::endl;
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// glGenVertexArrays(1, &VAO);
|
||||||
|
// glGenBuffers(1, &VBO);
|
||||||
|
// glBindVertexArray(VAO);
|
||||||
|
// glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
// // Attribut de position
|
||||||
|
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||||
|
// glEnableVertexAttribArray(0);
|
||||||
|
// // Attribut de coordonnée de texture
|
||||||
|
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
// glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
// juste pour voir le cube
|
||||||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
}
|
||||||
5
src/onDestroy.cpp
Normal file
5
src/onDestroy.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::onDestroy(){
|
||||||
|
|
||||||
|
}
|
||||||
10
src/onUpdate.cpp
Normal file
10
src/onUpdate.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#include "loops.h"
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::onUpdate(){
|
||||||
|
showFps();
|
||||||
|
|
||||||
|
cube(window,shaderProgram,objects);
|
||||||
|
|
||||||
|
}
|
||||||
26
src/utils/showFps.cpp
Normal file
26
src/utils/showFps.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
|
||||||
|
void djalim::OpenGlEngine::showFps()
|
||||||
|
{
|
||||||
|
static double thresh = 0.0;
|
||||||
|
|
||||||
|
thresh += elapsedSeconds;
|
||||||
|
|
||||||
|
if (thresh > 0.25)
|
||||||
|
{
|
||||||
|
double fps = (double)frameCount / thresh;
|
||||||
|
double msPerFrame = 1000.0 / fps;
|
||||||
|
|
||||||
|
std::ostringstream outs;
|
||||||
|
outs.precision(3);
|
||||||
|
outs << std::fixed
|
||||||
|
<< "FPS: " << fps << " "
|
||||||
|
<< "Frame Time: " << msPerFrame << "(ms)";
|
||||||
|
glfwSetWindowTitle(window, outs.str().c_str());
|
||||||
|
|
||||||
|
|
||||||
|
frameCount = 0;
|
||||||
|
thresh = 0.0;
|
||||||
|
}
|
||||||
|
frameCount++;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user