WIP I don't know what I'm doing lol help

This commit is contained in:
Mischa Spiegelmock
2018-06-03 15:05:59 +02:00
parent 02e29f50c2
commit 453b9e9767
8 changed files with 78 additions and 44 deletions

View File

@ -12,7 +12,7 @@ class PipelineContext
{
public:
int fps;
float time;
float time;
int frame;
float progress;

View File

@ -1,18 +1,18 @@
#include "Common.hpp"
#include "Renderable.hpp"
#include "RenderContext.hpp"
#include <math.h>
RenderContext::RenderContext()
: time(0),texsize(512), aspectRatio(1), aspectCorrect(false){};
RenderItem::RenderItem() : masterAlpha(1) {
glGenBuffers(1, &vbo);
glGenBuffers(1, &cbo);
check_gl_error();
}
RenderItem::~RenderItem() {
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &cbo);
check_gl_error();
}
@ -191,7 +191,7 @@ void Shape::Draw(RenderContext &context)
points[i][1]=temp_radius*sinf(t*3.1415927f*2 + ang + 3.1415927f*0.25f)+yval;
}
DrawVertices(GL_TRIANGLE_FAN, sides+2, sizeof(points), points, sizeof(colors), colors);
DrawVertices(context, GL_TRIANGLE_FAN, sides+2, sizeof(points), points, sizeof(colors), colors);
delete[] colors;
delete[] points;
@ -214,7 +214,7 @@ void Shape::Draw(RenderContext &context)
}
DrawVertices(GL_LINE_LOOP, sides, sizeof(points), points, 0, nullptr);
DrawVertices(context, GL_LINE_LOOP, sides, sizeof(points), points, 0, nullptr);
if (thickOutline==1)
glLineWidth(context.texsize < 512 ? 1 : context.texsize/512);
@ -222,7 +222,7 @@ void Shape::Draw(RenderContext &context)
delete[] points;
}
void Shape::DrawVertices(GLenum mode, GLsizei count, GLuint pointsSize, floatPair *points, GLuint colorsSize, floatQuad *colors) {
void Shape::DrawVertices(RenderContext &context, GLenum mode, GLsizei count, GLuint pointsSize, floatPair *points, GLuint colorsSize, floatQuad *colors) {
// draw a set of points with colors
@ -231,10 +231,21 @@ void Shape::DrawVertices(GLenum mode, GLsizei count, GLuint pointsSize, floatPai
check_gl_error();
glBufferData(GL_ARRAY_BUFFER, pointsSize, points, GL_STREAM_DRAW);
check_gl_error();
glVertexAttribPointer(context.shaderContext->positionAttribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
check_gl_error();
// bind colors...
if (colors && colorsSize) {
glBindBuffer(GL_ARRAY_BUFFER, cbo);
check_gl_error();
glBufferData(GL_ARRAY_BUFFER, colorsSize, colors, GL_STREAM_DRAW);
check_gl_error();
glVertexAttribPointer(context.shaderContext->colorAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);
check_gl_error();
}
glDrawArrays(mode, 0, count);
check_gl_error();
}

View File

@ -4,27 +4,12 @@
#include <typeinfo>
#include "TextureManager.hpp"
#include "projectM-opengl.h"
class BeatDetect;
#include "RenderContext.hpp"
typedef float floatPair[2];
typedef float floatTriple[3];
typedef float floatQuad[4];
class RenderContext
{
public:
float time;
int texsize;
float aspectRatio;
bool aspectCorrect;
BeatDetect *beatDetect;
TextureManager *textureManager;
RenderContext();
};
class RenderItem
{
public:
@ -34,8 +19,8 @@ public:
~RenderItem();
protected:
// vertex buffer storage
GLuint vbo;
// vertex and color buffer storage
GLuint vbo, cbo;
};
typedef std::vector<RenderItem*> RenderItemList;
@ -85,7 +70,7 @@ public:
virtual void Draw(RenderContext &context);
private:
void DrawVertices(GLenum mode, GLsizei count, GLuint pointsSize, floatPair *points, GLuint colorsSize, floatQuad *colors);
void DrawVertices(RenderContext &context, GLenum mode, GLsizei count, GLuint pointsSize, floatPair *points, GLuint colorsSize, floatQuad *colors);
};
class Text : RenderItem

View File

@ -115,7 +115,7 @@ void Renderer::SetPipeline(Pipeline &pipeline)
currentPipe = &pipeline;
shaderEngine.reset();
// N.B. i'm actually not sure if they're always fragment shaders... I think so... -mischa
shaderEngine.loadPresetShader(GL_FRAGMENT_SHADER, pipeline.warpShader, pipeline.warpShaderFilename);
// shaderEngine.loadPresetShader(GL_FRAGMENT_SHADER, pipeline.warpShader, pipeline.warpShaderFilename);
// enable!
// shaderEngine.loadPresetShader(GL_FRAGMENT_SHADER, pipeline.compositeShader, pipeline.compositeShaderFilename);
}
@ -149,6 +149,7 @@ void Renderer::RenderItems(const Pipeline &pipeline, const PipelineContext &pipe
renderContext.aspectRatio = aspect;
renderContext.textureManager = textureManager;
renderContext.beatDetect = beatDetect;
renderContext.shaderContext = &shaderEngine.context;
for (std::vector<RenderItem*>::const_iterator pos = pipeline.drawables.begin(); pos != pipeline.drawables.end(); ++pos)
{
@ -332,13 +333,16 @@ void Renderer::Interpolation(const Pipeline &pipeline)
check_gl_error();
glBufferData(GL_ARRAY_BUFFER, getMeshSize(), p, GL_DYNAMIC_DRAW);
check_gl_error();
glVertexAttribPointer(getPositionAttribute(), 2, GL_FLOAT, GL_FALSE, 0, 0);
check_gl_error();
// glVertexAttribPointer(getColorAttribute(), 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), 0);
// render each row of the mesh
for (int j = 0; j < mesh.height - 1; j++)
glDrawArrays(GL_TRIANGLE_STRIP, j * mesh.width * 2, mesh.width * 2);
// glDrawArrays(GL_TRIANGLE_STRIP, j * mesh.width * 2, mesh.width * 2);
check_gl_error();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
check_gl_error();
}
@ -744,3 +748,13 @@ void Renderer::CompositeOutput(const Pipeline &pipeline, const PipelineContext &
!= pipeline.compositeDrawables.end(); ++pos)
(*pos)->Draw(renderContext);
}
GLuint Renderer::getPositionAttribute() {
return shaderEngine.context.positionAttribute;
}
GLuint Renderer::getColorAttribute() {
return shaderEngine.context.colorAttribute;
}

View File

@ -60,7 +60,8 @@ public:
void ResetTextures();
void reset(int w, int h);
GLuint initRenderToTexture();
GLuint getPositionAttribute();
GLuint getColorAttribute();
void SetPipeline(Pipeline &pipeline);

View File

@ -36,22 +36,25 @@ void ShaderEngine::initShaderProgram()
{
// our program
program = glCreateProgram();
glProgramParameteri(program, GL_PROGRAM_SEPARABLE, GL_TRUE);
// glProgramParameteri(program, GL_PROGRAM_SEPARABLE, GL_TRUE);
check_gl_error();
// our VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
check_gl_error();
// our vertex shader
const char* vertexSource = R"glsl(
#version 150 core
in vec2 position;
in vec3 color;
out vec3 Color;
void main()
{
Color = color;
gl_Position = vec4(position, 0.0, 1.0);
}
)glsl";
@ -66,11 +69,12 @@ void ShaderEngine::initShaderProgram()
const char* fragmentSource = R"glsl(
#version 150 core
in vec3 Color;
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
outColor = vec4(Color, 1.0);
}
)glsl";
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
@ -78,21 +82,30 @@ void ShaderEngine::initShaderProgram()
glCompileShader(fragmentShader);
checkCompileStatus(fragmentShader, "internal fragment shader");
check_gl_error();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
check_gl_error();
glBindFragDataLocation(program, 0, "outColor");
check_gl_error();
relinkProgram();
glUseProgram(program);
// configure vertex position input for vertex shader
GLint posAttrib = glGetAttribLocation(program, "position");
context.positionAttribute = glGetAttribLocation(program, "position");
// glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(context.positionAttribute);
check_gl_error();
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
check_gl_error();
glEnableVertexAttribArray(posAttrib);
context.colorAttribute = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(context.colorAttribute);
check_gl_error();
// TODO: pre-lookup uniform locations here and save them
printf("shader program initialized\n");
}

