ED_RockStarGuy

06-17-2016, 02:36 AM

I have been trying to get a simple triangle to render on the screen but an unable to using the Projection, View and Transformation Matrix's but an unable to get anything to the screen.

Main loop

...

shader->start();

Matrix4f transformation = Math::createTransformationMatrix(Vec3<float>(0,0,-25),0,0,0, 1);

shader->loadTransformationMatrix(transformation);

Matrix4f camera = Math::createViewMatrix(Vec3<float>(0, 0, 0), 1, 0, 0);

shader->loadViewMatrix(camera);

Matrix4f projection = Math::getProjectionMatrix(1080,720);

shader->loadProjectionMatrix(projection);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(

0,

3,

GL_FLOAT,

GL_FALSE,

0,

(void*)0

);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);

shader->stop();

...

Math.h

static Matrix4f createTransformationMatrix(Vec3<float> translation, float rx, float ry, float rz, float scale) {

Matrix4f matrix;

Matrix4f::translate(translation, matrix,matrix);

Matrix4f::rotate(toRadians(rx), Vec3<float>(1, 0, 0), matrix, matrix);

Matrix4f::rotate(toRadians(ry), Vec3<float>(0, 1, 0), matrix, matrix);

Matrix4f::rotate(toRadians(rz), Vec3<float>(0, 0, 1), matrix, matrix);

Matrix4f::scale(Vec3<float>(scale, scale, scale), matrix, matrix);

return matrix;

}

static Matrix4f createViewMatrix(Vec3<float> pos, float pitch, float yaw, float roll) {

Matrix4f matrix;

Matrix4f::rotate(toRadians(pitch), Vec3<float>(1, 0, 0), matrix, matrix);

Matrix4f::rotate(toRadians(yaw), Vec3<float>(0, 1, 0), matrix, matrix);

Matrix4f::rotate(toRadians(roll), Vec3<float>(0, 0, 1), matrix, matrix);

Vec3<float> negativeCam(-pos.x, -pos.y, -pos.z);

Matrix4f::translate(negativeCam, matrix, matrix);

return matrix;

}

static Matrix4f getProjectionMatrix(int width, int height) {

float aspectRatio = (float)width / (float)height;

float y_scale = (float)((1 / tan(toRadians(FOV / 2)))*aspectRatio);

float x_scale = y_scale / aspectRatio;

float frustum_length = FAR_PLANE - NEAR_PLANE;

Matrix4f projectionMatrix;

projectionMatrix[0] = x_scale;

projectionMatrix[5] = y_scale;

projectionMatrix[10] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);

projectionMatrix[11] = -1;

projectionMatrix[14] = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);

projectionMatrix[15] = 0;

return projectionMatrix;

}

Vertex Shader

#version 330 core

layout(location = 0) in vec3 position;

uniform mat4 transformationMatrix;

uniform mat4 projectionMatrix;

uniform mat4 viewMatrix;

void main() {

//Works but renders without any camera or projection applied

//gl_Position.xyz = position;

//gl_Position.w = 1.0;

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

vec4 positionRelitiveToCam = viewMatrix * worldPosition;

gl_Position = projectionMatrix * positionRelitiveToCam;

}

Main loop

...

shader->start();

Matrix4f transformation = Math::createTransformationMatrix(Vec3<float>(0,0,-25),0,0,0, 1);

shader->loadTransformationMatrix(transformation);

Matrix4f camera = Math::createViewMatrix(Vec3<float>(0, 0, 0), 1, 0, 0);

shader->loadViewMatrix(camera);

Matrix4f projection = Math::getProjectionMatrix(1080,720);

shader->loadProjectionMatrix(projection);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(

0,

3,

GL_FLOAT,

GL_FALSE,

0,

(void*)0

);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);

shader->stop();

...

Math.h

static Matrix4f createTransformationMatrix(Vec3<float> translation, float rx, float ry, float rz, float scale) {

Matrix4f matrix;

Matrix4f::translate(translation, matrix,matrix);

Matrix4f::rotate(toRadians(rx), Vec3<float>(1, 0, 0), matrix, matrix);

Matrix4f::rotate(toRadians(ry), Vec3<float>(0, 1, 0), matrix, matrix);

Matrix4f::rotate(toRadians(rz), Vec3<float>(0, 0, 1), matrix, matrix);

Matrix4f::scale(Vec3<float>(scale, scale, scale), matrix, matrix);

return matrix;

}

static Matrix4f createViewMatrix(Vec3<float> pos, float pitch, float yaw, float roll) {

Matrix4f matrix;

Matrix4f::rotate(toRadians(pitch), Vec3<float>(1, 0, 0), matrix, matrix);

Matrix4f::rotate(toRadians(yaw), Vec3<float>(0, 1, 0), matrix, matrix);

Matrix4f::rotate(toRadians(roll), Vec3<float>(0, 0, 1), matrix, matrix);

Vec3<float> negativeCam(-pos.x, -pos.y, -pos.z);

Matrix4f::translate(negativeCam, matrix, matrix);

return matrix;

}

static Matrix4f getProjectionMatrix(int width, int height) {

float aspectRatio = (float)width / (float)height;

float y_scale = (float)((1 / tan(toRadians(FOV / 2)))*aspectRatio);

float x_scale = y_scale / aspectRatio;

float frustum_length = FAR_PLANE - NEAR_PLANE;

Matrix4f projectionMatrix;

projectionMatrix[0] = x_scale;

projectionMatrix[5] = y_scale;

projectionMatrix[10] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);

projectionMatrix[11] = -1;

projectionMatrix[14] = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);

projectionMatrix[15] = 0;

return projectionMatrix;

}

Vertex Shader

#version 330 core

layout(location = 0) in vec3 position;

uniform mat4 transformationMatrix;

uniform mat4 projectionMatrix;

uniform mat4 viewMatrix;

void main() {

//Works but renders without any camera or projection applied

//gl_Position.xyz = position;

//gl_Position.w = 1.0;

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

vec4 positionRelitiveToCam = viewMatrix * worldPosition;

gl_Position = projectionMatrix * positionRelitiveToCam;

}