how to avoid macro loops

Started by
31 comments, last by rip-off 11 years, 4 months ago

It works with s.append("\n"); no problem.

Also when I try to read the file as santa01 suggested there is something wierd happening. When function gets vertex shader it reads through it and exits just before


    theColor = inColor;
}

and when it reads through fragment shader it read the file entirely and adds some garbage characters at the end (when debugging).

I never used this approach so I might be missing something.

Advertisement

Are you phil67rpg?

If you made changes to your code and you have new errors, post the new code. It helps us not to take shots in the dark when trying to guess what your problem could be.

Anyway it sounds as though you either forgot to add the 0 or you added the 0 but at a fixed position.

I surely hope you did not forget the 0 because I mentioned it twice and it was included in the santa01’s code.

If you are inserting it at a fixed or miscalculated position it would explain why one shader is too short and the other is too long.

Again, nothing but conjecture until we see some code.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This is the code I used


ifstream file(sFile.c_str(), ios::binary);

	file.seekg(0, ios::end);
    int sourceLength = file.tellg(); // warning C4244: 'initializing' : conversion from 'std::streamoff' to 'int', possible loss of data
    GLchar* shaderSource = new GLchar[sourceLength + 1];
 
    file.seekg(0, ios::beg);
    file.read(shaderSource, sourceLength);
    shaderSource[sourceLength] = 0;
 
	file.close();

    uiShader = glCreateShader(a_iType);
    glShaderSource(uiShader, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(uiShader);
 
    delete[] shaderSource;

    GLint compileStatus;
    glGetShaderiv(uiShader, GL_COMPILE_STATUS, &compileStatus);

    if (compileStatus == GL_FALSE)
	{
        GLint infoLogLength;
        glGetShaderiv(uiShader, GL_INFO_LOG_LENGTH, &infoLogLength);
        
        GLchar* infoLog = new GLchar[infoLogLength + 1];
        glGetShaderInfoLog(uiShader, infoLogLength, NULL, infoLog);
        
		ofstream outfile;
		outfile.open ("log.txt");
		outfile << infoLog << endl;
		outfile.close();

        delete[] infoLog;
    }

it is almost exactly the same as santa01's.

You say that santa01's code did include 0 (where is it?)

it is almost exactly the same as santa01's.

You say that santa01's code did include 0 (where is it?)

Its on line:


shaderSource[sourceLength] = 0;

which does present in your sources...

you can always print shaderSource to stdout to see if there is any extra crap in the buffer. its also useful to look through the shaderSource contents in your IDE under debug, IDEs do usually print special character with their symbolic representation like \r\n etc. for instance if you develop under MacOS X you gonna have \r whereas shader compiler could expect \n instead, hence compilation errors.

and it definitely won't harm to examine shader source file in hex editor to check its real contents :)

When i print the content of shaderSource to file with


ofstream outfile;
outfile.open ("log.txt", fstream::out | fstream::app);
outfile << shaderSource << endl;
outfile << endl;
outfile.close();

I get normal contents of vertex and fragment shader - no extra symbols or anything.

But when I use debugger I see this

63944468.jpg
and this

36815480.jpg
any ideas?

You haven't executed the line that null terminates the string yet. Step over it and look again.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
When I get to delete[] shaderSource; line
vertex shader is the same as i posted and fragment shader doesn't have extra symbols at the end huh.png

Can you confirm that when source is explicitly defined in code lie this:


    const GLchar* shaderSource = "#version 330\n"
        "layout (location = 0) in vec3 inPosition;\n"
        "layout (location = 1) in vec3 inColor;\n"
        "smooth out vec3 theColor;\n"
        "void main()\n"
        "{\n"
            "gl_Position = vec4(inPosition, 1.0);\n"
            "theColor = inColor;\n"
        "}\n"

    glShaderSource(uiShader, 1, &shaderSource, NULL);
    glCompileShader(uiShader);

    // dont call delete[] on const arrays, you will get SIGSEGV

    GLint compileStatus;
    glGetShaderiv(uiShader, GL_COMPILE_STATUS, &compileStatus);

    if (compileStatus == GL_FALSE) {
        GLint infoLogLength;
        glGetShaderiv(uiShader, GL_INFO_LOG_LENGTH, &infoLogLength);
        
        GLchar* infoLog = new GLchar[infoLogLength + 1];
        glGetShaderInfoLog(uiShader, infoLogLength, NULL, infoLog);
        
		ofstream outfile;
		outfile.open ("log.txt");
		outfile << infoLog << endl;
		outfile.close();

        delete[] infoLog;
    }

it compiles without errors?

It compiles without errors but it doesn't have any effect. I tried your way, there was no effect from the shader but the debugger did show correct output from shaderSource. I also tried to use it this way


bool CShader::loadVertexShader(int a_iType)
{
	const GLchar* shaderSource = "#version 330\n"
        "layout (location = 0) in vec3 inPosition;\n"
        "layout (location = 1) in vec3 inColor;\n"
        "smooth out vec3 theColor;\n"
        "void main()\n"
        "{\n"
            "gl_Position = vec4(inPosition, 1.0);\n"
            "theColor = inColor;\n"
        "}\n";

	uiShader = glCreateShader(a_iType);
    glShaderSource(uiShader, 1, &shaderSource, NULL);
    glCompileShader(uiShader);

	ofstream outfile;
	outfile.open ("log.txt", fstream::out | fstream::app);
	outfile << shaderSource << endl;
	outfile << endl;
	outfile.close();

	GLint compileStatus;
    glGetShaderiv(uiShader, GL_COMPILE_STATUS, &compileStatus);
 
    if (compileStatus == GL_FALSE)
	{
        GLint infoLogLength;
        glGetShaderiv(uiShader, GL_INFO_LOG_LENGTH, &infoLogLength);
        
        GLchar* infoLog = new GLchar[infoLogLength + 1];
        glGetShaderInfoLog(uiShader, infoLogLength, NULL, infoLog);
        
		ofstream outfile;
		outfile.open ("log.txt");
		outfile << infoLog << endl;
		outfile.close();
 
        delete[] infoLog;
    }

	return 1;
}

also no effect. I did this just in case I don't miss anything when doing this part


// Load shaders and create shader program
//shVertex.loadShader("data/shaders/color.vert", GL_VERTEX_SHADER);
shVertex.loadVertexShader(GL_VERTEX_SHADER);
shFragment.loadShader("data/shaders/color.frag", GL_FRAGMENT_SHADER);

What do you mean by "no effect?" If things aren't drawing/showing, it possible your draw calls and/or vertex data are incorrect.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement