PDA

View Full Version : Moving away from deprecated functions



Jacob16682
08-11-2014, 08:35 PM
Ok so I was watching a tutorial on how to replace built in matrix functions and stuff like that and move completely into modern opengl. I made the class like the tutorial said and everything and replaced all functions with the new functions and it messed everything up.

This is what my program looks like now https://www.dropbox.com/s/1j7s5hy5yf6hh5k/Screenshot%202014-08-11%2018.15.41.png?dl=0

Here is the main.cpp and matrices.cpp where I think somewhere in there the problem is occurring.


// main.cpp
#include "World.h"
#include "player.h"
#include "functions.h"
#include "scenery.h"
#include "matrices.h"





SDL_Event event;
bool running=true, slowmo=false;
float color1=255;
float camx=0;
float camy=0;
float screenangle=0;
bool recover = false;

float timer = 0;

Player player[2];
PF pf[] = {&Player::att1, &Player::att2, &Player::att3, &Player::att4, &Player::att5};

float delta=0;

unsigned int texture;

shader* mainshader;

matrices pipeline;


int main(int argc, char* args[])
{

float currentticks=0;
float LastTicks=0;

//initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);

//set up game controller

//set up joystick
SDL_Joystick *joystick = nullptr;

cout << SDL_NumJoysticks()<<endl;

for(int i=0; i< SDL_NumJoysticks(); i++)
{
joystick = SDL_JoystickOpen(i);
cout<< SDL_JoystickName(joystick) << endl;
cout<< SDL_JoystickName(joystick) << " has " << SDL_JoystickNumAxes(joystick) << " number of axes and " << SDL_JoystickNumButtons(joystick) << " number of buttons." << endl;
}

//Set OpenGL memory usage
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

//create window
SDL_Window *screen = SDL_CreateWindow("Uber Fighters Xtreme", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL); //|SDL_WINDOW_FULLSCREEN);
SDL_GLContext glContext = SDL_GL_CreateContext(screen);

//setup OpenGL
glClearColor(0,0,1,1);
glViewport(0,0,640,640);
glShadeModel(GL_SMOOTH);

pipeline.matrixMode(PROJECTION_MATRIX);
pipeline.loadIdentity();

glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

mainshader= new shader("Assets/Shaders/vertex.txt", "Assets/Shaders/fragment.txt");

loadmap("Assets/level/textures/wall.png","Assets/level/maps/level.txt");


//setup FrameBuffers
Framebuffer screenfbo;
screenfbo.setup(640,640);


while ( running)
{
currentticks = SDL_GetTicks();
delta = (currentticks-LastTicks)/1000;
if(slowmo)
delta = delta/3;
LastTicks = currentticks;
events();

if(recover)
{
camx = 0;
camy = 0;
screenangle = 0;
recover = false;
}
/*if(timer>=2)
{
player[1].attacking = true;
player[1].attacknum = 0;
}*/
//logic
for(int i=0; i<2; i++)
{
player[i].combotimer += 1*delta;

if(player[i].combotimer >= 0.3 && !player[i].attacking)
player[i].combonum = 0;

if(!player[i].mright && !player[i].mleft)
{
player[i].idle();
}

player[i].trailinit(player[i].trailseconds);

player[i].land(player[i].landing);
player[i].jump(player[i].jumping);
player[i].fall(player[i].falling);

player[i].screenshake();

player[i].right(player[i].mright);
player[i].left(player[i].mleft);

(player[i].*pf[player[i].attacknum])(player[i].attacking);

player[i].stun(player[i].stunned);
player[i].flingup(player[i].fu);
}

//rendering
glBindFramebuffer(GL_FRAMEBUFFER, screenfbo.FBO);

glClear(GL_COLOR_BUFFER_BIT);

mainshader->useShader();

timer += 1*delta;
pipeline.pushMatrix();
pipeline.ortho(0,640,640,0,-1,1);
pipeline.translate(0, 0, 0);

glUniform1f(glGetUniformLocation(mainshader->getProgramID(),"time"),0);

pipeline.updateMatrices(mainshader->getProgramID());

for(int i=0; i<maxwall; i++)
{
wall[i].render();
}

pipeline.popMatrix();
int i = 1;
while(i>=0)
{
player[i].trailrender(1,1,1);




pipeline.pushMatrix();
pipeline.ortho(0,640,640,0,-1,1);
pipeline.translate(player[i].x + (player[i].width/2), player[i].y - (player[i].height/2), 0);
pipeline.rotate(player[i].yangle,0,1,0);
pipeline.rotate(player[i].angle,0,0,1);
pipeline.translate(-(player[i].x + (player[i].width/2)), -(player[i].y - (player[i].height/2)), 0);

pipeline.updateMatrices(mainshader->getProgramID());

player[i].render();

pipeline.popMatrix();
i--;
}
glBindFramebuffer(GL_FRAMEBUFFER,0);




pipeline.pushMatrix();
pipeline.ortho(0,640,640,0,-1,1);
pipeline.translate(((float)640/2)+camx,((float)640/2)+camy,0);
pipeline.rotate(screenangle,0,0,1);
pipeline.translate(-(float)640/2+camx,-(float)640/2+camy,0);

pipeline.updateMatrices(mainshader->getProgramID());

screenfbo.render();

pipeline.popMatrix();

mainshader->delShader();



SDL_GL_SwapWindow(screen);
}

screenfbo.~Framebuffer();
SDL_Quit();
SDL_GL_DeleteContext(glContext);
return 0;
}




