Jump to content

  • Log In with Google      Sign In   
  • Create Account


Maxjen

Member Since 07 Apr 2012
Offline Last Active Mar 25 2014 11:47 AM

Topics I've Started

C++ c

21 March 2014 - 10:27 AM

Hi,

 

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
{
private:
	T* elements;
	int elementCount;
	int elementCapacity;
	int freeElement; // first free element
	int* freeElementList; // each array element is the index of the next free element
public:
	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() {
		free(freeElementList);
		free(elements);
	}

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

			int* oldFreeElementList = freeElementList;
			freeElementList = (int*)malloc(elementCapacity * sizeof(int));
			memcpy(freeElementList, oldFreeElementList, elementCount * sizeof(int));
			free(oldFreeElementList);
			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));
			free(oldElements);
		}
		elements[freeElement] = element;

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

		return result;
	}

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

	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

Hi,

 

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

Hi,

 

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);
    glLinkProgram(programId);

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

    glDeleteShader(vertexShaderId);
    glDeleteShader(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

Hi,

 

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

project(Test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)

find_package(OpenGL)

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?


Getting data from multiple vertex data

30 December 2012 - 06:40 AM

Hi,

I'm trying to write a 2D map editor. It is based on triangles. The problem I have is drawing the selection. Here is how it should look like when I select one vertex:
Attached File  selection.png   83.25KB   44 downloads

What I have is Triangle class which creates a vertex buffer where all the triangle vertex positions, colors and texture coordinates are. I have a separate class where I store the data for the selection. It has a stl map which maps two integers (vertex indices, lower first) to a structure which contains two bool values to decide if the vertices are selected or not. Now all I need is a way too use that information too draw the edges like in the picture. So for every entry in that map I want to draw a line which gets the positions of the vertices from the vertex buffer of the Triangle class and the information whether or not the vertices are selected from e.g. another vertex buffer in the Selection class. The problem is that I don't know how or if it is possible to bind two vertex buffer at once. If anyone knows or can think of a better way to do this please tell me.

Oh, the picture is not exactly what I want. In my case there should be a third half selected edge that goes down from the selected vertex.

PARTNERS