librw/src/gl/gl3immed.cpp

306 lines
7.3 KiB
C++
Raw Normal View History

2020-04-30 17:54:38 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
2017-08-29 10:12:56 +02:00
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
2020-05-06 09:42:07 +02:00
#include "../rwrender.h"
2017-08-29 10:12:56 +02:00
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#include "rwgl3.h"
#include "rwgl3impl.h"
2017-08-29 10:12:56 +02:00
#include "rwgl3shader.h"
namespace rw {
namespace gl3 {
2019-01-10 16:11:39 +00:00
uint32 im2DVbo, im2DIbo;
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
uint32 im2DVao;
#endif
2017-08-29 10:12:56 +02:00
static int32 u_xform;
#define STARTINDICES 10000
#define STARTVERTICES 10000
static Shader *im2dShader;
static AttribDesc im2dattribDesc[3] = {
{ ATTRIB_POS, GL_FLOAT, GL_FALSE, 4,
sizeof(Im2DVertex), 0 },
{ ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4,
sizeof(Im2DVertex), offsetof(Im2DVertex, r) },
{ ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2,
sizeof(Im2DVertex), offsetof(Im2DVertex, u) },
};
static int primTypeMap[] = {
GL_POINTS, // invalid
GL_LINES,
GL_LINE_STRIP,
GL_TRIANGLES,
GL_TRIANGLE_STRIP,
GL_TRIANGLE_FAN,
GL_POINTS
};
void
openIm2D(void)
{
u_xform = registerUniform("u_xform");
2020-05-13 20:46:27 +02:00
#ifdef RW_GLES2
#include "gl2_shaders/im2d_gl2.inc"
#include "gl2_shaders/simple_fs_gl2.inc"
#else
2017-08-29 10:12:56 +02:00
#include "shaders/im2d_gl3.inc"
#include "shaders/simple_fs_gl3.inc"
2020-05-13 20:46:27 +02:00
#endif
2020-05-12 21:31:44 +02:00
const char *vs[] = { shaderDecl, header_vert_src, im2d_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, simple_frag_src, nil };
2020-04-26 19:33:43 +02:00
im2dShader = Shader::create(vs, fs);
assert(im2dShader);
2017-08-29 10:12:56 +02:00
glGenBuffers(1, &im2DIbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW);
2017-08-29 10:12:56 +02:00
glGenBuffers(1, &im2DVbo);
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW);
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
glGenVertexArrays(1, &im2DVao);
glBindVertexArray(im2DVao);
setAttribPointers(im2dattribDesc, 3);
#endif
2017-08-29 10:12:56 +02:00
}
void
closeIm2D(void)
{
glDeleteBuffers(1, &im2DIbo);
glDeleteBuffers(1, &im2DVbo);
im2dShader->destroy();
im2dShader = nil;
}
static Im2DVertex tmpprimbuf[3];
void
im2DRenderLine(void *vertices, int32 numVertices, int32 vert1, int32 vert2)
{
Im2DVertex *verts = (Im2DVertex*)vertices;
tmpprimbuf[0] = verts[vert1];
tmpprimbuf[1] = verts[vert2];
im2DRenderPrimitive(PRIMTYPELINELIST, tmpprimbuf, 2);
}
void
im2DRenderTriangle(void *vertices, int32 numVertices, int32 vert1, int32 vert2, int32 vert3)
{
Im2DVertex *verts = (Im2DVertex*)vertices;
tmpprimbuf[0] = verts[vert1];
tmpprimbuf[1] = verts[vert2];
tmpprimbuf[2] = verts[vert3];
im2DRenderPrimitive(PRIMTYPETRILIST, tmpprimbuf, 3);
}
void
im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
{
GLfloat xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
glBindVertexArray(im2DVao);
#endif
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im2DVertex), vertices);
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
im2dShader->use();
2020-05-15 10:55:01 +02:00
#ifndef RW_GL_USE_VAOS
setAttribPointers(im2dattribDesc, 3);
2020-05-15 10:55:01 +02:00
#endif
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
flushCache();
glDrawArrays(primTypeMap[primType], 0, numVertices);
2020-05-15 10:55:01 +02:00
#ifndef RW_GL_USE_VAOS
disableAttribPointers(im2dattribDesc, 3);
2020-05-15 10:55:01 +02:00
#endif
}
2017-08-29 10:12:56 +02:00
void
im2DRenderIndexedPrimitive(PrimitiveType primType,
void *vertices, int32 numVertices,
void *indices, int32 numIndices)
{
GLfloat xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
glBindVertexArray(im2DVao);
#endif
2017-08-29 10:12:56 +02:00
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, numIndices*2, indices);
2017-08-29 10:12:56 +02:00
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im2DVertex), vertices);
2017-08-29 10:12:56 +02:00
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
im2dShader->use();
2020-05-15 10:55:01 +02:00
#ifndef RW_GL_USE_VAOS
2017-08-29 10:12:56 +02:00
setAttribPointers(im2dattribDesc, 3);
2020-05-15 10:55:01 +02:00
#endif
2017-08-29 10:12:56 +02:00
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
flushCache();
glDrawElements(primTypeMap[primType], numIndices,
GL_UNSIGNED_SHORT, nil);
2020-05-15 10:55:01 +02:00
#ifndef RW_GL_USE_VAOS
2017-08-29 10:12:56 +02:00
disableAttribPointers(im2dattribDesc, 3);
2020-05-15 10:55:01 +02:00
#endif
2017-08-29 10:12:56 +02:00
}
// Im3D
static Shader *im3dShader;
static AttribDesc im3dattribDesc[3] = {
{ ATTRIB_POS, GL_FLOAT, GL_FALSE, 3,
sizeof(Im3DVertex), 0 },
{ ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4,
sizeof(Im3DVertex), offsetof(Im3DVertex, r) },
{ ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2,
sizeof(Im3DVertex), offsetof(Im3DVertex, u) },
};
static uint32 im3DVbo, im3DIbo;
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
static uint32 im3DVao;
#endif
2017-08-29 10:12:56 +02:00
static int32 num3DVertices; // not actually needed here
void
openIm3D(void)
{
2020-05-13 20:46:27 +02:00
#ifdef RW_GLES2
#include "gl2_shaders/im3d_gl2.inc"
#include "gl2_shaders/simple_fs_gl2.inc"
#else
2017-08-29 10:12:56 +02:00
#include "shaders/im3d_gl3.inc"
#include "shaders/simple_fs_gl3.inc"
2020-05-13 20:46:27 +02:00
#endif
2020-05-12 21:31:44 +02:00
const char *vs[] = { shaderDecl, header_vert_src, im3d_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, simple_frag_src, nil };
2020-04-26 19:33:43 +02:00
im3dShader = Shader::create(vs, fs);
assert(im3dShader);
2017-08-29 10:12:56 +02:00
glGenBuffers(1, &im3DIbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW);
2017-08-29 10:12:56 +02:00
glGenBuffers(1, &im3DVbo);
glBindBuffer(GL_ARRAY_BUFFER, im3DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im3DVertex), nil, GL_STREAM_DRAW);
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
glGenVertexArrays(1, &im3DVao);
glBindVertexArray(im3DVao);
setAttribPointers(im3dattribDesc, 3);
#endif
2017-08-29 10:12:56 +02:00
}
void
closeIm3D(void)
{
glDeleteBuffers(1, &im3DIbo);
glDeleteBuffers(1, &im3DVbo);
im3dShader->destroy();
im3dShader = nil;
}
void
2020-05-06 09:22:29 +02:00
im3DTransform(void *vertices, int32 numVertices, Matrix *world, uint32 flags)
2017-08-29 10:12:56 +02:00
{
if(world == nil){
Matrix ident;
ident.setIdentity();
world = &ident;
}
setWorldMatrix(world);
im3dShader->use();
2020-05-06 09:22:29 +02:00
if((flags & im3d::VERTEXUV) == 0)
SetRenderStatePtr(TEXTURERASTER, nil);
2020-05-15 10:55:01 +02:00
#ifdef RW_GL_USE_VAOS
glBindVertexArray(im2DVao);
#endif
2017-08-29 10:12:56 +02:00
glBindBuffer(GL_ARRAY_BUFFER, im3DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im3DVertex), nil, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im3DVertex), vertices);
2020-05-15 10:55:01 +02:00
#ifndef RW_GL_USE_VAOS
2017-08-29 10:12:56 +02:00
setAttribPointers(im3dattribDesc, 3);
2020-05-15 10:55:01 +02:00
#endif
2017-08-29 10:12:56 +02:00
num3DVertices = numVertices;
}
void
2020-04-24 19:06:11 +02:00
im3DRenderPrimitive(PrimitiveType primType)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo);
flushCache();
glDrawArrays(primTypeMap[primType], 0, num3DVertices);
}
void
im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices)
2017-08-29 10:12:56 +02:00
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, numIndices*2, indices);
2017-08-29 10:12:56 +02:00
flushCache();
glDrawElements(primTypeMap[primType], numIndices,
GL_UNSIGNED_SHORT, nil);
}
void
im3DEnd(void)
{
2020-05-15 10:55:01 +02:00
#ifndef RW_GL_USE_VAOS
disableAttribPointers(im3dattribDesc, 3);
#endif
2017-08-29 10:12:56 +02:00
}
}
}
#endif