diff --git a/shaders/world.frag.glsl b/shaders/world.frag.glsl index 9cf47b7..01c873f 100644 --- a/shaders/world.frag.glsl +++ b/shaders/world.frag.glsl @@ -6,13 +6,28 @@ uniform vec3 camera; uniform vec2 worldSize; uniform vec2 screenSize; +in vec2 texCoord; +in vec3 normal; + out vec4 outColor; +vec3 phongLighting(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 normal, in vec3 lightDir, in vec3 halfVec){ + float diffuseComponent = max(dot(normal, lightDir), 0); + float specularComponent = max(dot(halfVec, normal), 0); + return color*diffuseComponent*(kd+ks*pow(specularComponent, ns)); +} + void main() { - vec2 halfScreen = screenSize/2; - vec2 screenPos = gl_FragCoord.xy + camera.xy; - vec2 texCoord = (screenPos-halfScreen)/200; - vec3 texColor = texelFetch(colorMap, ivec2(screenPos*camera.z)).xyz; - outColor = vec4(texColor, 1.0); + vec2 worldCoord = texCoord*worldSize + camera.xy; + ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize)); + if(abs(mod(nbRevolutions.y, 2)) > 0.5) + { + worldCoord.x += worldSize.x/2; + nbRevolutions.x = int(floor(worldCoord.x / worldSize.x)); + } + worldCoord = worldCoord - nbRevolutions*worldSize; + vec3 texColor = texelFetch(colorMap, ivec2(worldCoord)).xyz; + float pseudoLighting = 0.4+dot(normal, normalize(vec3(-0.5))); // TODO : use phong lighting + outColor = vec4(texColor*pseudoLighting, 1.0); } diff --git a/shaders/world.vert.glsl b/shaders/world.vert.glsl index f5ecef1..0a71e1b 100644 --- a/shaders/world.vert.glsl +++ b/shaders/world.vert.glsl @@ -1,8 +1,18 @@ #version 330 core -layout(location = 0)in vec2 inPosition; +layout(location = 0)in vec3 inPosition; +layout(location = 2)in vec2 inTexCoord; +layout(location = 1)in vec3 inNormal; + +uniform mat4 mvp; +uniform vec3 camera; + +out vec2 texCoord; +out vec3 normal; void main(void) { - gl_Position = vec4(inPosition, 0.0, 1.0); + texCoord = inTexCoord.xy; + normal = inNormal.xyz; + gl_Position = mvp * vec4(inPosition*camera.z, 1.0); } diff --git a/src/pixelpipeline.cpp b/src/pixelpipeline.cpp index ff04b0c..1008cb2 100644 --- a/src/pixelpipeline.cpp +++ b/src/pixelpipeline.cpp @@ -6,6 +6,8 @@ #include #include #include "mapscene.h" +#include +#include #define SCROLL_SPEED 0.998f @@ -40,6 +42,20 @@ inline glm::vec3 getColor(const Pixel &p) } } +class ToreillerGenerator : public MeshGenerator +{ +public: + virtual glm::vec3 evalUV(float u, float v) + { + const float MAGIC_RATIO = 1; + + glm::vec2 relUV = glm::vec2(u-0.5f, v-0.5f); + float clockAngle = atan2(relUV.y, relUV.x); + float depthAngle = glm::length(relUV)*3.1416f*MAGIC_RATIO; + float r = sin(depthAngle); + return glm::vec3(r*cos(clockAngle), r*sin(clockAngle), cos(depthAngle)); + } +}; // MESH (2D points) @@ -118,18 +134,12 @@ PixelPipeline::PixelPipeline(MapScene *map) : m_mapFBO->initColorAttachments(); // set up vao - const float vertices[] = { - -1.0f, -1.0f, - 3.0f, -1.0f, - -1.0f, 3.0f - }; - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); - glGenBuffers(1, &m_vbo); - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - glBufferData(GL_ARRAY_BUFFER, 3 * 2 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*2, NULL); - glEnableVertexAttribArray(0); + + ToreillerGenerator gen; + m_toreiller = gen.generateParametricMesh(NULL, 40, 40, 1.0f); + m_toreiller->computeNormals(); + m_toreiller->mergeVertices(); + m_toreiller->initGL(); std::string vertSource = QtUtils::fileToString(":shaders/shaders/pixel.vert.glsl").toStdString(); std::string fragSource = QtUtils::fileToString(":shaders/shaders/pixel.frag.glsl").toStdString(); @@ -173,11 +183,10 @@ void PixelPipeline::updateChanges() void PixelPipeline::renderGL(Scene *scene) { m_targetFBO->bindFBO(); - glClearColor(0, 0, 0, 1); - glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, m_width, m_height); + glClearColor(0, 0, 0, 1); glDisable(GL_BLEND); - glDisable(GL_DEPTH_TEST); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); m_renderShader->bind(); m_renderShader->bindVec3(m_renderShader->getLocation("camera"), m_camera); m_renderShader->bindVec2(m_renderShader->getLocation("worldSize"), glm::vec2(m_mapWidth, m_mapHeight)); @@ -185,16 +194,18 @@ void PixelPipeline::renderGL(Scene *scene) m_mapTex->bind(0); m_renderShader->bindInteger(m_renderShader->getLocation("colorMap"), 0); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glBindVertexArray(m_vao); - glDrawArrays(GL_TRIANGLES, 0, 3); - glBindVertexArray(0); + glClearDepth(1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + m_renderShader->bindMat4(m_renderShader->getLocation("mvp"), m_mvp); + m_toreiller->draw(m_renderShader); } void PixelPipeline::resizeGL(int w, int h) { m_width = w; m_height = h; + m_mvp = glm::translate(glm::perspectiveFov(70.f, float(w), float(h), 0.1f, 10.f), glm::vec3(0, 0, -3)); } void PixelPipeline::cameraZoom(int nbScrolls) diff --git a/src/pixelpipeline.h b/src/pixelpipeline.h index fb6997c..9279c0f 100644 --- a/src/pixelpipeline.h +++ b/src/pixelpipeline.h @@ -2,12 +2,14 @@ #define PIXELPIPELINE_H #include +#include class FrameBuffer; class Texture; class Shader; class PixelMesh; class MapScene; +class Mesh; class PixelPipeline : public Pipeline { @@ -24,7 +26,9 @@ private: int m_height; glm::vec3 m_camera; - unsigned int m_vao, m_vbo; + Mesh *m_toreiller; + glm::mat4 m_mvp; + //unsigned int m_vao, m_vbo; public: PixelPipeline(MapScene *map);