Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a basic GLUT example (written on OS X, adapt as needed) that generates two checkerboard textures, loads a shader with two samplers and combines them by tinting each (one red, one blue) and blending. See if this works for you:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;GLUT/glut.h&gt; #include &lt;OpenGL/gl.h&gt; #include &lt;OpenGL/glu.h&gt; #define kTextureDim 64 GLuint t1; GLuint t2; /* adapted from the red book */ GLuint makeCheckTex() { GLubyte image[kTextureDim][kTextureDim][4]; // RGBA storage for (int i = 0; i &lt; kTextureDim; i++) { for (int j = 0; j &lt; kTextureDim; j++) { int c = ((((i &amp; 0x8) == 0) ^ ((j &amp; 0x8)) == 0))*255; image[i][j][0] = (GLubyte)c; image[i][j][1] = (GLubyte)c; image[i][j][2] = (GLubyte)c; image[i][j][3] = (GLubyte)255; } } GLuint texName; glGenTextures(1, &amp;texName); glBindTexture(GL_TEXTURE_2D, texName); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTextureDim, kTextureDim, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); return texName; } void loadShader() { #define STRINGIFY(A) #A const GLchar* source = STRINGIFY( uniform sampler2D tex1; uniform sampler2D tex2; void main() { vec4 s1 = texture2D(tex1, gl_TexCoord[0].st); vec4 s2 = texture2D(tex2, gl_TexCoord[0].st + vec2(0.0625, 0.0625)); gl_FragColor = mix(vec4(1, s1.g, s1.b, 0.5), vec4(s2.r, s2.g, 1, 0.5), 0.5); } ); GLuint program = glCreateProgram(); GLuint shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(shader, 1, &amp;source, NULL); glCompileShader(shader); GLint logLength; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &amp;logLength); if (logLength &gt; 0) { GLchar* log = (GLchar*)malloc(logLength); glGetShaderInfoLog(shader, logLength, &amp;logLength, log); printf("Shader compile log:\n%s\n", log); free(log); } glAttachShader(program, shader); glLinkProgram(program); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &amp;logLength); if (logLength &gt; 0) { GLchar* log = (GLchar*)malloc(logLength); glGetProgramInfoLog(program, logLength, &amp;logLength, log); printf("Program link log:\n%s\n", log); free(log); } GLuint t1Location = glGetUniformLocation(program, "tex1"); GLuint t2Location = glGetUniformLocation(program, "tex2"); glUniform1i(t1Location, 0); glUniform1i(t2Location, 1); glUseProgram(program); } void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_FLAT); t1 = makeCheckTex(); t2 = makeCheckTex(); loadShader(); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, t1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, t2); glBegin(GL_QUADS); //lower left glTexCoord2f(0, 0); glVertex2f(-1.0, -1.0); //upper left glTexCoord2f(0, 1.0); glVertex2f(-1.0, 1.0); //upper right glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0); //lower right glTexCoord2f(1.0, 0); glVertex2f(1.0, -1.0); glEnd(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2, 2, -2, 2, -2, 2); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char **argv) { glutInit(&amp;argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); glutInitWindowSize(512, 512); glutInitWindowPosition(0, 0); glutCreateWindow("GLSL Texture Blending"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(display); init(); glutMainLoop(); return 0; } </code></pre> <p>Hopefully the result will look something like this (you can comment out the <code>glUseProgram</code> call to see the first texture drawn without the shader): <img src="https://i.stack.imgur.com/nqQTQ.png" alt="alt text"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload