Jump to content
  • Advertisement
Sign in to follow this  
Brandon T

OpenGL 3 Questions: Matrices, VBO, Camera view.

This topic is 2144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'ved just started OpenGL and decided I'd dive right into hooking games and seeing if I can edit them and how they work. That's how I learn. So I've created an OpenGL wrapper of all functions including the extended ARB ones. I've used gDebugger and GLintercept to help figure out what extendeds are called.

I do not want to use glut. Anyway here's my questions:

1. Is there anyway for me to access the vertex buffers? I noticed gDebugger can somehow tell me exactly what the VBO's hold and I want to do this.

2. What are the 4 last rows in the modelview matrices for? I've read endless tutorials but not a single one tells me what it's for. The game I'm hooking uses matrices that look like this:

[source lang="cpp"]GL_VIEWPORT: {4, 165, 512, 334}

GL_MODELVIEW_MATRIX:
{0.99975264 , 0.0091297952, -0.020280767, 0}
{0 , 0.91186345 , 0.41049367 , 0}
{0.022241013 , -0.41039214 , 0.9116379 , 0}
{1819.2891 , -308.66016 , 5129.084 , 1} //These rows..

GL_PROJECTION_MATRIX:
{1.40625, -0, 0, 0}
{0, -2.1556885, 0, 0}
{0, -0, 1.0273149, 1}
{0, -0, -405.46298, 0} //And these ones..

GL_CURRENT_MATRIX_ARB:
{0.99975264 , 0.0091297952, -0.020280767, 0}
{0 , 0.91186345 , 0.41049367 , 0}
{0.022241013 , -0.41039214 , 0.9116379 , 0}
{1819.2891 , -308.66016 , 5129.084 , 1}[/source]

AND gDebugger shows:
[source lang="cpp"]glMultMatrixf([0.024158,-0.893824,0.447767,0.000000,0.000000,0.447898,0.894085,0.000000,-0.999708,-0.021599,0.010820,0.000000,25715.173828,26396.923828,-5469.418945,1.000000] ); [/source]


3. Finally, is there a way I can grab the camera position? I've noticed that GLIntercept can use freecam. How does it know what the current camera position & Look-At is inorder to let you move it?


This is what I have so far (I'm not sure how efficient or good my code is, but it's a good start I think?):
[source lang="cpp"]#include "RSHooks.hpp"

GLdouble CalculateAngle(GLdouble Size, GLdouble Distance)
{
GLdouble Rad = 2.0 * atan2(Size / 2.0, Distance);
return (180.0 * Rad) / 3.1415926535;
}

void PointOfView(GLdouble ObjectX, GLdouble ObjectY, GLdouble ObjectZ, GLdouble Roll, GLdouble Pitch, GLdouble Heading)
{
optr_glRotated(Roll, 0.0, 0.0, 1.0);
optr_glRotated(Pitch, 0.0, 1.0, 0.0);
optr_glRotated(Heading, 1.0, 0.0, 0.0);
optr_glTranslated(-ObjectX, -ObjectY, -ObjectZ);
}

void PolarView(GLdouble Distance, GLdouble Twist, GLdouble Elevation, GLdouble Azimuth)
{
optr_glTranslated(0.0, 0.0, -Distance);
optr_glRotated(-Twist, 0.0, 0.0, 1.0);
optr_glRotated(-Elevation, 1.0, 0.0, 0.0);
optr_glRotated(Azimuth, 0.0, 0.0, 1.0);
}

GLboolean WorldToScreen(GLfloat &X, GLfloat &Y, Vector3D World)
{
GLint ViewPort[4];
GLdouble Screen[3];
GLdouble ModelViewMatrix[16];
GLdouble ProjectionMatrix[16];

glGetIntegerv(GL_VIEWPORT, ViewPort);
glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, ProjectionMatrix);

if(gluProject(World.X, World.Y, World.Z, ModelViewMatrix, ProjectionMatrix, ViewPort, &Screen[0], &Screen[1], &Screen[2]) == GL_TRUE)
{
X = Screen[0];
Y = ViewPort[3] - Screen[1];
return GL_TRUE;
}
return GL_FALSE;
}

GLboolean WorldToScreen(GLfloat &X, GLfloat &Y, Vector3D World, GLdouble* ModelViewMatrix, GLdouble* ProjectionMatrix)
{
GLint ViewPort[4];
GLdouble Screen[3];
glGetIntegerv(GL_VIEWPORT, ViewPort);

if(gluProject(World.X, World.Y, World.Z, ModelViewMatrix, ProjectionMatrix, ViewPort, &Screen[0], &Screen[1], &Screen[2]) == GL_TRUE)
{
X = Screen[0];
Y = ViewPort[3] - Screen[1];
return GL_TRUE;
}
return GL_FALSE;
}

GLboolean ScreenToWorld(GLfloat X, GLfloat Y, Vector3D &World)
{
GLfloat Z = 0;
GLint ViewPort[4];
GLdouble Screen[3];
GLdouble ModelViewMatrix[16];
GLdouble ProjectionMatrix[16];

glGetIntegerv(GL_VIEWPORT, ViewPort);
glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, ProjectionMatrix);

optr_glReadPixels(X, ViewPort[3] - Y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &Z);
if (gluUnProject(X, ViewPort[3] - Y - 1, Z, ModelViewMatrix, ProjectionMatrix, ViewPort, &Screen[0], &Screen[1], &Screen[3]) == GL_TRUE)
{
World.X = Screen[0];
World.Y = Screen[1];
World.Z = Screen[2];
return GL_TRUE;
}
return GL_FALSE;
}

GLboolean ScreenToWorld(GLfloat X, GLfloat Y, Vector3D &World, GLdouble* ModelViewMatrix, GLdouble* ProjectionMatrix)
{
GLfloat Z = 0;
GLint ViewPort[4];
GLdouble Screen[3];
glGetIntegerv(GL_VIEWPORT, ViewPort);

optr_glReadPixels(X, ViewPort[3] - Y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &Z);
if (gluUnProject(X, ViewPort[3] - Y - 1, Z, ModelViewMatrix, ProjectionMatrix, ViewPort, &Screen[0], &Screen[1], &Screen[3]) == GL_TRUE)
{
World.X = Screen[0];
World.Y = Screen[1];
World.Z = Screen[2];
return GL_TRUE;
}
return GL_FALSE;
}

GL_EXPORT __stdcall void GLHook_glBindBufferARB(GLenum target, GLuint buffer)
{
BufferBound = true;
CurrentBuffer.Type = target;
CurrentBuffer.ID = buffer;

(*optr_glBindBufferARB) (target, buffer);
}

GL_EXPORT __stdcall void GLHook_glBindTexture(GLenum target, GLuint texture)
{
if (LogCalls) glLogCalls("glBindTexture: %d, %d\n", target, texture);

if (target == GL_TEXTURE_RECTANGLE) //GL_TEXTURE_2D
{
TextureFound = texture;
PI.TID = texture;
(*optr_glBindTexture) (target, texture);
PI.ID = GLHook_PanelItemCheckSum(target, 32);
return;
}
(*optr_glBindTexture) (target, texture);
}

GL_EXPORT __stdcall void GLHook_glBufferDataARB(GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage)
{
if (BufferBound && CurrentBuffer.Type == target && target == GL_ARRAY_BUFFER)
{
BufferBound = false;
CurrentBuffer.Size = size;
CurrentBuffer.Usage = usage;
ListOfBuffers.push_back(CurrentBuffer);
}
(*optr_glBufferDataARB) (target, size, data, usage);
}

GL_EXPORT __stdcall void GLHook_glBufferSubDataARB(GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid *data)
{
(*optr_glBufferSubDataARB) (target, offset, size, data);
}

GL_EXPORT __stdcall void GLHook_glCallList(GLuint list)
{
for (std::vector<FontChar>::iterator it = ListOfFonts.begin(); it != ListOfFonts.end(); ++it)
{
if (it->ID == list)
{
CurrentFontData.ID = it->ID;
for (int I = 0; I < 4; I++)
{
CurrentFontData.Quad.VX = it->Quad.VX;
CurrentFontData.Quad.VY = it->Quad.VY;
CurrentFontData.Quad.TX = it->Quad.TX;
CurrentFontData.Quad.TY = it->Quad.TY;
}
CurrentFontData.Symbol = (it->Symbol - 32);
FontData.push_back(CurrentFontData);
break;
}
}

(*optr_glCallList) (list);
}

GL_EXPORT __stdcall void GLHook_glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
{
if (!red && !green && !blue)
{
CurrentFontData.Shadow = GL_TRUE;
CurrentFontData.Rendering = GL_FALSE;
}
else
{
CurrentFontData.Shadow = GL_FALSE;
CurrentFontData.Rendering = GL_TRUE;
}

CurrentFontData.Colour = {red, green, blue, alpha};

(*optr_glColor4ub) (red, green, blue, alpha);
}

