From 8f9bc0da0912295d37acbddf8e83d999d395be07 Mon Sep 17 00:00:00 2001 From: MrDev023 Date: Sat, 8 Oct 2016 18:35:02 +0200 Subject: [PATCH] Update cmake --- CMakeLists.txt | 20 ++++++++-- configure.sh | 3 ++ res/shaders/test.frag | 11 ++++-- res/shaders/test.vert | 15 ++++++-- src/render/renderer.h | 20 ++++++---- src/render/vao.cpp | 39 ++++++++++++------- src/render/vbo.cpp | 90 +++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 164 insertions(+), 34 deletions(-) create mode 100644 configure.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 2bed42a..c9a72f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,10 @@ #require opengl,glew,glut,glu,al,alc,sndfile library +IF (WIN32) + SET(CMAKE_C_COMPILER C:/mingw32/bin/) + SET(CMAKE_CXX_COMPILER C:/mingw32/bin/) +ENDIF() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") cmake_minimum_required(VERSION 2.6) @@ -14,9 +19,19 @@ set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE}) #Inclusion de Boost IF (WIN32) - include_directories($ENV{MINGW_HOME}/usr/include) - link_directories($ENV{MINGW_HOME}/usr/lib) + include_directories($ENV{MINGW_HOME}/include) + link_directories($ENV{MINGW_HOME}/lib) ELSE() + INCLUDE(FindPkgConfig) + PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) + PKG_SEARCH_MODULE(SDL2_image REQUIRED SDL2_image) + PKG_SEARCH_MODULE(SDL2_mixer REQUIRED SDL2_mixer) + PKG_SEARCH_MODULE(SDL2_ttf REQUIRED SDL2_ttf) + find_package(GLEW REQUIRED) + find_package(OpenGL REQUIRED) + find_package(GLU REQUIRED) + find_package(GLUT REQUIRED) + find_package(OpenAL REQUIRED) include_directories(/usr/include) link_directories(/usr/lib) ENDIF() @@ -37,7 +52,6 @@ add_executable( ) - #Configuration de l'édition de liens diff --git a/configure.sh b/configure.sh new file mode 100644 index 0000000..0093a88 --- /dev/null +++ b/configure.sh @@ -0,0 +1,3 @@ +#!/bin/bash +sudo apt install libsdl2* libglew* libglut* libglew-dev:i386 libsndfile-dev -y +cmake . && cmake . 1>/dev/null # deux fois car sinon erreur de compilation diff --git a/res/shaders/test.frag b/res/shaders/test.frag index 2e95595..f6750f6 100644 --- a/res/shaders/test.frag +++ b/res/shaders/test.frag @@ -1,7 +1,10 @@ -#version 150 +#version 330 -uniform float y; +in vec4 color; +in vec3 normal; +in vec2 out_coord_texture; +uniform sampler2D myTexture; void main(void){ - gl_FragColor= vec4(1, 1 - abs(y)/50,1 - abs(y)/50,1); -} \ No newline at end of file + gl_FragColor = color * texture2D(myTexture,out_coord_texture); +} diff --git a/res/shaders/test.vert b/res/shaders/test.vert index c9ed157..5019887 100644 --- a/res/shaders/test.vert +++ b/res/shaders/test.vert @@ -1,6 +1,15 @@ -#version 150 +#version 330 + +layout (location = 0) in vec3 in_position; +layout (location = 1) in vec4 in_color; +layout (location = 2) in vec2 in_coord_texture; +layout (location = 3) in vec3 in_normal; + +out vec4 color; +out vec3 normal; +out vec2 out_coord_texture; void main(void){ gl_FrontColor = gl_Color; - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; -} \ No newline at end of file + gl_Position = gl_ModelViewProjectionMatrix * vec4(in_position,1.0f); +} diff --git a/src/render/renderer.h b/src/render/renderer.h index 34723aa..3226eab 100644 --- a/src/render/renderer.h +++ b/src/render/renderer.h @@ -32,34 +32,40 @@ class VAO; class VAO{ public: - static const int VERTEX_DATA = 0,COLOR_DATA = 1,TEX_COORD_DATA = 2,NORMAL_DATA = 3; std::vector data; + unsigned int n; + bool isIn3DRender; GLuint id; GLuint typeRender; - VAO(GLuint); + VAO(GLuint,unsigned int,bool); ~VAO(); void addData(int,float*,int); void bufferData(); - void render2D(); - void render3D(); + void render(); }; class VBO{ + private: + void render2D(); + void render3D(); + void bufferData2D(); + void bufferData3D(); + public: GLuint id; GLuint typeRender; + bool isIn3DRender; std::vector data; int size; VBO(); - VBO(GLuint); + VBO(GLuint,bool); ~VBO(); void addData(float*,int); void bufferData(); - void render2D(); - void render3D(); + void render(); void bindBuffer(); void unBindBuffer(); }; diff --git a/src/render/vao.cpp b/src/render/vao.cpp index f3af98c..e141a9c 100644 --- a/src/render/vao.cpp +++ b/src/render/vao.cpp @@ -1,34 +1,45 @@ #include "renderer.h" -VAO::VAO(GLuint typeRender){ +VAO::VAO(GLuint typeRender,unsigned int nm,bool isIn3DRender){ + glGenVertexArrays(1,&id); + glBindVertexArray(id); this->typeRender = typeRender; - this->data.push_back(new VBO(typeRender)); - this->data.push_back(new VBO(typeRender)); - this->data.push_back(new VBO(typeRender)); - this->data.push_back(new VBO(typeRender)); + for(int i = 0;i < 4;i++){ + data.push_back(new VBO(typeRender,isIn3DRender)); + } + glBindVertexArray(0); + this->n = nm; + this->isIn3DRender = isIn3DRender; } VAO::~VAO(){ - for(int i = 0;i < 4;i++){ + glBindVertexArray(id); + for(int i = 0;i < this->n;i++){ delete this->data[i]; } this->data.clear(); + glBindVertexArray(0); + glDeleteVertexArrays(1,&id); } -void VAO::addData(int type,float* data,int size){ - this->data[type]->addData(data,size); +void VAO::addData(int i,float* data,int size){ + glBindVertexArray(id); + this->data[i]->addData(data,size); + glBindVertexArray(0); } void VAO::bufferData(){ + glBindVertexArray(id); for(int i = 0;i < 4;i++){ this->data[i]->bufferData(); } + glBindVertexArray(0); } -void VAO::render2D(){ - +void VAO::render(){ + glBindVertexArray(id); + for(int i = 0;i < 4;i++){ + this->data[i]->render(); + } + glBindVertexArray(0); } - -void VAO::render3D(){ - -} \ No newline at end of file diff --git a/src/render/vbo.cpp b/src/render/vbo.cpp index d8ccfce..8fe76c4 100644 --- a/src/render/vbo.cpp +++ b/src/render/vbo.cpp @@ -4,12 +4,14 @@ VBO::VBO(){ this->typeRender = GL_TRIANGLE_STRIP; glGenBuffers(1,&this->id); this->size = 0; + this->isIn3DRender = true; } -VBO::VBO(GLuint typeRender){ +VBO::VBO(GLuint typeRender,bool isIn3DRender){ this->typeRender = typeRender; glGenBuffers(1,&this->id); this->size = 0; + this->isIn3DRender = isIn3DRender; } VBO::~VBO(){ @@ -17,6 +19,7 @@ VBO::~VBO(){ glDeleteBuffers(1,&this->id); } +// 2 ou 3 vertex + 4 color (RGBA) + TexCoords + Normal void VBO::addData(float* data,int size){ for(int i = 0;i < size;i++){ this->data.push_back(data[i]); @@ -25,8 +28,55 @@ void VBO::addData(float* data,int size){ } void VBO::bufferData(){ + if(this->isIn3DRender) bufferData3D(); + else bufferData2D(); +} + +void VBO::bufferData2D(){ glBindBuffer(GL_ARRAY_BUFFER,this->id); glBufferData(GL_ARRAY_BUFFER, this->size,&this->data[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, false, (2 + 3 + 4 + 2) * sizeof(float), 0); + + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 3, GL_FLOAT, false, (2 + 3 + 4 + 2) * sizeof(float), (void*)(2 * sizeof(float))); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 4, GL_FLOAT, false, (2 + 3 + 4 + 2) * sizeof(float), (void*)((2 + 3) * sizeof(float))); + + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, false, (2 + 3 + 4 + 2) * sizeof(float), (void*)((2 + 3 + 4) * sizeof(float))); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); + glDisableVertexAttribArray(3); + + glBindBuffer(GL_ARRAY_BUFFER,0); +} + +void VBO::bufferData3D(){ + glBindBuffer(GL_ARRAY_BUFFER,this->id); + glBufferData(GL_ARRAY_BUFFER, this->size,&this->data[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, false, (3 + 3 + 4 + 2) * sizeof(float), 0); + + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 3, GL_FLOAT, false, (3 + 3 + 4 + 2) * sizeof(float), (void*)(3 * sizeof(float))); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 4, GL_FLOAT, false, (3 + 3 + 4 + 2) * sizeof(float), (void*)((3 + 3) * sizeof(float))); + + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, false, (3 + 3 + 4 + 2) * sizeof(float), (void*)((3 + 3 + 4) * sizeof(float))); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); + glDisableVertexAttribArray(3); + glBindBuffer(GL_ARRAY_BUFFER,0); } @@ -37,11 +87,45 @@ void VBO::bindBuffer(){ void VBO::unBindBuffer(){ glBindBuffer(GL_ARRAY_BUFFER,0); } + +void VBO::render(){ + if(this->isIn3DRender) render3D(); + else render2D(); + +} void VBO::render2D(){ - // Plus tard pas besoin pour l instant + glBindBuffer(GL_ARRAY_BUFFER,this->id); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + + glDrawArrays(GL_QUADS, 0, size/(2 + 3 + 4 + 2)); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); + glDisableVertexAttribArray(3); + + glBindBuffer(GL_ARRAY_BUFFER,0); } void VBO::render3D(){ - // Plus tard pas besoin pour l instant + glBindBuffer(GL_ARRAY_BUFFER,this->id); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + + glDrawArrays(GL_QUADS, 0, size/(3 + 3 + 4 + 2)); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); + glDisableVertexAttribArray(3); + + glBindBuffer(GL_ARRAY_BUFFER,0); } \ No newline at end of file