Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diamond-msc/7167ba71776e48cdb150 to your computer and use it in GitHub Desktop.
Save diamond-msc/7167ba71776e48cdb150 to your computer and use it in GitHub Desktop.
CubeRenderer.cpp modified to demonstrate https://github.com/MSOpenTech/angle/issues/4
#include "pch.h"
#include "CubeRenderer.h"
typedef struct
{
// Handle to a program object
GLuint programObject;
// Attribute locations
GLint positionLoc;
GLint texCoordLoc;
// Sampler location
GLint samplerLoc;
// Texture handle
GLuint textureId;
} UserData;
// Texture object handle
GLuint textureId;
// 2x2 Image, 3 bytes per pixel (R, G, B)
GLubyte pixels[4 * 3] =
{
255, 0, 0, // Red
0, 255, 0, // Green
0, 0, 255, // Blue
255, 255, 0 // Yellow
};
///
// Create a simple 2x2 texture image with four different colors
//
GLuint CreateSimpleTexture2D()
{
// Use tightly packed data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate a texture object
glGenTextures(1, &textureId);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D, textureId);
// Load the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
return textureId;
}
UserData * userData = new UserData;
///
// Initialize the shader and program object
//
int Init()
{
// Get the attribute locations
userData->positionLoc = glGetAttribLocation(userData->programObject, "a_position");
userData->texCoordLoc = glGetAttribLocation(userData->programObject, "a_texCoord");
// Get the sampler location
userData->samplerLoc = glGetUniformLocation(userData->programObject, "s_texture");
// Load the texture
userData->textureId = CreateSimpleTexture2D();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
return TRUE;
}
///
// Draw a triangle using the shader pair created in Init()
//
void Draw()
{
GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-0.5f, -0.5f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
0.5f, -0.5f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
0.5f, 0.5f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(userData->programObject);
// Load the vertex position
glVertexAttribPointer(userData->positionLoc, 3, GL_FLOAT,
GL_FALSE, 5 * sizeof(GLfloat), vVertices);
// Load the texture coordinate
glVertexAttribPointer(userData->texCoordLoc, 2, GL_FLOAT,
GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3]);
glEnableVertexAttribArray(userData->positionLoc);
glEnableVertexAttribArray(userData->texCoordLoc);
// Bind the texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, userData->textureId);
// Set the sampler texture unit to 0
glUniform1i(userData->samplerLoc, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
//eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}
const char vShaderStr[] =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
const char fShaderStr[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D( s_texture, v_texCoord );\n"
"} \n";
CubeRenderer::CubeRenderer(): m_loadingComplete(false)
{
}
// The GL Context is ready. Load your resources
void CubeRenderer::CreateGLResources()
{
m_colorProgram = LoadProgram(vShaderStr, fShaderStr);
userData->programObject = m_colorProgram;
Init();
a_positionColor = glGetAttribLocation(m_colorProgram, "a_position");
a_colorColor = glGetAttribLocation(m_colorProgram, "a_color");
u_mvpColor = glGetUniformLocation(m_colorProgram, "u_mvp");
m_currentColor = 0;
m_loadingComplete = true;
}
void CubeRenderer::UpdatePerspectiveMatrix()
{
}
// called when the device rotation has changed
void CubeRenderer::OnOrientationChanged()
{
AngleBase::OnOrientationChanged();
UpdatePerspectiveMatrix();
}
void CubeRenderer::Update(float timeTotal, float timeDelta)
{
pixels[2] = rand() % 2 * 255;
glBindTexture(GL_TEXTURE_2D, textureId);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// expected: only the top left color flickers red/purple
}
// render your OpenGL scene
void CubeRenderer::OnRender()
{
glClearColor(0.439f, 0.098f, 0.098f, 1.000f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(m_colorProgram);
Draw();
}
void CubeRenderer::BackgroundColorNext()
{
}
void CubeRenderer::BackgroundColorPrevious()
{
}
GLuint CubeRenderer::LoadProgram(const char *vertShaderSrc, const char *fragShaderSrc)
{
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
// Load the vertex/fragment shaders
vertexShader = LoadShader(GL_VERTEX_SHADER, vertShaderSrc);
if (vertexShader == 0)
return 0;
fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fragShaderSrc);
if (fragmentShader == 0)
{
glDeleteShader(vertexShader);
return 0;
}
// Create the program object
programObject = glCreateProgram();
if (programObject == 0)
return 0;
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
// Link the program
glLinkProgram(programObject);
// Check the link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if (!linked)
{
GLint infoLen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1)
{
char* infoLog = (char*) malloc(sizeof(char) * infoLen);
glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
//esLogMessage("Error linking program:\n%s\n", infoLog);
free(infoLog);
}
glDeleteProgram(programObject);
return 0;
}
// Free up no longer needed shader resources
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return programObject;
}
GLuint CubeRenderer::LoadShader(GLenum type, const char *shaderSrc)
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = glCreateShader(type);
if (shader == 0)
return 0;
// Load the shader source
glShaderSource(shader, 1, &shaderSrc, NULL);
// Compile the shader
glCompileShader(shader);
// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1)
{
char* infoLog = (char*) malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
//esLogMessage("Error compiling shader:\n%s\n", infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment