2

I am trying to scale a model in OpenGL, but I dont know where to start, I am trying using glScalef(), but I dont know if is in this way, I dont know alot about openGL, I know more about theory(That I have to multiply my vector by a matrix, but I didnt found any good tutorial about this)... My code is that:

bool res = loadOBJ(object, vertices, uvs, normals);
indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs, indexed_normals);

glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_STATIC_DRAW);


glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(glm::vec2), &indexed_uvs[0], GL_STATIC_DRAW);


glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_STATIC_DRAW);

// Generate a buffer for the indices as well

glGenBuffers(1, &elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
    0,                  // attribute
    3,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
    );

// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
    1,                                // attribute
    2,                                // size
    GL_FLOAT,                         // type
    GL_FALSE,                         // normalized?
    0,                                // stride
    (void*)0                          // array buffer offset
    );

// 3rd attribute buffer : normals
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(
    2,                                // attribute
    3,                                // size
    GL_FLOAT,                         // type
    GL_FALSE,                         // normalized?
    0,                                // stride
    (void*)0                          // array buffer offset
    );

// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);

glScalef(10, 10, 10);

glDrawElements(
    GL_TRIANGLES,        // mode
    indices.size(),      // count
    GL_UNSIGNED_SHORT,   // type
    (void*)0             // element array buffer offset
    );
2
  • Search for how to use a modelview matrix to transform vertices in the vertex shader. Apr 14, 2016 at 23:38
  • glScalef is deprecated in OpenGL 3.0 and greater. It won't work with vertex buffers. Apr 15, 2016 at 2:28

2 Answers 2

10

Scaling is one of the three transformations you can do to the model matrix, along with translating and rotating.

The model matrix is one of the three matrices that transforms vertices to pixels on your screen, along with the view and projection matrices.

Since the type of scaling you want only applies to the model matrix, we'll skip the other two for now. I do however recommend reading up on all three and how they interact as they are essential to OpenGL.

Before we begin I suggest picking up a library such as GLM as it will do a lot of the heavy lifting for us. From here on I will use GLM syntax to keep things concise.

First let's store our scale in a 3d vector:

glm::vec3 scale = glm::vec3(10f, 10f, 10f);

Now we need a basic model matrix with no transformations:

glm::mat4 modelMatrix = glm::mat4();

Now we can apply the scale to our model matrix:

modelMatrix = glm::scale(modelMatrix, scale);

Now we've got a matrix that can be applied to any set of vertices and scale them by 10 in all three dimensions.

Next we need to get this information to the shader. Just like how glVertexAttribPointer tells the shader where to find vertex attributes, we're going to use glUniform to send our matrix:

GLuint location = getUniformLocation("model");
glUniformMatrix4fv(location, 1, false, glm::value_ptr(modelMatrix));

Here we're querying the shader for the location of the "model" uniform. Then we're submitting 1 uniform matrix (our modelMatrix) to that location.

Finally we need to use that matrix in the shader. Here is a super simple vertex shader that does what we need:

#version 400 core

in vec3 position;
in vec3 normal;
in vec2 uv;
uniform mat4 model;

void main() {
    gl_Position = model * vec4(position, 1.0f);
}

Normally we'd pass the normal and uv information along to the fragment shader but I am omitting that for now to keep things simple.

That is it. Hopefully this gets you where you're trying to go.


On a side note, the function glScalef is deprecated in GL3 and newer. I like using docs.gl as a reference since it differentiates between versions.

EDIT: How to build a shader program

The code I posted above was just the source for a vertex shader, which is only one part of a greater shader program. A shader program can have a vertex shader, geometry shader, and fragment shader. For now though we'll just focus on the two required ones; vertex and fragment shaders.

First put the vertex shader code above into a file named vertex.glsl. Loading a file is outside the scope of this answer so I'll just assume you have a function called loadSourceFromFile that takes a single argument, the filename.

Before we continue let's make a couple utility functions:

function Gluint compileShader(const char* source, GLuint type) {
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, source);
    glCompileShader(shader);
    return shader;
}

function void verifyShaderCompilation(GLuint shader) {
    GLunit status = glGetShaderi(shader, GL_COMPILE_STATUS);
    assert(status == GL_TRUE);
}

Now, let's compile that vertex shader:

const char* vertexSource = loadSourceFromFile("vertex.glsl");
Gluint vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER);
verifyShaderCompilation(vertexShader);

Next we have to build the fragment shader. Put the following code into another file called fragment.glsl:

#version 400 core

out vec4 color;

void main()
{
    color = vec4(0, 0.5, 0, 1);
}

This fragment shader will make every fragment it processes the color green. Now let's compile it just like we did the vertex shader:

const char* fragmentSource = loadSourceFromFile("fragment.glsl");
Gluint fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER);
verifyShaderCompilation(fragmentShader);

Now we've got two compiled shaders. It is time to link them together into a shader program:

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

You'll want to do all of this prior to calling glVertexAttribPointer as it needs to communicate with the shader program you've built.

EDIT: Debugging OpenGL functions

I recommend using the following function after every GL function call when trying to troubleshoot a problem:

function void verifyNoGLError() {
    int errorCode = glGetError();
    assert(errorCode == 0);
}

OpenGL is a complicated state machine and you'll want to know as soon as something isn't right.

2
  • So, I tried and I am getting this in the console: Linking program ERROR: Definition for "void main()" not found.
    – Antonio
    Apr 15, 2016 at 1:57
  • The vertex shader code I included needs to be compiled and linked into a shader program. I've updated my answer to show how this is done.
    – Exide
    Apr 15, 2016 at 2:35
4

You should really be using an MVP (Model, View and Projection) matrix. A matrix is a 2D array of floating point numbers, and is most commonly used with a size of 4x4.

So what are the Model, View and Projection matrices?

Model

Model is a matrix that represents an object's transform. We use a matrix as we can store the translation, scale and rotation in one variable. The position vector is stored in the first row, the rotation vector is stored in the second row and the scale vector is stored in the third row.

View

View is a matrix that represents the camera. It works similarly to the Model matrix.

Projection

Last, but not least, we have the Projection matrix. This holds the data for the camera's projection, as we can not store that in the View matrix. This matrix is a bit hard to explain but it basically describes how things look relative to the position of the camera. There are two common types of projections, Perspective and Orthogonal. Perspective makes objects get smaller the further away they are from the camera. Everything you see with your eyes is perspective. Orthogonal makes everything look the same size, no matter how far away from the camera.

Orthogonal vs Perspective

Orthogonal vs Perspective

So, we have these 'Matrices', but now what?

Well of course, it's not easy for us to work with matrices in our code, so there are libraries to help us do it. My favorite one is GLM. GLM helps us create and work with matrices. I suggest you read up on it. Once we have the Model, View and Projection matrices, we can multiply them with the vertex position, to get the final position of the vertex.

Now, MVP and transformations is a little bit too advanced to explain in an answer. I suggest you check out these two tutorials, they explain matrices and transformation (A darn lot better than I did), and tell you how use them in your code and shaders: Tutorial 1, Tutorial 2. You can also check out the next tutorial to see how to make a nice camera system.

Follow these great tutorials and I can ensure you a scaled Model :)

P.S. This is my first real answer so sorry if you found it hard to understand ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.