Bläddra i källkod

The engine now builds for GL and GL ES 1.x and 2.x, and runs for each

one except GL ES 2.x for now.
legacy
Sam Hocevar sam 13 år sedan
förälder
incheckning
c643668297
6 ändrade filer med 117 tillägg och 21 borttagningar
  1. +1
    -0
      configure.ac
  2. +4
    -0
      src/debugsphere.cpp
  3. +34
    -1
      src/lolgl.h
  4. +42
    -14
      src/scene.cpp
  5. +22
    -0
      src/shader.cpp
  6. +14
    -6
      src/video.cpp

+ 1
- 0
configure.ac Visa fil

@@ -93,6 +93,7 @@ PKG_CHECK_MODULES(GLES2, glesv2,
AC_CHECK_LIB(GL, glLoadIdentity,
[ac_cv_my_have_gl="yes"
AC_DEFINE(HAVE_GL_1X, 1, Define to 1 if GL 1.x is available)
AC_DEFINE(HAVE_GL_2X, 1, Define to 1 if GL 2.x is available)
GL_LIBS="-lGL"]) # FIXME: hackish
if test "${ac_cv_my_have_gl}" = "no"; then
AC_MSG_ERROR([[No OpenGL or OpenGL ES implementation found]])


+ 4
- 0
src/debugsphere.cpp Visa fil

@@ -143,7 +143,9 @@ void DebugSphere::TickDraw(float deltams)
}

float a = sinf(data->time);
#if 0
float b = sinf(data->time * 0.5f);
#endif

int const ndiv = 2;
int const ntriangles = 20 * (1 << (ndiv * 2))
@@ -158,6 +160,7 @@ void DebugSphere::TickDraw(float deltams)
data->DrawSphere(ndiv, t * (60.0f + 40.0f * a),
vertex_parser, normal_parser);

#if 0 // FIXME: does not work with GLES2
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

@@ -187,6 +190,7 @@ void DebugSphere::TickDraw(float deltams)

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#endif

free(vertex);
free(normal);


+ 34
- 1
src/lolgl.h Visa fil

@@ -16,7 +16,24 @@
#if !defined __DH_LOLGL_H__
#define __DH_LOLGL_H__

#define GL_GLEXT_PROTOTYPES

/* Only define one GL platform */
#if defined HAVE_GL_1X
# undef HAVE_GLES_1X
#endif
#if defined HAVE_GLES_2X
# undef HAVE_GL_1X
# undef HAVE_GLES_1X
#endif
#if defined HAVE_GL_2X
# undef HAVE_GL_1X
# undef HAVE_GLES_1X
# undef HAVE_GLES_2X
#endif

/* Include GL */
#if defined HAVE_GL_1X || defined HAVE_GL_2X
# if defined __APPLE__ && defined __MACH__
# include <OpenGL/gl.h>
# else
@@ -25,8 +42,24 @@
# endif
#elif defined HAVE_GLES_1X
# include <GLES/gl.h>
# include <GLES/glext.h>
#elif defined HAVE_GLES_2X
# include <GLES2/gl2.h>
# include <GLES2/gl2ext.h>
#endif

/* Redefine some function names */
#if defined HAVE_GL_1X
# define glClearDepthf glClearDepth
#elif defined HAVE_GL_2X
# define glClearDepthf glClearDepth
#elif defined HAVE_GLES_1X
# define glGenVertexArrays glGenVertexArraysOES
# define glDeleteVertexArrays glDeleteVertexArraysOES
#elif defined HAVE_GLES_2X
# include <GLES2/gl.h>
# define glGenVertexArrays glGenVertexArraysOES
# define glDeleteVertexArrays glDeleteVertexArraysOES
# define glBindVertexArray glBindVertexArrayOES
#endif

#endif // __DH_LOLGL_H__


+ 42
- 14
src/scene.cpp Visa fil

@@ -30,7 +30,7 @@ struct Tile
int x, y, z, o;
};

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
extern Shader *stdshader;
#endif
extern mat4 model_matrix;
@@ -56,7 +56,7 @@ private:
int ntiles;
float angle;

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X
GLuint vao;
#endif
GLuint *bufs;
@@ -81,7 +81,7 @@ Scene::Scene(float angle)
data->bufs = 0;
data->nbufs = 0;

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X
glGenVertexArrays(1, &data->vao);
#endif
}
@@ -91,7 +91,7 @@ Scene::~Scene()
/* FIXME: this must be done while the GL context is still active.
* Change the architecture to make sure of that. */
glDeleteBuffers(data->nbufs, data->bufs);
#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X
glDeleteVertexArrays(1, &data->vao);
#endif
free(data->bufs);
@@ -153,17 +153,21 @@ void Scene::Render() // XXX: rename to Blit()
model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
// XXX: end of debug stuff

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
GLuint uni, attr_pos, attr_tex;
uni = stdshader->GetUniformLocation("model_matrix");
attr_pos = stdshader->GetAttribLocation("in_Position");
attr_tex = stdshader->GetAttribLocation("in_TexCoord");

