카테고리 없음

opengl tutorial 4 (3D)

ksh950510 2023. 7. 20. 20:08
728x90

coordinates type

1. local coordinates (origin is the same as an object's origin):

usally located at the center of an object, but that's not always case


2. world coordinates (the origin is the center of the world):

these coordinates usually contain the location of other objects.


3. view coordinates (the same origin as a camera or viewpoint)
notice that these do not yet account for perspective


4. clip coordinates is added in the perspective
these are essentially the same as the view coordinates, except they clip(aka delete).
any vertices outside the normalized range, and can also account for perspective.


5. screen coordinates:

everything is flattened out such that it can be viewed on your screen.)

 

in order to move from a coordinates system, we make use of matrices.

1. Model Matrix: takes local coordinates to world coordinates.

2. View Matrix: takes world coordinates to view coordinates.

3. Projection Matrix: takes view coordinates to clip coordinates.

4. Final transformation: clip coordinates to space coordinates is done automatically

 

all these matrices we are applying, are 4D, but we won't have to actually write them ourselves, since glm can take care of that

//initialization
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 proj = glm::mat4(1.0f);

initialization must be done since otherwise the matrix is full of 0s and 

any transformation we would apply on it would result in the same empty matrix.

 

our object and camera is in center, so we'll want to move back and also a bit of up

(note that it's doing whole world itself, not us doing)

To do this we are going to use the function translate, plugging in our view matrix, and a vec3 which will indicate in which direction and how much to move the whole world.

view = glm::translate(view, glm::vec3(0.0f, -0.5f, -2.0f));

for the projection matrix we'll use the perspective function

glm::mat4 perspective(float fovy, float aspect, float zNear, float zFar);

proj = glm::perspective(glm::radians(45.0f), (float)(800/800), 0.1f, 100.0f);

the last two parameter(ZNear, zFar) set the closest and furthest point

the object is out of the parameter range, then object will be clipped

for example: near plane clipped, far plane clipped(disappear)

the last setting is that we have to set the uniform in shader, and main function

//main.cpp

// Outputs the matrices into the Vertex Shader
int modelLoc = glGetUniformLocation(shaderProgram.ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
int viewLoc = glGetUniformLocation(shaderProgram.ID, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
int projLoc = glGetUniformLocation(shaderProgram.ID, "proj");
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));

//shader

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

glUniformMatrix4fv() function

더보기

 

void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
 
location

Specifies the location of the uniform variable to be modified.

count

For the vector (glUniform*v) commands, specifies the number of elements that are to be modified. This should be 1 if the targeted uniform variable is not an array, and 1 or more if it is an array.

For the matrix (glUniformMatrix*) commands, specifies the number of matrices that are to be modified. This should be 1 if the targeted uniform variable is not an array of matrices, and 1 or more if it is an array of matrices.

transpose

For the matrix commands, specifies whether to transpose the matrix as the values are loaded into the uniform variable.

v0, v1, v2, v3

For the scalar commands, specifies the new values to be used for the specified uniform variable.

value

For the vector and matrix commands, specifies a pointer to an array of count values that will be used to update the specified uniform variable.

glm::value_ptr()
we give the matrix to this function

this is done so that we can point to the matrix object itself, no the data in it

 

when we make 3D object, we have to enable the Depth Buffer

also, clean the depth buffer

// Enables the Depth Buffer
glEnable(GL_DEPTH_TEST);

// Clean the back buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);