Skip to content

Commit

Permalink
OpenGL|Client: Adapting old drawing code for OpenGL 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Apr 23, 2017
1 parent 30df3c5 commit 1598fa7
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 170 deletions.
1 change: 0 additions & 1 deletion doomsday/apps/api/api_gl.h
Expand Up @@ -126,7 +126,6 @@ typedef enum dglprimtype_e {
DGL_TRIANGLE_FAN,
DGL_TRIANGLE_STRIP,
DGL_QUADS,
DGL_QUAD_STRIP,
DGL_POINTS,
} dglprimtype_t;

Expand Down
2 changes: 2 additions & 0 deletions doomsday/apps/client/include/gl/gl_main.h
Expand Up @@ -318,6 +318,8 @@ void GL_CalcLuminance(uint8_t const *buffer, int width, int height, int comps,
void DGL_AssertNotInPrimitive(void);

de::Matrix4f DGL_Matrix(DGLenum matrixMode);
void DGL_CurrentColor(DGLubyte *rgba);
void DGL_CurrentColor(float *rgba);

// Console commands.
D_CMD(UpdateGammaRamp);
Expand Down
@@ -1,5 +1,5 @@
# Shader for the DGL drawing routines that emulate OpenGL 1.x behavior.
shader dgl.draw {
vertex.path = "dgl_draw.vsh"
fragment.path = "dgl_draw.fsh"
path.vertex = "dgl_draw.vsh"
path.fragment = "dgl_draw.fsh"
}
Expand Up @@ -20,7 +20,7 @@

#version 330

layout(location = 0) in vec3 aVertex;
layout(location = 0) in vec4 aVertex;
layout(location = 1) in vec4 aColor;
layout(location = 2) in vec2 aTexCoord[3];

Expand Down
29 changes: 15 additions & 14 deletions doomsday/apps/client/src/gl/dgl_common.cpp
Expand Up @@ -458,28 +458,28 @@ dd_bool DGL_GetIntegerv(int name, int *v)
break;

case DGL_CURRENT_COLOR_R:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = int(color[0] * 255);
break;

case DGL_CURRENT_COLOR_G:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = int(color[1] * 255);
break;

case DGL_CURRENT_COLOR_B:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = int(color[2] * 255);
break;

case DGL_CURRENT_COLOR_A:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = int(color[3] * 255);
break;

case DGL_CURRENT_COLOR_RGBA:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
for(int i = 0; i < 4; ++i)
DGL_CurrentColor(color);
for (int i = 0; i < 4; ++i)
{
v[i] = int(color[i] * 255);
}
Expand Down Expand Up @@ -530,31 +530,31 @@ dd_bool DGL_GetFloatv(int name, float *v)
DENG_ASSERT_GL_CONTEXT_ACTIVE();

float color[4];
switch(name)
switch (name)
{
case DGL_CURRENT_COLOR_R:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = color[0];
break;

case DGL_CURRENT_COLOR_G:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = color[1];
break;

case DGL_CURRENT_COLOR_B:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = color[2];
break;

case DGL_CURRENT_COLOR_A:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
DGL_CurrentColor(color);
*v = color[3];
break;

case DGL_CURRENT_COLOR_RGBA:
LIBGUI_GL.glGetFloatv(GL_CURRENT_COLOR, color);
for(int i = 0; i < 4; ++i)
DGL_CurrentColor(color);
for (int i = 0; i < 4; ++i)
{
v[i] = color[i];
}
Expand Down Expand Up @@ -651,7 +651,8 @@ int DGL_Enable(int cap)
break;

case DGL_POINT_SMOOTH:
Deferred_glEnable(GL_POINT_SMOOTH);
//Deferred_glEnable(GL_POINT_SMOOTH);
// TODO: Not needed?
break;

default:
Expand Down
97 changes: 70 additions & 27 deletions doomsday/apps/client/src/gl/dgl_draw.cpp
Expand Up @@ -59,8 +59,8 @@ struct DGLDrawState
NUM_VERTEX_ATTRIB_ARRAYS
};

int primLevel = 0;
dglprimtype_t currentPrimitive = DGL_NO_PRIMITIVE;
dglprimtype_t primType = DGL_NO_PRIMITIVE;
int primIndex = 0;
QVector<Vertex> vertices;

struct GLData
Expand All @@ -70,6 +70,7 @@ struct DGLDrawState
GLUniform uTextureMatrix { "uTextureMatrix", GLUniform::Mat4 };
GLUniform uEnabledTextures { "uEnabledTextures", GLUniform::Int };
GLuint vertexArray = 0;
GLBuffer buffer;
};
std::unique_ptr<GLData> gl;

Expand All @@ -81,12 +82,39 @@ struct DGLDrawState
void commitVertex()
{
vertices.append(vertices.last());
++primIndex;

if (primType == DGL_QUADS)
{
if (primIndex == 4)
{
// 4 vertices become 6.
//
// 0--1 0--1 5
// | | => \ | |\
// | | \| | \
// 3--2 2 4--3

vertices.append(vertices.last());
vertices.append(vertices.last());

// 0 1 2 3 3 3 X
int const N = vertices.size();
vertices[N - 4] = vertices[N - 5];
vertices[N - 2] = vertices[N - 7];

primIndex = 0;
}
}
}

void clearVertices()
{
Vertex const last = (vertices.isEmpty()? Vertex() : vertices.last());
vertices.clear();
vertices.append(Vertex());
vertices.append(last);
primIndex = 0;
primType = DGL_NO_PRIMITIVE;
}

int numVertices() const
Expand All @@ -111,21 +139,16 @@ struct DGLDrawState

void beginPrimitive(dglprimtype_t primitive)
{
// We enter a Begin/End section.
primLevel++;
DENG2_ASSERT(primType == DGL_NO_PRIMITIVE);

DENG2_ASSERT(currentPrimitive == DGL_NO_PRIMITIVE);
currentPrimitive = primitive;
// We enter a Begin/End section.
primType = primitive;
}

