Browse Source

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 years ago
parent
commit
c643668297
6 changed files with 117 additions and 21 deletions
  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 View File

@@ -93,6 +93,7 @@ PKG_CHECK_MODULES(GLES2, glesv2,
AC_CHECK_LIB(GL, glLoadIdentity, AC_CHECK_LIB(GL, glLoadIdentity,
[ac_cv_my_have_gl="yes" [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_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 GL_LIBS="-lGL"]) # FIXME: hackish
if test "${ac_cv_my_have_gl}" = "no"; then if test "${ac_cv_my_have_gl}" = "no"; then
AC_MSG_ERROR([[No OpenGL or OpenGL ES implementation found]]) AC_MSG_ERROR([[No OpenGL or OpenGL ES implementation found]])


+ 4
- 0
src/debugsphere.cpp View File

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


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


int const ndiv = 2; int const ndiv = 2;
int const ntriangles = 20 * (1 << (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), data->DrawSphere(ndiv, t * (60.0f + 40.0f * a),
vertex_parser, normal_parser); vertex_parser, normal_parser);


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


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


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


free(vertex); free(vertex);
free(normal); free(normal);


+ 34
- 1
src/lolgl.h View File

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


#define GL_GLEXT_PROTOTYPES

/* Only define one GL platform */
#if defined HAVE_GL_1X #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__ # if defined __APPLE__ && defined __MACH__
# include <OpenGL/gl.h> # include <OpenGL/gl.h>
# else # else
@@ -25,8 +42,24 @@
# endif # endif
#elif defined HAVE_GLES_1X #elif defined HAVE_GLES_1X
# include <GLES/gl.h> # 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 #elif defined HAVE_GLES_2X
# include <GLES2/gl.h>
# define glGenVertexArrays glGenVertexArraysOES
# define glDeleteVertexArrays glDeleteVertexArraysOES
# define glBindVertexArray glBindVertexArrayOES
#endif #endif


#endif // __DH_LOLGL_H__ #endif // __DH_LOLGL_H__


+ 42
- 14
src/scene.cpp View File

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


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


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


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


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


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

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


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


#if defined HAVE_GL_2X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]); glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat), glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
vertex, GL_STATIC_DRAW); vertex, GL_STATIC_DRAW);
#if LOL_EXPERIMENTAL
glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0); 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); glVertexPointer(3, GL_FLOAT, 0, NULL);
#else
glVertexPointer(3, GL_FLOAT, 0, vertex);
#endif #endif


#if defined HAVE_GL_2X
glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]); glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat), glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
texture, GL_STATIC_DRAW); texture, GL_STATIC_DRAW);
#if LOL_EXPERIMENTAL
glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0); 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); glTexCoordPointer(2, GL_FLOAT, 0, NULL);
#else
glTexCoordPointer(2, GL_FLOAT, 0, texture);
#endif #endif


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


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


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


+ 22
- 0
src/shader.cpp View File

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


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


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


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


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


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


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


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


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


Shader::~Shader() Shader::~Shader()
{ {
#if defined HAVE_GL_2X || defined HAVE_GLES_2X
glDetachShader(data->prog_id, data->vert_id); glDetachShader(data->prog_id, data->vert_id);
glDetachShader(data->prog_id, data->frag_id); glDetachShader(data->prog_id, data->frag_id);
glDeleteShader(data->vert_id); glDeleteShader(data->vert_id);
glDeleteShader(data->frag_id); glDeleteShader(data->frag_id);
glDeleteProgram(data->prog_id); glDeleteProgram(data->prog_id);
#endif
delete data; delete data;
} }



+ 14
- 6
src/video.cpp View File

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


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


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


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


glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
#if defined HAVE_GL_2X || defined HAVE_GLES_1X
glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH);
#endif
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 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); 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); stdshader = Shader::Create(vertexshader, fragmentshader);
#endif #endif
} }
@@ -126,7 +131,8 @@ void Video::SetFov(float theta)


view_matrix = mat4(1.0f); 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; GLuint uni;
uni = stdshader->GetUniformLocation("proj_matrix"); uni = stdshader->GetUniformLocation("proj_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]); glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
@@ -162,7 +168,7 @@ void Video::Clear()


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


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


#if defined GL_BGRA #if defined GL_BGRA


Loading…
Cancel
Save