naev 0.11.5
opengl_render.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
27#include "naev.h"
30#include "opengl_render.h"
31
32#include "camera.h"
33#include "conf.h"
34#include "gui.h"
35#include "log.h"
36#include "ndata.h"
37#include "nstring.h"
38#include "opengl.h"
39
40#define OPENGL_RENDER_VBO_SIZE 256
42static gl_vbo *gl_renderVBO = 0;
43gl_vbo *gl_squareVBO = 0;
44static gl_vbo *gl_squareEmptyVBO = 0;
45gl_vbo *gl_circleVBO = 0;
46static gl_vbo *gl_lineVBO = 0;
47static gl_vbo *gl_triangleVBO = 0;
48static int gl_renderVBOtexOffset = 0;
49static int gl_renderVBOcolOffset = 0;
51void gl_beginSolidProgram(mat4 projection, const glColour *c)
52{
53 glUseProgram(shaders.solid.program);
54 glEnableVertexAttribArray(shaders.solid.vertex);
55 gl_uniformColour(shaders.solid.colour, c);
56 gl_uniformMat4(shaders.solid.projection, &projection);
57}
58
59void gl_endSolidProgram (void)
60{
61 glDisableVertexAttribArray(shaders.solid.vertex);
62 glUseProgram(0);
63 gl_checkErr();
64}
65
66void gl_beginSmoothProgram(mat4 projection)
67{
68 glUseProgram(shaders.smooth.program);
69 glEnableVertexAttribArray(shaders.smooth.vertex);
70 glEnableVertexAttribArray(shaders.smooth.vertex_colour);
71 gl_uniformMat4(shaders.smooth.projection, &projection);
72}
73
74void gl_endSmoothProgram() {
75 glDisableVertexAttribArray(shaders.smooth.vertex);
76 glDisableVertexAttribArray(shaders.smooth.vertex_colour);
77 glUseProgram(0);
78 gl_checkErr();
79}
80
90void gl_renderRect( double x, double y, double w, double h, const glColour *c )
91{
92 /* Set the vertex. */
93 mat4 projection = gl_view_matrix;
94 mat4_translate( &projection, x, y, 0. );
95 mat4_scale( &projection, w, h, 1. );
96
97 gl_renderRectH( &projection, c, 1 );
98}
99
109void gl_renderRectEmpty( double x, double y, double w, double h, const glColour *c )
110{
111 mat4 projection = gl_view_matrix;
112 mat4_translate( &projection, x, y, 0. );
113 mat4_scale( &projection, w, h, 1. );
114
115 gl_renderRectH( &projection, c, 0 );
116}
117
125void gl_renderRectH( const mat4 *H, const glColour *c, int filled )
126{
127 gl_beginSolidProgram(*H, c);
128 if (filled) {
129 gl_vboActivateAttribOffset( gl_squareVBO, shaders.solid.vertex, 0, 2, GL_FLOAT, 0 );
130 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
131 }
132 else {
133 gl_vboActivateAttribOffset( gl_squareEmptyVBO, shaders.solid.vertex, 0, 2, GL_FLOAT, 0 );
134 glDrawArrays( GL_LINE_STRIP, 0, 5 );
135 }
136 gl_endSolidProgram();
137}
138
147void gl_renderCross( double x, double y, double r, const glColour *c )
148{
149 glUseProgram(shaders.crosshairs.program);
150 glUniform1f(shaders.crosshairs.paramf, 1.); /* No outline. */
151 gl_renderShader( x, y, r, r, 0., &shaders.crosshairs, c, 1 );
152}
153
164void gl_renderTriangleEmpty( double x, double y, double a, double s, double length, const glColour *c )
165{
166 mat4 projection = gl_view_matrix;
167 mat4_translate( &projection, x, y, 0. );
168 if (a != 0.)
169 mat4_rotate2d( &projection, a );
170 mat4_scale( &projection, s*length, s, 1. );
171
172 gl_beginSolidProgram(projection, c);
173 gl_vboActivateAttribOffset( gl_triangleVBO, shaders.solid.vertex, 0, 2, GL_FLOAT, 0 );
174 glDrawArrays( GL_LINE_STRIP, 0, 4 );
175 gl_endSolidProgram();
176}
177
194void gl_renderTextureRaw( GLuint texture, uint8_t flags,
195 double x, double y, double w, double h,
196 double tx, double ty, double tw, double th,
197 const glColour *c, double angle )
198{
199 // Half width and height
200 double hw, hh;
201 mat4 projection, tex_mat;
202
203 glUseProgram(shaders.texture.program);
204
205 /* Bind the texture. */
206 glBindTexture( GL_TEXTURE_2D, texture);
207
208 /* Must have colour for now. */
209 if (c == NULL)
210 c = &cWhite;
211
212 hw = w*0.5;
213 hh = h*0.5;
214
215 /* Set the vertex. */
216 projection = gl_view_matrix;
217 if (angle==0.) {
218 mat4_translate( &projection, x, y, 0. );
219 mat4_scale( &projection, w, h, 1. );
220 }
221 else {
222 mat4_translate( &projection, x+hw, y+hh, 0. );
223 mat4_rotate2d( &projection, angle );
224 mat4_translate( &projection, -hw, -hh, 0. );
225 mat4_scale( &projection, w, h, 1. );
226 }
227 glEnableVertexAttribArray( shaders.texture.vertex );
228 gl_vboActivateAttribOffset( gl_squareVBO, shaders.texture.vertex,
229 0, 2, GL_FLOAT, 0 );
230
231 /* Set the texture. */
232 tex_mat = (flags & OPENGL_TEX_VFLIP) ? mat4_ortho(-1, 1, 2, 0, 1, -1) : mat4_identity();
233 mat4_translate( &tex_mat, tx, ty, 0. );
234 mat4_scale( &tex_mat, tw, th, 1. );
235
236 /* Set shader uniforms. */
237 gl_uniformColour(shaders.texture.colour, c);
238 gl_uniformMat4(shaders.texture.projection, &projection);
239 gl_uniformMat4(shaders.texture.tex_mat, &tex_mat);
240
241 /* Draw. */
242 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
243
244 /* Clear state. */
245 glDisableVertexAttribArray( shaders.texture.vertex );
246
247 /* anything failed? */
248 gl_checkErr();
249
250 glUseProgram(0);
251}
252
268void gl_renderTexture( const glTexture* texture,
269 double x, double y, double w, double h,
270 double tx, double ty, double tw, double th,
271 const glColour *c, double angle )
272{
273 gl_renderTextureRaw( texture->texture, texture->flags, x, y, w, h, tx, ty, tw, th, c, angle );
274}
275
288void gl_renderSDF( const glTexture *texture,
289 double x, double y, double w, double h,
290 const glColour *c, double angle, double outline )
291{
292 (void) outline; /* TODO handle outline. */
293 double hw, hh; /* Half width and height */
294 double sw, sh;
295 mat4 projection, tex_mat;
296
297 glUseProgram(shaders.texturesdf.program);
298
299 /* Bind the texture. */
300 glBindTexture( GL_TEXTURE_2D, texture->texture );
301
302 /* Must have colour for now. */
303 if (c == NULL)
304 c = &cWhite;
305
306 hw = w*0.5;
307 hh = h*0.5;
308
309 /* Set the vertex. */
310 projection = gl_view_matrix;
311 if (angle==0.) {
312 mat4_translate( &projection, x+hw, y+hh, 0. );
313 mat4_scale( &projection, hw, hh, 1. );
314 }
315 else {
316 mat4_translate( &projection, x+hw, y+hh, 0. );
317 mat4_rotate2d( &projection, angle );
318 mat4_scale( &projection, hw, hh, 1. );
319 }
320 glEnableVertexAttribArray( shaders.texturesdf.vertex );
321 gl_vboActivateAttribOffset( gl_circleVBO, shaders.texturesdf.vertex,
322 0, 2, GL_FLOAT, 0 );
323
324 /* Set the texture. */
325 /* TODO we would want to pad the texture a bit to get nice marked borders, but we have to actually pad the SDF first... */
326 sw = 0.;//1./w;
327 sh = 0.;//1./h;
328 tex_mat = (texture->flags & OPENGL_TEX_VFLIP) ? mat4_ortho(-1, 1, 2, 0, 1, -1) : mat4_identity();
329 mat4_scale( &tex_mat, texture->srw+2.*sw, texture->srh+2.*sh, 1. );
330 mat4_translate( &tex_mat, -sw, -sh, 0. );
331
332 /* Set shader uniforms. */
333 gl_uniformColour(shaders.texturesdf.colour, c);
334 gl_uniformMat4(shaders.texturesdf.projection, &projection);
335 gl_uniformMat4(shaders.texturesdf.tex_mat, &tex_mat);
336 glUniform1f( shaders.texturesdf.m, (2.0*texture->vmax*(w+2.)/texture->w) );
337
338 /* Draw. */
339 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
340
341 /* Clear state. */
342 glDisableVertexAttribArray( shaders.texturesdf.vertex );
343
344 /* anything failed? */
345 gl_checkErr();
346
347 glUseProgram(0);
348}
349
369 const glTexture* tb, double inter,
370 double x, double y, double w, double h,
371 double tx, double ty, double tw, double th, const glColour *c )
372{
373 /* No interpolation. */
374 if (tb == NULL) {
375 gl_renderTexture( ta, x, y, w, h, tx, ty, tw, th, c, 0. );
376 return;
377 }
378
379 /* Corner cases. */
380 if (inter >= 1.) {
381 gl_renderTexture( ta, x, y, w, h, tx, ty, tw, th, c, 0. );
382 return;
383 }
384 else if (inter <= 0.) {
385 gl_renderTexture( tb, x, y, w, h, tx, ty, tw, th, c, 0. );
386 return;
387 }
388
389 mat4 projection, tex_mat;
390
391 glUseProgram(shaders.texture_interpolate.program);
392
393 /* Bind the textures. */
394 glActiveTexture( GL_TEXTURE1 );
395 glBindTexture( GL_TEXTURE_2D, tb->texture);
396 glActiveTexture( GL_TEXTURE0 );
397 glBindTexture( GL_TEXTURE_2D, ta->texture);
398 /* Always end with TEXTURE0 active. */
399
400 /* Must have colour for now. */
401 if (c == NULL)
402 c = &cWhite;
403
404 /* Set the vertex. */
405 projection = gl_view_matrix;
406 mat4_translate( &projection, x, y, 0. );
407 mat4_scale( &projection, w, h, 1. );
408 glEnableVertexAttribArray( shaders.texture_interpolate.vertex );
409 gl_vboActivateAttribOffset( gl_squareVBO, shaders.texture_interpolate.vertex, 0, 2, GL_FLOAT, 0 );
410
411 /* Set the texture. */
412 tex_mat = (ta->flags & OPENGL_TEX_VFLIP) ? mat4_ortho(-1, 1, 2, 0, 1, -1) : mat4_identity();
413 mat4_translate( &tex_mat, tx, ty, 0. );
414 mat4_scale( &tex_mat, tw, th, 1. );
415
416 /* Set shader uniforms. */
417 glUniform1i(shaders.texture_interpolate.sampler1, 0);
418 glUniform1i(shaders.texture_interpolate.sampler2, 1);
419 gl_uniformColour(shaders.texture_interpolate.colour, c);
420 glUniform1f(shaders.texture_interpolate.inter, inter);
421 gl_uniformMat4(shaders.texture_interpolate.projection, &projection);
422 gl_uniformMat4(shaders.texture_interpolate.tex_mat, &tex_mat);
423
424 /* Draw. */
425 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
426
427 /* Clear state. */
428 glDisableVertexAttribArray( shaders.texture_interpolate.vertex );
429
430 /* anything failed? */
431 gl_checkErr();
432
433 glUseProgram(0);
434}
435
444void gl_gameToScreenCoords( double *nx, double *ny, double bx, double by )
445{
446 double cx,cy, gx,gy, z;
447
448 /* Get parameters. */
449 cam_getPos( &cx, &cy );
450 z = cam_getZoom();
451 gui_getOffset( &gx, &gy );
452
453 /* calculate position - we'll use relative coords to player */
454 *nx = (bx - cx) * z + gx + SCREEN_W*0.5;
455 *ny = (by - cy) * z + gy + SCREEN_H*0.5;
456}
457
464{
465 double cx,cy, gx,gy, z;
466 mat4 projection = lhs;
467
468 /* Get parameters. */
469 cam_getPos( &cx, &cy );
470 z = cam_getZoom();
471 gui_getOffset( &gx, &gy );
472
473 mat4_translate( &projection, gx + SCREEN_W*0.5, gy + SCREEN_H*0.5, 0. );
474 mat4_scale( &projection, z, z, 1. );
475 mat4_translate( &projection, -cx, cy, 0. );
476 return projection;
477}
478
487void gl_screenToGameCoords( double *nx, double *ny, int bx, int by )
488{
489 double cx,cy, gx,gy, z;
490
491 /* Get parameters. */
492 cam_getPos( &cx, &cy );
493 z = cam_getZoom();
494 gui_getOffset( &gx, &gy );
495
496 /* calculate position - we'll use relative coords to player */
497 *nx = (bx - SCREEN_W*0.5 - gx) / z + cx;
498 *ny = (by - SCREEN_H*0.5 - gy) / z + cy;
499}
500
514void gl_renderSprite( const glTexture* sprite, double bx, double by,
515 int sx, int sy, const glColour* c )
516{
517 double x,y, w,h, tx,ty, z;
518
519 /* Translate coords. */
520 z = cam_getZoom();
521 gl_gameToScreenCoords( &x, &y, bx - sprite->sw*0.5, by - sprite->sh*0.5 );
522
523 /* Scaled sprite dimensions. */
524 w = sprite->sw*z;
525 h = sprite->sh*z;
526
527 /* check if inbounds */
528 if ((x < -w) || (x > SCREEN_W+w) ||
529 (y < -h) || (y > SCREEN_H+h))
530 return;
531
532 /* texture coords */
533 tx = sprite->sw*(double)(sx)/sprite->w;
534 ty = sprite->sh*(sprite->sy-(double)sy-1)/sprite->h;
535
536 gl_renderTexture( sprite, x, y, w, h,
537 tx, ty, sprite->srw, sprite->srh, c, 0. );
538}
539
555void gl_renderSpriteScale( const glTexture* sprite, double bx, double by,
556 double scalew, double scaleh,
557 int sx, int sy, const glColour* c )
558{
559 double x,y, w,h, tx,ty, z;
560
561 /* Translate coords. */
562 z = cam_getZoom();
563 gl_gameToScreenCoords( &x, &y, bx - sprite->sw*0.5, by - sprite->sh*0.5 );
564
565 /* Scaled sprite dimensions. */
566 w = sprite->sw*z*scalew;
567 h = sprite->sh*z*scaleh;
568
569 /* check if inbounds */
570 if ((x < -w) || (x > SCREEN_W+w) ||
571 (y < -h) || (y > SCREEN_H+h))
572 return;
573
574 /* texture coords */
575 tx = sprite->sw*(double)(sx)/sprite->w;
576 ty = sprite->sh*(sprite->sy-(double)sy-1)/sprite->h;
577
578 gl_renderTexture( sprite, x, y, w, h,
579 tx, ty, sprite->srw, sprite->srh, c, 0. );
580}
581
597 double bx, double by, double angle,
598 int sx, int sy, const glColour *c )
599{
600 double x,y, w,h, tx,ty, z;
601
602 /* Translate coords. */
603 z = cam_getZoom();
604 gl_gameToScreenCoords( &x, &y, bx - sprite->sw*0.5, by - sprite->sh*0.5 );
605
606 /* Scaled sprite dimensions. */
607 w = sprite->sw*z;
608 h = sprite->sh*z;
609
610 /* check if inbounds */
611 if ((x < -w) || (x > SCREEN_W+w) ||
612 (y < -h) || (y > SCREEN_H+h))
613 return;
614
615 /* texture coords */
616 tx = sprite->sw*(double)(sx)/sprite->w;
617 ty = sprite->sh*(sprite->sy-(double)sy-1)/sprite->h;
618
619 gl_renderTexture( sprite, x, y, w, h,
620 tx, ty, sprite->srw, sprite->srh, c, angle );
621}
622
640 double bx, double by,
641 double scalew, double scaleh, double angle,
642 int sx, int sy, const glColour *c )
643{
644 double x,y, w,h, tx,ty, z;
645
646 /* Translate coords. */
647 z = cam_getZoom();
648 gl_gameToScreenCoords( &x, &y, bx - sprite->sw*0.5, by - sprite->sh*0.5 );
649
650 /* Scaled sprite dimensions. */
651 w = sprite->sw*z*scalew;
652 h = sprite->sh*z*scaleh;
653
654 /* check if inbounds */
655 if ((x < -w) || (x > SCREEN_W+w) ||
656 (y < -h) || (y > SCREEN_H+h))
657 return;
658
659 /* texture coords */
660 tx = sprite->sw*(double)(sx)/sprite->w;
661 ty = sprite->sh*(sprite->sy-(double)sy-1)/sprite->h;
662
663 gl_renderTexture( sprite, x, y, w, h,
664 tx, ty, sprite->srw, sprite->srh, c, angle );
665}
666
685 double inter, double bx, double by,
686 int sx, int sy, const glColour *c )
687{
688 gl_renderSpriteInterpolateScale( sa, sb, inter, bx, by, 1., 1., sx, sy, c );
689}
690
711 double inter, double bx, double by,
712 double scalew, double scaleh,
713 int sx, int sy, const glColour *c )
714{
715 double x,y, w,h, tx,ty, z;
716
717 /* Translate coords. */
718 gl_gameToScreenCoords( &x, &y, bx - scalew * sa->sw*0.5, by - scaleh * sa->sh*0.5 );
719
720 /* Scaled sprite dimensions. */
721 z = cam_getZoom();
722 w = sa->sw*z*scalew;
723 h = sa->sh*z*scaleh;
724
725 /* check if inbounds */
726 if ((x < -w) || (x > SCREEN_W+w) ||
727 (y < -h) || (y > SCREEN_H+h))
728 return;
729
730 /* texture coords */
731 tx = sa->sw*(double)(sx)/sa->w;
732 ty = sa->sh*(sa->sy-(double)sy-1)/sa->h;
733
734 gl_renderTextureInterpolate( sa, sb, inter, x, y, w, h,
735 tx, ty, sa->srw, sa->srh, c );
736}
737
748void gl_renderStaticSprite( const glTexture* sprite, double bx, double by,
749 int sx, int sy, const glColour* c )
750{
751 double x,y, tx,ty;
752
753 x = bx;
754 y = by;
755
756 /* texture coords */
757 tx = sprite->sw*(double)(sx)/sprite->w;
758 ty = sprite->sh*(sprite->sy-(double)sy-1)/sprite->h;
759
760 /* actual blitting */
761 gl_renderTexture( sprite, x, y, sprite->sw, sprite->sh,
762 tx, ty, sprite->srw, sprite->srh, c, 0. );
763}
764
783 double inter, double bx, double by,
784 int sx, int sy, const glColour *c )
785{
786 gl_renderStaticSpriteInterpolateScale( sa, sb, inter, bx, by, 1., 1., sx, sy, c );
787}
788
809 double inter, double bx, double by,
810 double scalew, double scaleh,
811 int sx, int sy, const glColour *c )
812{
813 double x,y, w,h, tx,ty;
814
815 x = bx;
816 y = by;
817
818 /* Scaled sprite dimensions. */
819 w = sa->sw*scalew;
820 h = sa->sh*scaleh;
821
822 /* check if inbounds */
823 if ((x < -w) || (x > SCREEN_W+w) ||
824 (y < -h) || (y > SCREEN_H+h))
825 return;
826
827 /* texture coords */
828 tx = sa->sw*(double)(sx)/sa->w;
829 ty = sa->sh*(sa->sy-(double)sy-1)/sa->h;
830
831 gl_renderTextureInterpolate( sa, sb, inter, x, y, w, h,
832 tx, ty, sa->srw, sa->srh, c );
833}
834
847void gl_renderScaleSprite( const glTexture* sprite,
848 double bx, double by,
849 int sx, int sy,
850 double bw, double bh, const glColour* c )
851{
852 double x,y, tx,ty;
853
854 x = bx;
855 y = by;
856
857 /* texture coords */
858 tx = sprite->sw*(double)(sx)/sprite->w;
859 ty = sprite->sh*(sprite->sy-(double)sy-1)/sprite->h;
860
861 /* actual blitting */
862 gl_renderTexture( sprite, x, y, bw, bh,
863 tx, ty, sprite->srw, sprite->srh, c, 0. );
864}
865
876void gl_renderScale( const glTexture* texture,
877 double bx, double by,
878 double bw, double bh, const glColour* c )
879{
880 double x,y, tx, ty;
881
882 /* here we use absolute coords */
883 x = bx;
884 y = by;
885
886 /* texture dimensions */
887 tx = ty = 0.;
888
889 /* Actual blitting. */
890 gl_renderTexture( texture, x, y, bw, bh,
891 tx, ty, texture->srw, texture->srh, c, 0. );
892}
893
905void gl_renderScaleAspect( const glTexture* texture,
906 double bx, double by, double bw, double bh,
907 const glColour *c )
908{
909 double scale;
910 double nw, nh;
911
912 scale = MIN( bw / texture->w, bh / texture->h );
913
914 nw = scale * texture->w;
915 nh = scale * texture->h;
916
917 bx += (bw-nw)*0.5;
918 by += (bh-nh)*0.5;
919
920 gl_renderScale( texture, bx, by, nw, nh, c );
921}
922
931void gl_renderStatic( const glTexture* texture,
932 double bx, double by, const glColour* c )
933{
934 double x,y;
935
936 /* here we use absolute coords */
937 x = bx;
938 y = by;
939
940 /* actual blitting */
941 gl_renderTexture( texture, x, y, texture->sw, texture->sh,
942 0., 0., texture->srw, texture->srh, c, 0. );
943}
944
957void gl_renderShader( double x, double y, double w, double h, double r, const SimpleShader *shd, const glColour *c, int center )
958{
959 mat4 projection = gl_view_matrix;
960 mat4_translate( &projection, x, y, 0. );
961 if (r != 0.)
962 mat4_rotate2d( &projection, r );
963 mat4_scale( &projection, w, h, 1. );
964 glUniform2f( shd->dimensions, w, h );
965 gl_renderShaderH( shd, &projection, c, center );
966}
967
976void gl_renderShaderH( const SimpleShader *shd, const mat4 *H, const glColour *c, int center )
977{
978 glEnableVertexAttribArray(shd->vertex);
979 gl_vboActivateAttribOffset( center ? gl_circleVBO : gl_squareVBO, shd->vertex, 0, 2, GL_FLOAT, 0 );
980
981 if (c != NULL)
982 gl_uniformColour(shd->colour, c);
983
984 gl_uniformMat4(shd->projection, H);
985
986 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
987
988 glDisableVertexAttribArray(shd->vertex);
989 glUseProgram(0);
990 gl_checkErr();
991}
992
1002void gl_renderCircle( double cx, double cy,
1003 double r, const glColour *c, int filled )
1004{
1005 /* Set the vertex. */
1006 mat4 projection = gl_view_matrix;
1007 mat4_translate( &projection, cx, cy, 0. );
1008 mat4_scale( &projection, r, r, 1. );
1009
1010 /* Draw! */
1011 gl_renderCircleH( &projection, c, filled );
1012}
1013
1021void gl_renderCircleH( const mat4 *H, const glColour *c, int filled )
1022{
1023 // TODO handle shearing and different x/y scaling
1024 GLfloat r = H->m[0][0] / gl_view_matrix.m[0][0];
1025
1026 glUseProgram( shaders.circle.program );
1027 glUniform2f( shaders.circle.dimensions, r, r );
1028 glUniform1i( shaders.circle.parami, filled );
1029 gl_renderShaderH( &shaders.circle, H, c, 1 );
1030}
1031
1041void gl_renderLine( double x1, double y1,
1042 double x2, double y2, const glColour *c )
1043{
1044 double a = atan2( y2-y1, x2-x1 );
1045 double s = hypotf( x2-x1, y2-y1 );
1046
1047 glUseProgram(shaders.sdfsolid.program);
1048 glUniform1f(shaders.sdfsolid.paramf, 1.); /* No outline. */
1049 gl_renderShader( (x1+x2)*0.5, (y1+y2)*0.5, s*0.5+0.5, 1.0, a, &shaders.sdfsolid, c, 1 );
1050}
1051
1060void gl_clipRect( int x, int y, int w, int h )
1061{
1062 double rx, ry, rw, rh;
1063 rx = (x + gl_screen.x) / gl_screen.mxscale;
1064 ry = (y + gl_screen.y) / gl_screen.myscale;
1065 rw = w / gl_screen.mxscale;
1066 rh = h / gl_screen.myscale;
1067 glScissor( rx, ry, rw, rh );
1068 glEnable( GL_SCISSOR_TEST );
1069}
1070
1074void gl_unclipRect (void)
1075{
1076 glDisable( GL_SCISSOR_TEST );
1077 glScissor( 0, 0, gl_screen.rw, gl_screen.rh );
1078}
1079
1086{
1087 GLfloat vertex[10];
1088
1089 /* Initialize the VBO. */
1090 gl_renderVBO = gl_vboCreateStream( sizeof(GLfloat) *
1091 OPENGL_RENDER_VBO_SIZE*(2 + 2 + 4), NULL );
1092 gl_renderVBOtexOffset = sizeof(GLfloat) * OPENGL_RENDER_VBO_SIZE*2;
1093 gl_renderVBOcolOffset = sizeof(GLfloat) * OPENGL_RENDER_VBO_SIZE*(2+2);
1094
1095 vertex[0] = 0.;
1096 vertex[1] = 0.;
1097 vertex[2] = 1.;
1098 vertex[3] = 0.;
1099 vertex[4] = 0.;
1100 vertex[5] = 1.;
1101 vertex[6] = 1.;
1102 vertex[7] = 1.;
1103 gl_squareVBO = gl_vboCreateStatic( sizeof(GLfloat) * 8, vertex );
1104
1105 vertex[0] = -1.;
1106 vertex[1] = -1.;
1107 vertex[2] = 1.;
1108 vertex[3] = -1.;
1109 vertex[4] = -1.;
1110 vertex[5] = 1.;
1111 vertex[6] = 1.;
1112 vertex[7] = 1.;
1113 gl_circleVBO = gl_vboCreateStatic( sizeof(GLfloat) * 8, vertex );
1114
1115 vertex[0] = 0.;
1116 vertex[1] = 0.;
1117 vertex[2] = 1.;
1118 vertex[3] = 0.;
1119 vertex[4] = 1.;
1120 vertex[5] = 1.;
1121 vertex[6] = 0.;
1122 vertex[7] = 1.;
1123 vertex[8] = 0.;
1124 vertex[9] = 0.;
1125 gl_squareEmptyVBO = gl_vboCreateStatic( sizeof(GLfloat) * 8, vertex );
1126
1127 vertex[0] = 0.;
1128 vertex[1] = 0.;
1129 vertex[2] = 1.;
1130 vertex[3] = 0.;
1131 gl_lineVBO = gl_vboCreateStatic( sizeof(GLfloat) * 4, vertex );
1132
1133 vertex[0] = 0.5*cos(4.*M_PI/3.);
1134 vertex[1] = 0.5*sin(4.*M_PI/3.);
1135 vertex[2] = 0.5*cos(0.);
1136 vertex[3] = 0.5*sin(0.);
1137 vertex[4] = 0.5*cos(2.*M_PI/3.);
1138 vertex[5] = 0.5*sin(2.*M_PI/3.);
1139 vertex[6] = vertex[0];
1140 vertex[7] = vertex[1];
1141 gl_triangleVBO = gl_vboCreateStatic( sizeof(GLfloat) * 8, vertex );
1142
1143 gl_checkErr();
1144
1145 return 0;
1146}
1147
1151void gl_exitRender (void)
1152{
1153 /* Destroy the VBO. */
1155 gl_vboDestroy( gl_squareVBO );
1156 gl_vboDestroy( gl_circleVBO );
1157 gl_vboDestroy( gl_squareEmptyVBO );
1158 gl_vboDestroy( gl_lineVBO );
1159 gl_vboDestroy( gl_triangleVBO );
1160 gl_renderVBO = NULL;
1161}
void cam_getPos(double *x, double *y)
Gets the camera position.
Definition camera.c:118
double cam_getZoom(void)
Gets the camera zoom.
Definition camera.c:97
void gui_getOffset(double *x, double *y)
Gets the GUI offset.
Definition gui.c:2062
void mat4_translate(mat4 *m, double x, double y, double z)
Translates a homogenous transformation matrix.
Definition mat4.c:99
mat4 mat4_identity(void)
Creates an identity matrix.
Definition mat4.c:195
void mat4_scale(mat4 *m, double x, double y, double z)
Scales a homogeneous transformation matrix.
Definition mat4.c:82
mat4 mat4_ortho(double left, double right, double bottom, double top, double nearVal, double farVal)
Creates an orthographic projection matrix.
Definition mat4.c:209
void mat4_rotate2d(mat4 *m, double angle)
Rotates an angle, in radians, around the z axis.
Definition mat4.c:111
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition naev.h:40
glInfo gl_screen
Definition opengl.c:51
void gl_exitRender(void)
Cleans up the OpenGL rendering routines.
void gl_renderShader(double x, double y, double w, double h, double r, const SimpleShader *shd, const glColour *c, int center)
Renders a simple shader.
void gl_renderShaderH(const SimpleShader *shd, const mat4 *H, const glColour *c, int center)
Renders a simple shader with a transformation.
static int gl_renderVBOcolOffset
void gl_renderRect(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
void gl_renderLine(double x1, double y1, double x2, double y2, const glColour *c)
Draws a line.
void gl_gameToScreenCoords(double *nx, double *ny, double bx, double by)
Converts in-game coordinates to screen coordinates.
#define OPENGL_RENDER_VBO_SIZE
void gl_renderSpriteInterpolate(const glTexture *sa, const glTexture *sb, double inter, double bx, double by, int sx, int sy, const glColour *c)
Blits a sprite interpolating, position is relative to the player.
int gl_initRender(void)
Initializes the OpenGL rendering routines.
void gl_renderCircleH(const mat4 *H, const glColour *c, int filled)
Draws a circle.
void gl_renderTexture(const glTexture *texture, double x, double y, double w, double h, double tx, double ty, double tw, double th, const glColour *c, double angle)
Texture blitting backend.
void gl_renderTriangleEmpty(double x, double y, double a, double s, double length, const glColour *c)
Renders a triangle at a given position.
void gl_unclipRect(void)
Clears the 2d clipping planes.
static int gl_renderVBOtexOffset
void gl_renderSpriteScale(const glTexture *sprite, double bx, double by, double scalew, double scaleh, int sx, int sy, const glColour *c)
Blits a sprite, position is relative to the player.
void gl_renderSpriteScaleRotate(const glTexture *sprite, double bx, double by, double scalew, double scaleh, double angle, int sx, int sy, const glColour *c)
Blits a sprite, position is relative to the player with scaling and rotation.
void gl_renderStatic(const glTexture *texture, double bx, double by, const glColour *c)
Blits a texture to a position.
void gl_renderSDF(const glTexture *texture, double x, double y, double w, double h, const glColour *c, double angle, double outline)
SDF Texture blitting backend.
void gl_renderScale(const glTexture *texture, double bx, double by, double bw, double bh, const glColour *c)
Blits a texture scaling it.
void gl_renderScaleSprite(const glTexture *sprite, double bx, double by, int sx, int sy, double bw, double bh, const glColour *c)
Blits a scaled sprite, position is in absolute screen coordinates.
void gl_renderTextureInterpolate(const glTexture *ta, const glTexture *tb, double inter, double x, double y, double w, double h, double tx, double ty, double tw, double th, const glColour *c)
Texture blitting backend for interpolated texture.
void gl_renderScaleAspect(const glTexture *texture, double bx, double by, double bw, double bh, const glColour *c)
Blits a texture scaling it to fit a rectangle, but conserves aspect ratio.
void gl_renderTextureRaw(GLuint texture, uint8_t flags, double x, double y, double w, double h, double tx, double ty, double tw, double th, const glColour *c, double angle)
Texture blitting backend.
void gl_renderSprite(const glTexture *sprite, double bx, double by, int sx, int sy, const glColour *c)
Blits a sprite, position is relative to the player.
void gl_renderStaticSpriteInterpolate(const glTexture *sa, const glTexture *sb, double inter, double bx, double by, int sx, int sy, const glColour *c)
Blits a sprite interpolating, position is relative to the player.
void gl_screenToGameCoords(double *nx, double *ny, int bx, int by)
Converts screen coordinates to in-game coordinates.
void gl_renderStaticSprite(const glTexture *sprite, double bx, double by, int sx, int sy, const glColour *c)
Blits a sprite, position is in absolute screen coordinates.
static gl_vbo * gl_renderVBO
void gl_renderRectEmpty(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
void gl_renderCross(double x, double y, double r, const glColour *c)
Renders a cross at a given position.
void gl_renderSpriteRotate(const glTexture *sprite, double bx, double by, double angle, int sx, int sy, const glColour *c)
Blits a sprite, position is relative to the player with rotation.
void gl_renderStaticSpriteInterpolateScale(const glTexture *sa, const glTexture *sb, double inter, double bx, double by, double scalew, double scaleh, int sx, int sy, const glColour *c)
Blits a sprite interpolating, position is relative to the player.
void gl_renderSpriteInterpolateScale(const glTexture *sa, const glTexture *sb, double inter, double bx, double by, double scalew, double scaleh, int sx, int sy, const glColour *c)
Blits a sprite interpolating, position is relative to the player.
void gl_renderRectH(const mat4 *H, const glColour *c, int filled)
Renders a rectangle.
mat4 gl_gameToScreenMatrix(mat4 lhs)
Return a transformation which converts in-game coordinates to screen coordinates.
void gl_clipRect(int x, int y, int w, int h)
Sets up 2d clipping planes around a rectangle.
void gl_renderCircle(double cx, double cy, double r, const glColour *c, int filled)
Draws a circle.
void gl_vboDestroy(gl_vbo *vbo)
Destroys a VBO.
Definition opengl_vbo.c:246
gl_vbo * gl_vboCreateStream(GLsizei size, const void *data)
Creates a stream vbo.
Definition opengl_vbo.c:145
void gl_vboActivateAttribOffset(gl_vbo *vbo, GLuint index, GLuint offset, GLint size, GLenum type, GLsizei stride)
Activates a VBO's offset.
Definition opengl_vbo.c:226
gl_vbo * gl_vboCreateStatic(GLsizei size, const void *data)
Creates a stream vbo.
Definition opengl_vbo.c:179
static const double c[]
Definition rng.c:264
int y
Definition opengl.h:42
int x
Definition opengl.h:41
double myscale
Definition opengl.h:58
int rh
Definition opengl.h:51
double mxscale
Definition opengl.h:57
int rw
Definition opengl.h:50
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:36
double sw
Definition opengl_tex.h:46
double sh
Definition opengl_tex.h:47
double w
Definition opengl_tex.h:40
uint8_t flags
Definition opengl_tex.h:57
double srh
Definition opengl_tex.h:49
GLuint texture
Definition opengl_tex.h:52
double sy
Definition opengl_tex.h:45
double h
Definition opengl_tex.h:41
double srw
Definition opengl_tex.h:48
Definition mat4.h:10