the sparrowrenderer no longer supports opengl inverior to 3.3 Core (goodbye crappy rendering)

This commit is contained in:
Anselme 2016-06-06 17:38:28 +02:00
parent d06c6d681d
commit b5f0395cca
25 changed files with 6707 additions and 41902 deletions

View File

@ -18,7 +18,7 @@ file(GLOB LIB_HEAD_LIST src/*.h)
set(IS_LIBRARY True)
set(USE_OPENGL True)
add_definitions(-Wno-comment -DGLEW_BUILD -DGLEW_STATIC)
add_definitions(-Wno-comment)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
add_definitions(-DRENDER_DEBUG)
endif()

View File

@ -74,7 +74,7 @@ public:
virtual void setVertexAttrib(int location, int nbComponents, int offset = 0, int instanceDivisor = 0)
{
if(m_type == VBO && SparrowRenderer::isModernOpenGLAvailable())
if(m_type == VBO)
{
glBindBuffer(GL_ARRAY_BUFFER, m_id);
glEnableVertexAttribArray(location);

View File

@ -1,67 +0,0 @@
#include "crappymodule.h"
#include "shader.h"
#include "camera.h"
#include "mesh.h"
#include "sparrowrenderer.h"
#include "scene.h"
#include "material.h"
#include <glm/ext.hpp>
void CrappyModule::renderGL(Camera* myCamera, Scene* scene)
{
glEnable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0.1f), 1)));
glLightfv(GL_LIGHT0, GL_POSITION, glm::value_ptr(glm::vec4(0)));
glLightfv(GL_LIGHT0, GL_DIFFUSE, glm::value_ptr(glm::vec4(0, 0, 0, 1)));
glLightfv(GL_LIGHT0, GL_SPECULAR, glm::value_ptr(glm::vec4(0, 0, 0, 1)));
int i=1;
for(SceneIterator<Light*>* lightIt = scene->getLights();
lightIt->isValid(); lightIt->next())
{
Light* l = lightIt->getItem();
glEnable(GL_LIGHT0 + i);
glLightfv(GL_LIGHT0 + i, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0), 1)));
if(l->isDirectionnal()){
glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(glm::vec4(-l->getDir(), 0)));
}else{
glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(glm::vec4(l->getPos(), 1)));
}
glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, glm::value_ptr(glm::vec4(l->getColor(), 1)));
glLightfv(GL_LIGHT0 + i, GL_SPECULAR, glm::value_ptr(glm::vec4(l->getColor(), 1)));
++i;
}
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
geometryIt->isValid(); geometryIt->next())
{
GeometryNode* node = geometryIt->getItem();
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(myCamera->getProjectionMatrix()));
glMatrixMode(GL_MODELVIEW);
if(!node->mesh->instances_offsets.empty())
{
for(const glm::vec3 &pos : node->mesh->instances_offsets)
{
glm::mat4 offsetMatrix = glm::translate(node->modelMatrix, pos);
glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * offsetMatrix;
glLoadMatrixf(glm::value_ptr(modelViewMatrix));
node->mesh->draw();
}
}
else
{
glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * node->modelMatrix;
glLoadMatrixf(glm::value_ptr(modelViewMatrix));
node->mesh->draw();
}
}
glDisable(GL_LIGHTING);
for(int j=0; j<i; ++j)
glDisable(GL_LIGHTING);
}

View File

@ -1,15 +0,0 @@
#ifndef CRAPPYMODULE_H
#define CRAPPYMODULE_H
#include "module.h"
#include <cstddef>
#include "opengl.h"
class CrappyModule : public Module
{
public:
void renderGL(Camera* myCamera, Scene* scene = NULL);
bool requiresModernOpenGL() {return false;}
};
#endif // CRAPPYMODULE_H

View File

@ -25,11 +25,8 @@ public:
}
virtual void renderGL(Camera* myCamera, Scene* scene);
virtual bool requiresModernOpenGL() {return true;}
virtual void resize(int w, int h) {width = w; height = h;}
// modern opengl methods
void setShaderSource(ShaderSource* source);
void compileShaders(Scene* scene);

1563
src/gl3w.cpp Normal file

File diff suppressed because it is too large Load Diff

1458
src/gl3w.h Normal file

File diff suppressed because it is too large Load Diff

3621
src/glcorearb.h Normal file

File diff suppressed because it is too large Load Diff

18607
src/glew.cpp

File diff suppressed because it is too large Load Diff

19804
src/glew.h

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,6 @@ struct Material
{
/**
* @brief bindAttributes should send the material attribute to the specified shader as a uniform
* if the shader is NULL, we can assume that modern opengl is not available and try to apply the material differently (glMaterial)
*/
virtual void bindAttributes(Shader*) = 0;

View File

@ -136,37 +136,14 @@ void Mesh::draw(Shader* shader, bool drawNormals, bool drawTexCoord, bool drawTa
{
if(isDoubleSided)
glDisable(GL_CULL_FACE);
bool crappy = (shader == NULL);
if(material != NULL)
material->bindAttributes(shader);
glBindVertexArray(vao);
if(crappy)
{
Buffer *b = buffers[buffersId[POSITION_BUFFER]];
b->bind();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)); // TODO : check 2D positions
if(!texCoords.empty() && drawTexCoord)
{
b = buffers[buffersId[TEXCOORD_BUFFER]];
b->bind();
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
}
if(!normals.empty() && drawNormals)
{
b = buffers[buffersId[NORMAL_BUFFER]];
b->bind();
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));
}
b->unbind();
}
if(indices.empty())
{
int size = positions3D.empty() ? positions2D.size() : positions3D.size();
if(!instances_offsets.empty() && !crappy)
if(!instances_offsets.empty())
{
glDrawArraysInstanced(primitive_type, 0, size, instances_offsets.size());
}
@ -179,7 +156,7 @@ void Mesh::draw(Shader* shader, bool drawNormals, bool drawTexCoord, bool drawTa
{
Buffer *b = buffers[buffersId[INDICES_BUFFER]];
b->bind();
if(!instances_offsets.empty() && !crappy)
if(!instances_offsets.empty())
{
glDrawElementsInstanced(primitive_type, indices.size(), GL_UNSIGNED_INT, NULL, instances_offsets.size());
}
@ -189,15 +166,6 @@ void Mesh::draw(Shader* shader, bool drawNormals, bool drawTexCoord, bool drawTa
}
b->unbind();
}
if(crappy)
{
glDisableClientState(GL_VERTEX_ARRAY);
if(!normals.empty() && drawNormals)
glDisableClientState(GL_NORMAL_ARRAY);
if(!texCoords.empty() && drawTexCoord)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glBindVertexArray(0);
if(isDoubleSided)

View File

@ -87,7 +87,7 @@ public:
* - destroyGL releases the allocated GPU memory, destroyGL is called automatically in Mesh's destructor and at the beginning of initGL
*/
void initGL();
void draw(Shader* shader = NULL, bool drawNormals = true, bool drawTexCoord = true, bool drawTangents = true);
void draw(Shader* shader, bool drawNormals = true, bool drawTexCoord = true, bool drawTangents = true);
void destroyGL();
/**

View File

@ -8,7 +8,6 @@ class Module
{
public:
virtual void renderGL(Camera* myCamera, Scene* scene) = 0;
virtual bool requiresModernOpenGL() {return true;}
virtual void resize(int width, int height) {}
virtual ~Module(){}
};

View File

@ -1,6 +1,6 @@
#ifndef OPENGL_INCLUDE_H
#define OPENGL_INCLUDE_H
#include "glew.h"
#include "gl3w.h"
#endif // OPENGL_INCLUDE_H

View File

@ -8,61 +8,43 @@
void PhongMaterial::bindAttributes(Shader* myShader)
{
if(SparrowRenderer::isModernOpenGLAvailable() && myShader != NULL)
// TODO store the attributes location (in the shader class maybe)
myShader->bindFloat(myShader->getLocation("materialNs"), shininess);
if(normal_map != NULL)
{
// TODO store the attributes location (in the shader class maybe)
myShader->bindFloat(myShader->getLocation("materialNs"), shininess);
normal_map->bind(NORMALS_SLOT);
myShader->bindInteger(myShader->getLocation("normalMap"), NORMALS_SLOT);
}
if(normal_map != NULL)
{
normal_map->bind(NORMALS_SLOT);
myShader->bindInteger(myShader->getLocation("normalMap"), NORMALS_SLOT);
}
if(ambient_texture != NULL)
{
ambient_texture->bind(AMBIENT_SLOT);
myShader->bindInteger(myShader->getLocation("ambientTexture"), AMBIENT_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKa"), ambient);
if(diffuse_texture != NULL)
{
diffuse_texture->bind(DIFFUSE_SLOT);
myShader->bindInteger(myShader->getLocation("diffuseTexture"), DIFFUSE_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKd"), diffuse);
if(specular_texture != NULL)
{
specular_texture->bind(SPECULAR_SLOT);
myShader->bindInteger(myShader->getLocation("specularTexture"), SPECULAR_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKs"), specular);
if(alpha_mask != NULL)
{
alpha_mask->bind(ALPHA_SLOT);
myShader->bindInteger(myShader->getLocation("alphaMask"), ALPHA_SLOT);
}
if(ambient_texture != NULL)
{
ambient_texture->bind(AMBIENT_SLOT);
myShader->bindInteger(myShader->getLocation("ambientTexture"), AMBIENT_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKa"), ambient);
if(diffuse_texture != NULL)
{
// Crappy rendering code
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, glm::value_ptr(glm::vec4(ambient, 1)));
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glm::value_ptr(glm::vec4(diffuse, 1)));
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, glm::value_ptr(glm::vec4(specular, 1)));
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
if(diffuse_texture != NULL)
diffuse_texture->bind(0);
else
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
}
diffuse_texture->bind(DIFFUSE_SLOT);
myShader->bindInteger(myShader->getLocation("diffuseTexture"), DIFFUSE_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKd"), diffuse);
if(specular_texture != NULL)
{
specular_texture->bind(SPECULAR_SLOT);
myShader->bindInteger(myShader->getLocation("specularTexture"), SPECULAR_SLOT);
}
else
myShader->bindVec3(myShader->getLocation("materialKs"), specular);
if(alpha_mask != NULL)
{
alpha_mask->bind(ALPHA_SLOT);
myShader->bindInteger(myShader->getLocation("alphaMask"), ALPHA_SLOT);
}
}

View File

@ -44,7 +44,7 @@ struct PhongMaterial : public Material
virtual ~PhongMaterial() {}
virtual void bindAttributes(Shader* myShader = NULL);
virtual void bindAttributes(Shader* myShader);
virtual unsigned int getFlags();
};

View File

@ -1,6 +1,5 @@
#include "pipeline.h"
#include "forwardmodule.h"
#include "crappymodule.h"
#include "shadersource.h"
#include "sparrowrenderer.h"
#include "scene.h"
@ -19,15 +18,9 @@ SimplePipeline::SimplePipeline(ShaderSource *forwardSource)
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
m_isCrappy = !(forwardSource != NULL && SparrowRenderer::isModernOpenGLAvailable());
if(!m_isCrappy)
{
ForwardModule *forward = new ForwardModule();
forward->setShaderSource(forwardSource);
modules.push_back(forward);
}
else
modules.push_back(new CrappyModule());
ForwardModule *forward = new ForwardModule();
forward->setShaderSource(forwardSource);
modules.push_back(forward);
}
void SimplePipeline::renderGL(Scene *scene)
@ -50,11 +43,8 @@ void SimplePipeline::resizeGL(int w, int h)
void SimplePipeline::refreshScene(Scene *scene)
{
if(!m_isCrappy)
{
ForwardModule *forward = (ForwardModule*)(modules[0]);
forward->compileShaders(scene);
}
ForwardModule *forward = (ForwardModule*)(modules[0]);
forward->compileShaders(scene);
}
StandardPipeline::StandardPipeline(const Settings &mySettings, const SourcePack &mySourcePack)

View File

@ -31,11 +31,9 @@ public:
/**
* @brief The SimplePipeline class creates a simple graphic pipeline able to render 3D meshes
* it is compatible with crappy rendering
*/
class SimplePipeline : public Pipeline
{
bool m_isCrappy;
Camera *m_camera;
glm::vec3 m_clearColor;

View File

@ -23,23 +23,17 @@ SkyboxModule::SkyboxModule(Texture* myCubeMap)
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW);
if(SparrowRenderer::isModernOpenGLAvailable())
{
renderTarget = FrameBuffer::screen;
shader = new Shader(vertSource, fragSource);
mvpLocation = shader->getLocation("MVP");
}
renderTarget = FrameBuffer::screen;
shader = new Shader(vertSource, fragSource);
mvpLocation = shader->getLocation("MVP");
glBindVertexArray(0);
}
SkyboxModule::~SkyboxModule()
{
if(SparrowRenderer::isModernOpenGLAvailable())
{
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(2, vbos);
}
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(2, vbos);
}
void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
@ -49,50 +43,20 @@ void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
glm::mat4 projectionMatrix = myCamera->getProjectionMatrix();
glDisable(GL_CULL_FACE);
glDepthMask(GL_FALSE);
if(!SparrowRenderer::isModernOpenGLAvailable())
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(viewMatrix));
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projectionMatrix));
}
else
{
renderTarget->bindFBO();
shader->bind();
shader->bindMat4(mvpLocation, projectionMatrix * viewMatrix);
}
renderTarget->bindFBO();
shader->bind();
shader->bindMat4(mvpLocation, projectionMatrix * viewMatrix);
cubeMap->bind(0);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
if(SparrowRenderer::isModernOpenGLAvailable())
{
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
}
else
{
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_CUBE_MAP);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
}
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL);
glBindVertexArray(0);
if(!SparrowRenderer::isModernOpenGLAvailable())
{
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_CUBE_MAP);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glEnable(GL_CULL_FACE);
glDepthMask(GL_TRUE);
}

