diff options
Diffstat (limited to 'src/libjin/render')
-rw-r--r-- | src/libjin/render/canvas.cpp | 123 | ||||
-rw-r--r-- | src/libjin/render/canvas.h | 33 | ||||
-rw-r--r-- | src/libjin/render/color.h | 27 | ||||
-rw-r--r-- | src/libjin/render/drawable.cpp | 92 | ||||
-rw-r--r-- | src/libjin/render/drawable.h | 47 | ||||
-rw-r--r-- | src/libjin/render/font.cpp | 189 | ||||
-rw-r--r-- | src/libjin/render/font.h | 58 | ||||
-rw-r--r-- | src/libjin/render/graphics.cpp | 115 | ||||
-rw-r--r-- | src/libjin/render/graphics.h | 37 | ||||
-rw-r--r-- | src/libjin/render/image.cpp | 97 | ||||
-rw-r--r-- | src/libjin/render/image.h | 34 | ||||
-rw-r--r-- | src/libjin/render/jsl.cpp | 84 | ||||
-rw-r--r-- | src/libjin/render/jsl.h | 44 | ||||
-rw-r--r-- | src/libjin/render/quad.h | 17 | ||||
-rw-r--r-- | src/libjin/render/rect.h | 12 | ||||
-rw-r--r-- | src/libjin/render/window.cpp | 83 | ||||
-rw-r--r-- | src/libjin/render/window.h | 53 |
17 files changed, 1145 insertions, 0 deletions
diff --git a/src/libjin/render/canvas.cpp b/src/libjin/render/canvas.cpp new file mode 100644 index 0000000..494b0fa --- /dev/null +++ b/src/libjin/render/canvas.cpp @@ -0,0 +1,123 @@ +#include "utils/macros.h" +#include "canvas.h" +#include "window.h" + +namespace jin +{ +namespace render +{ + Canvas::Canvas() :Drawable() + { + } + + Canvas::~Canvas() + { + } + + // no canvas has binded + shared GLint Canvas::cur = -1; + + bool Canvas::init(int w, int h) + { + Drawable::init(w, h); + Drawable::setVertices( + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, (float)h, + (float)w, (float)h, + (float)w, 0, + }, + new float [DRAWABLE_V_SIZE] { + 0, 1, + 0, 0, + 1, 0, + 1, 1 + } + ); + + GLint current_fbo; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo); + + // generate a new render buffer object + glGenFramebuffers(1, &fbo); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + // generate texture save target + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + + // unbind framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, current_fbo); + + if (status != GL_FRAMEBUFFER_COMPLETE_EXT) + return false; + return true; + } + + bool Canvas::hasbind(GLint fbo) + { + return cur == fbo; + } + + /** + * bind to canvas + */ + void Canvas::bind() + { + if (hasbind(fbo)) return; + + cur = fbo; + + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + glViewport(0, 0, width, height); + glOrtho(0, width, height, 0, -1, 1); + + // Switch back to modelview matrix + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + } + + /** + * bind to default screen render buffer. + */ + shared void Canvas::unbind() + { + if (hasbind(0)) return; + + cur = 0; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + Window* wnd = Window::get(); + int ww = wnd->getW(), + wh = wnd->getH(); + + glViewport(0, 0, ww, wh); + + // load back to normal + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + // set viewport matrix + glOrtho(0, ww, wh, 0, -1, 1); + + // switch to model matrix + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + } +} +}
\ No newline at end of file diff --git a/src/libjin/render/canvas.h b/src/libjin/render/canvas.h new file mode 100644 index 0000000..4600f8b --- /dev/null +++ b/src/libjin/render/canvas.h @@ -0,0 +1,33 @@ +#ifndef __JIN_CANVAS_H +#define __JIN_CANVAS_H +#include "drawable.h" +namespace jin +{ +namespace render +{ + class Canvas: public Drawable + { + public: + + Canvas(); + ~Canvas(); + + bool init(int w, int h); + + void bind(); + + static void unbind(); + + static bool hasbind(GLint fbo); + + private: + + GLuint fbo; + + // current binded fbo + static GLint cur; + }; +} +}// jin + +#endif diff --git a/src/libjin/render/color.h b/src/libjin/render/color.h new file mode 100644 index 0000000..dfb02e1 --- /dev/null +++ b/src/libjin/render/color.h @@ -0,0 +1,27 @@ +/** +* Some color operating here. +*/ +#ifndef __JIN_COLOR_H +#define __JIN_COLOR_H +#include "utils/endian.h" + +namespace jin +{ +namespace render +{ + + union color { + struct { +#if JIN_BYTEORDER == JIN_BIG_ENDIAN + unsigned char r, g, b, a; +#else + unsigned char a, b, g, r; +#endif + }rgba; + int word; + }; + +} +} + +#endif diff --git a/src/libjin/render/drawable.cpp b/src/libjin/render/drawable.cpp new file mode 100644 index 0000000..45d6044 --- /dev/null +++ b/src/libjin/render/drawable.cpp @@ -0,0 +1,92 @@ +#include "drawable.h" +#include "utils/matrix.h" +#include <stdlib.h> +namespace jin +{ +namespace render +{ + Drawable::Drawable():texture(0), width(0), height(0), ancx(0), ancy(0), textCoord(0), vertCoord(0) + { + } + + Drawable::~Drawable() + { + glDeleteTextures(1, &texture); + delete[] vertCoord; + delete[] textCoord; + } + + void Drawable::init(int w, int h) + { + texture = 0; + width = w; + height = h; + ancx = 0; + ancy = 0; + textCoord = 0; + vertCoord = 0; + } + + void Drawable::setVertices(float* v, float* t) + { + // render area + if (vertCoord) + delete[] vertCoord; + vertCoord = v; + + // textrue + if (textCoord) + delete[] textCoord; + textCoord = t; + } + + void Drawable::setAnchor(int x, int y) + { + ancx = x; + ancy = y; + } + + int Drawable::getWidth() + { + return width; + } + + int Drawable::getHeight() + { + return height; + } + + void Drawable::draw(int x, int y, float sx, float sy, float r) + { + // Must set textCoord and vertCoord before renderring + if (! textCoord||! vertCoord) return; + + static jin::util::Matrix t; + t.setTransformation(x, y, r, sx, sy, ancx, ancy); + + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, texture); + + // push modle matrix + glPushMatrix(); + glMultMatrixf((const GLfloat*)t.getElements()); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, textCoord); + glVertexPointer(2, GL_FLOAT, 0, vertCoord); + glDrawArrays(GL_QUADS, 0, 4); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + // pop the model matrix + glPopMatrix(); + + // bind texture to default screen + glBindTexture(GL_TEXTURE_2D, 0); + + glDisable(GL_TEXTURE_2D); + } +} +} diff --git a/src/libjin/render/drawable.h b/src/libjin/render/drawable.h new file mode 100644 index 0000000..5eaf705 --- /dev/null +++ b/src/libjin/render/drawable.h @@ -0,0 +1,47 @@ +#ifndef __JIN_DRAWABLE +#define __JIN_DRAWABLE +#include "3rdparty/GLee/GLee.h" +namespace jin +{ +namespace render +{ + class Drawable + { + public: + + Drawable(); + virtual ~Drawable(); + + /* pseudo constructor*/ + void init(int w = 0, int h = 0); + + /* set anchor of texture, (0, 0) by default */ + void setAnchor(int x, int y); + + void draw(int x, int y, float sx, float sy, float r); + + int getWidth(); + int getHeight(); + + inline GLuint getTexture() const { return texture; }; + + protected: +#define DRAWABLE_V_SIZE 8 + void setVertices(float* v, float* t); + + GLuint texture; + + int width, height; + + /* anchor point */ + int ancx, ancy; + + // render coords + float* textCoord; + float* vertCoord; + + }; +} +}// jin + +#endif diff --git a/src/libjin/render/font.cpp b/src/libjin/render/font.cpp new file mode 100644 index 0000000..287e0c0 --- /dev/null +++ b/src/libjin/render/font.cpp @@ -0,0 +1,189 @@ +#include "font.h" + +#include <stdio.h> + +#define STB_TRUETYPE_IMPLEMENTATION +#include "3rdparty/stb/stb_truetype.h" +#include "color.h" + +namespace jin +{ +namespace render +{ + +#define BITMAP_WIDTH 512 +#define BITMAP_HEIGHT 512 +#define PIXEL_HEIGHT 32 + + Font::Font():Drawable() + { + } + + // ttf file read buffer + static unsigned char ttf_buffer[1 << 20]; + + // bitmap for saving font data + static unsigned char temp_bitmap[BITMAP_WIDTH * BITMAP_HEIGHT]; + + void Font::loadf(const char* path) + { + fread(ttf_buffer, 1, 1 << 20, fopen(path, "rb")); + + loadb(ttf_buffer); + } + + /** + * load from memory + */ + void Font::loadb(const unsigned char* data) + { + stbtt_BakeFontBitmap(data, 0, PIXEL_HEIGHT, temp_bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, 32, 96, cdata); + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, BITMAP_WIDTH, + BITMAP_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glBindTexture(GL_TEXTURE_2D, 0); + } + + /** + * get texture quad + */ + static Quad getCharQuad(const stbtt_bakedchar* chardata, int pw, int ph, int char_index) + { + float ipw = 1.0f / pw, iph = 1.0f / ph; + const stbtt_bakedchar *b = chardata + char_index; + Quad q; + q.x = b->x0 * ipw; + q.y = b->y0 * iph; + q.w = b->x1 * ipw - b->x0 * ipw; + q.h = b->y1 * iph - b->y0 * iph; + return q; + } + + /** + * render function draw mutiple times + * in loop + */ + void Font::render( + const char* text, // rendered text + float x, float y, // render position + int fheight, // font height + int spacing, // font spacing + int lheight) // line height + { + float _x = x, + _y = y; + + int len = strlen(text); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + // for saving clip quad + stbtt_aligned_quad q; + + // render every given character + int xc = x; + int yc = y; + + float factor = fheight / (float)PIXEL_HEIGHT; + + for (int i = 0; i < len; ++i) + { + char c = text[i]; + if (c == '\n') + { + xc = x; + yc += lheight; + continue; + } + + // only support ASCII + Quad q = getCharQuad(cdata, 512, 512, c - 32); + float s0 = q.x, + s1 = q.x + q.w, + t0 = q.y, + t1 = q.y + q.h; + + // texture quad + float tc[] = { + s0, t1, + s1, t1, + s1, t0, + s0, t0 + }; + + // character bound box + stbtt_bakedchar box = cdata[c - 32]; + + float width = factor * (box.x1 - box.x0); + float height = factor * (box.y1 - box.y0); + float xoffset = factor * box.xoff; + // I don't know why add PIXEL_HEIGHT to box.yoff, but + // it works. + float yoffset = factor * (box.yoff + PIXEL_HEIGHT); + float xadvance = factor * box.xadvance; + + // render quad + float vc[] = { + xc + xoffset, yc + height + yoffset, + xc + width + xoffset, yc + height + yoffset, + xc + width + xoffset, yc + yoffset, + xc + xoffset, yc + yoffset + }; + + // forward to next character + xc += xadvance + spacing; + + glTexCoordPointer(2, GL_FLOAT, 0, tc); + glVertexPointer(2, GL_FLOAT, 0, vc); + glDrawArrays(GL_QUADS, 0, 4); + } + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + glBindTexture(GL_TEXTURE_2D, 0); + glDisable(GL_TEXTURE_2D); + } + + void Font::box(const char* str, int fheight, int spacing, int lheight, int* w, int * h) + { + int len = strlen(str); + + float xc = 0; + int yc = len ? lheight: 0; + int maxX = 0; + + float factor = fheight / (float)PIXEL_HEIGHT; + + for (int i = 0; i < len; ++i) + { + char c = str[i]; + if (c == '\n') + { + yc += lheight; + xc = 0; + continue; + } + stbtt_bakedchar box = cdata[c - 32]; + + xc += factor * box.xadvance + spacing; + if (xc > maxX) maxX = xc; + } + + *w = maxX; + *h = yc; + } + +} +}
\ No newline at end of file diff --git a/src/libjin/render/font.h b/src/libjin/render/font.h new file mode 100644 index 0000000..98a41b4 --- /dev/null +++ b/src/libjin/render/font.h @@ -0,0 +1,58 @@ +#ifndef __JIN_FONT_H +#define __JIN_FONT_H + +#include "drawable.h" +#include "3rdparty/stb/stb_truetype.h" +#include "quad.h" + +namespace jin +{ +namespace render +{ + /** + * Usage of stb_truetype.h here might be a little + * bit dummy. Implementation of Font is referring + * to stb_truetype.h L243~284. I basicly copy it:) + */ + class Font: public Drawable + { + public: + + Font(); + + /** + * load ttf font data from .ttf + */ + void loadf(const char* file); + + /** + * load ttf font data from memory + */ + void loadb(const unsigned char* data); + + /** + * render text to screen + */ + void render( + const char* str, // rendered text + float x, float y, // render position + int fheight, // font size + int spacing, // font spacing + int lheight // line height + ); + + void box(const char* str, int fheight, int spacing, int lheight, int* w, int * h); + + private: + + /** + * ASCII 32(space)..126(~) is 95 glyphs + */ + stbtt_bakedchar cdata[96]; + + }; + +} +} + +#endif
\ No newline at end of file diff --git a/src/libjin/render/graphics.cpp b/src/libjin/render/graphics.cpp new file mode 100644 index 0000000..15d8a9c --- /dev/null +++ b/src/libjin/render/graphics.cpp @@ -0,0 +1,115 @@ +#include "graphics.h" +#include "utils/math.h" +#include <string> +namespace jin +{ +namespace render +{ + + void point(int x, int y) + { + float vers[] = { x + 0.5f , y + 0.5f }; + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)vers); + glDrawArrays(GL_POINTS, 0, 1); + glDisableClientState(GL_VERTEX_ARRAY); + } + + void points(int n, GLshort* p) + { + glEnableClientState(GL_VERTEX_ARRAY); + + glVertexPointer(2, GL_SHORT, 0, (GLvoid*)p); + glDrawArrays(GL_POINTS, 0, n); + + glDisableClientState(GL_VERTEX_ARRAY); + } + + void line(int x1, int y1, int x2, int y2) + { + glDisable(GL_TEXTURE_2D); + float verts[] = { + x1, y1, + x2, y2 + }; + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)verts); + glDrawArrays(GL_LINES, 0, 2); + glDisableClientState(GL_VERTEX_ARRAY); + } + + void circle(RENDER_MODE mode, int x, int y, int r) + { + r = r < 0 ? 0 : r; + + int points = 40; + float two_pi = static_cast<float>(PI * 2); + if (points <= 0) points = 1; + float angle_shift = (two_pi / points); + float phi = .0f; + + float *coords = new float[2 * (points + 1)]; + for (int i = 0; i < points; ++i, phi += angle_shift) + { + coords[2 * i] = x + r * cos(phi); + coords[2 * i + 1] = y + r * sin(phi); + } + + coords[2 * points] = coords[0]; + coords[2 * points + 1] = coords[1]; + + polygon(mode, coords, points); + + delete[] coords; + } + + void rect(RENDER_MODE mode, int x, int y, int w, int h) + { + float coords[] = { x, y, x + w, y, x + w, y + h, x, y + h }; + polygon(mode, coords, 4); + } + + void triangle(RENDER_MODE mode, int x1, int y1, int x2, int y2, int x3, int y3) + { + float coords[] = { x1, y1, x2, y2, x3, y3 }; + polygon(mode, coords, 3); + } + + void polygon_line(float* p, int count) + { + float* verts = new float[count * 4]; + for (int i = 0; i < count; ++i) + { + // each line has two point n,n+1 + verts[i * 4] = p[i * 2]; + verts[i * 4 + 1] = p[i * 2 + 1]; + verts[i * 4 + 2] = p[(i + 1) % count * 2]; + verts[i * 4 + 3] = p[(i + 1) % count * 2 + 1]; + } + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)verts); + glDrawArrays(GL_LINES, 0, count * 2); + glDisableClientState(GL_VERTEX_ARRAY); + + delete[] verts; + } + + void polygon(RENDER_MODE mode, float* p, int count) + { + if (mode == LINE) + { + polygon_line(p, count); + } + else if (mode == FILL) + { + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, (const GLvoid*)p); + glDrawArrays(GL_POLYGON, 0, count); + glDisableClientState(GL_VERTEX_ARRAY); + } + } + +} +} diff --git a/src/libjin/render/graphics.h b/src/libjin/render/graphics.h new file mode 100644 index 0000000..242b19d --- /dev/null +++ b/src/libjin/render/graphics.h @@ -0,0 +1,37 @@ +#ifndef __JIN_GRAPHICS_H +#define __JIN_GRAPHICS_H + +#include "color.h" +#include "canvas.h" +#include "image.h" + +namespace jin +{ +namespace render +{ + typedef enum { + NONE = 0, + FILL , + LINE + }RENDER_MODE; + + /** + * TODO: + * drawPixels(int n, points) + */ + extern void line(int x1, int y1, int x2, int y2); + + extern void rect(RENDER_MODE mode, int x, int y, int w, int h); + + extern void triangle(RENDER_MODE mode, int x1, int y1, int x2, int y2, int x3, int y3); + + extern void circle(RENDER_MODE mode, int x, int y, int r); + + extern void point(int x, int y); + + extern void points(int n, GLshort* p, GLubyte* c); + + extern void polygon(RENDER_MODE mode, float* p, int count); +} +} +#endif diff --git a/src/libjin/render/image.cpp b/src/libjin/render/image.cpp new file mode 100644 index 0000000..ac5947a --- /dev/null +++ b/src/libjin/render/image.cpp @@ -0,0 +1,97 @@ +#include "image.h" +#include "3rdparty/stb/stb_image.h" +#include "utils/utils.h" +namespace jin +{ +namespace render +{ + Image::Image(): Drawable(), pixels(0) + { + } + + Image::~Image() + { + stbi_image_free(pixels); + } + + void Image::init() + { + Drawable::init(); + pixels = 0; + } + + color Image::getPixel(int x, int y) + { + if (without(x, 0, width) || without(y, 0, height)) + { + return { 0 }; + } + return pixels[x + y * width]; + } + + bool Image::loadf(const char* f) + { + unsigned char* imageData = stbi_load(f, &width, &height, NULL, STBI_rgb_alpha); + if (imageData == 0) return false; + pixels = (color*)imageData; + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, + height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); + + // set render vertices + Drawable::setVertices( + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, (float)height, + (float)width, (float)height, + (float)width, 0, + }, + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, 1, + 1, 1, + 1, 0 + } + ); + + return true; + } + + bool Image::loadb(const char* b, int size) + { + // ʹstbi_load_from_memory + unsigned char* imageData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha); + if (imageData == 0) return false; + pixels = (color*)imageData; + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, + height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); + + // set render vertices + Drawable::setVertices( + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, (float)height, + (float)width, (float)height, + (float)width, 0, + }, + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, 1, + 1, 1, + 1, 0 + } + ); + + return true; + } +} +}
\ No newline at end of file diff --git a/src/libjin/render/image.h b/src/libjin/render/image.h new file mode 100644 index 0000000..7375fc4 --- /dev/null +++ b/src/libjin/render/image.h @@ -0,0 +1,34 @@ +#ifndef __JIN_IMAGE_H +#define __JIN_IMAGE_H +#include "3rdparty/GLee/GLee.h" +#include "color.h" +#include "drawable.h" +namespace jin +{ +namespace render +{ + class Image: public Drawable + { + public: + Image(); + ~Image(); + + // just like Image() + void init(); + + // load from file + bool loadf(const char* f); + + // load from memory + bool loadb(const char* b, int size); + + color getPixel(int x, int y); + + private: + + color* pixels; + }; +} +} + +#endif
\ No newline at end of file diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp new file mode 100644 index 0000000..6fcee53 --- /dev/null +++ b/src/libjin/render/jsl.cpp @@ -0,0 +1,84 @@ +#include "utils/macros.h" +#include "jsl.h" +namespace jin +{ +namespace render +{ + //vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) + static const char* base_f = " " + "#version 120 \n" + "#define number float \n" + "#define Image sampler2D \n" + "#define Texel texture2D \n" + "#define extern uniform \n" + "uniform sampler2D _tex0_; \n" + "%s \n" + "void main(){ \n" + "gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n" + "}\0"; + + void JSLProgram::init(const char* program) + { + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &_max_texture_units); + + char* fs = (char*)alloca(strlen(program) + strlen(base_f)); + sprintf(fs, base_f, program); + GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, (const GLchar**)&fs, NULL); + glCompileShader(fragmentShader); + + pid = glCreateProgram(); + glAttachShader(pid, fragmentShader); + glLinkProgram(pid); + } + + shared std::map<std::string, GLint> JSLProgram::_texture_unit_pool; + shared GLint JSLProgram::_current_texture_unit = 0; + shared GLint JSLProgram::_max_texture_units = 0; + + shared GLint JSLProgram::getTextureUnit(const std::string& name) + { + std::map<std::string, GLint>::const_iterator it = _texture_unit_pool.find(name); + + if (it != _texture_unit_pool.end()) + return it->second; + + if (++_current_texture_unit >= _max_texture_units) + return 0; + + _texture_unit_pool[name] = _current_texture_unit; + return _current_texture_unit; + } + + void JSLProgram::use() + { + glUseProgram(pid); + } + + shared void JSLProgram::unuse() + { + glUseProgram(0); + } + + void JSLProgram::sendFloat(const char* variable, float number) + { + int loc = glGetUniformLocation(pid, variable); + glUniform1f(loc, number); + } + + void JSLProgram::sendImage(const char* variable, const Image* image) + { + GLint texture_unit = getTextureUnit(variable); + + GLint location = glGetUniformLocation(pid, variable); + + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(GL_TEXTURE_2D, image->getTexture()); // guarantee it gets bound + glUniform1i(location, texture_unit); + + // reset texture unit + glActiveTexture(GL_TEXTURE0); + } + +} +} diff --git a/src/libjin/render/jsl.h b/src/libjin/render/jsl.h new file mode 100644 index 0000000..570235b --- /dev/null +++ b/src/libjin/render/jsl.h @@ -0,0 +1,44 @@ +#ifndef __JIN_JSL_H +#define __JIN_JSL_H +#include <string> +#include <map> +#include "image.h" +#include "3rdparty/GLee/GLee.h" +namespace jin +{ +namespace render +{ + /** + * A JSL program for shadering textures which is + * actually a glsl program. + */ + class JSLProgram + { + public: + + void init(const char* program); + + void use(); + + static void unuse(); + + void sendFloat(const char* name, float number); + void sendImage(const char* name, const Image* image); + //void sendMatrix(const char* name, int size, const GLfloat* m, int count); + //void sendCanvas(const char* name, const Canvas& canvas); + + private: + JSLProgram(); + + // only id for identify glsl program + GLuint pid; + + static GLint _current_texture_unit; + static GLint _max_texture_units; + static std::map<std::string, GLint> _texture_unit_pool; + static GLint getTextureUnit(const std::string& name); + }; +} +} + +#endif diff --git a/src/libjin/render/quad.h b/src/libjin/render/quad.h new file mode 100644 index 0000000..3ae4cc4 --- /dev/null +++ b/src/libjin/render/quad.h @@ -0,0 +1,17 @@ +#ifndef __JIN_QUAD_H +#define __JIN_QUAD_H + +namespace jin +{ +namespace render +{ + + struct Quad + { + float x, y, w, h; + }; + +} +} + +#endif // !__JIN_RENDER_QUAD_H diff --git a/src/libjin/render/rect.h b/src/libjin/render/rect.h new file mode 100644 index 0000000..56b5bd1 --- /dev/null +++ b/src/libjin/render/rect.h @@ -0,0 +1,12 @@ +#ifndef __JIN_RECT_H +#define __JIN_RECT_H + +namespace jin +{ +class Rect +{ +public: + int x, y, w, h; +}; +}// jin +#endif
\ No newline at end of file diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp new file mode 100644 index 0000000..20a6adc --- /dev/null +++ b/src/libjin/render/window.cpp @@ -0,0 +1,83 @@ +#include "window.h" +#include "3rdparty/GLee/GLee.h" +#include "canvas.h" +#include "utils/macros.h" +namespace jin +{ +namespace render +{ + + shared Window* Window::g_wnd = 0; + + Window::Window(): wnd(0), ctx(0) + { + } + + Window::~Window() + { + } + + void Window::init(int pw, int ph, const char* t) + { + w = pw; + h = ph; + + if (wnd) + { + SDL_DestroyWindow(wnd); + SDL_FlushEvent(SDL_WINDOWEVENT); + } + + if (ctx) + { + SDL_GL_DeleteContext(ctx); + } + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL ; + + int wx = SDL_WINDOWPOS_UNDEFINED, + wy = SDL_WINDOWPOS_UNDEFINED; + + /* Create window */ + wnd = SDL_CreateWindow(t, wx, wy, w, h, flags); + + // Create an opengl context + ctx = SDL_GL_CreateContext(wnd); + SDL_GL_MakeCurrent(wnd, ctx); + + // Default clear color + glClearColor(0.f, 0.f, 0.f, 1.f); + glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + // Default render color + glColor4f(1, 1, 1, 1); + + /** + * Set the viewport to top-left corner. + * Bind to the default render buffer. + */ + Canvas::unbind(); + + // Swap window buffer + swapBuffers(); + } + + SDL_Window* Window::getWnd() + { + return wnd; + } + + SDL_GLContext Window::getCtx() + { + return ctx; + } + +} +}
\ No newline at end of file diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h new file mode 100644 index 0000000..f29c82d --- /dev/null +++ b/src/libjin/render/window.h @@ -0,0 +1,53 @@ +#ifndef __JIN_RENDER_WINDOW +#define __JIN_RENDER_WINDOW +#include "SDL2/SDL.h" +namespace jin +{ +namespace render +{ + class Window + { + public: + + void init(int w, int h, const char* t); + + SDL_Window* getWnd(); + + SDL_GLContext getCtx(); + + static inline Window* get() + { + return (g_wnd ? g_wnd : (g_wnd = new Window())); + } + + inline int Window::getW() + { + return w; + } + + inline int Window::getH() + { + return h; + } + + inline void Window::swapBuffers() + { + if (wnd) + SDL_GL_SwapWindow(wnd); + } + private: + + Window(); + ~Window(); + + static Window* g_wnd; + + SDL_Window* wnd; + + SDL_GLContext ctx; + + int w, h; + }; +} +} +#endif
\ No newline at end of file |