30#include "opengl_render.h"
40#define OPENGL_RENDER_VBO_SIZE 256
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;
51void gl_beginSolidProgram(
mat4 projection,
const glColour *
c)
53 glUseProgram(shaders.solid.program);
54 glEnableVertexAttribArray(shaders.solid.vertex);
55 gl_uniformColour(shaders.solid.colour,
c);
56 gl_uniformMat4(shaders.solid.projection, &projection);
59void gl_endSolidProgram (
void)
61 glDisableVertexAttribArray(shaders.solid.vertex);
66void gl_beginSmoothProgram(
mat4 projection)
68 glUseProgram(shaders.smooth.program);
69 glEnableVertexAttribArray(shaders.smooth.vertex);
70 glEnableVertexAttribArray(shaders.smooth.vertex_colour);
71 gl_uniformMat4(shaders.smooth.projection, &projection);
74void gl_endSmoothProgram() {
75 glDisableVertexAttribArray(shaders.smooth.vertex);
76 glDisableVertexAttribArray(shaders.smooth.vertex_colour);
90void gl_renderRect(
double x,
double y,
double w,
double h,
const glColour *
c )
93 mat4 projection = gl_view_matrix;
111 mat4 projection = gl_view_matrix;
127 gl_beginSolidProgram(*H,
c);
130 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
134 glDrawArrays( GL_LINE_STRIP, 0, 5 );
136 gl_endSolidProgram();
149 glUseProgram(shaders.crosshairs.program);
150 glUniform1f(shaders.crosshairs.paramf, 1.);
166 mat4 projection = gl_view_matrix;
172 gl_beginSolidProgram(projection,
c);
174 glDrawArrays( GL_LINE_STRIP, 0, 4 );
175 gl_endSolidProgram();
195 double x,
double y,
double w,
double h,
196 double tx,
double ty,
double tw,
double th,
197 const glColour *
c,
double angle )
201 mat4 projection, tex_mat;
203 glUseProgram(shaders.texture.program);
206 glBindTexture( GL_TEXTURE_2D, texture);
216 projection = gl_view_matrix;
227 glEnableVertexAttribArray( shaders.texture.vertex );
237 gl_uniformColour(shaders.texture.colour,
c);
238 gl_uniformMat4(shaders.texture.projection, &projection);
239 gl_uniformMat4(shaders.texture.tex_mat, &tex_mat);
242 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
245 glDisableVertexAttribArray( shaders.texture.vertex );
269 double x,
double y,
double w,
double h,
270 double tx,
double ty,
double tw,
double th,
271 const glColour *
c,
double angle )
273 gl_renderTextureRaw( texture->texture, texture->flags, x, y, w, h, tx, ty, tw, th,
c, angle );
289 double x,
double y,
double w,
double h,
290 const glColour *
c,
double angle,
double outline )
295 mat4 projection, tex_mat;
297 glUseProgram(shaders.texturesdf.program);
300 glBindTexture( GL_TEXTURE_2D, texture->texture );
310 projection = gl_view_matrix;
320 glEnableVertexAttribArray( shaders.texturesdf.vertex );
329 mat4_scale( &tex_mat, texture->srw+2.*sw, texture->srh+2.*sh, 1. );
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) );
339 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
342 glDisableVertexAttribArray( shaders.texturesdf.vertex );
370 double x,
double y,
double w,
double h,
371 double tx,
double ty,
double tw,
double th,
const glColour *
c )
375 gl_renderTexture( ta, x, y, w, h, tx, ty, tw, th,
c, 0. );
381 gl_renderTexture( ta, x, y, w, h, tx, ty, tw, th,
c, 0. );
384 else if (inter <= 0.) {
385 gl_renderTexture( tb, x, y, w, h, tx, ty, tw, th,
c, 0. );
389 mat4 projection, tex_mat;
391 glUseProgram(shaders.texture_interpolate.program);
394 glActiveTexture( GL_TEXTURE1 );
395 glBindTexture( GL_TEXTURE_2D, tb->
texture);
396 glActiveTexture( GL_TEXTURE0 );
397 glBindTexture( GL_TEXTURE_2D, ta->
texture);
405 projection = gl_view_matrix;
408 glEnableVertexAttribArray( shaders.texture_interpolate.vertex );
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);
425 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
428 glDisableVertexAttribArray( shaders.texture_interpolate.vertex );
446 double cx,cy, gx,gy, z;
454 *nx = (bx - cx) * z + gx + SCREEN_W*0.5;
455 *ny = (by - cy) * z + gy + SCREEN_H*0.5;
465 double cx,cy, gx,gy, z;
466 mat4 projection = lhs;
473 mat4_translate( &projection, gx + SCREEN_W*0.5, gy + SCREEN_H*0.5, 0. );
489 double cx,cy, gx,gy, z;
497 *nx = (bx - SCREEN_W*0.5 - gx) / z + cx;
498 *ny = (by - SCREEN_H*0.5 - gy) / z + cy;
515 int sx,
int sy,
const glColour*
c )
517 double x,y, w,h, tx,ty, z;
528 if ((x < -w) || (x > SCREEN_W+w) ||
529 (y < -h) || (y > SCREEN_H+h))
533 tx = sprite->
sw*(double)(sx)/sprite->
w;
534 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
537 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
556 double scalew,
double scaleh,
557 int sx,
int sy,
const glColour*
c )
559 double x,y, w,h, tx,ty, z;
566 w = sprite->
sw*z*scalew;
567 h = sprite->
sh*z*scaleh;
570 if ((x < -w) || (x > SCREEN_W+w) ||
571 (y < -h) || (y > SCREEN_H+h))
575 tx = sprite->
sw*(double)(sx)/sprite->
w;
576 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
579 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
597 double bx,
double by,
double angle,
598 int sx,
int sy,
const glColour *
c )
600 double x,y, w,h, tx,ty, z;
611 if ((x < -w) || (x > SCREEN_W+w) ||
612 (y < -h) || (y > SCREEN_H+h))
616 tx = sprite->
sw*(double)(sx)/sprite->
w;
617 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
620 tx, ty, sprite->
srw, sprite->
srh,
c, angle );
640 double bx,
double by,
641 double scalew,
double scaleh,
double angle,
642 int sx,
int sy,
const glColour *
c )
644 double x,y, w,h, tx,ty, z;
651 w = sprite->
sw*z*scalew;
652 h = sprite->
sh*z*scaleh;
655 if ((x < -w) || (x > SCREEN_W+w) ||
656 (y < -h) || (y > SCREEN_H+h))
660 tx = sprite->
sw*(double)(sx)/sprite->
w;
661 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
664 tx, ty, sprite->
srw, sprite->
srh,
c, angle );
685 double inter,
double bx,
double by,
686 int sx,
int sy,
const glColour *
c )
711 double inter,
double bx,
double by,
712 double scalew,
double scaleh,
713 int sx,
int sy,
const glColour *
c )
715 double x,y, w,h, tx,ty, z;
726 if ((x < -w) || (x > SCREEN_W+w) ||
727 (y < -h) || (y > SCREEN_H+h))
731 tx = sa->
sw*(double)(sx)/sa->
w;
732 ty = sa->
sh*(sa->
sy-(double)sy-1)/sa->
h;
749 int sx,
int sy,
const glColour*
c )
757 tx = sprite->
sw*(double)(sx)/sprite->
w;
758 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
762 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
783 double inter,
double bx,
double by,
784 int sx,
int sy,
const glColour *
c )
809 double inter,
double bx,
double by,
810 double scalew,
double scaleh,
811 int sx,
int sy,
const glColour *
c )
813 double x,y, w,h, tx,ty;
823 if ((x < -w) || (x > SCREEN_W+w) ||
824 (y < -h) || (y > SCREEN_H+h))
828 tx = sa->
sw*(double)(sx)/sa->
w;
829 ty = sa->
sh*(sa->
sy-(double)sy-1)/sa->
h;
848 double bx,
double by,
850 double bw,
double bh,
const glColour*
c )
858 tx = sprite->
sw*(double)(sx)/sprite->
w;
859 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
863 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
877 double bx,
double by,
878 double bw,
double bh,
const glColour*
c )
891 tx, ty, texture->srw, texture->srh,
c, 0. );
906 double bx,
double by,
double bw,
double bh,
912 scale =
MIN( bw / texture->w, bh / texture->h );
914 nw = scale * texture->w;
915 nh = scale * texture->h;
932 double bx,
double by,
const glColour*
c )
942 0., 0., texture->srw, texture->srh,
c, 0. );
957void gl_renderShader(
double x,
double y,
double w,
double h,
double r,
const SimpleShader *shd,
const glColour *
c,
int center )
959 mat4 projection = gl_view_matrix;
964 glUniform2f( shd->dimensions, w, h );
978 glEnableVertexAttribArray(shd->vertex);
982 gl_uniformColour(shd->colour,
c);
984 gl_uniformMat4(shd->projection, H);
986 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
988 glDisableVertexAttribArray(shd->vertex);
1003 double r,
const glColour *
c,
int filled )
1006 mat4 projection = gl_view_matrix;
1024 GLfloat r = H->m[0][0] / gl_view_matrix.m[0][0];
1026 glUseProgram( shaders.circle.program );
1027 glUniform2f( shaders.circle.dimensions, r, r );
1028 glUniform1i( shaders.circle.parami, filled );
1042 double x2,
double y2,
const glColour *
c )
1044 double a = atan2( y2-y1, x2-x1 );
1045 double s = hypotf( x2-x1, y2-y1 );
1047 glUseProgram(shaders.sdfsolid.program);
1048 glUniform1f(shaders.sdfsolid.paramf, 1.);
1049 gl_renderShader( (x1+x2)*0.5, (y1+y2)*0.5, s*0.5+0.5, 1.0, a, &shaders.sdfsolid,
c, 1 );
1062 double rx, ry, rw, rh;
1067 glScissor( rx, ry, rw, rh );
1068 glEnable( GL_SCISSOR_TEST );
1076 glDisable( GL_SCISSOR_TEST );
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];
void cam_getPos(double *x, double *y)
Gets the camera position.
double cam_getZoom(void)
Gets the camera zoom.
void gui_getOffset(double *x, double *y)
Gets the GUI offset.
void mat4_translate(mat4 *m, double x, double y, double z)
Translates a homogenous transformation matrix.
mat4 mat4_identity(void)
Creates an identity matrix.
void mat4_scale(mat4 *m, double x, double y, double z)
Scales a homogeneous transformation matrix.
mat4 mat4_ortho(double left, double right, double bottom, double top, double nearVal, double farVal)
Creates an orthographic projection matrix.
void mat4_rotate2d(mat4 *m, double angle)
Rotates an angle, in radians, around the z axis.
Header file with generic functions and naev-specifics.
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.
gl_vbo * gl_vboCreateStream(GLsizei size, const void *data)
Creates a stream vbo.
void gl_vboActivateAttribOffset(gl_vbo *vbo, GLuint index, GLuint offset, GLint size, GLenum type, GLsizei stride)
Activates a VBO's offset.
gl_vbo * gl_vboCreateStatic(GLsizei size, const void *data)
Creates a stream vbo.
Abstraction for rendering sprite sheets.