PDA

View Full Version : Draw Cubes In Different Dimensions



brotherofninth
10-11-2016, 07:46 AM
Cube myCube, myCube2;

Shader myShader; //shader object

ConsoleWindow console;

float Xpos = 2.5;
float Ypos = 2.5;

float spinY = 0.0;

int screenWidth = 480, screenHeight = 480;

//OPENGL FUNCTION PROTOTYPES
void init(); //called in winmain when the program starts.
void reshape(int width, int height);
void display(); //called in winmain to draw everything to the screen

/************* START OF OPENGL FUNCTIONS ****************/

float ProjectionMatrix[16]; // matrix for the orthographic projection
float ModelViewMatrix[16]; // matrix for the modelling and viewing
float ModelViewMatrix2[16]; // matrix for the modelling and viewing

void init()
{

glClearColor(0.0, 0.0, 0.0, 0.0); //sets the clear colour to yellow
//glClear(GL_COLOR_BUFFER_BIT) in the display function//will clear the buffer to this colour.

// Shaders
if (!myShader.load("BasicView", "glslfiles/basicTransformations.vert", "glslfiles/basicTransformations.frag"))
{
cout << "failed to load shader" << endl;
}

myCube.setDim(15);
myCube.constructGeometry(&myShader);

myCube2.setDim(5);
myCube2.constructGeometry(&myShader);

glEnable(GL_DEPTH_TEST);
}

void reshape(int width, int height) // Resize the OpenGL window
{
screenWidth = width;
screenHeight = height;

glViewport(0, 0, width, height); // set Viewport dimensions

MatrixRoutines<float>::perspective(60, (GLfloat)screenWidth / (GLfloat)screenHeight, 1, 200, ProjectionMatrix);

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(myShader.handle()); // use the shader

//All of our geometry will have the same projection matrix.
//we only need to set it once, since we are using the same shader.
GLuint matLocation = glGetUniformLocation(myShader.handle(), "ProjectionMatrix");
glUniformMatrix4fv(matLocation, 1, GL_FALSE, ProjectionMatrix);

//increment the spin variable per frame.
spinY += 1;
if (spinY>360) spinY = 0;

float t[16], r[16];

//MatrixRoutines<float>::setTranslation(0, 0, -80, t);
//MatrixRoutines<float>::setTranslation(0, 0, -160, t);
MatrixRoutines<float>::setTranslation(0, 0, -50, t);
MatrixRoutines<float>::setRotationY(spinY, r);

MatrixRoutines<float>::matrixMultiply4x4RigidBody(t, r, ModelViewMatrix);

//Pass the uniform for the modelview matrix - in this case just "r"

glUniformMatrix4fv(glGetUniformLocation(myShader.h andle(), "ModelViewMatrix"), 1, GL_FALSE, ModelViewMatrix);

myCube.render();

glUniformMatrix4fv(matLocation, 2, GL_FALSE, ProjectionMatrix);

MatrixRoutines<float>::setTranslation(5, 5, -50, t);
MatrixRoutines<float>::setRotationY(spinY, r);

MatrixRoutines<float>::matrixMultiply4x4RigidBody(t, r, ModelViewMatrix2);
glUniformMatrix4fv(glGetUniformLocation(myShader.h andle(), "ModelViewMatrix"), 2, GL_FALSE, ModelViewMatrix2);
myCube2.render();

glUseProgram(0); //turn off the current shader


}


I had build a class to draw cube and I am trying to draw 2 cubes: a big cube amd a small cube.
However, I am not sure about how to draw something in another dimension because of couple questions:

1. Do I need to create another shader for another cube?

2. Do I need another ProjectionMatrix for another cube?

3. If I would like to create a small cube on one of the big cube vertex, do I just have to do a translation of the small cube?

Thanks.

Dark Photon
10-11-2016, 04:46 PM
I had build a class to draw cube and I am trying to draw 2 cubes: a big cube amd a small cube.
However, I am not sure about how to draw something in another dimension because of couple questions:

Please put your code inside
... or ... tags as it makes your post more readable.

This sounds a bit like a homework problem. Is it? If so, please read The Forum Posting Guidelines (https://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1262303).