GL_EXPORT __stdcall void GLHook_glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
{
if (LogCalls) glLogCalls("glDrawElements: %d, %i, %d, %i\n", mode, count, type, indices);
if (ModelRendering)
{
Model* ModelPtr = &ListOfModels.back();
ModelPtr->ID = 0;
ModelPtr->TriangleCount = count / 3;

if (!ModelPtr->NullVertex)
{
const GLfloat* Pointer = static_cast<const GLfloat*>(ModelPtr->VertexPointer); //Can be used to iterate each vertex..
ModelPtr->X = *Pointer + ModelPtr->Stride;
ModelPtr->Y = *Pointer + ModelPtr->Stride + sizeof(GLfloat);
ModelPtr->Z = *Pointer + ModelPtr->Stride + (2 * sizeof(GLfloat));
}

if ((ModelPtr->Stride == Stride) && Overlay)
optr_glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
optr_glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
else
optr_glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
(*optr_glDrawElements) (mode, count, type, indices);
}

GL_EXPORT __stdcall void GLHook_glEnd(void)
{
if (CompassFound)
{
CompassFound = false;
CompassAngle.Compass.VertexCount = 0;
}

if (TextureFound)
{
if (PI.CoordCount == 4)
{
AreaFilter = {PI.TID, 550, 260, 740, 518};

CurrentItem.ID = PI.ID;
CurrentItem.X = PI.SX[0];
CurrentItem.Y = PI.SY[0];
CurrentItem.TextureID = PI.TID;

for (int I = 0; I < 4; I++)
{
CurrentItem.SX = PI.SX;
}

ListOfItems.push_back(CurrentItem);
}
PI.CoordCount = 0;
TextureFound = false;
}

(*optr_glEnd) ();
}

GL_EXPORT __stdcall void GLHook_glEndList(void)
{
if (FontFound)
{
CurrentFont.VCount = 0;
CurrentFont.TCount = 0;
CurrentFont.LastFont = false;
if (!ListOfFonts.size())
CurrentFont.Symbol = 32;
else
CurrentFont.Symbol = (!ListOfFonts.back().LastFont) ? ListOfFonts.back().Symbol + 1 : 32;

if (++FontListCount >= 255)
{
CurrentFont.LastFont = true;
FontListCount = 0;
FontFound = false;
}

ListOfFonts.push_back(CurrentFont);
}
(*optr_glEndList) ();
}

GL_EXPORT __stdcall void GLHook_glGenBuffersARB(GLsizei n, GLuint *buffers)
{
(*optr_glGenBuffersARB) (n, buffers);
}

GL_EXPORT __stdcall GLuint GLHook_glGenLists(GLsizei range)
{
if (range == 256)
{
FontListCount = 0;
FontFound = true;
}
return (*optr_glGenLists) (range);
}

GL_EXPORT __stdcall void GLHook_glMultiTexCoord2fARB(GLenum target, GLfloat s, GLfloat t)
{
if (target == GL_TEXTURE0 && (s == 51 || t == 51))
{
CompassFound = true;
CompassAngle.Compass.Found = true;
}
(*optr_glMultiTexCoord2fARB) (target, s, t);
}

GL_EXPORT __stdcall void GLHook_glNewList(GLuint list, GLenum mode)
{
if (FontFound)
{
CurrentFont.ID = list;
}
(*optr_glNewList) (list, mode);
}

GL_EXPORT __stdcall void GLHook_glPopMatrix(void)
{
if (ModelRendering)
{
GLfloat X = 0, Y = 0;
Model* ModelPtr = &ListOfModels.back();
Vector3D World = Vector3D(ModelPtr->X, ModelPtr->Y, ModelPtr->Z);
if (WorldToScreen(X, Y, World))
{
ModelPtr->SX = X;
ModelPtr->SY = Y;
}
}
ModelRendering = false;
(*optr_glPopMatrix) ();
}

GL_EXPORT __stdcall void GLHook_glTexCoord2f(GLfloat s, GLfloat t)
{
if (FontFound)
{
int I = ++CurrentFont.TCount;
if (I < 5)
{
CurrentFont.Quad.TX[I - 1] = s;
CurrentFont.Quad.TY[I - 1] = t;
}
}
(*optr_glTexCoord2f) (s, t);
}

GL_EXPORT __stdcall void GLHook_glTranslatef(GLfloat x, GLfloat y, GLfloat z)
{
CurrentFontData.Translate[0] = x;
CurrentFontData.Translate[1] = y;
CurrentFontData.Translate[2] = z;
(*optr_glTranslatef) (x, y, z);
}

GL_EXPORT __stdcall void GLHook_glVertex2f(GLfloat x, GLfloat y)
{
if (CompassFound)
{
int I = ++CompassAngle.Compass.VertexCount;
if (I < 5)
{
CompassAngle.Compass.VX[I - 1] = x;
CompassAngle.Compass.VY[I - 1] = y;
}
}
(*optr_glVertex2f) (x, y);
}

GL_EXPORT __stdcall void GLHook_glVertex2i(GLint x, GLint y)
{
if (LogCalls) glLogCalls("glVertex2i: %i, %i\n", x, y);

if (TextureFound)
{
int I = ++PI.CoordCount;
if (I < 5)
{
PI.SX[I - 1] = x;
PI.SY[I - 1] = y;
}
}

if (FontFound)
{
int I = ++CurrentFont.VCount;
if (I < 5)
{
CurrentFont.Quad.VX[I - 1] = x;
CurrentFont.Quad.VY[I - 1] = y;
}
}

(*optr_glVertex2i) (x, y);
}

GL_EXPORT __stdcall void GLHook_glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
{
if (LogCalls) glLogCalls("glVertexPointer: %i, %d, %i, %i\n", size, type, stride, pointer);
ModelRendering = true;
CurrentModel.Stride = stride;
CurrentModel.NullVertex = (pointer == NULL);
CurrentModel.VertexPointer = pointer; //Store Our Pointer for later calculations.
ListOfModels.push_back(CurrentModel);

(*optr_glVertexPointer) (size, type, stride, pointer);
}

GL_EXPORT __stdcall PROC GLHook_wglGetProcAddress(LPCSTR lpszProc)
{
std::string FunctionCalled(lpszProc);
if (FunctionCalled == "glBindBufferARB")
{
optr_glBindBufferARB = (ptr_glBindBufferARB) optr_wglGetProcAddress(lpszProc);
return (FARPROC) GLHook_glBindBufferARB;
}
else if (FunctionCalled == "glBufferDataARB")
{
optr_glBufferDataARB = (ptr_glBufferDataARB) optr_wglGetProcAddress(lpszProc);
return (FARPROC) GLHook_glBufferDataARB;
}
else if (FunctionCalled == "glBufferSubDataARB")
{
optr_glBufferSubDataARB = (ptr_glBufferSubDataARB) optr_wglGetProcAddress(lpszProc);
return (FARPROC) GLHook_glBufferSubDataARB;
}
else if (FunctionCalled == "glMultiTexCoord2fARB")
{
optr_glMultiTexCoord2fARB = (ptr_glMultiTexCoord2fARB) optr_wglGetProcAddress(lpszProc);
return (FARPROC) GLHook_glMultiTexCoord2fARB;
}
return (*optr_wglGetProcAddress) (lpszProc);
}

GL_EXPORT __stdcall BOOL GLHook_wglSwapBuffers(HDC hdc)
{
Commands();
if (LogCalls) glLogCalls("wglSwapBuffers");

if (Overlay)
{
glColor4f(0.0f, 1.0f, 1.0f, 1.0f);

for (std::vector<PanelItem>::iterator it = ListOfItems.begin(); it != ListOfItems.end(); ++it)
{
if (it->X > AreaFilter.X1 && it->Y > AreaFilter.Y1 && it->X < AreaFilter.X2 && it->Y < AreaFilter.Y2)
{
glPrint(it->X, it->Y + 18, "%i", it->ID);
}
}

for (std::vector<Model>::iterator it = ListOfModels.begin(); it != ListOfModels.end(); ++it)
{
if ((Overlay && (it->Stride == Stride)) || (Overlay && DrawAllStrides))
{
glPrint(it->SX, it->SY + 224, "%i", it->ID);
}
}

for (std::vector<FontInfo>::iterator it = FontData.begin(); it != FontData.end(); ++it)
{
if (it->Colour.R != 0 && it->Colour.G != 0 && it->Colour.B != 0) //Remove this later.. Just for testing.
{
glPrint(it->Translate[0] + it->Quad.VX[1], it->Translate[1] + it->Quad.VY[1], "%c", it->Symbol);
}
}

if (CompassAngle.Compass.Found)
{
CompassAngle.Compass.RadianAngle = 0;
CompassAngle.Compass.DegreeAngle = 0;
if (CompassAngle.Compass.VX[3] - CompassAngle.Compass.VX[0])
{
CompassAngle.Compass.RadianAngle = atan2f(CompassAngle.Compass.VY[3] - CompassAngle.Compass.VY[0], CompassAngle.Compass.VX[3] - CompassAngle.Compass.VX[0]);
CompassAngle.Compass.RadianAngle += CompassAngle.Compass.RadianAngle < 1 ? (2 * 3.1415926535) : 0; //A bit buggy. Degress is more accurate.
CompassAngle.Compass.DegreeAngle = (CompassAngle.Compass.RadianAngle * (180 / 3.1415926535));
}
glPrint(518, 48, "Deg: %f", CompassAngle.Compass.DegreeAngle);
glPrint(518, 58, "Rad: %f", CompassAngle.Compass.RadianAngle);
}
}

FontData.clear();
ListOfItems.clear();
ListOfModels.clear();
CompassAngle.Compass.Found = false;

return (*optr_wglSwapBuffers) (hdc);
}
[/source] Edited by Brandon T

Share this post


Link to post
Share on other sites
Advertisement
The last row holds the translation if your using row major matrices. if you multiply a model(world) matrix with the view matrix you rotate, (scale) and translate the object relative to the camera. the last row defines how much to translate the object to get it relative to the camera in view space. Edited by Wilds

Share this post


Link to post
Share on other sites

The last row holds the translation if your using row major matrices. if you multiply a model(world) matrix with the view matrix you rotate, (scale) and translate the object relative to the camera. the last row defines how much to translate the object to get it relative to the camera in view space.



Ohhh I see. Wonder why they'd use Row Major :S. That being said, if I untranslate it somehow (maybe get the first matrix before the multiplication is done), would I get the camera position? What would it give me?

I find it odd you mention row major because their default matrix is:

[source lang="cpp"][1] [0] [0] [0]
[0] [1] [0] [0]
[0] [0] [1] [0]
[0] [0] [0] [1][/source] Edited by Brandon T

Share this post


Link to post
Share on other sites
The default matrix (identity matrix) is the same in both column and row matrices.

I think you can get the position of the camera by getting the view matrix.
And doing the following:

PSeudo code:

CamPos.x = -(RightVector.Dot(PositionVector));
CamPos.y = -(UpVector.Dot(PositionVector));
CamPos.z = -(ForwardVector.Dot(PositionVector));


The .Dot is the dot product between vectors.

RightVector = first column of the view matrix (first 3 elements not the 4th)
UpVector = second column of the view matrix (first 3 elements not the 4th)
ForwardVector = third column of the view matrix (first 3 elements not the 4th);

PositionVector = the 4th column of the view matrix (first 3 elements not the 4th);

If your using row major matrices you should replace the column in the above sentences with rows.

Share this post


Link to post
Share on other sites

The default matrix (identity matrix) is the same in both column and row matrices.

I think you can get the position of the camera by getting the view matrix.
And doing the following:

PSeudo code:

CamPos.x = -(RightVector.Dot(PositionVector));
CamPos.y = -(UpVector.Dot(PositionVector));
CamPos.z = -(ForwardVector.Dot(PositionVector));


The .Dot is the dot product between vectors.

RightVector = first column of the view matrix (first 3 elements not the 4th)
UpVector = second column of the view matrix (first 3 elements not the 4th)
ForwardVector = third column of the view matrix (first 3 elements not the 4th);

PositionVector = the 4th column of the view matrix (first 3 elements not the 4th);

If your using row major matrices you should replace the column in the above sentences with rows.


Ahh well I just tried just now. I struggled badly. I cannot figure out how to get the view matrix.

I created a class for both matrices and vectors.. it isn't complete yet but I got stuck trying to figure out what the view matrix is. Or what I should do to get it.
I've tried logging glFrustrum but the game doesn't seem to use that at all. I've studied everything you told me and just finished reading up on the modelview matrix. It seems it is a combination of the view matrix and object matrix. Is there no way I can extract it?

VECTOR3D:
[source lang="cpp"]#include "Vector3D.hpp"

Vector3D::~Vector3D() {}

Vector3D::Vector3D() : X(0), Y(0), Z(0) {}

Vector3D::Vector3D(double x, double y, double z) : X(x), Y(y), Z(z) {}

Vector3D& Vector3D::operator ()(double x, double y, double z)
{
if (X != x && Y != y && Z != z)
{
X = x;
Y = y;
Z = z;
}
return *this;
}

bool Vector3D::operator == (const Vector3D& V) const
{
return ((X == V.X) && (Y == V.Y) && (Z == V.Z));
}

bool Vector3D::operator != (const Vector3D& V) const
{
return ((X != V.X) && (Y != V.Y) && (Z != V.Z));
}

Vector3D& Vector3D::operator = (const Vector3D& V)
{
if (this != &V)
{
X = V.X;
Y = V.Y;
Z = V.Z;
}
return *this;
}

Vector3D& Vector3D::operator += (const int& W)
{
if (W > 0)
{
X += W;
Y += W;
Z += W;
}
return *this;
}

Vector3D& Vector3D::operator += (const Vector3D& V)
{
X += V.X; Y += V.Y; Z += V.Z;
return *this;
}

Vector3D& Vector3D::operator -= (const int& W)
{
X -= W; Y -= W; Z -= W;
return *this;
}

Vector3D& Vector3D::operator -= (const Vector3D& V)
{
X -= V.X; Y -= V.Y; Z -= V.Z;
return *this;
}

Vector3D& Vector3D::operator *= (const int& W)
{
X *= W; Y *= W; Z *= W;
return *this;
}

double Vector3D::operator *= (const Vector3D& V)
{
return ((X * V.X) + (Y * V.Y) + (Z * V.Z));
}

Vector3D& Vector3D::operator /= (const int& W)
{
if (W != 0)
{
X /= W;
Y /= W;
Z /= W;
}
return *this;
}

Vector3D& Vector3D::operator ^= (const Vector3D& V)
{
X = (Y * V.Z) - (Z * V.Y);
Y = (Z * V.X) - (X * V.Z);
Z = (X * V.Y) - (Y * V.X);
return *this;
}

double Vector3D::Dot(const Vector3D& V)
{
return Vector3D(*this) *= V;
}

Vector3D& Vector3D::Cross(const Vector3D& V)
{
return Vector3D(*this) ^= V;
}

double Vector3D::Magnitude() const
{
return sqrtf((X * X) + (Y * Y) + (Z * Z));
}

double Vector3D::AngleRad(const Vector3D& V)
{
double I = Magnitude();
double J = V.Magnitude();
return (I == 0.0 || J == 0.0) ? 0.0 : acosf(Dot(V) / (I * J));
}

double Vector3D::AngleDeg(const Vector3D& V)
{
return (AngleRad(V) * (180 / 3.14159265)); //PI = 4 * atan(1);
}

Vector3D& Vector3D::Normalize()
{
double Size = this->Magnitude();
if (Size != 0)
{
X /= Size;
Y /= Size;
Z /= Size;
}
return *this;
}

Vector3D& Vector3D::operator + (const int& W)
{
return Vector3D(*this) += W;
}

Vector3D& Vector3D::operator + (const Vector3D& V)
{
return Vector3D(*this) += V;
}

Vector3D& Vector3D::operator - (const int& W)
{
return Vector3D(*this) -= W;
}

Vector3D& Vector3D::operator - (const Vector3D& V)
{
return Vector3D(*this) -= V;
}

Vector3D& Vector3D::operator * (const int& W)
{
return Vector3D(*this) *= W;
}

double Vector3D::operator * (const Vector3D& V)
{
return (*this *= V);
}

Vector3D& Vector3D::operator ^ (const Vector3D& V)
{
return Vector3D(*this) ^= V;
}

Vector3D& Vector3D::operator / (const int& W)
{
return Vector3D(*this) /= W;
}
[/source]


Matrices:
[source lang="cpp"]#include "Matrix.hpp"

Matrix::~Matrix(){}

Matrix::Matrix(uint32_t Rows, uint32_t Columns) : Data(0)
{
Data.resize(Rows);
for (std::vector<std::vector<double>>::iterator it = Data.begin(); it != Data.end(); ++it)
it->resize(Columns);
}

Matrix& Matrix::operator ()(uint32_t Rows, uint32_t Columns)
{
Data.resize(Rows);
for (std::vector<std::vector<double>>::iterator it = Data.begin(); it != Data.end(); ++it)
it->resize(Columns);
return *this;
}

Matrix::Dimensional Matrix::operator [](size_t I)
{
return Dimensional(*this, I);
}

bool Matrix::operator == (const Matrix& M) const
{
if (M.Data.size() == Data.size())
{
for (size_t I = 0; I < Data.size(); I++)
{
for (size_t J = 0; J < Data.size(); J++)
{
if (Data[J] != M.Data[J])
return false;
}
}
}
return true;
}

bool Matrix::operator != (const Matrix& M) const
{
return !(*this == M);
}

Matrix& Matrix::operator = (const Matrix& M)
{
Data = M.Data;
return *this;
}

Matrix& Matrix::operator += (const Matrix& M)
{
if (Data.size() == M.Data.size())
{
for (size_t I = 0; I < Data.size(); I++)
{
for (size_t J = 0; J < Data.size(); J++)
{
Data[J] += M.Data[J];
}
}
}
return *this;
}

Matrix& Matrix::operator -= (const Matrix& M)
{
if (Data.size() == M.Data.size())
{
for (size_t I = 0; I < Data.size(); I++)
{
for (size_t J = 0; J < Data.size(); J++)
{
Data[J] -= M.Data[J];
}
}
}
return *this;
}

Matrix& Matrix::operator *= (const int& I)
{
for (std::vector<std::vector<double>>::iterator Rows = Data.begin(); Rows != Data.end(); ++Rows)
{
for (std::vector<double>::iterator Columns = Rows->begin(); Columns != Rows->end(); ++Columns)
{
*Columns *= I;
}
}
return *this;
}

Matrix& Matrix::operator *= (const Matrix& M)
{
//Matrix Product = Matrix(Data.size(), M.Data[0].size());
if (Data.size() == M.Data[0].size())
{
for (size_t I = 0; I < Data.size(); I++)
{
for (size_t J = 0; J < M.Data[0].size(); J++)
{
double SumElements = 0.0;
for (size_t K = 0; K < Data.size(); K++)
{
SumElements += Data[K] * M.Data[K][J];
}
Data[J] = SumElements;
}
}
}
return *this;
}

Matrix& Matrix::operator + (const Matrix& M)
{
return Matrix(*this) += M;
}

Matrix& Matrix::operator - (const Matrix& M)
{
return Matrix(*this) -= M;
}

Matrix& Matrix::operator * (const int& I)
{
return Matrix(*this) *= I;
}

Matrix& Matrix::operator * (const Matrix& M)
{
return Matrix(*this) *= M;
}
[/source] Edited by Brandon T

Share this post


Link to post
Share on other sites
Is there no way I can extract it?[/quote]
No.

I assume that you are manipulated a game by using those 2 applications? That is actually in my opinion the worst way to learn opengl. Start from the ground up because most of the things you are asking are completely random in order. Learn things step by step instead of just "what happens if I change this number". A book should help you to tell you what those numbers are and how to use them.

Share this post


Link to post
Share on other sites

Is there no way I can extract it?

No.

I assume that you are manipulated a game by using those 2 applications? That is actually in my opinion the worst way to learn opengl. Start from the ground up because most of the things you are asking are completely random in order. Learn things step by step instead of just "what happens if I change this number". A book should help you to tell you what those numbers are and how to use them.
[/quote]

I'm using the two applications to get debugging information so I can use it to manipulate the game. I've learned the basics I think? That's how I was able to write the application above.

I just have a hard time understanding the matrices and how to GET them from the game itself (Not GetFloatv(GLModelView_Matrix, &MV[0])). I meant like getting a camera position from an application that isn't mine. Same with VertexBuffers.

I know how to do it if it was my own application (Just store it at the beginning and transform it whenever). What I do not know is how to do it for other applications. GLIntercept is able to move the game's camera around and I'm trying to achieve the same effect by hooking the game too. gDebugger is able to grab a VBO's data and display it like this (something I'm attempting to do but do not know where to start, I can grab textures.. Just not buffer data):

YYlaR.png

aCjXY.png

Share this post


Link to post
Share on other sites
=] I injected. Can you tell me if the below is good for drawing text? It works on my ATI cards but not on Intel/NVidia :S I have no clue why..


[source lang="cpp"]
void glPrint(int X, int Y, const char *Format, ...)
{
if (!FontsBuilt) BuildFonts();
if (Format == NULL) return;

optr_glRasterPos2i(X, Y);

char Text[256];
va_list ap;

va_start(ap, Format);
vsprintf(Text, Format, ap);
va_end(ap);

optr_glPushAttrib(GL_LIST_BIT);
optr_glListBase(Base - 32);
optr_glCallLists(strlen(Text), GL_UNSIGNED_BYTE, Text);
optr_glPopAttrib();
}[/source]


I solved the VertexBuffer Injection. Now the only thing I have left is learning the camera matrix! ={ Edited by Brandon T

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Similar Content

    • By McGrane
      Hey
      My laptop recently decided to die, so Ive been transferring my project to my work laptop just to get it up to date, and commit it. I was banging my head against the wall all day, as my textures where not displaying in my program- I was getting no errors and no indication of why it was occurring so I have been just trying to figure it out- I know the image loading was working ok, as im using image data elsewhere, I was pretty confident that the code was fine also, as ive never had an issue with displaying textures before, so I thought it might be the drivers on this laptop, (my old one was just using the built in IntelHD, while this laptop has a NVIDIA graphics card) but all seems to be up to date.
      Below are my basic shaders:
      Vertex Shader
      #version 330 core layout(location = 0) in vec3 position; layout(location = 1) in vec3 color; layout(location = 2) in vec3 normal; layout(location = 3) in vec2 texCoord; uniform mat4 Projection; uniform mat4 Model; out vec3 Color; out vec3 Normal; out vec2 TexCoord; void main() { gl_Position = Projection * Model * vec4( position, 1.0 ); Color = color; Normal = normal; TexCoord = vec2( texCoord.x, texCoord.y); } Fragment Shader
      #version 330 core in vec3 Color; in vec3 Normal; in vec2 TexCoord; uniform sampler2D textureData; void main() { vec4 textureColor = texture( textureData, TexCoord ); vec4 finalColor = textureColor * vec4( Color, 1.0f); gl_FragColor = finalColor; } Calling Code
      glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureID); glUniform1i(glGetUniformLocation(shaderID, "textureData"), textureID); Now this is the part i dont understand, I worked through my program, until I got to the above 'Calling Code'. This just displays a black texture.. my original issue. Out of desperation, I just tried changing the name in glGetUniformLocation from "textureData" to "textureData_invalid" to see if my error checks would through up something, but in actual fact, it is now displaying the texture as expected. Can anyone fathom a guess as too why this is occurring.. im assuming the random text is just picking up the correct location by c++ witchcraft, but why is the original one not getting picked up correctly and/or not working as expected
      I realize more code is probably needed to see how it all hangs together.. but it seems to come down to this as the issue
    • By QQemka
      Hello. So far i got decently looking 3d scene. I also managed to render a truetype font, on my way to implementing gui (windows, buttons and textboxes). There are several issues i am facing, would love to hear your feedback.
      1) I render text using atlas with VBO containing x/y/u/v of every digit in the atlas (calculated basing on x/y/z/width/height/xoffset/yoffset/xadvance data in binary .fnt format file, screenshot 1). I generated a Comic Sans MS with 32 size and times new roman with size 12 (screenshot 2 and 3). The first issue is the font looks horrible when rescaling. I guess it is because i am using fixed -1 to 1 screen space coords. This is where ortho matrix should be used, right?
      2) Rendering GUI. Situation is similar to above. I guess the widgets should NOT scale when scaling window, am i right? So what am i looking for is saying "this should be always in the middle, 200x200 size no matter the display window xy", and "this should stick to the bottom left corner". Is ortho matrix the cure for all such problems?
      3) The game is 3D but i have to go 2D to render static gui elements over the scene - and i want to do it properly! At the moment i am using matrix 3x3 for 2d transformations and vec3 for all kinds of coordinates. In shaders tho i technically still IS 3D. I have to set all 4 x y z w of the gl_Position while it would be much much more conventient to... just do the maths in 2d space. Can i achieve it somehow?
      4) Text again. I am kind of confused what is the reason of artifacts in Times New Roman font displaying (screenshot 1). I render from left to right, letter after letter. You can clearly see that letters on the right (so the ones rendered after ones on the left are covered by the previous one). I was toying around with blending options but no luck. I do not support kerning at the moment but that's definitely not the cause of error. The display of the small font looks dirty aliased too. I am knd of confused how to interpret the integer data and how should be scaled/adapted to the screen view. Is it just store the data as constant size and again - use ortho matrix?
      Thanks in advance for all your ideas and suggestions!
      https://i.imgur.com/4rd1VC3.png
      https://i.imgur.com/uHrSXfe.png
      https://i.imgur.com/xRTffPn.png
    • By plz717
      Hello, everyone! I hope my problem isn't too 'beginnerish'. I'm doing research on motion synthesis now, trying to implement the Deep Mimic paper (DeepMimic) by BINPENG XUE, in this paper, I need to first retarget character A's motion to another character B to make the reference motion clips for character B, since we don't have character B‘s reference motion. The most important thing is that in the paper, the author copied character A's joint's rotation with respective to joint's local coordinate system (not the parent) to character B. In my personal understanding, the joint's rotation with respective to joint's local coordinate system is something like that in the attached photo, where for the Elbow joint, i need to get the Elbow's rotation in the elbow's local coordinate system (i'm very grateful for you to share your ideas if i have misunderstanding about it 🙂)
      I have searched many materials on the internet about how to extract the local joint's information from FBX, the most relative one i found is the pivot rotation( and geometric transformation, object offset transformation). I'm a beginner in computer graphics, and i'm confused about whether the pivot rotation( or geometric transformation, object offset transformation) is exactly the joint's local rotation i'm seeking? I hope someone that have any ideas can help me, I'd be very grateful for any pointers in the right direction. Thanks in advance! 

    • By nOoNEE
      hello guys , i have some questions  what does glLinkProgram  and  glBindAttribLocation do?  i searched but there wasnt any good resource 
    • By owenjr
      Hi, I'm a Multimedia Engineering student. I am about to finish my dergree and I'm already thinking about what topic to cover in my final college project.
      I'm interested in the procedural animation with c++ and OpenGL of creatures, something like a spider for example. Can someone tell me what are the issues I should investigate to carry it out? I understand that it has some dependence on artificial intelligence but I do not know to what extent. Can someone help me to find information about it? Thank you very much.
       
      Examples: 
      - Procedural multi-legged walking animation
      - Procedural Locomotion of Multi-Legged Characters in Dynamic Environments
    • By Lewa
      So, i'm still on my quest to unterstanding the intricacies of HDR and implementing this into my engine. Currently i'm at the step to implementing tonemapping. I stumbled upon this blogposts:
      http://filmicworlds.com/blog/filmic-tonemapping-operators/
      http://frictionalgames.blogspot.com/2012/09/tech-feature-hdr-lightning.html
      and tried to implement some of those mentioned tonemapping methods into my postprocessing shader.
      The issue is that none of them creates the same results as shown in the blogpost which definitely has to do with the initial range in which the values are stored in the HDR buffer. For simplicity sake i store the values between 0 and 1 in the HDR buffer (ambient light is 0.3, directional light is 0.7)
      This is the tonemapping code:
      vec3 Uncharted2Tonemap(vec3 x) { float A = 0.15; float B = 0.50; float C = 0.10; float D = 0.20; float E = 0.02; float F = 0.30; return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F; } This is without the uncharted tonemapping:
      This is with the uncharted tonemapping:
      Which makes the image a lot darker.
      The shader code looks like this:
      void main() { vec3 color = texture2D(texture_diffuse, vTexcoord).rgb; color = Uncharted2Tonemap(color); //gamma correction (use only if not done in tonemapping code) color = gammaCorrection(color); outputF = vec4(color,1.0f); } Now, from my understanding is that tonemapping should bring the range down from HDR to 0-1.
      But the output of the tonemapping function heavily depends on the initial range of the values in the HDR buffer. (You can't expect to set the sun intensity the first time to 10 and the second time to 1000 and excpect the same result if you feed that into the tonemapper.) So i suppose that this also depends on the exposure which i have to implement?
      To check this i plotted the tonemapping curve:
      You can see that the curve goes only up to around to a value of 0.21 (while being fed a value of 1) and then basically flattens out. (which would explain why the image got darker.)
       
      My guestion is: In what range should the values in the HDR buffer be which then get tonemapped? Do i have to bring them down to a range of 0-1 by multiplying with the exposure?
      For example, if i increase the values of the light by 10 (directional light would be 7 and ambient light 3) then i would need to divide HDR values by 10 in order to get a value range of 0-1 which then could be fed into the tonemapping curve. Is that correct?
    • By nOoNEE
      i am reading this book : link
      in the OpenGL Rendering Pipeline section there is a picture like this: link
      but the question is this i dont really understand why it is necessary to turn pixel data in to fragment and then fragment into pixel could please give me a source or a clear Explanation that why it is necessary ? thank you so mu
       
       
    • By Inbar_xz
      I'm using the OPENGL with eclipse+JOGL.
      My goal is to create movement of the camera and the player.
      I create main class, which create some box in 3D and hold 
      an object of PlayerAxis.
      I create PlayerAxis class which hold the axis of the player.
      If we want to move the camera, then in the main class I call to 
      the func "cameraMove"(from PlayerAxis) and it update the player axis.
      That's work good.
      The problem start if I move the camera on 2 axis, 
      for example if I move with the camera right(that's on the y axis)
      and then down(on the x axis) -
      in some point the move front is not to the front anymore..
      In order to move to the front, I do
      player.playerMoving(0, 0, 1);
      And I learn that in order to keep the front move, 
      I need to convert (0, 0, 1) to the player axis, and then add this.
      I think I dont do the convert right.. 
      I will be glad for help!

      Here is part of my PlayerAxis class:
       
      //player coordinate float x[] = new float[3]; float y[] = new float[3]; float z[] = new float[3]; public PlayerAxis(float move_step, float angle_move) { x[0] = 1; y[1] = 1; z[2] = -1; step = move_step; angle = angle_move; setTransMatrix(); } public void cameraMoving(float angle_step, String axis) { float[] new_x = x; float[] new_y = y; float[] new_z = z; float alfa = angle_step * angle; switch(axis) { case "x": new_z = addVectors(multScalar(z, COS(alfa)), multScalar(y, SIN(alfa))); new_y = subVectors(multScalar(y, COS(alfa)), multScalar(z, SIN(alfa))); break; case "y": new_x = addVectors(multScalar(x, COS(alfa)), multScalar(z, SIN(alfa))); new_z = subVectors(multScalar(z, COS(alfa)), multScalar(x, SIN(alfa))); break; case "z": new_x = addVectors(multScalar(x, COS(alfa)), multScalar(y, SIN(alfa))); new_y = subVectors(multScalar(y, COS(alfa)), multScalar(x, SIN(alfa))); } x = new_x; y = new_y; z = new_z; normalization(); } public void playerMoving(float x_move, float y_move, float z_move) { float[] move = new float[3]; move[0] = x_move; move[1] = y_move; move[2] = z_move; setTransMatrix(); float[] trans_move = transVector(move); position[0] = position[0] + step*trans_move[0]; position[1] = position[1] + step*trans_move[1]; position[2] = position[2] + step*trans_move[2]; } public void setTransMatrix() { for (int i = 0; i < 3; i++) { coordiTrans[0][i] = x[i]; coordiTrans[1][i] = y[i]; coordiTrans[2][i] = z[i]; } } public float[] transVector(float[] v) { return multiplyMatrixInVector(coordiTrans, v); }  
      and in the main class i have this:
       
      public void keyPressed(KeyEvent e) { if (e.getKeyCode()== KeyEvent.VK_ESCAPE) { System.exit(0); //player move } else if (e.getKeyCode()== KeyEvent.VK_W) { //front //moveAmount[2] += -0.1f; player.playerMoving(0, 0, 1); } else if (e.getKeyCode()== KeyEvent.VK_S) { //back //moveAmount[2] += 0.1f; player.playerMoving(0, 0, -1); } else if (e.getKeyCode()== KeyEvent.VK_A) { //left //moveAmount[0] += -0.1f; player.playerMoving(-1, 0, 0); } else if (e.getKeyCode()== KeyEvent.VK_D) { //right //moveAmount[0] += 0.1f; player.playerMoving(1, 0, 0); } else if (e.getKeyCode()== KeyEvent.VK_E) { //moveAmount[0] += 0.1f; player.playerMoving(0, 1, 0); } else if (e.getKeyCode()== KeyEvent.VK_Q) { //moveAmount[0] += 0.1f; player.playerMoving(0, -1, 0); //camera move } else if (e.getKeyCode()== KeyEvent.VK_I) { //up player.cameraMoving(1, "x"); } else if (e.getKeyCode()== KeyEvent.VK_K) { //down player.cameraMoving(-1, "x"); } else if (e.getKeyCode()== KeyEvent.VK_L) { //right player.cameraMoving(-1, "y"); } else if (e.getKeyCode()== KeyEvent.VK_J) { //left player.cameraMoving(1, "y"); } else if (e.getKeyCode()== KeyEvent.VK_O) { //right round player.cameraMoving(-1, "z"); } else if (e.getKeyCode()== KeyEvent.VK_U) { //left round player.cameraMoving(1, "z"); } }  
      finallt found it.... i confused with the transformation matrix row and col. thanks anyway!
    • By Lewa
      So, i'm currently trying to implement an SSAO shader from THIS tutorial and i'm running into a few issues here.
      Now, this SSAO method requires view space positions and normals. I'm storing the normals in my deferred renderer in world-space so i had to do a conversion and reconstruct the position from the depth buffer.
      And something there goes horribly wrong (which has probably to do with worldspace to viewspace transformations).
      (here is the full shader source code if someone wants to take a look at it)
      Now, i suspect that the normals are the culprit.
      vec3 normal = ((uNormalViewMatrix*vec4(normalize(texture2D(sNormals, vTexcoord).rgb),1.0)).xyz); "sNormals" is a 2D texture which stores the normals in world space in a RGB FP16 buffer.
      Now i can't use the camera viewspace matrix to transform the normals into viewspace as the cameras position isn't set at (0,0,0), thus skewing the result.
      So what i did is to create a new viewmatrix specifically for this normal without the position at vec3(0,0,0);
      //"camera" is the camera which was used for rendering the normal buffer renderer.setUniform4m(ressources->shaderSSAO->getUniform("uNormalViewMatrix"), glmExt::createViewMatrix(glm::vec3(0,0,0),camera.getForward(),camera.getUp())//parameters are (position,forwardVector,upVector) ); Though i have the feeling this is the wrong approach. Is this right or is there a better/correct way of transforming a world space normal into viewspace?
    • By HawkDeath
      Hi,
      I'm trying mix two textures using own shader system, but I have a problem (I think) with uniforms.
      Code: https://github.com/HawkDeath/shader/tree/test
      To debug I use RenderDocs, but I did not receive good results. In the first attachment is my result, in the second attachment is what should be.
      PS. I base on this tutorial https://learnopengl.com/Getting-started/Textures.


  • Advertisement
  • Popular Now

  • Forum Statistics

    • Total Topics
      631399
    • Total Posts
      2999846
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!