package org engine import org lwjgl LWJGLException import org lwjgl op

  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
package org.engine;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class GLEngine {
private int width; // ширина окна
private int height; // высота окна
private String title; // заголовок окна
private Texture texture;
private float rotateX = 0.0f;
private void loadTexture() throws FileNotFoundException, IOException {
texture = TextureLoader.getTexture("JPG", new FileInputStream(new File("/Users/nikita/IdeaProjects/OpenGLLab1/stones.jpg")));
}
public GLEngine(String title, int width, int height) { // конструктор класса
this.width = width;
this.height = height;
this.title = title;
}
private void initGL() throws LWJGLException {
Display.setDisplayMode(new DisplayMode(width, height)); // задаём размер экрана
Display.setTitle(title); // задаём заголовок экрана
Display.create(); // создаём окно с заданным форматом пикселей
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
GL11.glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
GL11.glDepthFunc(GL11.GL_LESS); // The Type Of Depth Test To Do
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Color Shading
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity(); // Reset The Projection Matrix
GLU.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
try {
loadTexture();
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() throws LWJGLException {
initGL();
while (!Display.isCloseRequested()) { // пока окно не закрыто
render(); // рисуем
Display.update(); // обновляем экран
}
texture.release();
Display.destroy(); // уничтожаем окно
}
public void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // устанавливаем буферизацию для окна
GL11.glLoadIdentity(); // Reset The View
GL11.glTranslatef(0f, 0.0f, -5f);
GL11.glRotatef(rotateX, -5.0f, 1.0f, 0.0f);
texture.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
GL11.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glEnd();
rotateX += 1.0f;
}
}