PDA

View Full Version : GLSL random segfault



Atem boi
12-16-2016, 03:01 PM
I was making a Julia set, and everything worked fine, but then I moved my vertices code into a vertex shader, and then suddenly I had a segfault at glLinkProgram(shaderProgram);

I have no idea why, as before when I checked the logs for each shader, they compiled fine with no errors, and the same code works completely fine in a different project

Code for compiling shaders/linking


glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vert ices,GL_STATIC_DRAW);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1,&pVertex,NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&pFragment,NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();

glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);

Code for Fragment Shader:


#version 130

uniform sampler1D tex;

uniform vec2 c;
uniform int iter;


void main()
{
vec2 z;
z.x = 3.0 * (gl_TexCoord[0].x - 0.5);
z.y = 2.0 * (gl_TexCoord[0].y - 0.5);

int i;
for (i=0;i<iter;i++)
{
float x = (z.x * z.x - z.y * z.y) + c.x;
float y = (z.y * z.x + z.x * z.y) + c.y;

if ((x * x + y * y) > 4.0) break;
z.x = x;
z.y = y;

}
gl_FragColor = texture1D(tex, (i == iter ? 0.0 : float(i)) / 100.0);
}


Code for Vertex shader:


#version 130

attribute vec4 position;


void main()
{
gl_Position = vec4(position.xyz, 1.0);
}

Atem boi
12-17-2016, 02:08 AM
Alright, so now I've got the code working, but if I attempt to draw with glDrawArrays(...); it doesn't display anything, but does not fail if i manually draw with glBegin(); and glEnd();

This is what I'm using to load the texture, and bind it


glUniform1i(texUniform,0);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL _CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_T,GL _CLAMP_TO_EDGE);
unsigned char *tex = SOIL_load_image("pal.png",&width,&height,0,SOIL_LOAD_RGBA);
if (tex == 0)
{
std::cout << "Error loading texture";
}
glActiveTexture(GL_TEXTURE0 + 0);
glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,600,0,GL_RGBA ,GL_UNSIGNED_BYTE,tex);

john_connor
12-17-2016, 04:27 AM
did you check for any compilation / linking errors ??
did you check for any GL errors ??

https://www.opengl.org/wiki/Shader_Compilation#Example
https://www.opengl.org/wiki/OpenGL_Error


once you call glActiveTexture(...), you have to call (again) glBindTexture(...)

Atem boi
12-17-2016, 04:55 AM
All error logs I have set up return "No Errors"

I'll try what you said though

Edit: I've done what you said but to no avail, same thing



glUniform1i(texUniform,0);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_1D,tex);

Atem boi
12-17-2016, 05:48 AM
Issue's resolved now; even using OpenGL 4.5 hints, GLEW says that glGenFramebuffers isn't present, even though my hardware is physically capable of using FBOs, so I decided to just draw with legacy code instead :doh: