Bonjour a tous, je viens vous demander votre aide pour un petit soucie dans mon code.
je vous montre un petit screen:
Pièce jointe 167734
comme vous pouvez le voir le string fait n'importe quoi, il est en l'envers (miroir) et ne devrai pas se trouver a cet endroit, il devrai être constamment au position x=10 et y=10 de la fenêtre, la j'ai l'impression qu'il est considéré comme une partie du model.
mes codes:
ma classe gui:
mes classes pour le chargement du model:Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 public class Gui { Object gui; public void drawString(String text, int xPos, int yPos) { glEnable(GL_BLEND); ClientMain.font.drawString(xPos, yPos, text, Color.white); glColor4f(1,1,1,1); glDisable(GL_BLEND); } public Object getGui() { return gui; } }
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public class BufferTools { /** * @param v the vector that is to be turned into an array of floats * * @return a float array where [0] is v.x, [1] is v.y, and [2] is v.z */ public static float[] asFloats(Vector3f v) { return new float[]{v.x, v.y, v.z}; } /** * @param elements the amount of elements to check * * @return true if the contents of the two buffers are the same, false if not */ public static boolean bufferEquals(FloatBuffer bufferOne, FloatBuffer bufferTwo, int elements) { for (int i = 0; i < elements; i++) { if (bufferOne.get(i) != bufferTwo.get(i)) { return false; } } return true; } /** * @param values the byte values that are to be turned into a readable ByteBuffer * * @return a readable ByteBuffer */ public static ByteBuffer asByteBuffer(byte... values) { ByteBuffer buffer = BufferUtils.createByteBuffer(values.length); buffer.put(values); return buffer; } /** * @param buffer a readable buffer * @param elements the amount of elements in the buffer * * @return a string representation of the elements in the buffer */ public static String bufferToString(FloatBuffer buffer, int elements) { StringBuilder bufferString = new StringBuilder(); for (int i = 0; i < elements; i++) { bufferString.append(" ").append(buffer.get(i)); } return bufferString.toString(); } /** * @param matrix4f the Matrix4f that is to be turned into a readable FloatBuffer * * @return a FloatBuffer representation of matrix4f */ public static FloatBuffer asFloatBuffer(Matrix4f matrix4f) { FloatBuffer buffer = BufferUtils.createFloatBuffer(16); matrix4f.store(buffer); return buffer; } /** * @param matrix4f the Matrix4f that is to be turned into a FloatBuffer that is readable to OpenGL (but not to you) * * @return a FloatBuffer representation of matrix4f */ public static FloatBuffer asFlippedFloatBuffer(Matrix4f matrix4f) { FloatBuffer buffer = BufferUtils.createFloatBuffer(16); matrix4f.store(buffer); buffer.flip(); return buffer; } /** * @param values the float values that are to be turned into a readable FloatBuffer * * @return a readable FloatBuffer containing values */ public static FloatBuffer asFloatBuffer(float... values) { FloatBuffer buffer = BufferUtils.createFloatBuffer(values.length); buffer.put(values); return buffer; } /** * @param amountOfElements the amount of elements in the FloatBuffers * * @return an empty FloatBuffer with a set amount of elements */ public static FloatBuffer reserveData(int amountOfElements) { return BufferUtils.createFloatBuffer(amountOfElements); } /** * @param values the float values that are to be turned into a FloatBuffer * * @return a FloatBuffer readable to OpenGL (not to you!) containing values */ public static FloatBuffer asFlippedFloatBuffer(float... values) { FloatBuffer buffer = BufferUtils.createFloatBuffer(values.length); buffer.put(values); buffer.flip(); return buffer; } }
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 public class Model { private final List<Vector3f> vertices = new ArrayList<Vector3f>(); private final List<Vector2f> textureCoordinates = new ArrayList<Vector2f>(); private final List<Vector3f> normals = new ArrayList<Vector3f>(); private final List<Face> faces = new ArrayList<Face>(); private final HashMap<String, Material> materials = new HashMap<String, Material>(); private boolean enableSmoothShading = true; public boolean hasTextureCoordinates() { return getTextureCoordinates().size() > 0; } public boolean hasNormals() { return getNormals().size() > 0; } public List<Vector3f> getVertices() { return vertices; } public List<Vector2f> getTextureCoordinates() { return textureCoordinates; } public List<Vector3f> getNormals() { return normals; } public List<Face> getFaces() { return faces; } public boolean isSmoothShadingEnabled() { return enableSmoothShading; } public void setSmoothShadingEnabled(boolean smoothShadingEnabled) { this.enableSmoothShading = smoothShadingEnabled; } public HashMap<String, Material> getMaterials() { return materials; } public static class Material { @Override public String toString() { return "Material{" + "specularCoefficient=" + specularCoefficient + ", ambientColour=" + ambientColour + ", diffuseColour=" + diffuseColour + ", specularColour=" + specularColour + ", texture=" + texture + '}'; } /** Between 0 and 1000. */ public float specularCoefficient = 100; public float[] ambientColour = {0.2f, 0.2f, 0.2f}; public float[] diffuseColour = {0.3f, 1, 1}; public float[] specularColour = {1, 1, 1}; public Texture texture; } /** @author Oskar */ public static class Face { private final int[] vertexIndices = {-1, -1, -1}; private final int[] normalIndices = {-1, -1, -1}; private final int[] textureCoordinateIndices = {-1, -1, -1}; private Material material; public Material getMaterial() { return material; } public boolean hasNormals() { return normalIndices[0] != -1; } public boolean hasTextureCoordinates() { return textureCoordinateIndices[0] != -1; } public int[] getVertexIndices() { return vertexIndices; } public int[] getTextureCoordinateIndices() { return textureCoordinateIndices; } public int[] getNormalIndices() { return normalIndices; } public Face(int[] vertexIndices) { this.vertexIndices[0] = vertexIndices[0]; this.vertexIndices[1] = vertexIndices[1]; this.vertexIndices[2] = vertexIndices[2]; } public Face(int[] vertexIndices, int[] normalIndices) { this.vertexIndices[0] = vertexIndices[0]; this.vertexIndices[1] = vertexIndices[1]; this.vertexIndices[2] = vertexIndices[2]; this.normalIndices[0] = normalIndices[0]; this.normalIndices[1] = normalIndices[1]; this.normalIndices[2] = normalIndices[2]; } public Face(int[] vertexIndices, int[] normalIndices, int[] textureCoordinateIndices, Material material) { this.vertexIndices[0] = vertexIndices[0]; this.vertexIndices[1] = vertexIndices[1]; this.vertexIndices[2] = vertexIndices[2]; this.textureCoordinateIndices[0] = textureCoordinateIndices[0]; this.textureCoordinateIndices[1] = textureCoordinateIndices[1]; this.textureCoordinateIndices[2] = textureCoordinateIndices[2]; this.normalIndices[0] = normalIndices[0]; this.normalIndices[1] = normalIndices[1]; this.normalIndices[2] = normalIndices[2]; this.material = material; } } }
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public class ModelMain { private static Camera camera; private static int bunnyDisplayList; private String MODEL_LOCATION = "res/test.obj"; public ModelMain(String modelName){ this.MODEL_LOCATION = "res/" + modelName + ".obj"; setUpDisplayLists(); setUpCamera(); } public void setUpDisplayLists() { bunnyDisplayList = glGenLists(1); glNewList(bunnyDisplayList, GL_COMPILE); { Model m = null; try { m = OBJLoader.loadModel(new File(MODEL_LOCATION), 0.5f); } catch (FileNotFoundException e) { e.printStackTrace(); Display.destroy(); System.exit(1); } catch (IOException e) { e.printStackTrace(); Display.destroy(); System.exit(1); } glBegin(GL_TRIANGLES); for (Model.Face face : m.getFaces()) { Vector3f n1 = m.getNormals() .get(face.getNormalIndices()[0] - 1); glNormal3f(n1.x, n1.y, n1.z); Vector3f v1 = m.getVertices().get( face.getVertexIndices()[0] - 1); glVertex3f(v1.x, v1.y, v1.z); Vector3f n2 = m.getNormals() .get(face.getNormalIndices()[1] - 1); glNormal3f(n2.x, n2.y, n2.z); Vector3f v2 = m.getVertices().get( face.getVertexIndices()[1] - 1); glVertex3f(v2.x, v2.y, v2.z); Vector3f n3 = m.getNormals() .get(face.getNormalIndices()[2] - 1); glNormal3f(n3.x, n3.y, n3.z); Vector3f v3 = m.getVertices().get( face.getVertexIndices()[2] - 1); glVertex3f(v3.x, v3.y, v3.z); } glEnd(); } glEndList(); } public static void checkInput() { camera.processMouse(1, 80, -80); camera.processKeyboard(16, 1); if (Mouse.isButtonDown(0)) { Mouse.setGrabbed(true); } else if (Mouse.isButtonDown(1)) { Mouse.setGrabbed(false); } } public static void cleanUp() { glDeleteLists(bunnyDisplayList, 1); Display.destroy(); } public static void render() { glLoadIdentity(); camera.applyTranslations(); glCallList(bunnyDisplayList); } public void setUpCamera() { camera = new EulerCamera.Builder() .setAspectRatio( (float) Display.getWidth() / Display.getHeight()) .setRotation(-1.12f, 0.16f, 0f) .setPosition(-1.38f, 1.36f, 7.95f).setFieldOfView(60).build(); camera.applyOptimalStates(); camera.applyPerspectiveMatrix(); } }
voila je pense avoir mis tous ce qu'il faut, si vous arriver a trouver d'où vient le problème ça serait génialCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public class OBJLoader { public static int createDisplayList(Model m) { int displayList = glGenLists(1); glNewList(displayList, GL_COMPILE); { // glMaterialf(GL_FRONT, GL_SHININESS, 120); glColor3f(0.4f, 0.27f, 0.17f); // glBegin(GL_TRIANGLES); for (Model.Face face : m.getFaces()) { if (face.hasNormals()) { Vector3f n1 = m.getNormals().get(face.getNormalIndices()[0] - 1); glNormal3f(n1.x, n1.y, n1.z); } Vector3f v1 = m.getVertices().get(face.getVertexIndices()[0] - 1); glVertex3f(v1.x, v1.y, v1.z); if (face.hasNormals()) { Vector3f n2 = m.getNormals().get(face.getNormalIndices()[1] - 1); glNormal3f(n2.x, n2.y, n2.z); } Vector3f v2 = m.getVertices().get(face.getVertexIndices()[1] - 1); glVertex3f(v2.x, v2.y, v2.z); if (face.hasNormals()) { Vector3f n3 = m.getNormals().get(face.getNormalIndices()[2] - 1); glNormal3f(n3.x, n3.y, n3.z); } Vector3f v3 = m.getVertices().get(face.getVertexIndices()[2] - 1); glVertex3f(v3.x, v3.y, v3.z); } glEnd(); } glEndList(); return displayList; } private static FloatBuffer reserveData(int size) { return BufferUtils.createFloatBuffer(size); } private static float[] asFloats(Vector3f v) { return new float[]{v.x, v.y, v.z}; } public static int[] createVBO(Model model) { int vboVertexHandle = glGenBuffers(); int vboNormalHandle = glGenBuffers(); FloatBuffer vertices = reserveData(model.getFaces().size() * 9); FloatBuffer normals = reserveData(model.getFaces().size() * 9); for (Model.Face face : model.getFaces()) { vertices.put(asFloats(model.getVertices().get(face.getVertexIndices()[0] - 1))); vertices.put(asFloats(model.getVertices().get(face.getVertexIndices()[1] - 1))); vertices.put(asFloats(model.getVertices().get(face.getVertexIndices()[2] - 1))); normals.put(asFloats(model.getNormals().get(face.getNormalIndices()[0] - 1))); normals.put(asFloats(model.getNormals().get(face.getNormalIndices()[1] - 1))); normals.put(asFloats(model.getNormals().get(face.getNormalIndices()[2] - 1))); } vertices.flip(); normals.flip(); glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW); glVertexPointer(3, GL_FLOAT, 0, 0L); glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle); glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW); glNormalPointer(GL_FLOAT, 0, 0L); glBindBuffer(GL_ARRAY_BUFFER, 0); return new int[]{vboVertexHandle, vboNormalHandle}; } private static Vector3f parseVertex(String line) { String[] xyz = line.split(" "); float x = Float.valueOf(xyz[1]); float y = Float.valueOf(xyz[2]); float z = Float.valueOf(xyz[3]); return new Vector3f(x, y, z); } private static Vector3f parseNormal(String line) { String[] xyz = line.split(" "); float x = Float.valueOf(xyz[1]); float y = Float.valueOf(xyz[1]); float z = Float.valueOf(xyz[2]); return new Vector3f(x, y, z); } private static Model.Face parseFace(boolean hasNormals, String line) { String[] faceIndices = line.split(" "); int[] vertexIndicesArray = {Integer.parseInt(faceIndices[1].split("/")[0]), Integer.parseInt(faceIndices[2].split("/")[0]), Integer.parseInt(faceIndices[3].split("/")[0])}; if (hasNormals) { int[] normalIndicesArray = new int[3]; normalIndicesArray[0] = Integer.parseInt(faceIndices[1].split("/")[1]); normalIndicesArray[1] = Integer.parseInt(faceIndices[2].split("/")[1]); normalIndicesArray[2] = Integer.parseInt(faceIndices[3].split("/")[1]); return new Model.Face(vertexIndicesArray, normalIndicesArray); } else { return new Model.Face((vertexIndicesArray)); } } public static Model loadModel(File f, float scale) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(f)); Model m = new Model(); String line; while ((line = reader.readLine()) != null) { String prefix = line.split(" ")[0]; if (prefix.equals("#")) { continue; } else if (prefix.equals("v")) { m.getVertices().add(parseVertex(line)); } else if (prefix.equals("vn")) { m.getNormals().add(parseNormal(line)); } else if (prefix.equals("f")) { m.getFaces().add(parseFace(m.hasNormals(), line)); } else { reader.close(); throw new RuntimeException("OBJ file contains line which cannot be parsed correctly: " + line); } } glScalef(scale, scale, scale); reader.close(); return m; } }