I have been making a project in light weight java graphics library where i have loaded a model and rendered it with a color per triangle fine. My next step is to rotate the model around its center. From reading articles and source code i have a general understanding of what i need to do but i don't fully understand the glTranslatef() method. From my under standing you want to
glPushMatrix()
glTranslatef(0,0,0);
glRotatef(roation values);
glTranslatef(original coords);
render model
then glPopMatrix();
My main problem is i am manually doing the math in my glVertex() method calls instead of translating them i believe which is making the translation back from 0,0,0 to it's original spot difficult. Thanks for your time and help.
package render;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glNormal3f;
import static org.lwjgl.opengl.GL11.glVertex3f;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.util.vector.Vector3f;
import models.Faces;
import models.Material;
import models.Model;
import models.OBJLoader;
public class OBJRender {
Material material = new Material();
private int count;
Model m = null;
Vector3f rgb = null;
Vector3f v1 = new Vector3f();
Vector3f v2 = new Vector3f();
Vector3f v3 = new Vector3f();
public void load_model(String model_name){ //loads model data
try{
m = OBJLoader.loadModel(new File("res/obj/"+model_name+".obj"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void model_render(Vector3f coords, Vector3f degrees){
count = 0; //count of triangle for coloring
model_rotate(coords, degrees); //coords is the obj coords degrees is rotation coords
glBegin(GL_TRIANGLES);
for (Faces face : m.faces) {
String mat = OBJLoader.getMaterialList().get(count);
rgb = material.getMaterial(mat);
glColor3f(rgb.x, rgb.y, rgb.z);
Vector3f n1 = m.normals.get((int) face.normals.x - 1);
v1 = m.vertices.get((int) face.Vertex.x - 1);
Vector3f n2 = m.normals.get((int) face.normals.y - 1);
v2 = m.vertices.get((int) face.Vertex.y - 1);
Vector3f n3 = m.normals.get((int) face.normals.z - 1);
v3 = m.vertices.get((int) face.Vertex.z - 1);
glNormal3f(n1.x, n1.y, n1.z);
glVertex3f(coords.x-v1.x, coords.y+v1.y+80, coords.z-v1.z);
glNormal3f(n2.x, n2.y, n2.z);
glVertex3f(coords.x-v2.x, coords.y+v2.y+80, coords.z-v2.z);
glNormal3f(n3.x, n3.y, n3.z);
glVertex3f(coords.x-v3.x, coords.y+v3.y+80, coords.z-v3.z);
count++;
}
glClearColor(rgb.x,rgb.y,rgb.z,0);
glEnd();
glPopMatrix();
}
public void model_rotate(Vector3f coords, Vector3f degrees){
glPushMatrix();
glTranslatef(-0, -0, -0);
glRotatef(degrees.y,0,1,0);
}
}
glTranslate(center)
,glRotate(angle)
,glTranslate( - center)
. But when you're modifying the vertex coordinates, this has to be taken into account, and you have to compute the new center. (You should probably not do this. Just render the object as-it-is, and use only theglTranslate
etc. calls to transform it). – Marco13glTranslate
andglRotate
, of course (this was what the "etc." stood for). The key point is: You usually don't modify vertex coordinates manually when you want to transform the whole object. Instead, you use the "original" vertex coordinates, and transform the object by manipulating the modelview matrix withglTranslate
andglRotate
(andglScale
, if you want to). – Marco13