Page 2 of 2

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Wed Nov 06, 2013 12:30 pm
by K-Bal
You compile and link a shader only once. This is some code I wrote for loading some time ago. Hope it helps:

Code: Select all

		void DumpShaderInfo(GLuint shader){
			GLint length;
			GLsizei slength;
			glGetShaderiv(shader, GL_INFO_LOG_LENGTH , &length);
			if(length >= 1){
				std::string txt;
				txt.resize(length);
				glGetShaderInfoLog(shader, length, &slength, (GLchar*)txt.c_str());
				std::cout << txt << std::endl;
			}
		}

		void DumpShaderProgramInfo(GLuint shaderProgram){
			GLint length;
			GLsizei slength;
			int linked;
			glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linked);
			if(!linked){
				glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH , &length);
				if(length >= 1){
					std::string txt;
					txt.resize(length);
					glGetProgramInfoLog(shaderProgram, length, &slength, (GLchar*)txt.c_str());
					std::cout << txt << std::endl;
				}
			}
		}

		GLuint LoadShader(const std::string& vertex, const std::string& fragment){
			GLuint program = glCreateProgram();
			// Vertex
			GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
			const char* vertex_c_str = vertex.c_str();
			int vertex_length = int(vertex.length());
			glShaderSource(vertexShader, 1, &vertex_c_str, &vertex_length);
			glCompileShader(vertexShader);
			DumpShaderInfo(vertexShader);
			glAttachShader(program, vertexShader);
			glDeleteShader(vertexShader);
			// Fragment
			GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
			const char* fragment_c_str = fragment.c_str();
			int fragment_length = int(fragment.length());
			glShaderSource(fragmentShader, 1, &fragment_c_str, &fragment_length);
			glCompileShader(fragmentShader);
			DumpShaderInfo(fragmentShader);
			glAttachShader(program, fragmentShader);
			glDeleteShader(fragmentShader);
			// Linkage
			glLinkProgram(program);
			DumpShaderProgramInfo(program);
			glutReportErrors();
			return program;
		}

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Wed Nov 06, 2013 2:40 pm
by Benjamin100
Thanks.
I had already compiled the shaders, that wasn't the problem.
I did a little test and found out that it had used the same text in the vertex shader as the fragment shader. I'm not sure why. It looks like they are set to reading different text files. Once I put the vertex shader source in the source code itself, without using the reading function I made, it worked fine.
Appears that the fragment shader wasn't doing anything because of the trouble with the vertex shader.
After I fixed the vertex shader the triangle turned green like I wanted the fragment shader to do.
Basically, I just have to fix the file reading function. It is probably something obvious I am overlooking.
Thanks for all the help anyways.

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Wed Nov 06, 2013 2:48 pm
by K-Bal
Use the dump functions I provided. You will notice such errors much quicker.

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Fri Nov 08, 2013 12:08 pm
by Benjamin100
Well, it seems the file reading function does work... sometimes.

My problem is that these two outputs SHOULD look identical, but they don't;

Code: Select all

const GLchar* SOURCE_FOR_VERTEX_SHADER= readFile("TriangleVertexShader.txt");
const GLchar* SOURCE_FOR_FRAGMENT_SHADER= readFile("TriangleFragmentShader.txt"); 
cout<<SOURCE_FOR_VERTEX_SHADER <<endl<< SOURCE_FOR_FRAGMENT_SHADER; //Why doesn't this....
cout<<endl;
cout<<readFile("TriangleVertexShader.txt")<<endl<<readFile("TriangleFragmentShader.txt");  //look identical to this?
The first pair of outputs both only show the fragment shader, and the second pair both only show the vertex shader.
It doesn't make any sense.

EDIT: I figured out it was the file reading function. I was using a weird way to convert strings to const char*.
I didn't know I could use the "c_str()" method. All better now.

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Mon Nov 11, 2013 11:55 am
by Benjamin100
Well,
I finally got a triangle colored, with diffuse lighting and in perspective.
Feels like such an accomplishment.
Thanks for all the help everybody.
EDIT: I know this is hardly much of an accomplishment, but it is certainly more than I could have done without you.

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Wed Nov 13, 2013 10:23 am
by Benjamin100
I'm having trouble with the attribute for my vertex position.
I'm not really sure how I'm suppose to use it in the vertex shader.
I'm doing a translation backwards, so it is moving, (the model view matrix). It was working fine with "gl_Vertex" but now I'm trying to replace "gl_Vertex" with my attribute for the vertex position. It isn't working. No errors with binding uniforms or attributes.
The screen comes up with the clearing color, black, as opposed to white which it often does when a binding error shows up.

Here is the vertex shader;

Code: Select all

varying vec3 normal;
varying vec3 lightVector;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
attribute vec3 vertexPosition;
void main()
{
   gl_Position = modelViewProjectionMatrix*vec4(vertexPosition,1.0f);
   normal=gl_NormalMatrix* gl_Normal;
   vec4  modelViewVertex=modelViewMatrix*vec4(vertexPosition,1.0f);
   lightVector= vec3(gl_LightSource[0].position -modelViewVertex);
 //I know I will probably need to replace "gl_LightSource[0]" later, but it was working fine with "gl_Vertex".
}

Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Fri Nov 15, 2013 9:50 am
by Benjamin100
All right. I replaced the normal matrix and vertex normal.
Now it appears there are errors definitely inside the vertex shader, (errors are coming up with uniforms.)
At least that seems to help narrow it down.
The "transpose" and "inverse" functions are used in the main source code, and that matrix is normalized in the vertex shader and used.
Please tell me if you see anything notably wrong in here. Thanks the assistance;

Code: Select all


//Vertex Shader
varying vec3 normal;
varying vec3 lightVector;
uniform mat4 modelViewMatrix;
uniform mat3 normalMatrix;
uniform mat4 modelViewProjectionMatrix;
attribute vec3 vertexPosition;
void main()
{
   vec4 vertex4=vec4(vertexPosition,1.0f);  
   gl_Position = modelViewProjectionMatrix*vertex4;
   vec3 normalVertex=normalize(vertexPosition);
   normalMatrix=normalize(normalMatrix);
   normal=normalMatrix* normalVertex;
   vec4  modelViewVertex=modelViewMatrix*vertex4;
   lightVector= vec3(gl_LightSource[0].position -modelViewVertex);
}


Re: The Secret that is OpenGL - Getting Started/Resources

Posted: Mon Nov 18, 2013 9:31 pm
by qpHalcy0n
Would need to see your client code (C/C++) that does the attribute binding. There are a whole slew of generic and non-generic semantic binding functions that aren't always compatible or implemented on specific hardware targets. This is really just an atrociously horrid implementation of high level GPU code on the Khronos's part and their bid to clean it up to compete with the comparatively sparkling clean MS HLSL. So I wouldn't blame you if this is causing frustration...