Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Danicco

Member Since 18 Jan 2011
Offline Last Active Apr 08 2013 10:48 PM
-----

Topics I've Started

Why is my Rotation not centered?

23 March 2013 - 04:36 PM

After much reading, trial and error, I managed to get the following code:

void Game::Draw()
{
    mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    mat4 mView = glm::lookAt(cameraPosition, cameraTarget, headsUp);

    mat4 mModel = mat4(1.0f);
    quat qModelRotation = quat(vec3(object.rotationX, object.rotationY, object.rotationZ)); //values are in degrees 0~360 already
    mat4 mModelRotation = mat4_cast(qModelRotation); //quat to mat4
    mModel = mModel * mModelRotation; //applying the rotation to my model
    //mModel = mModel * mModelTranslation * mModelRotation; //this if I put translation as well

    mat4 MVP = mProjection * mView * mModel;
    //send MVP to the shader, where gl_Position = vertex * MVP;
}

The object's rotating fine on the Z-axis, around it's center, but in the X and Y axis it's rotating around one of it's edges, why?

My object's a cube with values from -1 to 1 only.

 

I've been suggested to translate to the origin, rotating, and then translating, but I'm multiplying the rotation with the model without any translation and it's center is still off.


Matrices general questions

21 March 2013 - 11:42 AM

I've a very old project in openGL that used glRotateglTranslate, and other glMatrix functions, and I'm updating it to the more modern approach of using matrices for calculations.

 

But I'm having some problems because I have very little to no experience with matrices (I always avoided it in my older project, I preferred using basic trigonometry because I found it to be easier), and I'd like to ask these questions:

 

I'm calculating, on each frame, this matrix:





mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
mat4 mView = glm::lookAt(
    glm::vec3(_camera.posX, _camera.posY, _camera.posZ), //Camera Position		
    glm::vec3(_camera.posX + _camera.directionX, _camera.posY - _camera.directionY, _camera.posZ - _camera.directionZ), //Eye Position
    glm::vec3(0, 1, 0) //Head Up
);
mat4 mModel = glm::mat4(1.0f);
mat4 mMVP = mProjection * mView * mModel;	

I'd like to change my object's position, so I tried this:





mat4 mModel = glm::mat4(1, 0, 0, object->posX, 
                        0, 1, 0, object->posY, 
		        0, 0, 1, object->posZ,
		        0, 0, 0, 1);

Because I read that the last column's represents the x, y, z translating positions of the plane.

But I guess I'm mistaken, because with this my object seems "stuck" and doesn't move at all (and my navigation stops).

 

I thought of multiplying it by a vec3(posX, posY, posZ), but then wouldn't the result be a another vec3? I read that when multiplying matrices such as 4x4 and a 4x2, the result is a 4x2, so I don't know how I can calculate this. I guess rotations will be even more complicated...

 

Another question I have is, I had a targetX, targetY, targetZ for my camera object, and I did this:





glTranslatef(-_camera.targetX, -_camera.targetY, -_camera.targetZ); //go to the target
glRotatef(_camera.RotationX(), 1.0f, 0.0f, 0.0f); //rotateX
glRotatef(_camera.RotationY(), 0.0f, 1.0f, 0.0f); //rotateY
glTranslatef(-_camera.posX, -_camera.posY, -_camera.posZ); //to go camera position

//Loop to draw all objects, assuming "camera position" as the new identity

And this way I could control where I was looking to, and change the zoom or switch to first person by making the target = position.

With the matrices' calculations I posted above, I don't know how I can achieve this, since the camera's position I send to the mView should change according to where I'm looking, and the center target as well right?

Any help on how I can achieve this?

 

Any info on how can I get to understand matrices and it's relations to the world space is really appreciated, I'm reading some tutorials about it but it still feels very overwhelming, too much information that I can't relate to the world's coordinates.

 

Thanks!


Shaders and VBOs, Performance and Relation

19 March 2013 - 02:17 PM

I'm following some new tutorials about OpenGL, since the ones I had done before were way outdated (like 2000ish), and I have some questions about the Shader x VBO relation.

My vertex shader code is this:



#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP;

void main()
{
	//option 1
	vec4 v = vec4(vertexPosition_modelspace, 1);
	gl_Position = MVP * v;

	//option 2
	gl_Position.xyz = vertexPosition_modelspace;
	gl_Position.w = 1.0;
}

(Only one of the options is enabled, I just put both here to save some space)

And the drawing code is this:



glBindBuffer(GL_ARRAY_BUFFER, object->_VertexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->_VertexIndexID);
		
glEnableClientState(GL_VERTEX_ARRAY);
	
//glVertexPointer(3, GL_FLOAT, 0, 0); //I was using this originally
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glDrawElements(GL_TRIANGLES, object->GetTotalIndexes(), GL_UNSIGNED_INT, 0);
		
glDisableClientState(GL_VERTEX_ARRAY);

Most VBOs tutorials I find use the glVertexPointer() function, but it seems that when I have more than one array of values in the shader (like vertices, colors and normals), I need to use glVertexAttribPointer() instead, and match the first value with the location = X in the shader code.

I didn't understand very well what's this relation about, does this mean the values currently bound in the C++ code are sent to the layout(location) variable in my shader code?

 

Also, I've read a bunch of tutorials about OpenGL and topics about it, and I see plenty of them talking about matrix calculations and such, but I never bothered to understand what they meant. I had always reached the results I desired without them, but seeing I'm finding more and more about them in newer tutorials, I'm curious. For example, there's a tutorial that shows how to calculate the "view" of each object with matrices (and GLM):



mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
mat4 mView = glm::lookAt(
	glm::vec3(0, 0, 0), //Camera Position
	glm::vec3(0, 0, 0), //Looking to Position
	glm::vec3(0, 1, 0) //Head Up
	);
mat4 mModel = glm::mat4(1.0f); //model values
mat4 mMVP = mProjection * mView * mModel;

Then I send the mMVP to the shader, and using the option1 in the shader, I get the correct view for the object(s) on screen.

 

However, I was achieving the same doing this:



glLoadIdentity();
glTranslatef(-_camera.targetX, -_camera.targetY, -_camera.targetZ); //Looking to Position
glRotatef(_camera.RotationX(), 1.0f, 0.0f, 0.0f); //0-360 values for rotations
glRotatef(_camera.RotationY(), 0.0f, 1.0f, 0.0f);
glTranslatef(-_camera.posX, -_camera.posY, -_camera.posZ); //camera position

//Draw Objects

And with this, I used the option2 in the shader.

 

On my machine, option2 was about 25% faster.

But I don't know if the shader calculations are done by the graphics card (meaning my processor was faster at calculating them) or not (then it's the reverse).

Another option I was considering was not calculating the matrices in the C++ code, but just send them to the shader and let it calculate the final options, but I'm guessing this would be even slower.

 

I've read that I shouldn't use FPS as a "performance" check because it's inconstant, but I don't know any other way to compare methods and figure which is better.

 

So, what's the generally better accepted method?

If you have arguments on why should one use matrices instead of the glFunctions (although I've read that glRotatef() is being marked as deprecated), I'd love to hear it as well. Most tutorials and books don't say which is better, it's usually "do this and you get the results you want".

I plan on developing games for mobiles so I'm really concerned about performance and also for curiosity, I'd really like to understand this better.


Performance, VBOs and Objects

10 March 2013 - 05:51 PM

Hi,

 

I was making a simple game in C++ and OpenGL with what I learned from a bunch of pretty old tutorials (which I discovered later), and now I'm in the process of updating everything and I've got some questions about the VBO implementation.

 

I had this code:

 

class Vertex
{
    public:
        float posX, posY, posZ;
};
class Face
{
    public:
        Vertex** vertexes;
        //bunch of functions to get and set the vertexes and their values
};
class Object3D : public Object
{
    public:
        Face* faces;
        Vertex* vertexes;
        //bunch of functions to initialize the vertexes and faces
};

 

So my Object3D had an array of all vertexes informations, each with their X, Y and Z, and another array for Faces, which gave me the addresses of 3 Vertexes that composed 1 Face of the object.

It worked fine since I was calling:

 

void Game::Draw(Object3D* object)
{
    for(int i = 0; i < object->totalFaces; i++)
    {
        glVertex3f(object->faces[i].GetVertex[0]->posX, object->faces[i].GetVertex[0]->posY, object->faces[i].GetVertex[0]->posZ);
        glVertex3f(object->faces[i].GetVertex[1]->posX, object->faces[i].GetVertex[1]->posY, object->faces[i].GetVertex[1]->posZ);
        glVertex3f(object->faces[i].GetVertex[2]->posX, object->faces[i].GetVertex[2]->posY, object->faces[i].GetVertex[2]->posZ);
    }
}

 

But now that I'm going to turn everything into VBOs, I need a single array with the sequential float values of each of the object's faces, so it means I'll even have to repeat some vertex's values sometime when they share the same point.

This is where I'm at, I'm trying to figure what would be the best way to do this.

My animation code (unfinished) moved the vertexes according to the bone's movement and weight, and all I had to do was to change the values in my "vertexes[]" array of the Object3D, and I didn't had to worry about it's faces.

Now, I will have to change the new vertex data array I'll be buffering as VBO, and I'd like to get some ideas on how.

 

I have no experience releasing games, and I never seen a professional/released engine code before, so I've got literally no idea what's the common approach to this.

 

For now I'm thinking in getting something like this:

 

class Object3D : public class Object
{
    public:
        float* vertexData; // = new float[totalVertexes]; - this will be what I'll send/map to the VBO as data
        //same as before, vertex and faces arrays
};

 

And everytime there's a change in the model, I'll change the vertexes array as usual, and then update the values in "vertexData" and finally update the VBO.

This seems a bad idea as I'll be doing this for every animated object in the screen... and I'd be using twice as much memory for every model since I have two variables with the same values.

 

Isn't there a way to send the VBO data in a different manner? If it searched by index, I think I could create a class and override the brackets operator to send the address that I want, but since it's function asks for the size, I'm guessing it moves by memory size...

 

Is there any good approaches to this, or what's the best way to do this? (I'd like to totally scrap the vertexes array and only work with the vertexData I've sent/mapped to the VBO)


Heap Corruption with ifstream and delete[]

28 December 2012 - 02:28 PM

I have this code:

 



ifstream reader;
reader.open("myFile.bmp", ios::binary);

//Read the headers and stuff, fill some variables
unsigned char *bitmapArray = new unsigned char[imageWidth * imageHeight * 4]; //array with enough values for RGBA of the BMP

int c = 0;
while(!reader.eof())
{
    reader.read(pixelInfo, 3); //pixelInfo is a char[3]

    //Filling RGBA info into my bitmapArray
    bitmapArray[c] = pixeInfo[0];
    bitmapArray[c + 1] = pixeInfo[1];
    bitmapArray[c + 2] = pixeInfo[2];
    bitmapArray[c + 3] = 255; 
    c += 4; //increasing C for the next values
}
reader.close();

//Here I use bitmapArray for some stuff

delete[] bitmapArray; //This gives me HEAP CORRUPTION error

If I don't mess with bitmapArray inside the while loop (simply commenting 4 lines, still leaving the reader there), it goes through fine.

The size of the image I'm using is 40000 bytes long, if I declare the array as "unsigned char bitmapArray[40000];" I still get the same error, but if I add a single char it doesn't - "unsigned char bitmapArray[40001];"

I checked the address the debugger says it's corrupted, but it isn't nowhere close to the starting/ending addresses of bitmapArray.

 

The error is: "HEAP CORRUPTION DETECTED: after Normal block (#161) at 0x00413F58. CRT detected that the application wrote to memory after end of heap buffer."

 

Why is this happening?


PARTNERS