split transform into modelview/projection
This commit is contained in:
parent
5adc26b089
commit
513599d855
@ -28,12 +28,14 @@ const GLushort indices[] = {
|
||||
0, 1, 2, 3, 4, 5
|
||||
};
|
||||
GLuint program, vs, fs, data_vbo, index_vbo;
|
||||
GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc, transform_loc;
|
||||
GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc,
|
||||
projection_loc, modelview_loc;
|
||||
enum Locations {
|
||||
LOC_POSITION,
|
||||
LOC_NORMAL
|
||||
};
|
||||
static guMatrix4x4 transform;
|
||||
static guMatrix4x4 projection;
|
||||
static guMatrix4x4 modelview;
|
||||
|
||||
char * loadFile(const char *fname)
|
||||
{
|
||||
@ -133,14 +135,17 @@ bool init(int width, int height)
|
||||
glClearColor (0.0, 0.0, 0.0, 0.0);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glViewport(0, 0, WIDTH, HEIGHT);
|
||||
guMatrixLoadIdentity(&transform);
|
||||
guMatrixLoadIdentity(&projection);
|
||||
guMatrixLoadIdentity(&modelview);
|
||||
float near = 0.1;
|
||||
float scale = near * 0.577 * 2;
|
||||
float aspect = (float)width / (float)height;
|
||||
guMatrixFrustum(&transform, -aspect * scale, aspect * scale, -scale, scale, near, 1000.0);
|
||||
// guPerspective(&transform, 60, (float)width / (float)height, 0.1, 1000.0);
|
||||
guMatrixTranslate(&transform, 0, 0, -1);
|
||||
// guMatrixTranslate(&transform, 1, 0.3, 0);
|
||||
guMatrixFrustum(&projection, -aspect * scale, aspect * scale, -scale, scale, near, 1000.0);
|
||||
#if 0
|
||||
guPerspective(&projection, 60, (float)width / (float)height, 0.1, 1000.0);
|
||||
#endif
|
||||
guMatrixTranslate(&modelview, 0, 0, -1);
|
||||
// guMatrixTranslate(&modelview, 1, 0.3, 0);
|
||||
|
||||
vs = makeShader(GL_VERTEX_SHADER, "v_shader.glsl");
|
||||
fs = makeShader(GL_FRAGMENT_SHADER, "f_shader.glsl");
|
||||
@ -175,14 +180,16 @@ bool init(int width, int height)
|
||||
diffuse_loc = glGetUniformLocation(program, "diffuse");
|
||||
specular_loc = glGetUniformLocation(program, "specular");
|
||||
shininess_loc = glGetUniformLocation(program, "shininess");
|
||||
transform_loc = glGetUniformLocation(program, "transform");
|
||||
projection_loc = glGetUniformLocation(program, "projection");
|
||||
modelview_loc = glGetUniformLocation(program, "modelview");
|
||||
|
||||
glUseProgram(program);
|
||||
glUniform4f(ambient_loc, 0.2, 0.2, 0.2, 1.0);
|
||||
glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0);
|
||||
glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0);
|
||||
glUniform1f(shininess_loc, 85.0);
|
||||
glUniformMatrix4fv(transform_loc, 1, GL_TRUE, &transform[0][0]);
|
||||
glUniformMatrix4fv(projection_loc, 1, GL_TRUE, &projection[0][0]);
|
||||
glUniformMatrix4fv(modelview_loc, 1, GL_TRUE, &modelview[0][0]);
|
||||
|
||||
data_vbo = makeBuffer(GL_ARRAY_BUFFER, data, sizeof(data));
|
||||
index_vbo = makeBuffer(GL_ELEMENT_ARRAY_BUFFER, indices, sizeof(indices));
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
uniform mat4 transform;
|
||||
uniform mat4 projection;
|
||||
uniform mat4 modelview;
|
||||
|
||||
attribute vec3 pos;
|
||||
attribute vec3 normal;
|
||||
@ -9,7 +10,7 @@ varying vec3 normal_i;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = transform * vec4(pos, 1);
|
||||
gl_Position = projection * modelview * vec4(pos, 1);
|
||||
pos_i = vec3(gl_Position.x, gl_Position.y, gl_Position.z);
|
||||
normal_i = vec3(transform * vec4(normal, 0.0));
|
||||
normal_i = vec3(modelview * vec4(normal, 0.0));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user