How to scale a model in OpenGL?

Started by
3 comments, last by Heelp 8 years ago

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
    );
Advertisement

Trying or doing? glScalef is what you want. It is adding to the matrix stack used to multiply all incoming vectors. Make sure you call glLoadIdentity before, otherwise it will continually multiply the scale matrix everyframe inflating it to insanity and not working.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

It might be wise you learning newer opengl than doing this where glScale no-longer exists (I think OpenGL 3.2 onwards). I personally find it more intuitive (and you might do since you said you know the theory behind it).

If dpadam's suggestion doesn't work (it should) try this before it:

glMatrixMode(GL_MODELVIEW);

You probably already have it somewhere so you might not need it.

Are you able to see what you are renderign when you comment out the scale line?

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

glScale works on the previous system where you have fixed unction pipeline.

I can assume from your code works with shaders.

The correct way:

You calculate your matrices:

auto mvp = mProjectionMatrix* mViewMatrix* modelMatrix;
projection is the overall projection (I use perspective).
View matrix is your environment.
modelMatrix is where you should scale your model.
Using glm I do:
modelMatrix = glm::scale(modelMatrix, glm::vec3(0.2f, 0.2f, 0.2f)); this for example reduces the size by 80%.
then you need to pass it to the shader:
glUniform4f(colorLocation, 1.0f, 0.0f, 0.0f, 0.2f);
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, glm::value_ptr(mvp));
shader:
uniform mat4 Mvp;
void main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position = Mvp* vec4(vertexPosition_modelspace,1);
///... unrelated code .../
}

That I have to multiply my vector by a matrix, but I didnt found any good tutorial about this.

http://www.opengl-tutorial.org/

This topic is closed to new replies.

Advertisement