add function to CommonRenderInterface to update/activate a texture

add example how to render a texture 2d quad in SimpleOpenGL3
This commit is contained in:
Erwin Coumans
2016-04-20 12:55:21 -07:00
parent da948a0729
commit 4e61f0cab9
5 changed files with 93 additions and 26 deletions

View File

@@ -134,7 +134,12 @@ extern int gShapeIndex;
struct InternalTextureHandle
{
GLuint m_glTexture;
int m_width;
int m_height;
};
@@ -148,7 +153,7 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
GLuint m_defaultTexturehandle;
b3AlignedObjectArray<GLuint> m_textureHandles;
b3AlignedObjectArray<InternalTextureHandle> m_textureHandles;
GLRenderToTexture* m_shadowMap;
GLuint m_shadowTexture;
@@ -518,9 +523,9 @@ int GLInstancingRenderer::registerGraphicsInstance(int shapeIndex, const float*
int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width, int height)
{
b3Assert(glGetError() ==GL_NO_ERROR);
glActiveTexture(GL_TEXTURE0);
int textureIndex = m_data->m_textureHandles.size();
const GLubyte* image= (const GLubyte*)texels;
const GLubyte* image= (const GLubyte*)texels;
GLuint textureHandle;
glGenTextures(1,(GLuint*)&textureHandle);
glBindTexture(GL_TEXTURE_2D,textureHandle);
@@ -528,18 +533,51 @@ int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width
b3Assert(glGetError() ==GL_NO_ERROR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image);
b3Assert(glGetError() ==GL_NO_ERROR);
glGenerateMipmap(GL_TEXTURE_2D);
b3Assert(glGetError() ==GL_NO_ERROR);
InternalTextureHandle h;
h.m_glTexture = textureHandle;
h.m_width = width;
h.m_height = height;
m_data->m_textureHandles.push_back(textureHandle);
m_data->m_textureHandles.push_back(h);
return textureIndex;
}
void GLInstancingRenderer::updateTexture(int textureIndex, const unsigned char* texels)
{
if (textureIndex>=0)
{
glActiveTexture(GL_TEXTURE0);
b3Assert(glGetError() ==GL_NO_ERROR);
InternalTextureHandle& h = m_data->m_textureHandles[textureIndex];
glBindTexture(GL_TEXTURE_2D,h.m_glTexture);
b3Assert(glGetError() ==GL_NO_ERROR);
const GLubyte* image= (const GLubyte*)texels;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, h.m_width,h.m_height,0,GL_RGB,GL_UNSIGNED_BYTE,image);
b3Assert(glGetError() ==GL_NO_ERROR);
glGenerateMipmap(GL_TEXTURE_2D);
b3Assert(glGetError() ==GL_NO_ERROR);
}
}
void GLInstancingRenderer::activateTexture(int textureIndex)
{
glActiveTexture(GL_TEXTURE0);
if (textureIndex>=0)
{
glBindTexture(GL_TEXTURE_2D,m_data->m_textureHandles[textureIndex].m_glTexture);
} else
{
glBindTexture(GL_TEXTURE_2D,0);
}
}
void GLInstancingRenderer::updateShape(int shapeIndex, const float* vertices)
{
b3GraphicsInstance* gfxObj = m_graphicsInstances[shapeIndex];
@@ -559,7 +597,7 @@ int GLInstancingRenderer::registerShape(const float* vertices, int numvertices,
if (textureId>=0)
{
gfxObj->m_texturehandle = m_data->m_textureHandles[textureId];
gfxObj->m_texturehandle = m_data->m_textureHandles[textureId].m_glTexture;
}
gfxObj->m_primitiveType = primitiveType;