Shading Programm not working ("he" ignores it)

Hi,
i am new to OpenGL programming, but i am not new to C++!

I tried to get shaders working with this TuT: en.wikipedia.org/wiki/Vertex_Buffer_Object
The code compiles right and I can see a white cube(i edited data und some functions for initialising more veritces…)!
I thought, that everything is working, but if i change the color in the FragmentShader.frag, it dont changes. Its always white :confused:
I also can remove all inside the shader file… It dont crashes… It were still white.
But if i delete the Shader Files its crashing…
I dont know whats wrong.

Here is the code:


const GLfloat data[] =
{
	//x		y	  z

	//back
	-1.0,  1.0, -1.0, //top, left
	 1.0,  1.0, -1.0, //top, right
	 1.0, -1.0, -1.0, //bottom, right
	-1.0, -1.0, -1.0, //bottom, left

	//left
	-1.0,  1.0, -1.0, //top, left
	-1.0,  1.0,  1.0, //top, right
	-1.0, -1.0,  1.0, //bottom, right
	-1.0, -1.0, -1.0, //bottom, left

	//fore
	-1.0,  1.0,  1.0, //top, left
	 1.0,  1.0,  1.0, //top, right
	 1.0, -1.0,  1.0, //bottom, right
	-1.0, -1.0,  1.0, //bottom, left

	//top
	-1.0,  1.0, -1.0, //top, left
	 1.0,  1.0, -1.0, //top, right
	 1.0,  1.0,  1.0, //bottom, right
	-1.0,  1.0,  1.0, //bottom, left

	//bottom
	-1.0, -1.0, -1.0, //top, left
	 1.0, -1.0, -1.0, //top, right
	 1.0, -1.0,  1.0, //bottom, right
	-1.0, -1.0,  1.0, //bottom, left

	//right
	 1.0,  1.0,  1.0, //top, left
	 1.0,  1.0, -1.0, //top, right
	 1.0, -1.0, -1.0, //bottom, right
	 1.0, -1.0,  1.0  //bottom, left
};

void OpenGL::InitShaderProgramm()
{
	/* Assign our program handle a "name" */
	shaderProgram = glCreateProgram();

	/* Attach our shaders to our program */
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);

	/* Bind attribute index 0 (shaderAttribute) to in_Position*/
	/* "in_Position" will represent "data" array's contents in the vertex shader */
	glBindAttribLocation(shaderProgram, shaderAttribute, "position");
	
	/* Link shader program*/
	glLinkProgram(shaderProgram);
}

char* filetobuf(char *file)
{
	FILE *fptr;
	long length;
	char *buf;

	fptr = fopen(file, "rb"); /* Open file for reading */
	if (!fptr) /* Return NULL on failure */
		return NULL;
	fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */
	length = ftell(fptr); /* Find out how many bytes into the file we are */
	buf = (char*)malloc(length + 1); /* Allocate a buffer for the entire length of the file and a null terminator */
	fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */
	fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */
	fclose(fptr); /* Close the file */
	buf[length] = 0; /* Null terminator */

	return buf; /* Return the buffer */
}

void OpenGL::InitShaders()
{
	/* Read our shaders into the appropriate buffers */
	vertexSource = filetobuf("VertexShader.vert");
	fragmentSource = filetobuf("FragmentShader.frag");

	/* Assign our handles a "name" to new shader objects */
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

	/* Associate the source code buffers with each handle */
	glShaderSource(vertexShader, 1, (const GLchar**)&vertexSource, 0);
	glShaderSource(fragmentShader, 1, (const GLchar**)&fragmentSource, 0);

	/* Free the temporary allocated memory */
	free(vertexSource);
	free(fragmentSource);

	/* Compile our shader objects */
	glCompileShader(vertexShader);
	glCompileShader(fragmentShader);
}

void OpenGL::InitVBO()
{
	/* Create a new VBO and use the variable "VBO" to store the VBO id */
	glGenBuffers(1, &VBO);

	/* Make the new VBO active */
	glBindBuffer(GL_ARRAY_BUFFER, VBO);

	/* Upload vertex data to the video device */
	glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

	/* Specify that our coordinate data is going into attribute index 0(shaderAttribute), and contains three floats per vertex */
	glVertexAttribPointer(shaderAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);

	/* Enable attribute index 0(shaderAttribute) as being used */
	glEnableVertexAttribArray(shaderAttribute);

	/* Make the new VBO active. */
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
}

void OpenGL::Init()
{
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);   //sets up the display mode
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("OpenGl Engine");                  //creates a window
	glutDisplayFunc(MainDraw);                                    //specifies our redraw function
	glutKeyboardFunc(processNormalKeys);
	if (glewInit() != GLEW_OK)
		return;
	this->InitVBO();
	this->InitShaders();
	this->InitShaderProgramm();
	/* Set shader program as being actively used */
	glUseProgram(shaderProgram);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Red, Green, Blue, Alpha (bgclolor)
	glEnable(GL_DEPTH_TEST); // enable Z-Buffer
	glMatrixMode(GL_MODELVIEW); // chose stack
	glLoadIdentity(); // reset matrix
	//glOrtho(-2, 2, 1.5, -1.5, -1, 1); // set matrix visual range (make z buffer useless)
	glFrustum(-1.6, 1.6, -1.2, 1.2, 1.5, 6.5); // set Clipping
												/*Near-Clipping has a distance of 1.5
												  it is 3.2 * 2.4 big
												  Far-Clipping has a distance of 6.5*/

	glTranslatef(0, 0, -3.5); // => movelike x, y, z (cam pos)
	//glRotatef(60, 1, 1, 0); // =>rotate 60 degree on axe (1,1,0) x,y,z
}

mfg
lolxdfly

EDIT:
I had a syntax error in the VertexShader.vert… now it is working a bit more :slight_smile:
The one and only problem is now, that the complete screen is white, so the shader were excuted for the complete window and not for my object :confused:
How can i bin the shader to my object??
Or is the shader file wrong??


//vertex
#version 410 // Specify which version of GLSL we are using.
 
// in_Position was bound to attribute index 0("shaderAttribute")
in  vec3 in_Position;
 
void main() 
{
    gl_Position = vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
}

//fragment
#version 410 // Specify which version of GLSL we are using.
 
precision highp float; // Video card drivers require this line to function properly
 
out vec4 out_color;
 
void main() 
{
    out_color = vec4(0.4, 0.4, 1.0, 1.0); //Set colour of each fragment to WHITE
}

why are you using glBindAttribLocation if you are using a recent version of glsl?


	glBindAttribLocation(shaderProgram, shaderAttribute, "position"); 
        -> glBindAttribLocation(shaderProgram, ShaderAttribute, "in_Position"); 

what you have to do is like this


getAttribLocation();
enableVertexArrayAttribute();
vertexArrayPointerToLocation();

you do not really need that line, why did you put here?


precision highp float; // Video card drivers require this line to function properly