enable a generated texture

This commit is contained in:
Josh Holtrop 2015-10-12 22:24:00 -04:00
parent 872dae3f6e
commit 9be9f3fc69
2 changed files with 45 additions and 8 deletions

48
app.cc
View File

@ -22,6 +22,8 @@ static struct
} uniforms;
glm::mat4 modelview;
glm::mat4 projection;
GLuint texture;
GLubyte texture_data[16][16][4];
#define WIDTH 800
#define HEIGHT 800
@ -30,6 +32,10 @@ bool init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
#if 0
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#endif
glViewport(0, 0, WIDTH, HEIGHT);
try
{
@ -47,12 +53,13 @@ bool init(void)
uniforms.modelview = program->get_uniform_location("modelview");
cube_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW,
{-1, -1, -1, 0, 0, -1, 0, 0,
1, -1, -1, 0, 0, -1, 1, 0,
1, 1, -1, 0, 0, -1, 1, 1,
-1, 1, -1, 0, 0, -1, 0, 1});
{-1, -1, 1, 0, 0, 1, 0, 0,
1, -1, 1, 0, 0, 1, 1, 0,
1, 1, 1, 0, 0, 1, 1, 1,
-1, 1, 1, 0, 0, 1, 0, 1});
cube_array = make_shared<glcxx::Array>();
cube_array->bind();
cube_buffer->bind();
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
@ -60,8 +67,36 @@ bool init(void)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat)));
modelview = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -3));
modelview = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -2));
projection = glm::perspective(60.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 1000.0f);
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
if (((j / 2) & 1) == ((i / 2) & 1))
{
texture_data[i][j][0] = 255;
texture_data[i][j][1] = 255;
texture_data[i][j][2] = 255;
texture_data[i][j][3] = 255;
}
else
{
texture_data[i][j][0] = 0;
texture_data[i][j][1] = 0;
texture_data[i][j][2] = 0;
texture_data[i][j][3] = 0;
}
}
}
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, &texture_data[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
catch (glcxx::Error & e)
{
@ -79,7 +114,8 @@ void display(SDL_Window * window)
cube_array->bind();
glUniform4f(uniforms.ambient, 1.0, 1.0, 1.0, 1.0);
glUniform4f(uniforms.specular, 1.0, 1.0, 1.0, 1.0);
glUniform1f(uniforms.shininess, 1.0);
glUniform1f(uniforms.shininess, 150.0);
glUniform1i(uniforms.tex, 0);
glUniformMatrix4fv(uniforms.modelview, 1, GL_FALSE, &modelview[0][0]);
glUniformMatrix4fv(uniforms.projection, 1, GL_FALSE, &projection[0][0]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

View File

@ -14,7 +14,8 @@ void main(void)
float NdotL, RdotEye;
lightDir = normalize(vec3(-0.1, 0, -0.9));
color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */
vec4 texture_color = texture2D(tex, tex_coord_i);
color = vec4(0.2, 0.2, 0.2, texture_color.a) * ambient; /* ambient light */
n = normalize(normal_i);
NdotL = dot(n, -lightDir);
@ -22,7 +23,7 @@ void main(void)
if (NdotL > 0.0)
{
/* diffuse component */
color += texture2D(tex, tex_coord_i) * NdotL;
color += texture_color * NdotL;
/* specular component */
RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n)));
if (RdotEye > 0.0)