//matrices.cpp
#include "matrices.h"



matrices::matrices()
{
modelMatrix.push_back(glm::mat4(1.0)); //identity
viewMatrix.push_back(glm::mat4(1.0)); //identity
projectionMatrix.push_back(glm::mat4(1.0)); //identity

currentMatrix=MODEL_MATRIX;
matricesReady = true;

modelViewMatrix=glm::mat4(1.0);
modelViewProjectionMatrix=glm::mat4(1.0);
normalMatrix=glm::mat3(1.0);
}

void matrices::loadIdentity()
{
if(currentMatrix==MODEL_MATRIX || currentMatrix==VIEW_MATRIX)
{
modelMatrix[modelMatrix.size()-1]=glm::mat4(1.0);
viewMatrix[viewMatrix.size()-1]=glm::mat4(1.0);
}

else
{
projectionMatrix[projectionMatrix.size()-1]=glm::mat4(1.0);
}

matricesReady = false;
}

void matrices::matrixMode(int i)
{
if(i==MODEL_MATRIX)
currentMatrix=MODEL_MATRIX;

else if(i==VIEW_MATRIX)
currentMatrix=VIEW_MATRIX;

else if(i==PROJECTION_MATRIX)
currentMatrix=PROJECTION_MATRIX;
}

//transformation
void matrices::translate(float x, float y, float z)
{
//combining transformation = multiplying transformation matrix together
if(currentMatrix==MODEL_MATRIX)
modelMatrix[modelMatrix.size()-1] *= glm::translate(glm::vec3(x,y,z));

else if(currentMatrix==VIEW_MATRIX) //camera
viewMatrix[viewMatrix.size()-1] *= glm::translate(glm::vec3(-x,-y,-z));
matricesReady=false;

}


void matrices::scale(float x, float y, float z)
{
if(currentMatrix==MODEL_MATRIX)
modelMatrix[modelMatrix.size()-1] *= glm::scale(glm::vec3(x,y,z));

else if(currentMatrix==VIEW_MATRIX) //camera
viewMatrix[viewMatrix.size()-1] *= glm::scale(glm::vec3(-x,-y,-z));
matricesReady=false;
}


void matrices::scale(float x)
{
if(currentMatrix==MODEL_MATRIX)
modelMatrix[modelMatrix.size()-1] *= glm::scale(glm::vec3(x,x,x));

else if(currentMatrix==VIEW_MATRIX) //camera
viewMatrix[viewMatrix.size()-1] *= glm::scale(glm::vec3(-x,-x,-x));
matricesReady=false;
}


void matrices::rotate(float angle, int x, int y, int z)
{
if(currentMatrix==MODEL_MATRIX)
modelMatrix[modelMatrix.size()-1] *= glm::rotate(angle, glm::vec3(x,y,z));

else if(currentMatrix==VIEW_MATRIX) //camera
viewMatrix[viewMatrix.size()-1] *= glm::rotate(-angle, glm::vec3(x,y,z));
matricesReady=false;
}

//projection
void matrices::perspective(float angle, float aRatio, float n, float f)
{
projectionMatrix[projectionMatrix.size()-1] = glm::perspective(angle,aRatio,n,f);
matricesReady=false;
}
void matrices::ortho(float left, float right, float bottom, float top, float n, float f)
{
projectionMatrix[projectionMatrix.size()-1] = glm::ortho(left,right,bottom,top,n,f);
matricesReady=false;
}

