blubee

08-05-2015, 01:41 PM

I wrote a pretty long question here: http://stackoverflow.com/questions/31842366/setting-up-opengl-view-and-projection-transformations

but I'll also add some of the questions details down below.

I am making some basic steps towards rendering with opengl. I wrote some shaders as well as a some math functions to do the linear algebra.

I can get a quad that's 2d tris to render on screen but when it comes to applying any sort of transformations I see nothing.

Here is the relevant code as to the matrix data that I am sending to the gpu, etc.

**setup matricies**

mat4identity(&vm);

vec3f eye = {0, 0, 0};

vec3f center = {0, 0, -1};

vec3f up = {0, 1, 0};

mat4LookAt(&vm, &eye, ¢er, &up);

mat4print("lookat",&vm);

mat4OrthoProjection(&opm, 200, 0, 200, 0, 0.001, 1000.0);

mat4print("2d projection",&opm);

**render code**

glUseProgram(sp);

glBindVertexArray(vao1);

pMat = getUniformLocation(sp, "proj");

vMat = getUniformLocation(sp, "view");

transMat = getUniformLocation(sp, "transform");

glUniformMatrix4dv(pMat, 1, GL_FALSE, opm.m);

glUniformMatrix4dv(vMat, 1, GL_FALSE, vm.m);

glUniformMatrix4dv(transMat, 1, GL_FALSE, tm.m);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glBindVertexArray(0);

the mat4 is a column major struct in c.

**mat4 lookat function**

mat4* lookat(vec3f* eye, vec3f* center, vec3f* up) {

vec3f x = {1, 0, 0};

vec3f y = {0, 1, 0};

vec3f z = {0, 0, 1};

z = vec3fsub(eye, center);

vec3fnorm(z);

x = vec3fcross(y, z);

y = vec3fcross(z, x);

vec3fnorm(x);

vec3fnorm(y);

double x1, y1, z1;

x1 = vec3fdotr(&x, eye);

y1 = vec3fdotr(&y, eye);

z1 = vec3fdotr(&z, eye);

double data[] = { (&x)->p[0], (&y)->p[0], (&z)->p[0], 0,

(&x)->p[1], (&y)->p[1], (&z)->p[1], 0,

(&x)->p[2], (&y)->p[2], (&z)->p[2], 0,

x1, y1, z1, 1 };

return makeMat4(data);

}

**orthographic projection code**

mat4* mat4Projection(double r, double l, double t, double b, double n, double f)

{

double data[] = { (2*n)/(r-l), 0, (r+l)/(r-l), 0,

0, (2*n)/(t-b), (t+b)/(t-b), 0,

0, 0, (-(f+n))/(f-n), (-2*(f*n))/(f-n),

0, 0, -1, 1 };

return makeMat4(data);

}

I also try to send down just a simple transform matrix that moves the objects 20 units in the +z direction. Running the above code and printing out the matrices along the way I get these results.

lookat matrix

[ 1.000000 0.000000 0.000000 0.000000 ]

[ 0.000000 1.000000 0.000000 0.000000 ]

[ 0.000000 0.000000 1.000000 0.000000 ]

[ 0.000000 0.000000 0.000000 1.000000 ]

2d projection matrix

[ 0.010000 0.000000 0.000000 -1.000000 ]

[ 0.000000 0.010000 0.000000 -1.000000 ]

[ 0.000000 0.000000 -0.002000 -1.000002 ]

[ 0.000000 0.000000 0.000000 1.000000 ]

translation matrix

[ 1.000000 0.000000 0.000000 0.000000 ]

[ 0.000000 1.000000 0.000000 0.000000 ]

[ 0.000000 0.000000 1.000000 20.000000 ]

[ 0.000000 0.000000 0.000000 1.000000 ]

The error could be at this line when I am sending the data to the gpu

glUniformMatrix4dv(pMat, 1, GL_FALSE, opm.m);

but again I am not sure. This is the vertex shader:

#version 330 core

uniform mat4 view;

uniform mat4 proj;

in vec3 position;

void main()

{

gl_Position = proj * view * vec4(position.x, position.y, position.z, 1.0);

}

removing the proj and view transformations I get the rectangle in the middle of the screen. Using either projection or view transformations and I can't see anything on screen, just the clear background.

but I'll also add some of the questions details down below.

I am making some basic steps towards rendering with opengl. I wrote some shaders as well as a some math functions to do the linear algebra.

I can get a quad that's 2d tris to render on screen but when it comes to applying any sort of transformations I see nothing.

Here is the relevant code as to the matrix data that I am sending to the gpu, etc.

**setup matricies**

mat4identity(&vm);

vec3f eye = {0, 0, 0};

vec3f center = {0, 0, -1};

vec3f up = {0, 1, 0};

mat4LookAt(&vm, &eye, ¢er, &up);

mat4print("lookat",&vm);

mat4OrthoProjection(&opm, 200, 0, 200, 0, 0.001, 1000.0);

mat4print("2d projection",&opm);

**render code**

glUseProgram(sp);

glBindVertexArray(vao1);

pMat = getUniformLocation(sp, "proj");

vMat = getUniformLocation(sp, "view");

transMat = getUniformLocation(sp, "transform");

glUniformMatrix4dv(pMat, 1, GL_FALSE, opm.m);

glUniformMatrix4dv(vMat, 1, GL_FALSE, vm.m);

glUniformMatrix4dv(transMat, 1, GL_FALSE, tm.m);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glBindVertexArray(0);

the mat4 is a column major struct in c.

**mat4 lookat function**

mat4* lookat(vec3f* eye, vec3f* center, vec3f* up) {

vec3f x = {1, 0, 0};

vec3f y = {0, 1, 0};

vec3f z = {0, 0, 1};

z = vec3fsub(eye, center);

vec3fnorm(z);

x = vec3fcross(y, z);

y = vec3fcross(z, x);

vec3fnorm(x);

vec3fnorm(y);

double x1, y1, z1;

x1 = vec3fdotr(&x, eye);

y1 = vec3fdotr(&y, eye);

z1 = vec3fdotr(&z, eye);

double data[] = { (&x)->p[0], (&y)->p[0], (&z)->p[0], 0,

(&x)->p[1], (&y)->p[1], (&z)->p[1], 0,

(&x)->p[2], (&y)->p[2], (&z)->p[2], 0,

x1, y1, z1, 1 };

return makeMat4(data);

}

**orthographic projection code**

mat4* mat4Projection(double r, double l, double t, double b, double n, double f)

{

double data[] = { (2*n)/(r-l), 0, (r+l)/(r-l), 0,

0, (2*n)/(t-b), (t+b)/(t-b), 0,

0, 0, (-(f+n))/(f-n), (-2*(f*n))/(f-n),

0, 0, -1, 1 };

return makeMat4(data);

}

I also try to send down just a simple transform matrix that moves the objects 20 units in the +z direction. Running the above code and printing out the matrices along the way I get these results.

lookat matrix

[ 1.000000 0.000000 0.000000 0.000000 ]

[ 0.000000 1.000000 0.000000 0.000000 ]

[ 0.000000 0.000000 1.000000 0.000000 ]

[ 0.000000 0.000000 0.000000 1.000000 ]

2d projection matrix

[ 0.010000 0.000000 0.000000 -1.000000 ]

[ 0.000000 0.010000 0.000000 -1.000000 ]

[ 0.000000 0.000000 -0.002000 -1.000002 ]

[ 0.000000 0.000000 0.000000 1.000000 ]

translation matrix

[ 1.000000 0.000000 0.000000 0.000000 ]

[ 0.000000 1.000000 0.000000 0.000000 ]

[ 0.000000 0.000000 1.000000 20.000000 ]

[ 0.000000 0.000000 0.000000 1.000000 ]

The error could be at this line when I am sending the data to the gpu

glUniformMatrix4dv(pMat, 1, GL_FALSE, opm.m);

but again I am not sure. This is the vertex shader:

#version 330 core

uniform mat4 view;

uniform mat4 proj;

in vec3 position;

void main()

{

gl_Position = proj * view * vec4(position.x, position.y, position.z, 1.0);

}

removing the proj and view transformations I get the rectangle in the middle of the screen. Using either projection or view transformations and I can't see anything on screen, just the clear background.