The Secret that is OpenGL - Getting Started/Resources

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

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

Post 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;
		}
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

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

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

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

Post by K-Bal »

Use the dump functions I provided. You will notice such errors much quicker.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

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

Post 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.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

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

Post 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.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

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

Post 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".
}
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

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

Post 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);
}

qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

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

Post 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...
Post Reply