Skip to content

Instantly share code, notes, and snippets.

@superblaubeere27
Created November 2, 2018 13:33
Show Gist options
  • Save superblaubeere27/c7a935f672e8567c79dcd7da71f54bd3 to your computer and use it in GitHub Desktop.
Save superblaubeere27/c7a935f672e8567c79dcd7da71f54bd3 to your computer and use it in GitHub Desktop.
Draw sin (LWJGL 2)
package me.superblaubeere27;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
public class Main {
private static double TIME;
private static boolean PAUSE = false;
private static int STEP_SIZE = 10;
private void start() {
try {
for (DisplayMode availableDisplayMode : Display.getAvailableDisplayModes()) {
if (availableDisplayMode.getWidth() == 1920 && availableDisplayMode.getHeight() == 1080 && availableDisplayMode.isFullscreenCapable()) {
Display.setDisplayMode(availableDisplayMode);
Display.setFullscreen(true);
break;
}
}
Display.setVSyncEnabled(true);
Display.setResizable(true);
Display.setTitle("Sine");
Display.create(new PixelFormat(0, 8, 0, 8));
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(-1);
}
// init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while (!Display.isCloseRequested()) {
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
render();
Display.update();
update();
}
Display.destroy();
}
private void update() {
while (Keyboard.next()) {
int k = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter() + 256 : Keyboard.getEventKey();
if (Keyboard.getEventKeyState()) {
if (k == Keyboard.KEY_ADD) {
STEP_SIZE++;
System.out.println("Stepsize: " + STEP_SIZE);
}
if (k == Keyboard.KEY_SUBTRACT) {
STEP_SIZE--;
System.out.println("Stepsize: " + STEP_SIZE);
}
if (k == Keyboard.KEY_PAUSE) {
PAUSE = !PAUSE;
}
}
}
}
private void render() {
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_BLEND);
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GL11.glLineWidth(2.0f);
GL11.glBegin(GL11.GL_LINE_STRIP);
TIME = PAUSE ? TIME : System.currentTimeMillis() / 100.0;
for (int i = 0; i < Display.getWidth(); i += STEP_SIZE) {
double sin = Math.sin(i / 75.0 + TIME);
GL11.glVertex2d(i, Display.getHeight() / 2.0 + (sin * Display.getHeight() / 4.0));
}
GL11.glEnd();
GL11.glBegin(GL11.GL_LINE_STRIP);
for (int i = 0; i < Display.getWidth(); i += STEP_SIZE) {
double sin = Math.cos(i / 60.0 + TIME);
GL11.glVertex2d(i, Display.getHeight() / 2.0 + (sin * Display.getHeight() / 4.0));
}
GL11.glEnd();
}
public static void main(String[] argv) {
Main quadExample = new Main();
quadExample.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment