-
Notifications
You must be signed in to change notification settings - Fork 2
Object class
--This wiki is still under construction--
The Object class
The object class is an class in the GLGE 3D core. The object describes a 3D object on the scene, that can be transformed using a Transform. An instance of the Object class can be drawn using the public function "draw".
The Object class has a default constructor, that initializes the Object without information about further look of the object.
Example:
Object nameOfTheObject;
To create an instance of the Object class, two pointer arrays can be used. But then, you need to also supply the
size of the pointer arrays. The first pointer should point to an array of the type "Vertex", the second needs to be of the type "unsigned int" or "uint". After the size of the pointer arrays, an optional transform can be inputted to start the object with an offset from the world center, rotate or scale it. After the transform is an optional bool called "isStatic". This bool is for the future, it momentary dose nothing. Later, this bool should disable the world camera to make it static on the screen.
Example:
Object nameOfTheObject = Object(pointerToVertices, pointerToIndices, sizeof(pointerToVertices), sizeof(pointerToIndices), Transform(vec3(3,8,-1), vec3(12,56,23), vec3(2,0.5,1.7)), false);
The creation of an Object using std::vectors is similar to the construction of an Object using pointer arrays. The main difference is, that the size of the std::vectors not needs to be inputted. The types of the std::vector also should be "Vertex" and "unsigned int" or "uint". After the std::vectors, an optional transform can be inputted to start the object with an offset from the world center, rotate or scale it. After the transform is an optional bool called "isStatic". This bool is for the future, it momentanly dose nothing. Later, this bool should disable the world camera to make it static on the screen.
Example:
Object nameOfTheObject = Object(vectorOfVertices, vectorOfIndices, Transform(vec3(0.5,-3.6,0), vec3(-4.7,605.8,33), vec32(0.5,-4,3.1415926535), false));
An functional instance of the Object class can also be created by inputting an mesh. The mesh is an instance of the Mesh class, it is created similar to an Object, but has no transform or isStatic mode. The mesh is inputted first into the constructor, then comes the optional transform to start the object with an offset from the world center, rotate or scale it. After the transform is an optional bool called "isStatic". This bool is for the future, it momentanly dose nothing. Later, this bool should disable the world camera to make it static on the screen.
Example:
Object nameOfTheObject = Object(meshForTheObject, Transform(vec3(0.5,-3.6,0), vec3(-4.7,605.8,33), vec32(0.5,-4,3.1415926535), false));
One of the newer editions for constructing an Object is, that it can be loaded from an file. This is important, because defining the meshes in the code is tedious. But, if an file is loaded, it is important to also send the file, else the object won't show up.
Example:
Object nameOfTheObject = Object("assets/cube.obj", GLGE_OBJ, Transform(vec3(0.5,-3.6,0), vec3(-4.7,605.8,33), vec32(0.5,-4,3.1415926535), false));
- .obj(Wavefront files) | Type Identieier: GLGE_OBJ | Type id: 1
If an object is created, it is assigned the default 3D vertex shader (GLGE_DEFAULT_3D_VERTEX) and the default 3D fragment shader(GLGE_DEFAULT_3D_FRAGMENT). These shaders are mostly what you need, but you can also assign your own shaders to an Object.
To assign an shader to the Object, the function "setShader" is used.
An Object can be assigned shaders from two files. The first file is the file for the vertex shader, the second file is the fiel for the fragment shader. Both shaders are needed to create an Object.
Example:
instanceOfObject.setShader(pathToVertexShader, pathToFragmentShader);
Also, an Object can create shaders if the shader source code is inputted as an std::string. This is, so dynamic shader creation is supported. If an shader is written in the program using no external files, they won't be needed by any other person trying to execute the program. Also here, both shader types (Vertex shader, Fragment shader) are needed.
Example:
instanceOfObject.setShader(vertexShaderSource,fragmentShaderSource);
To still load an file from an path stored in an std::string, the function "c_str" can be used.
Example2:
instanceOfObject.setShader(pathToVertexShader.c_str(),pathToFragmentShader.c_str());
To bind a shader stored in an instance of the Shader class, the Shader class has an simple function: "getShader". Using this version, only one input is needed. So, no separate Vertex and Fragment shader.
Example:
instanceOfObject.setShader(instanceOfShaderClass.getShader());
A shader can also be bound to an Object using an GLuint. The GLuint is like an pointer in OpenGL, it says where on the graphics card the requested, already compiled shader is. So, compiling an shader yourself and then binding it also works. This method is not recommended for beginners, but experts, that know what they are doing, may have an better time using it.
Example:
instanceOfObject.setShader(GLuintToShaderOnGraphicsCard);
--WARNING: the element below is subject to change--
You can apply an texture to an object, like you can directly apply shaders to objects. The only difference is, that the way using std::strings is shader-only. An bound texture can be used later in an shader, the default shader already has an implementation for textures. The function for binding textures is called "setTexture"
A texture can be loaded directly from texture files. The image loading uses the stb_image library made by nothing. If you want any information about the supported file formats, check out his repository. To load an image, input the path to the image file to the function. But don't load too many textures, else there is not enough space on the graphics card to store them.
Example:
instanceOfObject.setTexture(pathToTexture);
A texture can also be copied from an other Object. If the texture is copied, if one of the Objects deletes the texture, it will be missing on both. But on the other side, the graphics card only needs to store one instance of the image, so as many objects as you want can share one texture. The texture is transferred using an GLuint, like the shaders, so it can be stored in an external variable.
Example:
instanceOfObject.setTexture(secondInstanceOfObject.getTexture());
If is not a lot of fun playing an game with objects, that can't be moved. So, moving objects is an important part of games. But not only moving is important, rotating and scaling is important too. So, GLGE provides an easy way to change the transformation of the objects.
The transform of an Object can be changed completely by overwriting it with another transform. But because the transform of the Object class is protected, a function is given. The function is called "setTransform".
Example:
instanceOfObject.setTransform(anVariableOfTypeTransform);
If you only want to change the position of the object, there are a few functions that can be used. You can change the position by setting an position or by moving the Object. If the position is set, the Object will jump to the position relative to world 0,0,0. If the Object is moved, it will jump to the position in the relative coordinate system of the object, not including the rotation of the Object (x+dx, y+dy, z+dz). Both functions accept three floats for x,y,z or 3D vectors(vec3).
Examples:
instanceOfObject.setPos(0,0,0);
instanceOfObject.setPos(vec3(0,0,0));
instanceOfObject.move(1,1,1);
instanceOfObject.move(vec3(1,1,1));
If the Object should change its rotation, but not scale or position, you can use the "rotate" or "setRotation" function of the Object. The rotate function changes the rotation of the Object by adding the inputs to the existing rotation. The setRotation function sets the rotation, ignoring the rotation of the Object in the moment. The imputed rotation of the function will be exactly the new rotation of the Object. Both functions accept three floats or on 3D vector(vec3).
WARNING: The rotation is stored using floats. If the numbers get too large, the rotation won't be accurate.
Examples:
instanceOfObject.setRotation(0,0,0);
instanceOfObject.setRotation(vec3(0,0,0));
instanceOfObject.rotate(1,1,1);
instanceOfObject.rotate(vec3(1,1,1));
WARNING: in the library, the scale is named inconsistently, once it is named "scale" and another time it is named "size".
The scaling of an Object might not be as important as its position or rotation, but it is also important. The scaling of an object can be set using the functions "setScale", "scale" and "addScale". The function setScale sets the scale of the object to the inputs, ignoring the scale the Object had before. The function scale multiplies every element of the Object's scale with the inputted variable(s). The function addScale adds the inputted amount to the previous scale. Every function accepts an 3D vector(vec3), three floats and one float. If only one float is inputted, it is handled as an vector with the inputted value on all 3 axis (3 -> vec3(3,3,3)).
Examples:
instanceOfObject.setSize(vec3(1,1,1));<br />
instanceOfObject.setSize(1,1,1);<br />
instanceOfObject.setSize(1);<br />
instanceOfObject.scale(vec3(2,2,2));<br />
instanceOfObject.scale(2,2,2);<br />
instanceOfObject.scale(2);<br />
instanceOfObject.addScale(vec3(1,1,1));<br />
instanceOfObject.addScale(1,1,1);<br />
instanceOfObject.addScale(1);<br />
--THE REST OF THE FUNCTIONS WILL COME LATER--
Page: GLGE-Wiki-Hompage.v.0.1