Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Apr 2012
Offline Last Active Mar 24 2015 07:43 PM

Topics I've Started

Crazy GLSL bug

03 January 2015 - 08:39 AM



I am experiencing a really weird GLSL bug. I have implemented point lights following this tutorial, but I get some strange artefacts and I narrowed them down to the following line in my fragment shader:

specularFactor = pow(specularFactor, matSpecularPower);

specularFactor is of type "highp float" and matSpecularPower is "uniform highp float" and set to 1.0 for debugging purposes. If I change the line to the following everything looks different and I don't have those black artefacts:

specularFactor = pow(specularFactor, 1.0);

Here are some screenshots:

with first line

with second line


How is this possible if matSpecular is also set to 1.0? And I am sure it is because I also get the first result if I change it to this:

if (matSpecularPower == 1.0) {
	specularFactor = pow(specularFactor, matSpecularPower);

And the second result if I change it to this:

if (matSpecularPower == 1.0) {
	specularFactor = pow(specularFactor, 1.0);

I am on Linux and have an Intel graphics card if that is important. Help, please! Even ideas of things I could try would be appreciated.

C++ c

21 March 2014 - 10:27 AM



I made a very simple container class because I need a vector where I can remove elements in the middle without changing the position of the other elements but I am worried that it doesn't free up everything when I delete it. Here is the code:

template <typename T>
class Vector
	T* elements;
	int elementCount;
	int elementCapacity;
	int freeElement; // first free element
	int* freeElementList; // each array element is the index of the next free element
	Vector(int initialCapacity) {
		elementCount = 0;
		elementCapacity = initialCapacity;
		elements = (T*)malloc(elementCapacity * sizeof(T));

		freeElement = 0;
		freeElementList = (int*)malloc(elementCapacity * sizeof(int));
		for (int i = 0; i < elementCapacity; ++i)
			freeElementList[i] = i + 1;

	~Vector() {

	int addElement(T& element) {
		if (elementCount == elementCapacity) {
			elementCapacity *= 2;

			int* oldFreeElementList = freeElementList;
			freeElementList = (int*)malloc(elementCapacity * sizeof(int));
			memcpy(freeElementList, oldFreeElementList, elementCount * sizeof(int));
			for (int i = elementCount; i < elementCapacity; ++i)
				freeElementList[i] = i + 1;

			T* oldElements = elements;
			elements = (T*)malloc(elementCapacity * sizeof(T));
			memcpy(elements, oldElements, elementCount * sizeof(T));
		elements[freeElement] = element;

		int result = freeElement;
		freeElement = freeElementList[freeElement];

		return result;

	void removeElement(int index) {
		freeElementList[index] = freeElement;
		freeElement = index;

	T getElement(int index) {
		return elements[index];


For example if I use it to store STL vectors will the destructor actually delete all their elements?


EDIT: sorry, I forgot to set the right title.sad.png It was supposed to be something like "C++ custom container"

glm and perspective matrices

04 February 2014 - 06:03 PM



I made a simple scene with only one triangle and am now trying to use a projection matrix created by glm. However when I multiply each vertex with it in my shader the triangle disappears.


This is how I create the projection matrix:

projectionMatrix = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 100.0f);


My triangle vertices have these coordinates:

v1(-1, -1, 1)

v2(1, -1, 1)

v3(0, 1, 1)


I multiply the vertices with the matrix like this:

gl_Position = projectionMatrix * vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);


I am also a bit confused how the projection matrix works. After reading this tutorial I compared the matrix shown there with the one in glm and they differ slightly. For example glm uses the w component of the vector to store -z instead of +z. Doesn't this flip the signs of the x and y components after perspective divide? I also manually multiplied vectors with zNear(0.1f) and zFar(100.0f) as their z component. I think the multiplication should give values that become -1 and 1 after perspective divide but it doesn't.


glm::vec4 v1(-1.0f, -1.0f, 0.1f, 1.0f);
v1 = projectionMatrix * v1;        // v1 = ( -1.810660, -2.414213, -0.300400, -0.100000)


glm::vec4 v2(1.0f, -1.0f, 100.0f, 1.0f);
v2 = projectionMatrix * v2;       // v2 = ( 1.810660, -2.414213, -100.400406, -100.000000)


Hopefully someone can help me.

glGetUniformLocation not working

01 September 2013 - 10:07 AM



I am trying to use uniform variables in my shader but for some reason glGetUniformLocation always returns -1.  I am mostly following Lazy Foo's tutorial. Here is the relevant code:

PointLineShader::PointLineShader() {
    GLuint vertexShaderId = loadShader("Shader/pointLine.vert", GL_VERTEX_SHADER);
    GLuint fragmentShaderId = loadShader("Shader/pointLine.frag", GL_FRAGMENT_SHADER);

    programId = glCreateProgram();
    glAttachShader(programId, vertexShaderId);
    glAttachShader(programId, fragmentShaderId);

    glDetachShader(programId, vertexShaderId);
    glDetachShader(programId, fragmentShaderId);


    positionLocation = glGetAttribLocation(programId, "in_Position");
    if (positionLocation == -1) {
        printf("%s is not a valid glsl program variable!\n", "in_Position");

    colorLocation = glGetAttribLocation(programId, "in_Color");
    if (colorLocation == -1) {
        printf("%s is not a valid glsl program variable!\n", "in_Position");

    // this doesn't work
    projectionMatrixLocation = glGetUniformLocation(programId, "projectionMatrix");
    if (projectionMatrixLocation == -1) {
        printf("%s is not a valid glsl program variable!\n", "projectionMatrix");

    modelViewMatrixLocation = glGetUniformLocation(programId, "modelViewMatrix");
    if (modelViewMatrixLocation == -1) {
        printf("%s is not a valid glsl program variable!\n", "modelViewMatrix");

And the vertex shader:

#version 400

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

in vec2 in_Position;
in vec4 in_Color;
out vec4 ex_Color;
void main(void)
	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(in_Position.x, in_Position.y, 0.0, 1.0);
	//gl_Position = projectionMatrix * modelViewMatrix * vec4(in_Position.x, in_Position.y, 0.0, 1.0);
	ex_Color = in_Color;

To initialize openGL I am using SDL2 and glew. I noticed that I set the openGL version to 3.2 in SDL, but in the shaders I have "#version 400". If I change or remove it the shader won't compile.


I am not sure if it might be related, but textures also don't work right now even though I am using the same code as in my other program which uses SFML except for the image loading functions.

link libSDL2.a with cmake

10 July 2013 - 06:51 AM



I'm trying to compile a simple SDL2 application with cmake under Linux. My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)


add_executable(${PROJECT_NAME} ${SRC_LIST})

#target_link_libraries(${PROJECT_NAME} /usr/local/lib/libSDL2.so ${OPENGL_gl_LIBRARY})
target_link_libraries(${PROJECT_NAME} /usr/local/lib/libSDL2.a pthread dl rt ${OPENGL_gl_LIBRARY})

It works immediately if I link it to libSDL2.so, but if I link it to libSDL2.a I have to add pthread, dl and rt or it gives me undefined reference errors like /usr/bin/ld: /usr/local/lib/libSDL2.a(SDL_syssem.o): undefined reference to symbol 'sem_getvalue@@GLIBC_2.2.5'. Can somebody explain why this is?

And is it correct that if I link my application to libSDL2.a that I can execute the binary file on any Linux installation without requiring SDL2 to be installed?