• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Sean_Seanston

OpenGL
Shaders won't work when I use Wrapper Classes but give no errors?

3 posts in this topic

Ok, I apologize that this is a very general question that necessitates a lot of code (only some of which is really relevant to the problem though, I at least assume) but I've been wracking my brain and haven't come any closer to figuring this one out and have no idea what's happening.

 

The situation is:

 

1. I've been roughly following a series of OpenGL tutorials, and in this particular part he creates 2 wrapper classes to more easily use shaders and shader programs: http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=6

2. Originally, I just ignored the wrappers temporarily and made the first 4 tutorials work just using a more basic method.

3. After getting tutorial 4 working (displaying a series of 3D pyramids), I decided to alter my code to incorporate 2 wrapper classes that are based on the classes he uses in the tutorial. As far as I can see, my classes appear to function identically to his.

4. My old method works fine, but when I replace the relevant code with its wrapper-using equivalent... I get no errors and yet all I see is a black screen.

 

I've looked through the functions of the wrapper classes and used OutputDebugString() etc. to confirm that variables are being set correctly, and as far as I can tell... shaders seem to be being loaded correctly, and the program seems to be adding shaders, linking and being set to be used correctly. e.g. I printed out the strings that were meant to contain the shader code and they both looked fine.

If I open the tutorial code in VS, I can run that fine and so far I haven't been able to notice any difference that seems relevant to the shader classes.

 

One thing though, is that I'm using GLFW and the tutorial is just using the Windows API. Can't imagine that would matter, but then I don't know.

 

Here's the main section of code. I've included labels inside square brackets [] to help identify and make sense of the 2 relevant groups of code. The code from the working version without the wrappers is commented out and marked as such, the code relevant to the wrapper implementation is marked similarly. You can presumably ignore everything else. I've checked the shader loading function in my Shader class and it appears to function identically to the global load/buildShader() functions in the working non-wrapper version.

Also, the main loop has some irrelevant code relating to interpolation and framerate that currently does very little/nothing.

 

Looking at the code, is there any reason that jumps out as being a possible cause of this kind of behaviour? It seems I've just directly replaced the old code with its wrapper counterpart, but nothing is displayed.

#include "main.h"

//[NON-WORKING WRAPPER CODE:
ShaderProgram program;
Shader vShader;
Shader fShader;
//NON-WORKING WRAPPER CODE]

//[WORKING CODE WITHOUT WRAPPERS:
/*
GLuint buildShader( GLenum eShaderType, const std::string &shaderText )
{
    GLuint shader = glCreateShader( eShaderType );
    const char *strFileData = shaderText.c_str();
    glShaderSource( shader, 1, &strFileData, NULL );

    glCompileShader(shader);

    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE)
    {
        //With ARB_debug_output, we already get the info log on compile failure.
        if(!glext_ARB_debug_output)
        {
            GLint infoLogLength;
            glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);

            GLchar *strInfoLog = new GLchar[infoLogLength + 1];
            glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);

            const char *strShaderType = NULL;
            switch(eShaderType)
            {
                case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
                case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
                case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
            }

            fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
            delete[] strInfoLog;
        }

        throw std::runtime_error("Compile failure in shader.");
    }

    return shader;
}

GLuint loadShader( GLenum eShaderType, std::string sFile )
{
    GLuint shaderID = 0;
    std::string shaderString;

    //Open file
    std::ifstream sourceFile( sFile.c_str() );

    //If file is valid
    if( sourceFile )
    {
        //Assign file contents to string
        shaderString.assign( ( std::istreambuf_iterator<char>( sourceFile ) ), ( std::istreambuf_iterator<char>() ) );

        //Build shader using string of file contents
        shaderID = buildShader( eShaderType, shaderString );
    }
    else
    {
        printf( "Unable to open file %s\n", sFile.c_str() );
    }

    //Return shader
    return shaderID;
}
*/
//WORKING CODE WITHOUT WRAPPERS]

void init()
{
	//One VBO for vertices positions, one for colors
	GLuint vbo[2];

	//One VAO for pyramid
	GLuint vao;

	glGenVertexArrays( 1, &vao );
	glBindVertexArray( vao );

	float fPyramid[36]; // Pyramid data - 4 triangles of 3 vertices of 3 floats
	float fPyramidColor[36]; // Same for color

	// Setup pyramid

	// Front face
	fPyramid[0] = 0.0f; fPyramid[1] = 5.0f; fPyramid[2] = 0.0f;
	fPyramid[3] = -3.0f; fPyramid[4] = 0.0f; fPyramid[5] = 3.0f;
	fPyramid[6] = 3.0f; fPyramid[7] = 0.0f; fPyramid[8] = 3.0f;

	// Back face
	fPyramid[9] = 0.0f; fPyramid[10] = 5.0f; fPyramid[11] = 0.0f;
	fPyramid[12] = 3.0f; fPyramid[13] = 0.0f; fPyramid[14] = -3.0f;
	fPyramid[15] = -3.0f; fPyramid[16] = 0.0f; fPyramid[17] = -3.0f;

	// Left face
	fPyramid[18] = 0.0f; fPyramid[19] = 5.0f; fPyramid[20] = 0.0f;
	fPyramid[21] = -3.0f; fPyramid[22] = 0.0f; fPyramid[23] = -3.0f;
	fPyramid[24] = -3.0f; fPyramid[25] = 0.0f; fPyramid[26] = 3.0f;

	// Right face
	fPyramid[27] = 0.0f; fPyramid[28] = 5.0f; fPyramid[29] = 0.0f;
	fPyramid[30] = 3.0f; fPyramid[31] = 0.0f; fPyramid[32] = 3.0f;
	fPyramid[33] = 3.0f; fPyramid[34] = 0.0f; fPyramid[35] = -3.0f;

	// Setup pyramid color
	for( int i = 0; i < 4; i++ )
	{
		fPyramidColor[i*9] = 1.0f; fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+2] = 0.0f;
		if(i < 2)
		{
			fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+4] = 1.0f; fPyramidColor[i*9+5] = 0.0f;
			fPyramidColor[i*9+2] = 0.0f; fPyramidColor[i*9+7] = 0.0f; fPyramidColor[i*9+8] = 1.0f;
		}
		else
		{
			fPyramidColor[i*9+2] = 0.0f; fPyramidColor[i*9+7] = 1.0f; fPyramidColor[i*9+8] = 0.0f;
			fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+4] = 0.0f; fPyramidColor[i*9+5] = 1.0f;
		}
	}

	glGenBuffers( 2, vbo );

	glBindBuffer( GL_ARRAY_BUFFER, vbo[0] );
	glBufferData( GL_ARRAY_BUFFER, 36*sizeof(float), fPyramid, GL_STATIC_DRAW );

	glEnableVertexAttribArray( 0 );
	glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 );

	glBindBuffer( GL_ARRAY_BUFFER, vbo[1] );
	glBufferData( GL_ARRAY_BUFFER, 36*sizeof(float), fPyramidColor, GL_STATIC_DRAW );
	glEnableVertexAttribArray( 1 );
	glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, 0 );

	glBindBuffer( GL_ARRAY_BUFFER, 0 );

//[NON-WORKING WRAPPER CODE:
	vShader.loadShader( GL_VERTEX_SHADER, "shader.vert" );
	fShader.loadShader( GL_FRAGMENT_SHADER, "shader.frag" );

	program.createProgram();
	program.addShader( &vShader );
	program.addShader( &fShader );

	program.linkProgram();
	program.useProgram();
//NON-WORKING WRAPPER CODE]

