-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarro3DSimplificado.java
More file actions
executable file
·162 lines (141 loc) · 5.28 KB
/
Carro3DSimplificado.java
File metadata and controls
executable file
·162 lines (141 loc) · 5.28 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.glu.GLUquadric;
import javax.swing.*;
public class Carro3DSimplificado implements GLEventListener {
private GLU glu;
public static void main(String[] args) {
JFrame frame = new JFrame("Carro 3D Simplificado");
GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities caps = new GLCapabilities(profile);
GLCanvas canvas = new GLCanvas(caps);
canvas.addGLEventListener(new Carro3DSimplificado());
frame.add(canvas);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
glu = new GLU();
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glClearColor(0.8f, 0.9f, 1.0f, 1.0f);
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(6.0f, 10.0f, 14.0f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f);
gl.glRotatef(25.0f, 1.0f, 0.0f, 0.0f);
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
dibujarCarro(gl);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float)w/h, 1.0f, 100.0f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
@Override
public void dispose(GLAutoDrawable drawable) {}
private void dibujarCarro(GL2 gl) {
// Carrocería (cubo rojo)
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glPushMatrix();
gl.glScalef(1.3f, 0.5f, 2.2f);
dibujarCuboSolido(gl);
gl.glPopMatrix();
// Techo (pirámide amarilla)
gl.glColor3f(1.0f, 1.0f, 0.0f);
gl.glPushMatrix();
gl.glTranslatef(0.0f, 0.6f, 0.0f);
gl.glScalef(1.6f, 0.8f, 1.0f);
dibujarPiramideSolida(gl);
gl.glPopMatrix();
// Ruedas negras (cilindros)
gl.glColor3f(0.1f, 0.1f, 0.1f);
dibujarRuedaCompleta(gl, -1.5f, -0.3f, 1.5f);
dibujarRuedaCompleta(gl, 1.3f, -0.3f, 1.5f);
dibujarRuedaCompleta(gl, -1.5f, -0.3f, -1.5f);
dibujarRuedaCompleta(gl, 1.3f, -0.3f, -1.5f);
}
private void dibujarCuboSolido(GL2 gl) {
gl.glBegin(GL2.GL_QUADS);
// Frontal
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// Trasera
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f( 1.0f, 1.0f, -1.0f);
gl.glVertex3f( 1.0f, -1.0f, -1.0f);
// Superior
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f( 1.0f, 1.0f, -1.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// Inferior
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f( 1.0f, -1.0f, -1.0f);
gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
// Izquierda
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
// Derecha
gl.glVertex3f( 1.0f, -1.0f, -1.0f);
gl.glVertex3f( 1.0f, 1.0f, -1.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glEnd();
}
private void dibujarPiramideSolida(GL2 gl) {
// Base
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glVertex3f( 1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glEnd();
// Caras
gl.glBegin(GL2.GL_TRIANGLES);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glEnd();
}
private void dibujarRuedaCompleta(GL2 gl, float x, float y, float z) {
GLUquadric quadric = glu.gluNewQuadric();
gl.glPushMatrix();
gl.glTranslatef(x, y, z);
gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
// Cilindro principal
glu.gluCylinder(quadric, 0.3f, 0.3f, 0.2f, 32, 1);
// Disco frontal
glu.gluDisk(quadric, 0.0f, 0.3f, 32, 1);
// Disco trasero
gl.glTranslatef(0.0f, 0.0f, 0.2f);
glu.gluDisk(quadric, 0.0f, 0.3f, 32, 1);
gl.glPopMatrix();
glu.gluDeleteQuadric(quadric);
}
}