Advertisement
Guest User

Untitled

a guest
Nov 4th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. package slim.test.example;
  2.  
  3. import static org.lwjgl.opengl.GL11.GL_BLEND;
  4. import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
  5. import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
  6. import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
  7. import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
  8. import static org.lwjgl.opengl.GL11.GL_PROJECTION;
  9. import static org.lwjgl.opengl.GL11.GL_QUADS;
  10. import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
  11. import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
  12. import static org.lwjgl.opengl.GL11.glBegin;
  13. import static org.lwjgl.opengl.GL11.glBlendFunc;
  14. import static org.lwjgl.opengl.GL11.glClear;
  15. import static org.lwjgl.opengl.GL11.glClearColor;
  16. import static org.lwjgl.opengl.GL11.glDisable;
  17. import static org.lwjgl.opengl.GL11.glEnable;
  18. import static org.lwjgl.opengl.GL11.glEnd;
  19. import static org.lwjgl.opengl.GL11.glLoadIdentity;
  20. import static org.lwjgl.opengl.GL11.glMatrixMode;
  21. import static org.lwjgl.opengl.GL11.glOrtho;
  22. import static org.lwjgl.opengl.GL11.glTexCoord2f;
  23. import static org.lwjgl.opengl.GL11.glVertex2f;
  24. import static org.lwjgl.opengl.GL11.glViewport;
  25.  
  26. import java.io.IOException;
  27.  
  28. import org.lwjgl.LWJGLException;
  29. import org.lwjgl.opengl.Display;
  30. import org.lwjgl.opengl.DisplayMode;
  31.  
  32. public class VertexArrayExample {
  33.    
  34.     public static void main(String[] args) throws LWJGLException {
  35.         new VertexArrayExample().start();
  36.     }
  37.    
  38.     public void start() throws LWJGLException {
  39.         Display.setTitle("Vertex Array Example");
  40.         Display.setResizable(false);
  41.         Display.setDisplayMode(new DisplayMode(800, 600));
  42.         Display.setVSyncEnabled(true);
  43.         Display.create();
  44.        
  45.         create();
  46.        
  47.         while (!Display.isCloseRequested()) {
  48.             render();
  49.            
  50.             Display.update();
  51.             Display.sync(60);
  52.         }
  53.        
  54.         Display.destroy();
  55.     }
  56.    
  57.     Texture tex;
  58.     ShaderProgram shader;
  59.    
  60.     protected void create() {
  61.         glEnable(GL_TEXTURE_2D);
  62.         glDisable(GL_DEPTH_TEST);
  63.         glEnable(GL_BLEND);
  64.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  65.         glClearColor(0f, 0f, 0f, 0f);
  66.        
  67.         glViewport(0, 0, Display.getWidth(), Display.getHeight());
  68.         glMatrixMode(GL_PROJECTION);
  69.         glLoadIdentity();
  70.         glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
  71.         glMatrixMode(GL_MODELVIEW);
  72.         glLoadIdentity();
  73.        
  74.         // Load some textures
  75.         try {
  76.             tex = new Texture(VertexArrayExample.class.getResource("/res/small.png"));
  77.         } catch (IOException e) {
  78.             throw new RuntimeException("couldn't load texture");
  79.         }
  80.        
  81.     }
  82.    
  83.     protected void render() {
  84.         glClear(GL_COLOR_BUFFER_BIT);
  85.        
  86.         draw(tex, 20, 20);
  87.     }
  88.    
  89.     public void draw(Texture tex, int x, int y) {
  90.         tex.bind();
  91.        
  92.         glBegin(GL_QUADS);
  93.         glTexCoord2f(0, 0);
  94.         glVertex2f(x, y);
  95.         glTexCoord2f(1, 0);
  96.         glVertex2f(x+tex.width, y);
  97.         glTexCoord2f(1, 1);
  98.         glVertex2f(x+tex.width, y+tex.height);
  99.         glTexCoord2f(0, 1);
  100.         glVertex2f(x, y+tex.height);
  101.         glEnd();
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement