Class: Nuklear::Renderer::OpenGL2

Inherits:
Nuklear::Renderer show all
Defined in:
ext/nuklear_renderer_opengl2/nuklear_renderer_opengl2.c

Constant Summary

Constants inherited from Nuklear::Renderer

Vertex

Instance Attribute Summary

Attributes inherited from Nuklear::Renderer

#commands, #context, #convert_config, #drawable_size, #null_texture_handle, #vertex_indices, #vertices, #window_size

Instance Method Summary collapse

Methods inherited from Nuklear::Renderer

#nk_draw_foreach, renderer_class

Constructor Details

#initializeObject



55
56
57
58
59
60
61
62
63
# File 'ext/nuklear_renderer_opengl2/nuklear_renderer_opengl2.c', line 55

VALUE renderer_initialize(VALUE self) {
  if (!gladLoadGL())
    rb_raise(rb_eStandardError, "Failed to init GLAD");
  // printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));

  rb_call_super(0, NULL);

  return self;
}

Instance Method Details

#draw(cmd) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'ext/nuklear_renderer_opengl2/nuklear_renderer_opengl2.c', line 152

VALUE renderer_draw_gl(VALUE self, VALUE cmd) {
  VALUE window_size = rb_funcall(self, rb_intern("window_size"), 0);
  VALUE drawable_size = rb_funcall(self, rb_intern("drawable_size"), 0);
  VALUE window_width = rb_ary_entry(window_size, 0);
  VALUE window_height = rb_ary_entry(window_size, 1);
  VALUE drawable_width = rb_ary_entry(drawable_size, 0);
  VALUE drawable_height = rb_ary_entry(drawable_size, 1);
  int width = FIX2INT(window_width),
      height = FIX2INT(window_height);
  int display_width = FIX2INT(drawable_width),
      display_height = FIX2INT(drawable_height);
  struct nk_vec2 scale;
  scale.x = (float)display_width/(float)width;
  scale.y = (float)display_height/(float)height;

  VALUE clip_rect = rb_hash_aref(cmd, ID2SYM(rb_intern("clip_rect")));
  float x = (float) NUM2DBL(RARRAY_AREF(clip_rect, 0));
  float y = (float) NUM2DBL(RARRAY_AREF(clip_rect, 1));
  float w = (float) NUM2DBL(RARRAY_AREF(clip_rect, 2));
  float h = (float) NUM2DBL(RARRAY_AREF(clip_rect, 3));
  glBindTexture(GL_TEXTURE_2D, (GLuint) NUM2UINT(rb_hash_aref(cmd, ID2SYM(rb_intern("texture_handle")))));
  glScissor((GLint)(x * scale.x),
            (GLint)((height - (GLint)(y + h)) * scale.y),
            (GLint)(w * scale.x),
            (GLint)(h * scale.y));
  unsigned int num_elements = NUM2UINT(rb_hash_aref(cmd, ID2SYM(rb_intern("element_count"))));

  struct nk_buffer *element_buffer = NULL;
  Data_Get_Struct(rb_funcall(self, rb_intern("vertex_indices"), 0), struct nk_buffer, element_buffer);
  const nk_draw_index *offset = (const nk_draw_index *) nk_buffer_memory_const(element_buffer);
  offset += NUM2UINT(rb_hash_aref(cmd, ID2SYM(rb_intern("offset"))));
  glDrawElements(GL_TRIANGLES, (GLsizei)num_elements, GL_UNSIGNED_SHORT, (void *) offset);
  return self;
}

#nk_convert(rcontext) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/nuklear_renderer_opengl2/nuklear_renderer_opengl2.c', line 122

VALUE renderer_nk_convert_gl(VALUE self, VALUE rcontext) {
  VALUE result = rb_call_super(1, &rcontext);
  VALUE rconfig = rb_funcall(self, rb_intern("convert_config"), 0);
  GLsizei vs = (GLsizei) FIX2INT(rb_hash_aref(rconfig, ID2SYM(rb_intern("vertex_size"))));

  size_t vp, vt, vc; // vertex struct offsets
  VALUE rvertex_layout = rb_hash_aref(rconfig, ID2SYM(rb_intern("vertex_layout")));
  for (int i = 0; i < RARRAY_LEN(rvertex_layout); i++) {
    VALUE rlayout = RARRAY_AREF(rvertex_layout, i);
    int offset = FIX2INT(RARRAY_AREF(rlayout, 2));
    enum nk_draw_vertex_layout_attribute attr = rb2nk_attribute(RARRAY_AREF(rlayout, 0));
    switch(attr) {
      case NK_VERTEX_POSITION: vp = (size_t) offset; break;
      case NK_VERTEX_COLOR:    vc = (size_t) offset; break;
      case NK_VERTEX_TEXCOORD: vt = (size_t) offset; break;
      default: break;
    }
  }

  /* setup vertex buffer pointer */
  struct nk_buffer *vertex_buffer = NULL;
  Data_Get_Struct(rb_funcall(self, rb_intern("vertices"), 0), struct nk_buffer, vertex_buffer);
  const void *vertices = nk_buffer_memory_const(vertex_buffer);
  glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));

  return result;
}

#render(rcontext) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/nuklear_renderer_opengl2/nuklear_renderer_opengl2.c', line 65

VALUE renderer_render_gl(VALUE self, VALUE rcontext) {
  VALUE window_size = rb_funcall(self, rb_intern("window_size"), 0);
  VALUE drawable_size = rb_funcall(self, rb_intern("drawable_size"), 0);
  VALUE window_width = rb_ary_entry(window_size, 0);
  VALUE window_height = rb_ary_entry(window_size, 1);
  VALUE drawable_width = rb_ary_entry(drawable_size, 0);
  VALUE drawable_height = rb_ary_entry(drawable_size, 1);
  int width = FIX2INT(window_width),
      height = FIX2INT(window_height);
  int display_width = FIX2INT(drawable_width),
      display_height = FIX2INT(drawable_height);

  glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT);
  glDisable(GL_CULL_FACE);
  glDisable(GL_DEPTH_TEST);
  glEnable(GL_SCISSOR_TEST);
  glEnable(GL_BLEND);
  glEnable(GL_TEXTURE_2D);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  /* setup viewport/project */
  glViewport(0,0,(GLsizei)display_width,(GLsizei)display_height);
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glEnableClientState(GL_COLOR_ARRAY);

  rb_call_super(1, &rcontext);
  
  /* default OpenGL state */
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  glDisableClientState(GL_COLOR_ARRAY);

  glDisable(GL_CULL_FACE);
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_SCISSOR_TEST);
  glDisable(GL_BLEND);
  glDisable(GL_TEXTURE_2D);

  glBindTexture(GL_TEXTURE_2D, 0);
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glPopAttrib();

  return self;
}