void matrices::pushMatrix()
{
glm::mat4 matrix;
if(currentMatrix==MODEL_MATRIX)
{
matrix = modelMatrix[modelMatrix.size()-1];
modelMatrix.push_back(matrix);
}
else if(currentMatrix==VIEW_MATRIX)
{
matrix = viewMatrix[viewMatrix.size()-1];
viewMatrix.push_back(matrix);
}
else if(currentMatrix==PROJECTION_MATRIX)
{
matrix = projectionMatrix[projectionMatrix.size()-1];
projectionMatrix.push_back(matrix);
}
matricesReady=false;
}
void matrices::popMatrix()
{
if(currentMatrix==MODEL_MATRIX)
{
if(modelMatrix.size()>1)
modelMatrix.pop_back();
}
else if(currentMatrix==VIEW_MATRIX)
{
if(viewMatrix.size()>1)
viewMatrix.pop_back();
}
else if(currentMatrix==PROJECTION_MATRIX)
{
if(projectionMatrix.size()>1)
projectionMatrix.pop_back();
}
matricesReady=false;
}

glm::mat4& matrices::getModelMatrix()
{
return modelMatrix[modelMatrix.size()-1];
}

glm::mat4& matrices::getModelViewMatrix()
{
if(!matricesReady)
modelViewMatrix=viewMatrix[viewMatrix.size()-1] * modelMatrix[modelMatrix.size()-1];
return modelViewMatrix;
}

glm::mat4& matrices::getModelViewProjectionMatrix()
{
if(!matricesReady)
modelViewProjectionMatrix = projectionMatrix[projectionMatrix.size()-1] * viewMatrix[viewMatrix.size()-1] * modelMatrix[modelMatrix.size()-1];
return modelViewProjectionMatrix;
}

glm::mat4& matrices::getProjectionMatrix()
{
return projectionMatrix[projectionMatrix.size()-1];
}

glm::mat3& matrices::getNormalMatrix()
{

if(!matricesReady)
normalMatrix=glm::mat3(getModelViewMatrix());
return normalMatrix;
}

//GLSL
void matrices::updateMatrices(unsigned int programID)
{
if(1)
{
//calculate mtrices
modelViewMatrix=viewMatrix[viewMatrix.size()-1] * modelMatrix[modelMatrix.size()-1];
modelViewMatrix=viewMatrix[viewMatrix.size()-1] * modelMatrix[modelMatrix.size()-1];
normalMatrix=glm::mat3(getModelViewMatrix());

//matricesReady=true;
}


glUniformMatrix4fv(glGetUniformLocation(programID,"modelMatrix"),1,GL_FALSE, &modelMatrix[modelMatrix.size()-1][0][0]);
glUniformMatrix4fv(glGetUniformLocation(programID,"viewMatrix"),1,GL_FALSE, &viewMatrix[viewMatrix.size()-1][0][0]);
glUniformMatrix4fv(glGetUniformLocation(programID,"projectionMatrix"),1,GL_FALSE, &projectionMatrix[projectionMatrix.size()-1][0][0]);
glUniformMatrix4fv(glGetUniformLocation(programID,"modelViewMatrix"),1,GL_FALSE, &modelViewMatrix[0][0]);
glUniformMatrix4fv(glGetUniformLocation(programID,"modelViewProjectionMatrix"),1,GL_FALSE, &modelViewProjectionMatrix[0][0]);
glUniformMatrix3fv(glGetUniformLocation(programID,"normalMatrix"),1,GL_FALSE, &normalMatrix[0][0]);
}



I tried really hard to figure this out on my own, but just can't. I hope with some help from the professionals we can make this work. Thanks!

Oh P.S. here is the rest of the code along with the sharers and other Assets incase you wanted to run the whole thing https://www.dropbox.com/s/guz2vsw2nrj3r7h/Code.zip?dl=0 (warning may be a little different from code above due to me trying to fix the problem)

carsten neumann
08-12-2014, 01:33 AM
You do all changes to the projection matrix instead of only those related to the camera projection and keeping placement of objects on the model matrix. Also, in matrix::updateMatrices() you never recalculate modelViewProjecitonMatrix but you compute modelViewMatrix twice.

Jacob16682
08-12-2014, 07:58 AM
I don't know how I missed that! Thank you! I was looking in the wrong place I guess! :doh: