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;

View File

@@ -63,6 +63,9 @@ public:
virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType=B3_GL_TRIANGLES, int textureIndex=-1);
virtual int registerTexture(const unsigned char* texels, int width, int height);
virtual void updateTexture(int textureIndex, const unsigned char* texels);
virtual void activateTexture(int textureIndex);
///position x,y,z, quaternion x,y,z,w, color r,g,b,a, scaling x,y,z
virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling);

View File

@@ -44,17 +44,7 @@ void InternalOpenGL2RenderCallbacks::display2()
PrimInternalData* data = getData();
glUseProgram(data->m_shaderProg);
float identity[16]={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
glUniformMatrix4fv(data->m_viewmatUniform, 1, false, identity);
glUniformMatrix4fv(data->m_projMatUniform, 1, false, identity);
glBindBuffer(GL_ARRAY_BUFFER, s_vertexBuffer);
glBindVertexArray(s_vertexArrayObject);
assert(glGetError()==GL_NO_ERROR);