Skip to content

Instantly share code, notes, and snippets.

@Coditivity
Last active October 28, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coditivity/0eedc86447a509f6b5ef to your computer and use it in GitHub Desktop.
Save Coditivity/0eedc86447a509f6b5ef to your computer and use it in GitHub Desktop.
package com.company;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
public class QuadExample {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW); //setting active matrix to model view
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
userLogic();
rect.render();;
triangle.render();
stationaryRectangle.render();
renderText(); //uncomment the commented parts and add actual text rendering code here
Display.update();
}
Display.destroy();
}
MyRectangle rect = new MyRectangle(50, 250, 50, 50);
MyEquiTriangle triangle = new MyEquiTriangle(50, 50, 50);
StationaryRectangle stationaryRectangle = new StationaryRectangle(400, 400, 90, 90);
public void userLogic() {
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
rect.setLocation(rect.x - .1f, rect.y);
rect.setRotation(rect.rotation-.5f);
} else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
rect.setLocation(rect.x + .1f, rect.y);
rect.setRotation(rect.rotation+.5f);
} if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
triangle.setLocation(triangle.x, triangle.y+.1f);
triangle.setRotation(triangle.rotation-.5f);
} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
triangle.setLocation(triangle.x, triangle.y-.1f);
triangle.setRotation(triangle.rotation+.5f);
}
}
private class MyEquiTriangle {
float x, y;
float rotation;
float side;
float vertX1, vertY1, vertX2, vertY2, vertx3, vertY3;
public MyEquiTriangle(float xPos, float yPos, float sideLength) {
x = xPos;
y = yPos;
vertX1 = -sideLength/2; //things get a bit tricky here. You need to find the incircle
vertX2 = sideLength/2; // of a triangle to find it's symmetric rotation point.
vertY1 = (float) -Math.sqrt(3) * sideLength/ 6f; //Since this is an equilateral triangle, things
vertY2 = (float) -Math.sqrt(3) * sideLength/ 6f; //get a bit more easy
vertx3 = 0;
vertY3 = (float) Math.sqrt(3) * sideLength /3 ;
}
public void setLocation(float xPos, float yPos) {
x = xPos;
y = yPos;
x = Math.max(0, x);
x = Math.min(Display.getWidth(), x);
y = Math.max(0, y);
y = Math.min(Display.getHeight(), y);
}
public void setRotation(float rot) {
rotation = rot;
if(rotation>=360) {
rotation-=360;
}
else if(rotation<0) {
rotation+=360;
}
}
public void render() {
GL11.glLoadIdentity();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(rotation, 0, 0, 1);
GL11.glScalef(1, 1, 1);
GL11.glBegin(GL11.GL_TRIANGLES);
GL11.glVertex2f(vertX1, vertY1);
GL11.glVertex2f(vertX2, vertY2);
GL11.glVertex2f(vertx3, vertY3);
GL11.glEnd();
}
}
private class MyRectangle {
float x, y, width, height;
float rotation;
public MyRectangle(float posX, float posY, float w, float h) {
x = posX;
y = posY;
width = w;
height = h;
}
public void setLocation(float xPos, float yPos) {
x = xPos;
y = yPos;
x = Math.max(0, x);
x = Math.min(Display.getWidth(), x);
y = Math.max(0, y);
y = Math.min(Display.getHeight(), y);
}
public void setRotation(float rot) {
rotation = rot;
if(rotation>=360) {
rotation-=360;
}
else if(rotation<0) {
rotation+=360;
}
}
public void render() {
GL11.glLoadIdentity();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(rotation, 0, 0, 1);
GL11.glScalef(1, 1, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(-width / 2, -height / 2);
GL11.glVertex2f(width / 2, -height / 2);
GL11.glVertex2f(width / 2, height / 2);
GL11.glVertex2f(-width / 2, height / 2);
GL11.glEnd();
}
}
private class StationaryRectangle {
float x, y;
float width, height;
public StationaryRectangle (float posX, float posY, float w, float h){
x = posX;
y = posY;
width = w;
height = h;
}
public void render() {
GL11.glLoadIdentity();
GL11.glTranslatef(x, y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(-width / 2, -height / 2);
GL11.glVertex2f(width / 2, -height / 2);
GL11.glVertex2f(width / 2, height / 2);
GL11.glVertex2f(-width / 2, height / 2);
GL11.glEnd();
}
}
public void renderText() {
GL11.glLoadIdentity();
//GL11.glTranslatef();
//GL11.glRotatef();
//GL11.glScalef();
//draw text here
}
public static void main(String[] argv) {
QuadExample quadExample = new QuadExample();
quadExample.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment