Browse Source

Try to factor all the recent GLSL stuff in the new Shader class.

legacy
Sam Hocevar sam 14 years ago
parent
commit
01be151c62
12 changed files with 187 additions and 51 deletions
  1. +1
    -1
      src/Makefile.am
  2. +1
    -0
      src/core.h
  3. +13
    -9
      src/scene.cpp
  4. +101
    -0
      src/shader.cpp
  5. +37
    -0
      src/shader.h
  6. +10
    -41
      src/video.cpp
  7. +2
    -0
      win32/deushax.vcxproj
  8. +6
    -0
      win32/deushax.vcxproj.filters
  9. +2
    -0
      win32/editor.vcxproj
  10. +6
    -0
      win32/editor.vcxproj.filters
  11. +2
    -0
      win32/monsterz.vcxproj
  12. +6
    -0
      win32/monsterz.vcxproj.filters

+ 1
- 1
src/Makefile.am View File

@@ -9,7 +9,7 @@ liblol_a_SOURCES = \
timer.cpp timer.h bitfield.h profiler.cpp profiler.h input.h input.cpp \
world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \
text.cpp text.h emitter.cpp emitter.h numeric.h \
worldentity.cpp worldentity.h \
worldentity.cpp worldentity.h shader.cpp shader.h \
\
sdlinput.cpp sdlinput.h \
\


+ 1
- 0
src/core.h View File

@@ -42,6 +42,7 @@
#include "dict.h"
#include "map.h"
#include "layer.h"
#include "shader.h"

// Managers
#include "ticker.h"


+ 13
- 9
src/scene.cpp View File

@@ -36,7 +36,7 @@ struct Tile
};

#if LOL_EXPERIMENTAL
extern GLuint prog;
extern Shader *stdshader;
#endif

/*
@@ -160,29 +160,33 @@ void Scene::Render() // XXX: rename to Blit()
{ 0.0, 1.0 },
{ 1.0, 0.0 } };

GLuint vao, vbo[3];
GLuint vao, vbo[3], attr;

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(3, &vbo[0]);

attr = stdshader->GetAttribLocation("in_Position");
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(attr, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr);

attr = stdshader->GetAttribLocation("in_Color");
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), colors, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(attr, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr);

attr = stdshader->GetAttribLocation("in_TexCoord");
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tex, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);
glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr);

delete[] vertices;

glUseProgram(prog);
stdshader->Bind();
glBindVertexArray(vao);
Tiler::Bind(1 << 16);
glDrawArrays(GL_TRIANGLES, 0, 6);


+ 101
- 0
src/shader.cpp View File

@@ -0,0 +1,101 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include <cmath>
#include <cstdio>

#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#if defined __APPLE__ && defined __MACH__
# include <OpenGL/gl.h>
#else
# define GL_GLEXT_PROTOTYPES
# include <GL/gl.h>
#endif

#include "core.h"

/*
* Shader implementation class
*/

class ShaderData
{
friend class Shader;

private:
GLuint prog_id, vert_id, frag_id;
};

/*
* Public Shader class
*/

Shader::Shader(char const *vert, char const *frag)
: data(new ShaderData())
{
char buf[4096];
GLsizei len;

data->vert_id = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(data->vert_id, 1, &vert, NULL);
glCompileShader(data->vert_id);

glGetShaderInfoLog(data->vert_id, sizeof(buf), &len, buf);
if (len > 0)
fprintf(stderr, "ERROR: failed to compile vertex shader: %s", buf);

data->frag_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(data->frag_id, 1, &frag, NULL);
glCompileShader(data->frag_id);

glGetShaderInfoLog(data->frag_id, sizeof(buf), &len, buf);
if (len > 0)
fprintf(stderr, "ERROR: failed to compile fragment shader: %s", buf);

data->prog_id = glCreateProgram();
glAttachShader(data->prog_id, data->vert_id);
glAttachShader(data->prog_id, data->frag_id);

glLinkProgram(data->prog_id);
glValidateProgram(data->prog_id);
}

int Shader::GetAttribLocation(char const *attr) const
{
return glGetAttribLocation(data->prog_id, attr);
}

int Shader::GetUniformLocation(char const *uni) const
{
return glGetUniformLocation(data->prog_id, uni);
}

void Shader::Bind() const
{
glUseProgram(data->prog_id);
}

Shader::~Shader()
{
glDetachShader(data->prog_id, data->vert_id);
glDetachShader(data->prog_id, data->frag_id);
glDeleteShader(data->vert_id);
glDeleteShader(data->frag_id);
glDeleteProgram(data->prog_id);
delete data;
}


+ 37
- 0
src/shader.h View File

@@ -0,0 +1,37 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

//
// The Shader class
// --------------
//

#if !defined __DH_SHADER_H__
#define __DH_SHADER_H__

class ShaderData;

class Shader
{
public:
Shader(char const *vert, char const *frag);
~Shader();

int GetAttribLocation(char const *attr) const;
int GetUniformLocation(char const *uni) const;

void Bind() const;

private:
ShaderData *data;
};

#endif // __DH_SHADER_H__


+ 10
- 41
src/video.cpp View File

@@ -29,8 +29,7 @@
#include "core.h"

#if LOL_EXPERIMENTAL
GLuint prog, sh1, sh2;
GLint uni_m1, uni_m2, uni_m3;
Shader *stdshader;

float4x4 projection_matrix, view_matrix, model_matrix;
#endif
@@ -86,37 +85,7 @@ void Video::Setup(int width, int height)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

#if LOL_EXPERIMENTAL
sh1 = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sh1, 1, &vertexshader, NULL);
glCompileShader(sh1);

char buf[4096];
GLsizei dummy;
glGetShaderInfoLog(sh1, 4096, &dummy, buf);
fprintf(stderr, "sh1 %i: %s", sh1, buf);

sh2 = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sh2, 1, &fragmentshader, NULL);
glCompileShader(sh2);

glGetShaderInfoLog(sh2, 4096, &dummy, buf);
fprintf(stderr, "sh2 %i: %s", sh2, buf);

prog = glCreateProgram();
glAttachShader(prog, sh1);
glAttachShader(prog, sh2);

glBindAttribLocation(prog, 0, "in_Position");
glBindAttribLocation(prog, 1, "in_Color");
glBindAttribLocation(prog, 2, "in_TexCoord");
glLinkProgram(prog);
glValidateProgram(prog);

uni_m1 = glGetUniformLocation(prog, "projection_matrix");
uni_m2 = glGetUniformLocation(prog, "view_matrix");
uni_m3 = glGetUniformLocation(prog, "model_matrix");

glClearColor(0.4f, 0.6f, 0.9f, 0.0f);
stdshader = new Shader(vertexshader, fragmentshader);
#endif
}

@@ -199,9 +168,13 @@ void Video::Clear()
model_matrix[1][1] = 0.5f;
model_matrix[2][2] = 0.5f;

glUniformMatrix4fv(uni_m1, 1, GL_FALSE, &projection_matrix[0][0]);
glUniformMatrix4fv(uni_m2, 1, GL_FALSE, &view_matrix[0][0]);
glUniformMatrix4fv(uni_m3, 1, GL_FALSE, &model_matrix[0][0]);
GLuint uni;
uni = stdshader->GetUniformLocation("projection_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &projection_matrix[0][0]);
uni = stdshader->GetUniformLocation("view_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
uni = stdshader->GetUniformLocation("model_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &model_matrix[0][0]);
#else
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
@@ -225,11 +198,7 @@ void Video::Clear()
void Video::Destroy()
{
#if LOL_EXPERIMENTAL
glDetachShader(prog, sh1);
glDetachShader(prog, sh2);
glDeleteShader(sh1);
glDeleteShader(sh2);
glDeleteProgram(prog);
delete stdshader;
#endif
}



+ 2
- 0
win32/deushax.vcxproj View File

@@ -35,6 +35,7 @@
<ClInclude Include="..\src\sampler.h" />
<ClInclude Include="..\src\scene.h" />
<ClInclude Include="..\src\sdlinput.h" />
<ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\text.h" />
<ClInclude Include="..\src\ticker.h" />
<ClInclude Include="..\src\tiler.h" />
@@ -67,6 +68,7 @@
<ClCompile Include="..\src\sampler.cpp" />
<ClCompile Include="..\src\scene.cpp" />
<ClCompile Include="..\src\sdlinput.cpp" />
<ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\text.cpp" />
<ClCompile Include="..\src\ticker.cpp" />
<ClCompile Include="..\src\tiler.cpp" />


+ 6
- 0
win32/deushax.vcxproj.filters View File

@@ -63,6 +63,9 @@
<ClInclude Include="..\src\sdlinput.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\shader.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\text.h">
<Filter>lolengine</Filter>
</ClInclude>
@@ -148,6 +151,9 @@
<ClCompile Include="..\src\sdlinput.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\shader.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\text.cpp">
<Filter>lolengine</Filter>
</ClCompile>


+ 2
- 0
win32/editor.vcxproj View File

@@ -35,6 +35,7 @@
<ClInclude Include="..\src\sampler.h" />
<ClInclude Include="..\src\scene.h" />
<ClInclude Include="..\src\sdlinput.h" />
<ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\text.h" />
<ClInclude Include="..\src\ticker.h" />
<ClInclude Include="..\src\tiler.h" />
@@ -67,6 +68,7 @@
<ClCompile Include="..\src\sampler.cpp" />
<ClCompile Include="..\src\scene.cpp" />
<ClCompile Include="..\src\sdlinput.cpp" />
<ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\text.cpp" />
<ClCompile Include="..\src\ticker.cpp" />
<ClCompile Include="..\src\tiler.cpp" />


+ 6
- 0
win32/editor.vcxproj.filters View File

@@ -63,6 +63,9 @@
<ClInclude Include="..\src\sdlinput.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\shader.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\text.h">
<Filter>lolengine</Filter>
</ClInclude>
@@ -148,6 +151,9 @@
<ClCompile Include="..\src\sdlinput.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\shader.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\text.cpp">
<Filter>lolengine</Filter>
</ClCompile>


+ 2
- 0
win32/monsterz.vcxproj View File

@@ -40,6 +40,7 @@
<ClInclude Include="..\src\sampler.h" />
<ClInclude Include="..\src\scene.h" />
<ClInclude Include="..\src\sdlinput.h" />
<ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\text.h" />
<ClInclude Include="..\src\ticker.h" />
<ClInclude Include="..\src\tiler.h" />
@@ -76,6 +77,7 @@
<ClCompile Include="..\src\sampler.cpp" />
<ClCompile Include="..\src\scene.cpp" />
<ClCompile Include="..\src\sdlinput.cpp" />
<ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\text.cpp" />
<ClCompile Include="..\src\ticker.cpp" />
<ClCompile Include="..\src\tiler.cpp" />


+ 6
- 0
win32/monsterz.vcxproj.filters View File

@@ -63,6 +63,9 @@
<ClInclude Include="..\src\sdlinput.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\shader.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\text.h">
<Filter>lolengine</Filter>
</ClInclude>
@@ -153,6 +156,9 @@
<ClCompile Include="..\src\sdlinput.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\shader.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\text.cpp">
<Filter>lolengine</Filter>
</ClCompile>


Loading…
Cancel
Save