View File

@ -11,11 +11,12 @@
#include "Common.hpp"
#include "projectM-opengl.h"
class ShaderEngine;
#include "ShaderContext.hpp"
#include "Pipeline.hpp"
#include "PipelineContext.hpp"
class ShaderEngine;
#include "TextureManager.hpp"
#include "BeatDetect.hpp"
#include "HLSLTranslator.hpp"
#include <cstdlib>
@ -77,6 +78,7 @@ public:
void reset();
void setAspect(float aspect);
std::string profileName;
ShaderContext context;
};
#endif /* SHADERENGINE_HPP_ */

View File

@ -32,7 +32,6 @@ void Waveform::Draw(RenderContext &context)
//if (samples > 2048) samples = 2048;
if (additive)
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
else
@ -86,7 +85,16 @@ void Waveform::Draw(RenderContext &context)
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, samples * 2, p, GL_DYNAMIC_DRAW);
glVertexAttribPointer(context.shaderContext->positionAttribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
check_gl_error();
GLuint cbo;
glGenBuffers(1, &cbo);
glBindBuffer(GL_ARRAY_BUFFER, cbo);
glBufferData(GL_ARRAY_BUFFER, samples * 4, colors, GL_STREAM_DRAW);
// glEnableVertexAttribArray(context.shaderContext->colorAttribute);
glVertexAttribPointer(context.shaderContext->colorAttribute, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDeleteBuffers(1, &cbo);
// glVertexPointer(2,GL_FLOAT,0,p);
// glColorPointer(4,GL_FLOAT,0,colors);