void endPrimitive()
{
DENG2_ASSERT(primLevel > 0);
DENG2_ASSERT(currentPrimitive != DGL_NO_PRIMITIVE);

if (primLevel > 0)
if (primType != DGL_NO_PRIMITIVE)
{
primLevel--;
drawPrimitives();
}
clearVertices();
Expand Down Expand Up @@ -175,36 +198,46 @@ struct DGLDrawState
uint const stride = sizeof(Vertex);
auto &GL = LIBGUI_GL;

// Upload the vertex data.
gl->buffer.setData(&vertices[0], sizeof(Vertex) * vertices.size(), gl::Dynamic);

GL.glBindVertexArray(gl->vertexArray);
LIBGUI_ASSERT_GL_OK();

// Updated pointers.
GL.glVertexAttribPointer(VAA_VERTEX, 3, GL_FLOAT, GL_FALSE, stride, &vertices[0].vertex);
GL.glVertexAttribPointer(VAA_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE, stride, &vertices[0].color);
GL.glVertexAttribPointer(VAA_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, stride, &vertices[0].texCoord[0]);
GL.glVertexAttribPointer(VAA_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, stride, &vertices[0].texCoord[1]);
GL.glVertexAttribPointer(VAA_TEXCOORD2, 2, GL_FLOAT, GL_FALSE, stride, &vertices[0].texCoord[2]);
GL.glBindBuffer(GL_ARRAY_BUFFER, gl->buffer.glName());
LIBGUI_ASSERT_GL_OK();

Vertex const *basePtr = nullptr;

// Updated pointers.
GL.glVertexAttribPointer(VAA_VERTEX, 3, GL_FLOAT, GL_FALSE, stride, &basePtr->vertex);
GL.glVertexAttribPointer(VAA_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE, stride, &basePtr->color);
GL.glVertexAttribPointer(VAA_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, stride, &basePtr->texCoord[0]);
GL.glVertexAttribPointer(VAA_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, stride, &basePtr->texCoord[1]);
GL.glVertexAttribPointer(VAA_TEXCOORD2, 2, GL_FLOAT, GL_FALSE, stride, &basePtr->texCoord[2]);
LIBGUI_ASSERT_GL_OK();

GL.glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void glUnbindArrays()
{
LIBGUI_GL.glBindVertexArray(0);
LIBGUI_ASSERT_GL_OK();
}

GLenum glPrimitive() const
{
switch (currentPrimitive)
switch (primType)
{
case DGL_POINTS: return GL_POINTS;
case DGL_LINES: return GL_LINES;
case DGL_LINE_LOOP: return GL_LINE_LOOP;
case DGL_LINE_STRIP: return GL_LINE_STRIP;
case DGL_QUADS: return GL_QUADS;
case DGL_QUAD_STRIP: return GL_QUAD_STRIP;
case DGL_TRIANGLES: return GL_TRIANGLES;
case DGL_TRIANGLE_FAN: return GL_TRIANGLE_FAN;
case DGL_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
case DGL_QUADS: return GL_TRIANGLES; // converted

case DGL_NO_PRIMITIVE: DENG2_ASSERT(!"No primitive type specified"); return GL_NONE;
}
Expand All @@ -227,6 +260,7 @@ struct DGLDrawState
{
glBindArrays();
LIBGUI_GL.glDrawArrays(glPrimitive(), 0, numVertices());
LIBGUI_ASSERT_GL_OK();
glUnbindArrays();
}
gl->shader.endUse();
Expand All @@ -235,6 +269,17 @@ struct DGLDrawState

static DGLDrawState dglDraw;

void DGL_CurrentColor(DGLubyte *rgba)
{
std::memcpy(rgba, dglDraw.vertex().color.constPtr(), 4);
}

void DGL_CurrentColor(float *rgba)
{
Vector4f colorf = dglDraw.vertex().color.toVector4f() / 255.0;
std::memcpy(rgba, colorf.constPtr(), sizeof(float) * 4);
}

#undef DGL_Color3ub
DENG_EXTERN_C void DGL_Color3ub(DGLubyte r, DGLubyte g, DGLubyte b)
{
Expand Down Expand Up @@ -401,8 +446,7 @@ DENG_EXTERN_C void DGL_Vertices3fctv(int num, const dgl_fct3vertex_t* vec)
#undef DGL_Begin
DENG_EXTERN_C void DGL_Begin(dglprimtype_t mode)
{
if(novideo)
return;
if (novideo) return;

DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
Expand All @@ -412,14 +456,13 @@ DENG_EXTERN_C void DGL_Begin(dglprimtype_t mode)

void DGL_AssertNotInPrimitive(void)
{
DENG_ASSERT(dglDraw.currentPrimitive == DGL_NO_PRIMITIVE);
DENG_ASSERT(dglDraw.primType == DGL_NO_PRIMITIVE);
}

#undef DGL_End
DENG_EXTERN_C void DGL_End(void)
{
if(novideo)
return;
if (novideo) return;

DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
Expand All @@ -437,7 +480,7 @@ DENG_EXTERN_C void DGL_DrawLine(float x1, float y1, float x2, float y2, float r,
#undef DGL_DrawRect
DENG_EXTERN_C void DGL_DrawRect(RectRaw const *rect)
{
if(!rect) return;
if (!rect) return;
GL_DrawRect(Rectanglei::fromSize(Vector2i(rect->origin.xy),
Vector2ui(rect->size.width, rect->size.height)));
}
Expand Down
5 changes: 4 additions & 1 deletion doomsday/apps/client/src/gl/gl_main.cpp
Expand Up @@ -855,6 +855,8 @@ void GL_BindTextureUnmanaged(GLuint glName, gl::Wrapping wrapS, gl::Wrapping wra
{
if(ClientApp::busyRunner().inWorkerThread()) return;

LIBGUI_ASSERT_GL_OK();

if(glName == 0)
{
GL_SetNoTexture();
Expand All @@ -865,7 +867,7 @@ void GL_BindTextureUnmanaged(GLuint glName, gl::Wrapping wrapS, gl::Wrapping wra
DENG_ASSERT_GL_CONTEXT_ACTIVE();

LIBGUI_GL.glBindTexture(GL_TEXTURE_2D, glName);
Sys_GLCheckError();
LIBGUI_ASSERT_GL_OK();

LIBGUI_GL.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_Wrap(wrapS));
LIBGUI_GL.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_Wrap(wrapT));
Expand All @@ -874,6 +876,7 @@ void GL_BindTextureUnmanaged(GLuint glName, gl::Wrapping wrapS, gl::Wrapping wra
{
LIBGUI_GL.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, GL_GetTexAnisoMul(texAniso));
}
LIBGUI_ASSERT_GL_OK();
}

void GL_Bind(GLTextureUnit const &glTU)
Expand Down

0 comments on commit 1598fa7

Please sign in to comment.