Java lwjgl rotating a model around its center -
i have been making project in light weight java graphics library have loaded model , rendered color per triangle fine. next step rotate model around center. reading articles , source code have general understanding of need don't understand gltranslatef() method. under standing want to
glpushmatrix()
gltranslatef(0,0,0);
glrotatef(roation values);
gltranslatef(original coords);
render model
then glpopmatrix();
my main problem manually doing math in glvertex() method calls instead of translating them believe making translation 0,0,0 it's original spot difficult. time , 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 coloring model_rotate(coords, degrees); //coords obj coords degrees rotation coords glbegin(gl_triangles); (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); } }
although still not entirely clear actual question (apart "what have change code want"), attempt answer:
when load object, example, obj file, not modify vertex coordinates. use them read file. when want transform object whole, use opengl matrix operations - in case, gltranslate
, glrotate
.
in order rotate object center, first have know center. in doubt, can compute vertices or bounding box of object.
then, in order rotate object around center, have
- move object, using
gltranslate
, center of object @ origin - rotate object, using
glrotate
- move object original position, using
gltranslate
note these operations applied object in opposite order appear in source code. sequence of calls be
gltranslatef( center.x, center.y, center.z); glrotatef(rotationangledeg,0,0,1); gltranslatef(-center.x, -center.y, -center.z);
here mcve uses simple rectangle consisting of 4 vertices object, , rotates center. can see in glvertex
calls, uses vertices (in case, read obj file). whole transformation done on gl_modelview
matrix, using commands mentioned above.
import static org.lwjgl.opengl.gl11.*; import java.awt.canvas; import javax.swing.jframe; import org.lwjgl.lwjglexception; import org.lwjgl.opengl.display; import org.lwjgl.util.glu.glu; import org.lwjgl.util.vector.vector3f; public class rotateaboutcenter { public static void main(string[] args) { jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(500,500); f.setlocationrelativeto(null); canvas canvas = new canvas(); f.add(canvas); try { display.setparent(canvas); f.setvisible(true); display.create(); } catch (lwjglexception e) { e.printstacktrace(); } while (!display.iscloserequested()) { draw(); display.update(); try { thread.sleep(10); } catch (interruptedexception e) { thread.currentthread().interrupt(); } rotationangledeg += 1.0f; } display.destroy(); } private static float rotationangledeg = 0; // vertices of model private static vector3f v0 = new vector3f(10,10,0); private static vector3f v1 = new vector3f(20,10,0); private static vector3f v2 = new vector3f(20,20,0); private static vector3f v3 = new vector3f(10,20,0); // center of model private static vector3f center = new vector3f(15,15,0); private static void draw() { // basic setup of view etc. int w = display.getwidth(); int h = display.getheight(); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); glu.gluperspective(45, (float) w / (float) h, 0.1f, 1000); glmatrixmode(gl_modelview); glloadidentity(); glu.glulookat(0,0,70,0,0,0,0,1,0); glpushmatrix(); // =================================================================== // now, model transform. remember has // read "backwards": // third step: move model center // again @ original position gltranslatef(center.x, center.y, center.z); // second step: rotate model origin (which // center of model) glrotatef(rotationangledeg,0,0,1); // first step: translate model center @ origin gltranslatef(-center.x, -center.y, -center.z); // =================================================================== // draw object, original coordinates. // transforms contained in modelview matrix. glbegin(gl_triangles); glcolor3f(1,0,0); glvertex3f(v0.x, v0.y, v0.z); glvertex3f(v1.x, v1.y, v1.z); glvertex3f(v3.x, v3.y, v3.z); glvertex3f(v1.x, v1.y, v1.z); glvertex3f(v2.x, v2.y, v2.z); glvertex3f(v3.x, v3.y, v3.z); glend(); glpopmatrix(); } }
Comments
Post a Comment