View File

@ -19,7 +19,6 @@ class SkyboxModule : public Module
int width;
int height;
// modern opengl variables
GLuint vao;
GLuint vbos[2];
GLuint mvpLocation;
@ -32,7 +31,6 @@ public:
SkyboxModule(Texture* myCubeMap);
~SkyboxModule();
virtual void renderGL(Camera* myCamera, Scene* scene = NULL);
virtual bool requiresModernOpenGL() {return false;}
virtual void resize(int w, int h) {width = w; height = h;}
void setRenderTarget(const FrameBuffer* target);
};

View File

@ -22,35 +22,30 @@ const GLfloat QUAD_VERTICES[] = {
// main methods
bool SparrowRenderer::modernOpenglAvailable = false;
void SparrowRenderer::initGL(int w, int h, bool forceCrappy)
void SparrowRenderer::initGL(int w, int h)
{
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
int err = gl3wInit();
if (err != 0)
fprintf(stderr, "Warning: glewInit failed!\n");
modernOpenglAvailable = !forceCrappy &&
GLEW_ARB_vertex_program &&
GLEW_ARB_draw_buffers &&
GLEW_ARB_framebuffer_object &&
glewIsSupported("GL_VERSION_3_3") &&
GLEW_VERSION_3_3 &&
strcmp("3.30",(const char *)glGetString(GL_SHADING_LANGUAGE_VERSION)) <= 0;
bool modernOpenglAvailable = gl3wIsSupported(3, 3);
#ifdef RENDER_DEBUG
printf("Using SparrowRenderer %d.%d\n\n", SparrowRenderer_VERSION_MAJOR, SparrowRenderer_VERSION_MINOR);
if(modernOpenglAvailable)
printf("Modern OpenGL available.\n");
else
fprintf(stderr, "Warning: modern OpenGL not supported!\nEnabling fallback crappy rendering mode\n");
printf("OpenGL %s\nGLSL %s\nRenderer %s\nVendor %s\n",
glGetString(GL_VERSION),
glGetString(GL_SHADING_LANGUAGE_VERSION),
glGetString(GL_RENDERER),
glGetString(GL_VENDOR));
#endif
if(!modernOpenglAvailable)
{
fprintf(stderr, "Warning: modern OpenGL not supported! (Opengl 3.3 Core)\nPlease update your drivers or get a real GPU.\n");
exit(EXIT_FAILURE);
}
// allocate quad
if(m_quadVAO == 0)
@ -98,11 +93,6 @@ void SparrowRenderer::renderGL()
glFinish();
}
bool SparrowRenderer::isModernOpenGLAvailable()
{
return modernOpenglAvailable;
}
// utils
void SparrowRenderer::drawQuad()
{

View File

@ -21,11 +21,9 @@ public:
~SparrowRenderer();
// main methods
void initGL(int w, int h, bool forceCrappy = false);
void initGL(int w, int h);
void resizeGL(int w, int h);
void renderGL();
static bool isModernOpenGLAvailable();
// utils
static void drawQuad();
@ -41,8 +39,6 @@ protected:
static GLuint m_quadVBO;
Scene* m_scene;
static bool modernOpenglAvailable;
};
#endif // SPARROWRENDERER_H

File diff suppressed because it is too large Load Diff