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_uniformColor(shaders.solid.color,
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_color);
71 gl_uniformMat4(shaders.smooth.projection, &projection);
74void gl_endSmoothProgram() {
75 glDisableVertexAttribArray(shaders.smooth.vertex);
76 glDisableVertexAttribArray(shaders.smooth.vertex_color);
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_uniformColor(shaders.texture.color,
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 );
296 double x,
double y,
double w,
double h,
297 double tx,
double ty,
double tw,
double th,
const glColour *
c )
301 gl_renderTexture( ta, x, y, w, h, tx, ty, tw, th,
c, 0. );
307 gl_renderTexture( ta, x, y, w, h, tx, ty, tw, th,
c, 0. );
310 else if (inter <= 0.) {
311 gl_renderTexture( tb, x, y, w, h, tx, ty, tw, th,
c, 0. );
315 mat4 projection, tex_mat;
317 glUseProgram(shaders.texture_interpolate.program);
320 glActiveTexture( GL_TEXTURE1 );
321 glBindTexture( GL_TEXTURE_2D, tb->
texture);
322 glActiveTexture( GL_TEXTURE0 );
323 glBindTexture( GL_TEXTURE_2D, ta->
texture);
331 projection = gl_view_matrix;
334 glEnableVertexAttribArray( shaders.texture_interpolate.vertex );
343 glUniform1i(shaders.texture_interpolate.sampler1, 0);
344 glUniform1i(shaders.texture_interpolate.sampler2, 1);
345 gl_uniformColor(shaders.texture_interpolate.color,
c);
346 glUniform1f(shaders.texture_interpolate.inter, inter);
347 gl_uniformMat4(shaders.texture_interpolate.projection, &projection);
348 gl_uniformMat4(shaders.texture_interpolate.tex_mat, &tex_mat);
351 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
354 glDisableVertexAttribArray( shaders.texture_interpolate.vertex );
372 double cx,cy, gx,gy, z;
380 *nx = (bx - cx) * z + gx + SCREEN_W/2.;
381 *ny = (by - cy) * z + gy + SCREEN_H/2.;
391 double cx,cy, gx,gy, z;
392 mat4 projection = lhs;
399 mat4_translate( &projection, gx + SCREEN_W/2., gy + SCREEN_H/2., 0. );
415 double cx,cy, gx,gy, z;
423 *nx = (bx - SCREEN_W/2. - gx) / z + cx;
424 *ny = (by - SCREEN_H/2. - gy) / z + cy;
441 int sx,
int sy,
const glColour*
c )
443 double x,y, w,h, tx,ty, z;
454 if ((x < -w) || (x > SCREEN_W+w) ||
455 (y < -h) || (y > SCREEN_H+h))
459 tx = sprite->
sw*(double)(sx)/sprite->
w;
460 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
463 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
482 double scalew,
double scaleh,
483 int sx,
int sy,
const glColour*
c )
485 double x,y, w,h, tx,ty, z;
492 w = sprite->
sw*z*scalew;
493 h = sprite->
sh*z*scaleh;
496 if ((x < -w) || (x > SCREEN_W+w) ||
497 (y < -h) || (y > SCREEN_H+h))
501 tx = sprite->
sw*(double)(sx)/sprite->
w;
502 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
505 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
523 double bx,
double by,
double angle,
524 int sx,
int sy,
const glColour *
c )
526 double x,y, w,h, tx,ty, z;
537 if ((x < -w) || (x > SCREEN_W+w) ||
538 (y < -h) || (y > SCREEN_H+h))
542 tx = sprite->
sw*(double)(sx)/sprite->
w;
543 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
546 tx, ty, sprite->
srw, sprite->
srh,
c, angle );
566 double bx,
double by,
567 double scalew,
double scaleh,
double angle,
568 int sx,
int sy,
const glColour *
c )
570 double x,y, w,h, tx,ty, z;
577 w = sprite->
sw*z*scalew;
578 h = sprite->
sh*z*scaleh;
581 if ((x < -w) || (x > SCREEN_W+w) ||
582 (y < -h) || (y > SCREEN_H+h))
586 tx = sprite->
sw*(double)(sx)/sprite->
w;
587 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
590 tx, ty, sprite->
srw, sprite->
srh,
c, angle );
611 double inter,
double bx,
double by,
612 int sx,
int sy,
const glColour *
c )
637 double inter,
double bx,
double by,
638 double scalew,
double scaleh,
639 int sx,
int sy,
const glColour *
c )
641 double x,y, w,h, tx,ty, z;
652 if ((x < -w) || (x > SCREEN_W+w) ||
653 (y < -h) || (y > SCREEN_H+h))
657 tx = sa->
sw*(double)(sx)/sa->
w;
658 ty = sa->
sh*(sa->
sy-(double)sy-1)/sa->
h;
675 int sx,
int sy,
const glColour*
c )
683 tx = sprite->
sw*(double)(sx)/sprite->
w;
684 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
688 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
709 double inter,
double bx,
double by,
710 int sx,
int sy,
const glColour *
c )
735 double inter,
double bx,
double by,
736 double scalew,
double scaleh,
737 int sx,
int sy,
const glColour *
c )
739 double x,y, w,h, tx,ty;
749 if ((x < -w) || (x > SCREEN_W+w) ||
750 (y < -h) || (y > SCREEN_H+h))
754 tx = sa->
sw*(double)(sx)/sa->
w;
755 ty = sa->
sh*(sa->
sy-(double)sy-1)/sa->
h;
774 double bx,
double by,
776 double bw,
double bh,
const glColour*
c )
784 tx = sprite->
sw*(double)(sx)/sprite->
w;
785 ty = sprite->
sh*(sprite->
sy-(double)sy-1)/sprite->
h;
789 tx, ty, sprite->
srw, sprite->
srh,
c, 0. );
803 double bx,
double by,
804 double bw,
double bh,
const glColour*
c )
817 tx, ty, texture->srw, texture->srh,
c, 0. );
832 double bx,
double by,
double bw,
double bh,
838 scale =
MIN( bw / texture->w, bh / texture->h );
840 nw = scale * texture->w;
841 nh = scale * texture->h;
858 double bx,
double by,
const glColour*
c )
868 0., 0., texture->srw, texture->srh,
c, 0. );
883void gl_renderShader(
double x,
double y,
double w,
double h,
double r,
const SimpleShader *shd,
const glColour *
c,
int center )
885 mat4 projection = gl_view_matrix;
890 glUniform2f( shd->dimensions, w, h );
904 glEnableVertexAttribArray(shd->vertex);
908 gl_uniformColor(shd->color,
c);
910 gl_uniformMat4(shd->projection, H);
912 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
914 glDisableVertexAttribArray(shd->vertex);
929 double r,
const glColour *
c,
int filled )
932 mat4 projection = gl_view_matrix;
950 GLfloat r = H->m[0][0] / gl_view_matrix.m[0][0];
952 glUseProgram( shaders.circle.program );
953 glUniform2f( shaders.circle.dimensions, r, r );
954 glUniform1i( shaders.circle.parami, filled );
968 double x2,
double y2,
const glColour *
c )
970 double a = atan2( y2-y1, x2-x1 );
971 double s = hypotf( x2-x1, y2-y1 );
973 glUseProgram(shaders.sdfsolid.program);
974 glUniform1f(shaders.sdfsolid.paramf, 1.);
975 gl_renderShader( (x1+x2)/2., (y1+y2)/2., s/2.+0.5, 1.0, a, &shaders.sdfsolid,
c, 1 );
988 double rx, ry, rw, rh;
993 glScissor( rx, ry, rw, rh );
994 glEnable( GL_SCISSOR_TEST );
1002 glDisable( GL_SCISSOR_TEST );
1059 vertex[0] = 0.5*cos(4.*M_PI/3.);
1060 vertex[1] = 0.5*sin(4.*M_PI/3.);
1061 vertex[2] = 0.5*cos(0.);
1062 vertex[3] = 0.5*sin(0.);
1063 vertex[4] = 0.5*cos(2.*M_PI/3.);
1064 vertex[5] = 0.5*sin(2.*M_PI/3.);
1065 vertex[6] = vertex[0];
1066 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_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.