stdshader->Bind();
glUniformMatrix4fv(uni, 1, GL_FALSE, &model_matrix[0][0]);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
# if !defined HAVE_GLES_2X
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GEQUAL, 0.01f);
# endif
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#else
@@ -204,42 +208,66 @@ void Scene::Render() // XXX: rename to Blit()
vertex + 18 * (j - i), texture + 12 * (j - i));
}

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X
glBindVertexArray(data->vao);
glEnableVertexAttribArray(attr_pos);
glEnableVertexAttribArray(attr_tex);
#elif defined HAVE_GLES_2X
glEnableVertexAttribArray(attr_pos);
glEnableVertexAttribArray(attr_tex);
#else
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
#endif

#if defined HAVE_GL_2X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
vertex, GL_STATIC_DRAW);
#if LOL_EXPERIMENTAL
glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr_pos);
#else
#elif defined HAVE_GLES_2X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, vertex);
#elif defined HAVE_GL_1X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
vertex, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, NULL);
#else
glVertexPointer(3, GL_FLOAT, 0, vertex);
#endif

#if defined HAVE_GL_2X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
texture, GL_STATIC_DRAW);
#if LOL_EXPERIMENTAL
glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr_tex);
#else
#elif defined HAVE_GLES_2X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, texture);
#elif defined HAVE_GL_1X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
texture, GL_STATIC_DRAW);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
#else
glTexCoordPointer(2, GL_FLOAT, 0, texture);
#endif

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
stdshader->Bind();
#endif

Tiler::Bind(data->tiles[i].code);
glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X
glBindVertexArray(0);
glDisableVertexAttribArray(attr_pos);
glDisableVertexAttribArray(attr_tex);
#elif defined HAVE_GLES_2X
glDisableVertexAttribArray(attr_pos);
glDisableVertexAttribArray(attr_tex);
#else
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);


+ 22
- 0
src/shader.cpp Visa fil

@@ -32,7 +32,9 @@ class ShaderData
friend class Shader;

private:
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
GLuint prog_id, vert_id, frag_id;
#endif
uint32_t vert_crc, frag_crc;

/* Global shader cache */
@@ -74,10 +76,13 @@ void Shader::Destroy(Shader *shader)
Shader::Shader(char const *vert, char const *frag)
: data(new ShaderData())
{
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
char buf[4096];
GLsizei len;
#endif

data->vert_crc = Hash::Crc32(vert);
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
data->vert_id = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(data->vert_id, 1, &vert, NULL);
glCompileShader(data->vert_id);
@@ -85,8 +90,10 @@ Shader::Shader(char const *vert, char const *frag)
glGetShaderInfoLog(data->vert_id, sizeof(buf), &len, buf);
if (len > 0)
fprintf(stderr, "ERROR: failed to compile vertex shader: %s", buf);
#endif

data->frag_crc = Hash::Crc32(frag);
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
data->frag_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(data->frag_id, 1, &frag, NULL);
glCompileShader(data->frag_id);
@@ -101,30 +108,45 @@ Shader::Shader(char const *vert, char const *frag)

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

int Shader::GetAttribLocation(char const *attr) const
{
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
return glGetAttribLocation(data->prog_id, attr);
#else
(void)attr;
return -1;
#endif
}

int Shader::GetUniformLocation(char const *uni) const
{
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
return glGetUniformLocation(data->prog_id, uni);
#else
(void)uni;
return -1;
#endif
}

void Shader::Bind() const
{
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
glUseProgram(data->prog_id);
#endif
}

Shader::~Shader()
{
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
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);
#endif
delete data;
}


+ 14
- 6
src/video.cpp Visa fil

@@ -23,12 +23,12 @@
#include "core.h"
#include "lolgl.h"

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
Shader *stdshader;
#endif
mat4 proj_matrix, view_matrix, model_matrix;

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
static char const *vertexshader =
"#version 130\n"
"\n"
@@ -60,6 +60,7 @@ static char const *fragmentshader =
//" gl_FragColor = 0.5 * (texture2D(in_Texture, vec2(gl_TexCoord[0]))\n"
//" + vec4(pass_Color, 1.0));\n"
" gl_FragColor = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
//" gl_FragColor = vec4(0.5, 1.0, 0.0, 0.5);\n"
"}\n";
#endif

@@ -73,13 +74,17 @@ void Video::Setup(int width, int height)
glViewport(0, 0, width, height);

glEnable(GL_TEXTURE_2D);
#if defined HAVE_GL_2X || defined HAVE_GLES_1X
glShadeModel(GL_SMOOTH);
#endif
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glClearDepthf(1.0);

#if defined HAVE_GL_2X || defined HAVE_GLES_1X
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
#endif

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
stdshader = Shader::Create(vertexshader, fragmentshader);
#endif
}
@@ -126,7 +131,8 @@ void Video::SetFov(float theta)

view_matrix = mat4(1.0f);

#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
stdshader->Bind(); /* Required on GLES 2.x? */
GLuint uni;
uni = stdshader->GetUniformLocation("proj_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
@@ -162,7 +168,7 @@ void Video::Clear()

void Video::Destroy()
{
#if LOL_EXPERIMENTAL
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
Shader::Destroy(stdshader);
#endif
}
@@ -173,7 +179,9 @@ void Video::Capture(uint32_t *buffer)
glGetIntegerv(GL_VIEWPORT, v);
int width = v[2], height = v[3];

#if defined HAVE_GL_1X || defined HAVE_GL_2X
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
#endif
glPixelStorei(GL_PACK_ALIGNMENT, 1);

#if defined GL_BGRA


Laddar…
Avbryt
Spara