git-svn-id: svn://ultimatepp.org/upp/trunk@6457 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2013-10-21 12:04:53 +00:00
parent 784127b5d5
commit 0ae03ab1b2
6 changed files with 30 additions and 701 deletions

View file

@ -1,111 +0,0 @@
#include "SDLGLDraw.h"
static GLuint LoadShader(const char *src, GLenum type) {
GLuint shader;
GLint compiled;
shader = glCreateShader(type);
if(!shader)
return 0;
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled) {
String error = "shader failed to compile ";
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1) {
Buffer<char> infoLog(infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
error << ~infoLog;
}
Panic(error);
}
return shader;
}
void GLProgram::Create(const char *vertex_shader_, const char *fragment_shader_,
Tuple2<int, const char *> *bind_attr, int bind_count)
{
Clear();
program = glCreateProgram();
vertex_shader = LoadShader(vertex_shader_, GL_VERTEX_SHADER);
fragment_shader = LoadShader(fragment_shader_, GL_FRAGMENT_SHADER);
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
for(int i = 0; i < bind_count; i++)
glBindAttribLocation(program, bind_attr[i].a, bind_attr[i].b);
glLinkProgram(program);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if(!linked){
GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1) {
Buffer<char> infoLog(infoLen);
glGetProgramInfoLog(program, infoLen, NULL, infoLog);
Panic(infoLog);
}
Panic("Failed to link");
Clear();
}
}
void GLProgram::Create(const char *vertex_shader, const char *fragment_shader,
int attr1, const char *attr1name)
{
Tuple2<int, const char *> bind[] = {
{ attr1, attr1name },
};
Create(vertex_shader, fragment_shader, bind, __countof(bind));
}
void GLProgram::Create(const char *vertex_shader, const char *fragment_shader,
int attr1, const char *attr1name,
int attr2, const char *attr2name)
{
Tuple2<int, const char *> bind[] = {
{ attr1, attr1name },
{ attr2, attr2name },
};
Create(vertex_shader, fragment_shader, bind, __countof(bind));
}
void GLProgram::Create(const char *vertex_shader, const char *fragment_shader,
int attr1, const char *attr1name,
int attr2, const char *attr2name,
int attr3, const char *attr3name)
{
Tuple2<int, const char *> bind[] = {
{ attr1, attr1name },
{ attr2, attr2name },
{ attr3, attr3name },
};
Create(vertex_shader, fragment_shader, bind, __countof(bind));
}
void GLProgram::Clear()
{
if(program)
glDeleteProgram(program);
if(vertex_shader)
glDeleteShader(vertex_shader);
if(fragment_shader)
glDeleteShader(fragment_shader);
}
GLProgram::GLProgram()
{
vertex_shader = fragment_shader = program = 0;
}

View file

@ -8,39 +8,6 @@ using namespace Upp;
#define LAYOUTFILE <SDLGLDraw/SDLGLDraw.lay>
#include <CtrlCore/lay.h>
enum {
ATTRIB_VERTEX = 1,
ATTRIB_COLOR,
ATTRIB_TEXPOS,
};
class GLProgram {
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
public:
void Create(const char *vertex_shader, const char *fragment_shader,
Tuple2<int, const char *> *bind_attr = NULL, int bind_count = 0);
void Create(const char *vertex_shader, const char *fragment_shader,
int attr1, const char *attr1name);
void Create(const char *vertex_shader, const char *fragment_shader,
int attr1, const char *attr1name,
int attr2, const char *attr2name);
void Create(const char *vertex_shader, const char *fragment_shader,
int attr1, const char *attr1name,
int attr2, const char *attr2name,
int attr3, const char *attr3name);
void Clear();
int GetAttrib(const char *name) { return glGetAttribLocation(program, name); }
int GetUniform(const char *name) { return glGetUniformLocation(program, name); }
void Use() { glUseProgram(program); }
GLProgram();
};
class SDLGLDraw : public TopWindow {
public:
virtual void Paint(Draw& w);

View file

@ -5,8 +5,7 @@ uses
file
SDLGLDraw.h,
GLutil.cpp,
main2.cpp,
test.iml,
main.cpp,
SDLGLDraw.lay;

View file

@ -1,291 +1,39 @@
#include "SDLGLDraw.h"
#if 0
#define IMAGECLASS TestImg
#define IMAGEFILE <SDLGLDraw/test.iml>
#include <Draw/iml_header.h>
extern unsigned short *BaseAddress;
GLProgram gl_image, gl_rect;
GLuint m_posLoc;
GLuint m_texLoc;
GLuint m_samplerLoc;
GLuint m_texture;
GLuint m_posLoc2;
GLuint m_colorLoc;
GLuint u_projection, u_projection2;
float vertexCoords[] =
{
10, 10,
20, 10,
20, 20,
10, 20,
};
float texCoords[] =
{
0.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0
};
float colors[] =
{
1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
};
GLushort indices[] = { 0, 1, 2, 2, 1, 3 };
#define INDEX 0 //ARRAY
void CheckError()
{
/* Check for error conditions. */
GLenum gl_error = glGetError( );
if( gl_error != GL_NO_ERROR ) {
Panic(String().Cat() << " OpenGL error: " << gl_error);
}
}
void InitTexture(const Image &img)
{
// Generate a texture object
glGenTextures ( 1, &m_texture );
// Bind the texture object
glBindTexture ( GL_TEXTURE_2D, m_texture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
CheckError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
CheckError();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
CheckError();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
CheckError();
// Set the filtering mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
Size sz = img.GetSize();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sz.cx, sz.cy, 0, GL_BGRA, GL_UNSIGNED_BYTE, ~img);
}
void initializeGL()
{
/*
glDepthFunc(GL_ALWAYS);
CheckError();
glDisable(GL_DEPTH_TEST);
CheckError();
glDisable(GL_STENCIL_TEST);
CheckError();
glDisable(GL_CULL_FACE);
CheckError();
*/
gl_image.Create(
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"uniform mat4 u_projection; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = u_projection * a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n",
"#ifdef GL_ES\n"
"precision mediump float; \n"
"precision mediump int; \n"
"#endif\n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D( s_texture, v_texCoord ); \n"
"} \n"
);
m_posLoc = gl_image.GetAttrib("a_position");
m_texLoc = gl_image.GetAttrib("a_texCoord");
m_samplerLoc = gl_image.GetUniform("s_texture");
u_projection = gl_image.GetUniform("u_projection");
gl_rect.Create(
"attribute vec4 a_position; \n"
"attribute vec4 a_color; \n"
"uniform mat4 u_projection; \n"
"varying vec4 v_color; \n"
"void main() \n"
"{ \n"
" gl_Position = u_projection * a_position; \n"
" v_color = a_color * vec4((1.0 / 255.0), (1.0 / 255.0), (1.0 / 255.0), 1); \n"
"}",
"#ifdef GL_ES\n"
"precision mediump float;\n"
"precision mediump int;\n"
"#endif\n"
"varying vec4 v_color;\n"
"\n"
"void main()\n"
"{\n"
" gl_FragColor = v_color;\n"
"}"
);
m_posLoc2 = gl_rect.GetAttrib("a_position");
m_colorLoc = gl_rect.GetAttrib("a_color");
u_projection2 = gl_rect.GetUniform("u_projection");
glEnableVertexAttribArray(m_posLoc2);
glEnableVertexAttribArray(m_colorLoc);
InitTexture(CtrlImg::exclamation());
}
void GLOrtho(float left, float right, float bottom, float top, float near, float far, GLuint u_projection)
{
float a = 2.0f / (right - left);
float b = 2.0f / (top - bottom);
float c = -2.0f / (far - near);
float tx = - (right + left)/(right - left);
float ty = - (top + bottom)/(top - bottom);
float tz = - (far + near)/(far - near);
float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, c, 0,
tx, ty, tz, 1
};
glUniformMatrix4fv(u_projection, 1, 0, &ortho[0]);
}
void GLRect(Size sz, const Rect& rect, const Color& color)
{
gl_rect.Use();
GLOrtho(0, sz.cx, sz.cy, 0, 0.0f, 1.0f, u_projection2);
float vertexCoords[] = {
rect.left, rect.top,
rect.left, rect.bottom,
rect.right, rect.bottom,
rect.right, rect.top,
};
// float h = 128.0;
// GLfloat r = color.GetR() / h;
// GLfloat g = color.GetG() / h;
// GLfloat b = color.GetB() / h;
GLubyte r = color.GetR();
GLubyte g = color.GetG();
GLubyte b = color.GetB();
GLubyte colors[] = {
r, g, b,
r, g, b,
r, g, b,
r, g, b,
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
glVertexAttribPointer(gl_rect.GetAttrib("a_position"), 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), vertexCoords );
glVertexAttribPointer(gl_rect.GetAttrib("a_color"), 3, GL_UNSIGNED_BYTE, GL_FALSE, 3 * sizeof(GLubyte), colors);
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
// glDisableVertexAttribArray(m_posLoc2);
// glDisableVertexAttribArray(m_colorLoc);
}
void paintGL(Size sz)
{
glClear( GL_COLOR_BUFFER_BIT );
#if 0
// Use the program object
glUseProgram ( m_program );
glVertexAttribPointer(m_posLoc, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), vertexCoords );
glVertexAttribPointer(m_texLoc, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), texCoords );
glEnableVertexAttribArray(m_posLoc);
glEnableVertexAttribArray(m_texLoc);
glBindTexture(GL_TEXTURE_2D, m_texture);
glUniform1i( m_samplerLoc, 0 );
glDisableVertexAttribArray(m_posLoc);
glDisableVertexAttribArray(m_texLoc);
#endif
#if 0
CheckError();
glVertexAttribPointer( m_posLoc2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), vertexCoords );
CheckError();
glVertexAttribPointer( m_colorLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), colors );
CheckError();
glEnableVertexAttribArray( m_posLoc );
CheckError();
glEnableVertexAttribArray( m_colorLoc );
CheckError();
glBindTexture(GL_TEXTURE_2D, m_texture);
/*
copyScreen( m_ScreenPtr, BaseAddress );
glTexSubImage2D( GL_TEXTURE_2D,0,
0,0, 256,256,
GL_RGB,GL_UNSIGNED_SHORT_5_6_5, m_ScreenPtr );
*/
CheckError();
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
#endif
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
GLRect(sz, RectC(i * 16, j * 16, 12, 12), decode(i, 0, LtBlue(), 1, White(), LtRed()));
// CheckError();
}
extern int SDLwidth;
extern int SDLheight;
#define IMAGECLASS TestImg
#define IMAGEFILE <SDLGLDraw/test.iml>
#include <Draw/iml_source.h>
void SDLGLDraw::Paint(Draw& w)
{
// w.DrawRect(GetSize(), White());
// w.DrawText(0, 0, "Hello world!");
w.DrawRect(GetSize(), LtGray);
w.DrawRect(100, 100, 16, 16, Blue());
w.DrawImage(100, 100, TestImg::test());
w.DrawRect(140, 100, 16, 16, Blue());
w.DrawImage(141, 101, TestImg::test(), Rect(1, 1, 15, 15));
w.DrawRect(200, 100, 16, 16, Blue());
w.DrawImage(200, 100, TestImg::test(), Cyan());
w.DrawRect(240, 100, 16, 16, Blue());
w.DrawImage(241, 101, TestImg::test(), Rect(1, 1, 15, 15), Cyan());
paintGL(Size(SDLwidth, SDLheight));
w.DrawText(10, 10, "Text1", StdFont(), Red());
w.DrawText(10, 50, "Text2", StdFont(), Black());
w.DrawText(500, 500, 300, "Angled", StdFont(), Black());
}
SDLGLDraw::SDLGLDraw()
{
glewInit();
initializeGL();
}
GUI_APP_MAIN
{
SDLGLDraw().Run();
}
#endif

