Skip to content

Commit

Permalink
fixed(?) projection matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ingframin committed Nov 29, 2021
1 parent 4a476d0 commit 50543f7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"./src/ShaderProgram.cpp",
"./src/Display.cpp",
"./src/model2D.cpp",
"-O3",
// "-O3",
"-o./build/game.exe",
"-I./include",
"-L./lib",
Expand Down
Binary file modified build/game.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion build/shaders/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ void main()
{
Color = color;
Texcoord = texcoord;
gl_Position = transform * projection* vec4(position, 1.0);
gl_Position = projection * transform * vec4(position, 1.0);
//gl_Position = transform * vec4(position, 1.0);
}
21 changes: 12 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ int main(int argc, char **argv)
Display disp {"OpenGL Playground",1280,720};

auto vertices_v = std::vector<Vertex>();
vertices_v.push_back({-0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({ 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({ 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({-0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({-0.5f, 0.5f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({ 0.5f, 0.5f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({ 0.5f, -0.5f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});
vertices_v.push_back({-0.5f, -0.5f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f});

GLfloat vertices[] = {
// Position Color Texcoords
Expand Down Expand Up @@ -56,8 +56,11 @@ int main(int argc, char **argv)
float obZ = 0.0f;
float sc = 1.0f;
float ang = 0.0f;
auto projection = math_utils::perspective(30,10.0f,0.01f);

auto projection = math_utils::perspective(90,1000.0f,0.1f);
auto m = projection.getM();
for(int i = 0; i<4;i++){
printf("%.4f,%.4f,%.4f,%.4f\n",m[4*i],m[4*i+1],m[4*i+2],m[4*i+3]);
}
while (running)
{
if (SDL_PollEvent(&windowEvent))
Expand Down Expand Up @@ -99,10 +102,10 @@ int main(int argc, char **argv)
if(keys[SDL_SCANCODE_F]){
ang -= 0.01f;
}
if(keys[SDL_SCANCODE_Y]){
if(keys[SDL_SCANCODE_Y]&&obZ<0.7){
obZ += 0.01f;
}
if(keys[SDL_SCANCODE_H]){
if(keys[SDL_SCANCODE_H]&&obZ>-1){
obZ -= 0.01f;
}
// Clear the screen to black
Expand All @@ -111,7 +114,7 @@ int main(int argc, char **argv)
//Set transformation matrix
translation = math_utils::translate(obX, obY, obZ);

rotation = math_utils::rotateZ(ang);
rotation = math_utils::rotateZ(ang).product(math_utils::rotateX(M_PI/6).product(math_utils::rotateY(M_PI/6)));

scaling = math_utils::scale(sc*disp.getRatio(), sc, 1.0f);
global_transform = translation.product(rotation.product(scaling));
Expand Down
2 changes: 1 addition & 1 deletion src/math_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace math_utils{
S, 0.0f, 0.0f, 0.0f,
0.0f, S, 0.0f, 0.0f,
0.0f, 0.0f, fn, fz,
0.0f, 0.0f,-1.0f, 0.0f
0.0f, 0.0f, 0.0f, 1.0f
};
return mat4(proj_matrix);
}
Expand Down
12 changes: 12 additions & 0 deletions webgl/DrawRectangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

function main(){
var canvas = document.getElementById("drwcanvas");
if(!canvas){
console.log("Error loading canvas");
return;
}
var ctx = canvas.getContext("2d");
ctx.fillStyle = 'rgba(0, 0, 255, 1.0)';
ctx.fillRect(100,100,500,500);

}
13 changes: 13 additions & 0 deletions webgl/quad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body onload="main()">
<canvas id="drwcanvas" width=800 height=800></canvas>
<script src="DrawRectangle.js"></script>
</body>
</html>

0 comments on commit 50543f7

Please sign in to comment.