//[WORKING CODE WITHOUT WRAPPERS:
/*	GLuint vertShader = loadShader( GL_VERTEX_SHADER, "shader.vert" );
	GLuint fragShader = loadShader( GL_FRAGMENT_SHADER, "shader.frag" );

	GLuint program;

	program = glCreateProgram();
	glAttachShader( program, vertShader );
	glAttachShader( program, fragShader );
	glLinkProgram( program );

	GLint status;
	glGetProgramiv ( program, GL_LINK_STATUS, &status );
	if ( status == GL_FALSE )
	{
		if( !glext_ARB_debug_output )
		{
			GLint infoLogLength;
			glGetProgramiv( program, GL_INFO_LOG_LENGTH, &infoLogLength );

			GLchar *strInfoLog = new GLchar[ infoLogLength + 1 ];
			glGetProgramInfoLog( program, infoLogLength, NULL, strInfoLog );
			fprintf( stderr, "Linker failure: %s\n", strInfoLog );
			delete[] strInfoLog;
		}

		throw std::runtime_error( "Shader could not be linked." );
	}

	//Set shader program
	glUseProgram( program );
//WORKING CODE WITHOUT WRAPPERS]

	glEnable( GL_DEPTH_TEST );
	glClearDepth( 1.0 );

	//Get locations for projection and modelView matrices
//[WORKING CODE WITHOUT WRAPPERS:
//	Engine::instance()->modelViewLoc = glGetUniformLocation( program, "modelViewMatrix" );
//	Engine::instance()->projLoc = glGetUniformLocation( program, "projectionMatrix" );
//WORKING CODE WITHOUT WRAPPERS]

//[NON-WORKING WRAPPER CODE:
	Engine::instance()->modelViewLoc = glGetUniformLocation( program.getProgramID(), "modelViewMatrix" );
	Engine::instance()->projLoc = glGetUniformLocation( program.getProgramID(), "projectionMatrix" );
//NON-WORKING WRAPPER CODE]

	Engine::instance()->setProjMatrix( FOV, ASPECT_RATIO, NEAR_CLIP, FAR_CLIP );

	//Create projection matrix uniform
	glUniformMatrix4fv( Engine::instance()->projLoc, 1, GL_FALSE, glm::value_ptr( Engine::instance()->getProjMatrix() ) );
}

void display()
{
	static float fRotationAngle = 0.0f;
	const float PIover180 = 3.1415f/180.0f;

	//Set clear color and clear color buffer
	glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	//Create ModelView matrix
	glm::mat4 modelView = glm::lookAt( glm::vec3( 0, 15, 20 ), glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ) );
	//LOOKAT: ( CAMERA POS, CAMERA TARGET, UP VECTOR )

		//Draw vertex data
		//Apply rotation of modelView matrix around Y axis and store in mCurrent
		glm::mat4 current = glm::rotate( modelView, fRotationAngle, glm::vec3( 0.0f, 1.0f, 0.0f ) );
		glUniformMatrix4fv( Engine::instance()->modelViewLoc, 1, GL_FALSE, glm::value_ptr( current ) );
		glDrawArrays( GL_TRIANGLES, 0, 12 );

		 // One on the left
		current = glm::translate( modelView, glm::vec3( -20.0f, 10.0f*float( sin( fRotationAngle * PIover180 ) ), 0.0f ) );
		glUniformMatrix4fv( Engine::instance()->modelViewLoc, 1, GL_FALSE, glm::value_ptr( current ) );
		glDrawArrays( GL_TRIANGLES, 0, 12 ); 

		// One on the right
		current = glm::translate( modelView, glm::vec3( 20.0f, -10.0f*float( sin( fRotationAngle * PIover180 ) ), 0.0f ) );
		glUniformMatrix4fv( Engine::instance()->modelViewLoc, 1, GL_FALSE, glm::value_ptr( current ) );
		glDrawArrays( GL_TRIANGLES, 0, 12 ); 

		//And one translating and rotating on top
		current = glm::translate( modelView, glm::vec3( 20.0f*float(sin( fRotationAngle*PIover180 ) ), 10.0f, 0.0f ) );
		current = glm::rotate( current, fRotationAngle, glm::vec3( 1.0f, 0.0f, 0.0f ) );
		glUniformMatrix4fv( Engine::instance()->modelViewLoc, 1, GL_FALSE, glm::value_ptr( current ) );
		glDrawArrays(GL_TRIANGLES, 0, 12); 

		float fScaleValue = 1.5f+float(sin(fRotationAngle*PIover180))*0.5f;
		current = glm::translate(modelView, glm::vec3(0.0f, -10.0f, 0.0f));
		current = glm::scale(current, glm::vec3(fScaleValue, fScaleValue, fScaleValue));
		current = glm::rotate(current, fRotationAngle, glm::vec3(1.0f, 0.0f, 0.0f));
		current = glm::rotate(current, fRotationAngle, glm::vec3(0.0f, 1.0f, 0.0f));
		current = glm::rotate(current, fRotationAngle, glm::vec3(0.0f, 0.0f, 1.0f));
		glUniformMatrix4fv( Engine::instance()->modelViewLoc, 1, GL_FALSE, glm::value_ptr( current ) );
		glDrawArrays(GL_TRIANGLES, 0, 12); 

	//Increment rotation angle
	fRotationAngle += 0.05f;
	fRotationAngle > 360.0f ? fRotationAngle = 0.0f : fRotationAngle = fRotationAngle;

	//Swap front and back buffers
	glfwSwapBuffers();
}

void cleanUp()
{
	glDisableVertexAttribArray( 0 );
	glDisableVertexAttribArray( 1 );

	glUseProgram( 0 );
}

//Called whenever the window is resized. The new window size is given, in pixels.
//This is an opportunity to call glViewport or glScissor to keep up with the change in size.
void reshape( int w, int h )
{
	glViewport( 0, 0, ( GLsizei ) w, ( GLsizei ) h );
	Engine::instance()->setProjMatrix( FOV, ( float ) w /( float ) h, NEAR_CLIP, FAR_CLIP );

	//Create projection matrix uniform
	glUniformMatrix4fv( Engine::instance()->projLoc, 1, GL_FALSE, glm::value_ptr( Engine::instance()->getProjMatrix() ) );
}

void APIENTRY DebugFunc(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
						const GLchar* message, const GLvoid* userParam)
{
	std::string srcName;
	switch(source)
	{
	case GL_DEBUG_SOURCE_API_ARB: srcName = "API"; break;
	case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB: srcName = "Window System"; break;
	case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB: srcName = "Shader Compiler"; break;
	case GL_DEBUG_SOURCE_THIRD_PARTY_ARB: srcName = "Third Party"; break;
	case GL_DEBUG_SOURCE_APPLICATION_ARB: srcName = "Application"; break;
	case GL_DEBUG_SOURCE_OTHER_ARB: srcName = "Other"; break;
	}

	std::string errorType;
	switch(type)
	{
	case GL_DEBUG_TYPE_ERROR_ARB: errorType = "Error"; break;
	case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB: errorType = "Deprecated Functionality"; break;
	case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB: errorType = "Undefined Behavior"; break;
	case GL_DEBUG_TYPE_PORTABILITY_ARB: errorType = "Portability"; break;
	case GL_DEBUG_TYPE_PERFORMANCE_ARB: errorType = "Performance"; break;
	case GL_DEBUG_TYPE_OTHER_ARB: errorType = "Other"; break;
	}

	std::string typeSeverity;
	switch(severity)
	{
	case GL_DEBUG_SEVERITY_HIGH_ARB: typeSeverity = "High"; break;
	case GL_DEBUG_SEVERITY_MEDIUM_ARB: typeSeverity = "Medium"; break;
	case GL_DEBUG_SEVERITY_LOW_ARB: typeSeverity = "Low"; break;
	}

	printf("%s from %s,\t%s priority\nMessage: %s\n",
		errorType.c_str(), srcName.c_str(), typeSeverity.c_str(), message);
}

int main()
{
	if( !glfwInit() )
	{
		return -1;
	}

	glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
	glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 3 );
	glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
#ifdef DEBUG
	glfwOpenWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
#endif

	//Create window
	if( !glfwOpenWindow( 1000, 500, 8, 8, 8, 8, 24, 8, GLFW_WINDOW ) )
	{
		glfwTerminate();
		return -1;
	}

	//Load OpenGL functions
	if( !glload::LoadFunctions() )
	{
		glfwTerminate();
		return -1;
	}

	//Set window title
	glfwSetWindowTitle( "GLFW Demo" );

	if( glext_ARB_debug_output )
	{
		glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB );
		glDebugMessageCallbackARB( DebugFunc, ( void* )15 );
	}

	//Initialize scene
	init();

	glfwSetWindowSizeCallback( reshape );

	//Variables for framerate management
	DWORD next_game_tick = GetTickCount();
	int loops = 0;
	float interpolation;

	//Main loop
	while( Engine::instance()->isRunning() )
	{
		float time = ( float )GetTickCount();
		loops = 0;

		//Handle events
		if( glfwGetKey( GLFW_KEY_ESC ) || !glfwGetWindowParam( GLFW_OPENED ) )
		{
			break;
		}

		while( GetTickCount() > next_game_tick && loops < Engine::MAX_FRAMESKIP )
		{		
			//Do event handling and updating of current active state
			Engine::instance()->handleEvents();
			Engine::instance()->update();

			next_game_tick += Engine::SKIP_TICKS;
			loops++;
		}

		//Calculate interpolation value
		interpolation = float( GetTickCount() + Engine::SKIP_TICKS - next_game_tick ) / float( Engine::SKIP_TICKS );

		//Render
		display();
	}

	cleanUp();

	glfwTerminate();
	
	return 0;
}
 
0

Share this post


Link to post
Share on other sites

Any guesses at all to what might possibly lead to something like this? One of the thoughts I had was something going out of scope or similar... i.e. maybe things were being set up ok in theory but wound up empty or full of junk when they were needed. That's why I moved the Shader and ShaderProgram object declarations out of the init() function where the non-wrapper equivalents are, but had no success with that either obviously. I also would have imagined these kind of scenarios would result in at least some form of error.

 

Here are the shader classes anyway, though I THINK they seem to wrap the very same functionality that works without the wrappers:

//Class to wrap a shader
class Shader
{
public:
	Shader();

	bool loadShader( GLenum eShaderType, std::string sFile );
	void deleteShader();

	GLuint getShaderID();

	bool isLoaded();

private:
	GLuint shaderID;
	GLenum shaderType;
	bool loaded;
};

//Class to wrap a shader program
class ShaderProgram
{
public:
	ShaderProgram();

	void createProgram();
	void deleteProgram();

	bool addShader( Shader* shader );
	bool linkProgram();

	void useProgram();

	GLuint getProgramID();

private:
	GLuint programID;
	bool linked;
};
Shader::Shader() : loaded ( false )
{
}

bool Shader::loadShader( GLenum eShaderType, std::string sFile )
{
	GLuint shaderID = 0;
	std::string shaderString;

	//Open file
	std::ifstream sourceFile( sFile.c_str() );

	//If file is valid
	if( sourceFile )
	{
		//Assign file contents to string
		shaderString.assign( ( std::istreambuf_iterator<char>( sourceFile ) ), ( std::istreambuf_iterator<char>() ) );

		//Build shader using string of file contents
		shaderID = glCreateShader( eShaderType );

		const char *strFileData = shaderString.c_str();

		glShaderSource( shaderID, 1, &strFileData, NULL );

		glCompileShader( shaderID );

		GLint status;
		glGetShaderiv( shaderID, GL_COMPILE_STATUS, &status );
		if ( status == GL_FALSE )
		{
			//With ARB_debug_output, we already get the info log on compile failure.
			if( !glext_ARB_debug_output )
			{
				GLint infoLogLength;
				glGetShaderiv( shaderID, GL_INFO_LOG_LENGTH, &infoLogLength );

				GLchar *strInfoLog = new GLchar[infoLogLength + 1];
				glGetShaderInfoLog( shaderID, infoLogLength, NULL, strInfoLog );

				const char *strShaderType = NULL;
				switch( eShaderType )
				{
					case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
					case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
					case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
				}

				fprintf( stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog );
				delete[] strInfoLog;
			}

			throw std::runtime_error( "Compile failure in shader." );
			return false;
		}
	}
	else
	{
		printf( "Unable to open file %s\n", sFile.c_str() );

		return false;
	}

	shaderType = eShaderType;
	loaded = true;
	return true;
}

void Shader::deleteShader()
{
	if( !isLoaded() )
	{
		return;
	}

	loaded = false;
	glDeleteShader( shaderID );
}

GLuint Shader::getShaderID()
{
	return shaderID;
}

bool Shader::isLoaded()
{
	return loaded;
}

ShaderProgram::ShaderProgram() : linked( false )
{
}

void ShaderProgram::createProgram()
{
	programID = glCreateProgram();
}

void ShaderProgram::deleteProgram()
{
	if ( !linked )
	{
		return;
	}

	linked = false;
	glDeleteProgram( programID );
}

bool ShaderProgram::addShader( Shader* shader )
{
	if( !shader->isLoaded() )
	{
		return false;
	}

	glAttachShader( programID, shader->getShaderID() );

	return true;
}

bool ShaderProgram::linkProgram()
{
	glLinkProgram( programID );

	int status;
	glGetProgramiv( programID, GL_LINK_STATUS, &status );

	linked = ( status == GL_TRUE );
	return linked;
}

void ShaderProgram::useProgram()
{
	if( linked )
	{
		glUseProgram( programID );
	}
}

GLuint ShaderProgram::getProgramID()
{
	return programID;
}

0

Share this post


Link to post
Share on other sites

Ok, I'm closing in on the problem but haven't quite found it yet.

 

The ShaderProgram class would appear to be working correctly and the problem must lie within the Shader class.

 

If I use the old shaders, i.e.

GLuint vertShader = loadShader( GL_VERTEX_SHADER, "shader.vert" );
GLuint fragShader = loadShader( GL_FRAGMENT_SHADER, "shader.frag" );

In conjunction with the ShaderProgram class, by attaching them using the old method:

glAttachShader( program.getProgramID(), vertShader );
glAttachShader( program.getProgramID(), fragShader );

Everything then works fine.

 

Hence, there must be something wrong with the Shader class... possibly in the loading of the shaders, though I could swear the shaders appear to be loading correctly since the GL_COMPILE_STATUS isn't returning false and the full correct text of the shaders seems to be getting loaded.

 

Going to keep looking, but maybe someone else can spot something with this new information...

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By Solid_Spy
      Hello, I have been working on SH Irradiance map rendering, and I have been using a GLSL pixel shader to render SH irradiance to 2D irradiance maps for my static objects. I already have it working with 9 3D textures so far for the first 9 SH functions.
      In my GLSL shader, I have to send in 9 SH Coefficient 3D Texures that use RGBA8 as a pixel format. RGB being used for the coefficients for red, green, and blue, and the A for checking if the voxel is in use (for the 3D texture solidification shader to prevent bleeding).
      My problem is, I want to knock this number of textures down to something like 4 or 5. Getting even lower would be a godsend. This is because I eventually plan on adding more SH Coefficient 3D Textures for other parts of the game map (such as inside rooms, as opposed to the outside), to circumvent irradiance probe bleeding between rooms separated by walls. I don't want to reach the 32 texture limit too soon. Also, I figure that it would be a LOT faster.
      Is there a way I could, say, store 2 sets of SH Coefficients for 2 SH functions inside a texture with RGBA16 pixels? If so, how would I extract them from inside GLSL? Let me know if you have any suggestions ^^.
    • By KarimIO
      EDIT: I thought this was restricted to Attribute-Created GL contexts, but it isn't, so I rewrote the post.
      Hey guys, whenever I call SwapBuffers(hDC), I get a crash, and I get a "Too many posts were made to a semaphore." from Windows as I call SwapBuffers. What could be the cause of this?
      Update: No crash occurs if I don't draw, just clear and swap.
      static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 32, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 24, // 24Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if (!(hDC = GetDC(windowHandle))) return false; unsigned int PixelFormat; if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) return false; if (!SetPixelFormat(hDC, PixelFormat, &pfd)) return false; hRC = wglCreateContext(hDC); if (!hRC) { std::cout << "wglCreateContext Failed!\n"; return false; } if (wglMakeCurrent(hDC, hRC) == NULL) { std::cout << "Make Context Current Second Failed!\n"; return false; } ... // OGL Buffer Initialization glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); glUseProgram(myprogram); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (void *)indexStart); SwapBuffers(GetDC(window_handle));  
    • By Tchom
      Hey devs!
       
      I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.
       
      Vertex Shader
      uniform mat4 u_MVPMatrix; uniform mat4 u_MVMatrix; attribute vec4 a_Position; attribute vec3 a_Normal; attribute vec2 a_TexCoordinate; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { v_Position = vec3(u_MVMatrix * a_Position); v_TexCoordinate = a_TexCoordinate; v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); gl_Position = u_MVPMatrix * a_Position; } Fragment Shader
      precision mediump float; uniform vec4 u_LightPos["+numLights+"]; uniform vec4 u_LightColours["+numLights+"]; uniform float u_LightPower["+numLights+"]; uniform sampler2D u_Texture; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { gl_FragColor = (texture2D(u_Texture, v_TexCoordinate)); float diffuse = 0.0; vec4 colourSum = vec4(1.0); for (int i = 0; i < "+numLights+"; i++) { vec3 toPointLight = vec3(u_LightPos[i]); float distance = length(toPointLight - v_Position); vec3 lightVector = normalize(toPointLight - v_Position); float diffuseDiff = 0.0; // The diffuse difference contributed from current light diffuseDiff = max(dot(v_Normal, lightVector), 0.0); diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower[i])* distance * distance))); //Determine attenuatio diffuse += diffuseDiff; gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours[i]))*diffuseDiff))); //The expensive part } diffuse += 0.1; //Add ambient light gl_FragColor.rgb *= diffuse; } Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance
    • By yahiko00
      Hi,
      Not sure to post at the right place, if not, please forgive me...
      For a game project I am working on, I would like to implement a 2D starfield as a background.
      I do not want to deal with static tiles, since I plan to slowly animate the starfield. So, I am trying to figure out how to generate a random starfield for the entire map.
      I feel that using a uniform distribution for the stars will not do the trick. Instead I would like something similar to the screenshot below, taken from the game Star Wars: Empire At War (all credits to Lucasfilm, Disney, and so on...).

      Is there someone who could have an idea of a distribution which could result in such a starfield?
      Any insight would be appreciated
    • By afraidofdark
      I have just noticed that, in quake 3 and half - life, dynamic models are effected from light map. For example in dark areas, gun that player holds seems darker. How did they achieve this effect ? I can use image based lighting techniques however (Like placing an environment probe and using it for reflections and ambient lighting), this tech wasn't used in games back then, so there must be a simpler method to do this.
      Here is a link that shows how modern engines does it. Indirect Lighting Cache It would be nice if you know a paper that explains this technique. Can I apply this to quake 3' s light map generator and bsp format ?
  • Popular Now