View file

@ -1,281 +0,0 @@
#include "SDLGLDraw.h"
#if 1
int u_rectprojection;
int u_imageprojection;
int u_imagetexture;
GLProgram gl_image, gl_rect;
void CheckError()
{
/* Check for error conditions. */
GLenum gl_error = glGetError( );
if( gl_error != GL_NO_ERROR ) {
Panic(String().Cat() << " OpenGL error: " << gl_error);
}
}
GLuint m_texture;
void InitTexture(const Image &img)
{
// Generate a texture object
glGenTextures ( 1, &m_texture );
// Bind the texture object
glBindTexture ( GL_TEXTURE_2D, m_texture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
CheckError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
CheckError();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
CheckError();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
CheckError();
// Set the filtering mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
Size sz = img.GetSize();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sz.cx, sz.cy, 0, GL_BGRA, GL_UNSIGNED_BYTE, ~img);
}
void initializeGL()
{
/*
glDepthFunc(GL_ALWAYS);
CheckError();
glDisable(GL_DEPTH_TEST);
CheckError();
glDisable(GL_STENCIL_TEST);
CheckError();
glDisable(GL_CULL_FACE);
CheckError();
*/
gl_image.Create(
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"uniform mat4 u_projection; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = u_projection * a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n",
"#ifdef GL_ES\n"
"precision mediump float; \n"
"precision mediump int; \n"
"#endif\n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D(s_texture, v_texCoord); \n"
"} \n",
ATTRIB_VERTEX, "a_position",
ATTRIB_TEXPOS, "a_texCoord"
);
u_imagetexture = gl_image.GetUniform("s_texture");
u_imageprojection = gl_image.GetUniform("u_projection");
gl_rect.Create(
"attribute vec4 a_position; \n"
"attribute vec4 a_color; \n"
"uniform mat4 u_projection; \n"
"varying vec4 v_color; \n"
"void main() \n"
"{ \n"
" gl_Position = u_projection * a_position; \n"
" v_color = a_color * vec4((1.0 / 255.0), (1.0 / 255.0), (1.0 / 255.0), 1); \n"
"}",
"#ifdef GL_ES\n"
"precision mediump float;\n"
"precision mediump int;\n"
"#endif\n"
"varying vec4 v_color;\n"
"\n"
"void main()\n"
"{\n"
" gl_FragColor = v_color;\n"
"}",
ATTRIB_VERTEX, "a_position",
ATTRIB_COLOR, "a_color"
);
u_rectprojection = gl_rect.GetUniform("u_projection");
glEnableVertexAttribArray(ATTRIB_VERTEX);
glEnableVertexAttribArray(ATTRIB_TEXPOS);
glEnableVertexAttribArray(ATTRIB_COLOR);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
InitTexture(CtrlImg::exclamation());
}
void GLOrtho(float left, float right, float bottom, float top, float near, float far, GLuint u_projection)
{
right *= 1.25;
bottom *= 1.25;
float a = 2.0f / (right - left);
float b = 2.0f / (top - bottom);
float c = -2.0f / (far - near);
float tx = - (right + left) / (right - left);
float ty = - (top + bottom) / (top - bottom);
float tz = - (far + near) / (far - near);
float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, c, 0,
tx, ty, tz, 1
};
glUniformMatrix4fv(u_projection, 1, 0, ortho);
}
void GLRect(Size sz, const Rect& rect, const Color& color)
{
gl_rect.Use();
GLOrtho(0, sz.cx, sz.cy, 0, 0.0f, 1.0f, u_rectprojection); // TODO
GLshort vertex[] = {
rect.left, rect.top,
rect.left, rect.bottom,
rect.right, rect.bottom,
rect.right, rect.top,
};
GLubyte r = color.GetR();
GLubyte g = color.GetG();
GLubyte b = color.GetB();
GLubyte colors[] = {
r, g, b,
r, g, b,
r, g, b,
r, g, b,
};
static GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_SHORT, GL_FALSE, 2 * sizeof(GLshort), vertex);
glVertexAttribPointer(ATTRIB_COLOR, 3, GL_UNSIGNED_BYTE, GL_FALSE, 3 * sizeof(GLubyte), colors);
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}
void GLImage(Size sz, Point p, const Image& m)
{
gl_image.Use();
GLOrtho(0, sz.cx, sz.cy, 0, 0.0f, 1.0f, u_imageprojection); // TODO
sz = m.GetSize();
DDUMP(sz);
GLshort vertexCoords[] = {
p.x, p.y,
p.x, p.y + sz.cy,
p.x + sz.cx, p.y + sz.cy,
p.x + sz.cx, p.y,
};
static GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
static float texCoords[] = // TODO
{
0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0
};
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_SHORT, GL_FALSE, 2 * sizeof(GLshort), vertexCoords );
glVertexAttribPointer(ATTRIB_TEXPOS, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), texCoords );
glBindTexture(GL_TEXTURE_2D, GetTextureForImage(m));
glUniform1i(u_imagetexture, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}
void paintGL(Size sz)
{
// glClear( GL_COLOR_BUFFER_BIT );
// GLRect(sz, sz, LtRed());
// GLRect(sz, Rect(sz).Deflated(2), Blue());
// return;
// GLOrtho(0, sz.cx, sz.cy, 0, 0.0f, 1.0f);
DDUMP(sz);
// GLRect(sz, sz, LtGray());
GLRect(sz, RectC(sz.cx - 1, sz.cy - 1, 1, 1), Black());
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
GLRect(sz, RectC(i * 16, j * 16, 2, 2), decode(i, 0, LtBlue(), 1, White(), LtRed()));
for(int i = 0; i < sz.cx / 2; i++)
GLRect(sz, RectC(i * 2, 500, 1, 2), Black());
for(int i = 0; i < 5; i++)
GLRect(sz, RectC(i * 2, 520, 1, 1), Black());
for(int i = 0; i < 5; i++)
GLRect(sz, RectC(i, 530 + 2 * i, 1, 1), Black());
for(int i = 0; i < 5; i++)
GLRect(sz, RectC(2 * i, 600 + 2 * i, 1, 1), Black());
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++) {
GLRect(sz, RectC(120 + i * 40, 120 + j * 40, 32, 32), LtCyan());
GLImage(sz, Point(120 + i * 40, 120 + j * 40), CtrlImg::reporticon());
}
// CheckError();
}
namespace Upp {
extern int SDLwidth;
extern int SDLheight;
};
void SDLGLDraw::Paint(Draw& w)
{
w.DrawRect(GetSize(), White());
w.DrawText(0, 0, "Hello world!");
paintGL(Size(SDLwidth, SDLheight));
}
SDLGLDraw::SDLGLDraw()
{
glewInit();
initializeGL();
}
GUI_APP_MAIN
{
SDLGLDraw().Run();
}
#endif

View file

@ -0,0 +1,7 @@
PREMULTIPLIED
IMAGE_ID(test)
IMAGE_BEGIN_DATA
IMAGE_DATA(120,156,99,16,96,16,96,128,129,255,255,129,136,82,204,64,38,70,214,79,142,157,104,108,6,34,193,168,254,81,253,180)
IMAGE_DATA(208,79,81,250,165,36,255,80,128,1,121,11,158,144,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
IMAGE_END_DATA(64, 1)