Last active
January 14, 2022 03:39
-
-
Save layneson/80fdae7b71cb145415115caf29ebda27 to your computer and use it in GitHub Desktop.
OpenGL shader texture issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 400 | |
uniform usamplerBuffer u_indexer; | |
uniform sampler2D u_tex; | |
out vec4 out_color; | |
void main() { | |
// THIS IS WHAT I WANT TO WORK: | |
uint index = texelFetch(u_indexer, 0).r; | |
out_color = vec4(texture(u_tex, vec2(float(index) / 3.0, 0)).rgb, 1); | |
// THIS WORKS: | |
out_color = vec4(texture(u_tex, vec2(0.34, 0)).rgb, 1); | |
// AND THIS WORKS: | |
uint index = texelFetch(u_indexer, 0).r; | |
if (index == 0) out_color = vec4(1, 0, 0, 1); | |
if (index == 1) out_color = vec4(0, 1, 0, 1); | |
if (index == 2) out_color = vec4(0, 0, 1, 1); | |
// THIS DOESN'T WORK: | |
uint index = texelFetch(u_indexer, 0).r; | |
if (gl_FragCoord.x < 300) { | |
if (index == 0) out_color = vec4(1, 0, 0, 1); | |
if (index == 1) out_color = vec4(0, 1, 0, 1); | |
if (index == 2) out_color = vec4(0, 0, 1, 1); | |
} else { | |
out_color = vec4(texture(u_tex, vec2(0.34, 0)).rgb, 1); | |
} | |
// NEITHER DOES THIS: | |
if (gl_FragCoord.x < 300) { | |
uint index = texelFetch(u_indexer, 0).r; | |
if (index == 0) out_color = vec4(1, 0, 0, 1); | |
if (index == 1) out_color = vec4(0, 1, 0, 1); | |
if (index == 2) out_color = vec4(0, 0, 1, 1); | |
} else { | |
out_color = vec4(texture(u_tex, vec2(0.34, 0)).rgb, 1); | |
} | |
// THIS DOESN'T WORK... | |
uint index = texelFetch(u_indexer, 0).r; | |
out_color = vec4(texture(u_tex, vec2(float(index) / 3.0, 0)).rgb, 1); | |
if (index >= 0) out_color = vec4(1, 1, 1, 1); // I don't see a white screen... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GL/glew.h> | |
#include <GLFW/glfw3.h> | |
#include <stdio.h> | |
int main() { | |
if (!glfwInit()) return 1; | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); | |
auto* window = glfwCreateWindow(800, 800, "Texture Issue", NULL, NULL); | |
if (!window) return 1; | |
glfwMakeContextCurrent(window); | |
if (glewInit() != GLEW_OK) return 1; | |
auto program = glCreateProgram(); | |
auto vertex_shader = glCreateShader(GL_VERTEX_SHADER); | |
static char vertex_shader_source[1024*5] = { 0 }; | |
int vertex_shader_source_len = 1024*5; | |
{ | |
auto file = fopen("vertex.glsl", "r"); | |
vertex_shader_source_len = fread(vertex_shader_source, 1, vertex_shader_source_len, file); | |
fclose(file); | |
} | |
char* vss = vertex_shader_source; | |
glShaderSource(vertex_shader, 1, &vss, &vertex_shader_source_len); | |
glCompileShader(vertex_shader); | |
int vs_compile_status; | |
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vs_compile_status); | |
if (vs_compile_status != GL_TRUE) return 1; | |
auto fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); | |
static char fragment_shader_source[1024*5] = { 0 }; | |
int fragment_shader_source_len = 1024*5; | |
{ | |
auto file = fopen("fragment.glsl", "r"); | |
fragment_shader_source_len = fread(fragment_shader_source, 1, fragment_shader_source_len, file); | |
fclose(file); | |
} | |
char* fss = fragment_shader_source; | |
glShaderSource(fragment_shader, 1, &fss, &fragment_shader_source_len); | |
glCompileShader(fragment_shader); | |
int fs_compile_status; | |
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fs_compile_status); | |
if (fs_compile_status != GL_TRUE) return 1; | |
glAttachShader(program, vertex_shader); | |
glAttachShader(program, fragment_shader); | |
glLinkProgram(program); | |
int link_status; | |
glGetProgramiv(program, GL_LINK_STATUS, &link_status); | |
if (link_status != GL_TRUE) return 1; | |
unsigned int vao; | |
glGenVertexArrays(1, &vao); | |
float pos[] = { | |
-1, -1, | |
1, -1, | |
1, 1, | |
1, 1, | |
-1, 1, | |
-1, -1, | |
}; | |
unsigned int pos_vbo; | |
glGenBuffers(1, &pos_vbo); | |
glBindVertexArray(vao); | |
glBindBuffer(GL_ARRAY_BUFFER, pos_vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(pos), pos, GL_STATIC_DRAW); | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0); | |
glEnableVertexAttribArray(0); | |
int indexer_loc = glGetUniformLocation(program, "u_indexer"); | |
glUniform1i(indexer_loc, 0); | |
int tex_loc = glGetUniformLocation(program, "u_tex"); | |
glUniform1i(tex_loc, 1); | |
// This value selects which of the three pixels of the texture to use. | |
unsigned char indexer[] = { 2 }; | |
unsigned int indexer_tbo; | |
glGenBuffers(1, &indexer_tbo); | |
glBindBuffer(GL_TEXTURE_BUFFER, indexer_tbo); | |
glBufferData(GL_TEXTURE_BUFFER, sizeof(indexer), indexer, GL_STATIC_DRAW); | |
unsigned int indexer_tex; | |
glGenTextures(1, &indexer_tex); | |
glBindTexture(GL_TEXTURE_BUFFER, indexer_tex); | |
glTexBuffer(GL_TEXTURE_BUFFER, GL_R8I, indexer_tbo); | |
unsigned int tex; | |
glGenTextures(1, &tex); | |
// Three pixels. 8-bit RGB. | |
unsigned char tex_data[] = { | |
255, 0, 0, | |
0, 255, 0, | |
0, 0, 255, | |
}; | |
glBindTexture(GL_TEXTURE_2D, tex); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 3, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
int ww, wh; | |
glfwGetWindowSize(window, &ww, &wh); | |
glViewport(0, 0, ww, wh); | |
while (!glfwWindowShouldClose(window)) { | |
glfwPollEvents(); | |
glClearColor(0, 0, 0, 1); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glActiveTexture(GL_TEXTURE0); | |
glBindTexture(GL_TEXTURE_BUFFER, indexer_tex); | |
glActiveTexture(GL_TEXTURE1); | |
glBindTexture(GL_TEXTURE_2D, tex); | |
glUseProgram(program); | |
glBindVertexArray(vao); | |
glDrawArrays(GL_TRIANGLES, 0, 6); | |
glfwSwapBuffers(window); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 400 | |
layout(location = 0) in vec2 in_pos; | |
uniform usamplerBuffer u_indexer; | |
uniform sampler2D u_tex; | |
void main() { | |
gl_Position = vec4(in_pos.x, -in_pos.y, 0, 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment