예제 #1
0
  public VertexArray(float[] vertices, byte[] indices, float[] textureCoordinates) {
    count = indices.length;

    vao = glGenVertexArrays();
    glBindVertexArray(vao);

    vbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
    glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0);
    glEnableVertexAttribArray(Shader.VERTEX_ATTRIB);

    tbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, tbo);
    glBufferData(
        GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(textureCoordinates), GL_STATIC_DRAW);
    glVertexAttribPointer(Shader.TCOORD_ATTRIB, 2, GL_FLOAT, false, 0, 0);
    glEnableVertexAttribArray(Shader.TCOORD_ATTRIB);

    ibo = glGenBuffers();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
  }
예제 #2
0
  protected boolean prepare(int fbufWidth, int fbufHeight) {
    if (ctx.useShader(this) && glIsProgram(program)) {
      glUseProgram(program);
      ctx.checkGLError("Shader.prepare useProgram");

      glUniform2f(uScreenSizeLoc, fbufWidth, fbufHeight);
      // ctx.checkGLError("Shader.prepare uScreenSizeLoc set to " + fbufWidth + " " + fbufHeight);

      glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
      ctx.checkGLError("Shader.prepare BindBuffer");

      glEnableVertexAttribArray(aMatrix);
      glEnableVertexAttribArray(aTranslation);
      glEnableVertexAttribArray(aPosition);
      if (aTexture != -1) glEnableVertexAttribArray(aTexture);
      ctx.checkGLError("Shader.prepare AttribArrays enabled");

      glVertexAttribPointer(aMatrix, 4, GL_FLOAT, false, VERTEX_STRIDE, 0);
      glVertexAttribPointer(aTranslation, 2, GL_FLOAT, false, VERTEX_STRIDE, 16);
      glVertexAttribPointer(aPosition, 2, GL_FLOAT, false, VERTEX_STRIDE, 24);
      if (aTexture != -1) glVertexAttribPointer(aTexture, 2, GL_FLOAT, false, VERTEX_STRIDE, 32);
      ctx.checkGLError("Shader.prepare AttribPointer");
      return true;
    }
    return false;
  }
예제 #3
0
파일: Tile.java 프로젝트: joaogl/LD29
  public void render(int x, int y, float extra) {
    setExtraLevel(extra);
    glPushMatrix();
    shader.bind();
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    {
      glTranslatef(x, y, 0);
      glActiveTexture(GL_TEXTURE1);
      glBindTexture(GL_TEXTURE_2D, texture);
      glActiveTexture(GL_TEXTURE2);
      glBindTexture(GL_TEXTURE_2D, Texture.Lava);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
      glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
      glActiveTexture(GL_TEXTURE1);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
      glBindTexture(GL_TEXTURE_2D, 0);

      /*
       * glTranslatef(x, y, 0); glBindTexture(GL_TEXTURE_2D, texture); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0);
       */
    }
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
    glBindVertexArray(0);
    shader.release();
    glPopMatrix();
  }
예제 #4
0
  public void draw() {
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);

    glDrawArrays(GL_TRIANGLES, 0, size);

    glDisableVertexAttribArray(0);
  }
예제 #5
0
  public void renderBuffer(OpenGLBuffer buffer, ITextureObject texture) {
    GL13.glActiveTexture(GL13.GL_TEXTURE0 + 0);
    texture.bind();
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glBindBuffer(GL_ARRAY_BUFFER, buffer.getVboID());
    glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE_IN_FLOATS * 4, 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.SIZE_IN_FLOATS * 4, 12);
    glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.SIZE_IN_FLOATS * 4, 20);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer.getIboID());
    glDrawElements(GL_TRIANGLES, buffer.getIndicesCount(), GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
  }
예제 #6
0
파일: Mesh.java 프로젝트: Zeejfps/gamedev
  public void draw() {

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  }
예제 #7
0
파일: Player.java 프로젝트: joaogl/LD29
 public void render() {
   glPushMatrix();
   glEnable(GL_BLEND);
   shader.bind();
   glBindVertexArray(vao);
   glEnableVertexAttribArray(0);
   glEnableVertexAttribArray(1);
   {
     glTranslatef(x, y, 0);
     glActiveTexture(GL_TEXTURE1);
     glBindTexture(GL_TEXTURE_2D, texture);
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
     glBindTexture(GL_TEXTURE_2D, 0);
   }
   glDisableVertexAttribArray(1);
   glDisableVertexAttribArray(0);
   glBindVertexArray(0);
   shader.release();
   glDisable(GL_BLEND);
   glPopMatrix();
 }
예제 #8
0
 private static void setUpVBOs() {
   int[] vbos;
   try {
     model = OBJLoader.loadModel(new File(MODEL_LOCATION));
     int vboVertexHandle = glGenBuffers();
     int vboNormalHandle = glGenBuffers();
     FloatBuffer vertices = BufferTools.reserveData(model.faces.size() * 9);
     FloatBuffer normals = BufferTools.reserveData(model.faces.size() * 9);
     for (Face face : model.faces) {
       vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.x - 1)));
       vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.y - 1)));
       vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.z - 1)));
       normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.x - 1)));
       normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.y - 1)));
       normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.z - 1)));
     }
     vertices.flip();
     normals.flip();
     glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
     glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
     glEnableVertexAttribArray(attributeVertex);
     glVertexAttribPointer(attributeVertex, 3, false, 0, vertices);
     glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
     glEnableVertexAttribArray(attributeNormal);
     glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
     glNormalPointer(GL_FLOAT, 0, 0L);
     // TODO: This really isn't finished yet. :-(
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     cleanUp();
     System.exit(1);
   } catch (IOException e) {
     e.printStackTrace();
     cleanUp();
     System.exit(1);
   }
 }
예제 #9
0
  public Mandelbrot(
      long platform,
      CLCapabilities platformCaps,
      GLFWWindow window,
      int deviceType,
      boolean debugGL,
      int maxIterations) {
    this.platform = platform;

    this.window = window;
    this.maxIterations = maxIterations;

    IntBuffer size = BufferUtils.createIntBuffer(2);

    nglfwGetWindowSize(window.handle, memAddress(size), memAddress(size) + 4);
    ww = size.get(0);
    wh = size.get(1);

    nglfwGetFramebufferSize(window.handle, memAddress(size), memAddress(size) + 4);
    fbw = size.get(0);
    fbh = size.get(1);

    glfwMakeContextCurrent(window.handle);
    GLCapabilities glCaps = GL.createCapabilities();
    if (!glCaps.OpenGL30) throw new RuntimeException("OpenGL 3.0 is required to run this demo.");

    debugProc = debugGL ? GLUtil.setupDebugMessageCallback() : null;

    glfwSwapInterval(0);

    errcode_ret = BufferUtils.createIntBuffer(1);

    try {
      // Find devices with GL sharing support
      {
        long device = getDevice(platform, platformCaps, deviceType);
        if (device == NULL) device = getDevice(platform, platformCaps, CL_DEVICE_TYPE_CPU);

        if (device == NULL)
          throw new RuntimeException("No OpenCL devices found with OpenGL sharing support.");

        this.device = device;
        this.deviceCaps = CL.createDeviceCapabilities(device, platformCaps);
      }

      // Create the context
      PointerBuffer ctxProps = BufferUtils.createPointerBuffer(7);
      switch (Platform.get()) {
        case WINDOWS:
          ctxProps
              .put(CL_GL_CONTEXT_KHR)
              .put(glfwGetWGLContext(window.handle))
              .put(CL_WGL_HDC_KHR)
              .put(wglGetCurrentDC());
          break;
        case LINUX:
          ctxProps
              .put(CL_GL_CONTEXT_KHR)
              .put(glfwGetGLXContext(window.handle))
              .put(CL_GLX_DISPLAY_KHR)
              .put(glfwGetX11Display());
          break;
        case MACOSX:
          ctxProps
              .put(APPLEGLSharing.CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE)
              .put(CGLGetShareGroup(CGLGetCurrentContext()));
      }
      ctxProps.put(CL_CONTEXT_PLATFORM).put(platform).put(NULL).flip();
      clContext =
          clCreateContext(
              ctxProps,
              device,
              clContextCB =
                  new CLContextCallback() {
                    @Override
                    public void invoke(long errinfo, long private_info, long cb, long user_data) {
                      log(String.format("cl_context_callback\n\tInfo: %s", memUTF8(errinfo)));
                    }
                  },
              NULL,
              errcode_ret);
      checkCLError(errcode_ret);

      // create command queues for every GPU, setup colormap and init kernels

      IntBuffer colorMapBuffer = BufferUtils.createIntBuffer(32 * 2);
      initColorMap(colorMapBuffer, 32, Color.BLUE, Color.GREEN, Color.RED);

      clColorMap =
          clCreateBuffer(
              clContext, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, colorMapBuffer, errcode_ret);
      checkCLError(errcode_ret);

      // create command queue and upload color map buffer
      clQueue = clCreateCommandQueue(clContext, device, NULL, errcode_ret);
      checkCLError(errcode_ret);

      // load program(s)
      if (deviceType == CL_DEVICE_TYPE_GPU)
        log("OpenCL Device Type: GPU (Use -forceCPU to use CPU)");
      else log("OpenCL Device Type: CPU");

      log("Max Iterations: " + maxIterations + " (Use -iterations <count> to change)");
      log("Display resolution: " + ww + "x" + wh + " (Use -res <width> <height> to change)");

      log("OpenGL glCaps.GL_ARB_sync = " + glCaps.GL_ARB_sync);
      log("OpenGL glCaps.GL_ARB_cl_event = " + glCaps.GL_ARB_cl_event);

      buildProgram();

      // Detect GLtoCL synchronization method
      syncGLtoCL = !glCaps.GL_ARB_cl_event; // GL3.2 or ARB_sync implied
      log(syncGLtoCL ? "GL to CL sync: Using clFinish" : "GL to CL sync: Using OpenCL events");

      // Detect CLtoGL synchronization method
      syncCLtoGL = !deviceCaps.cl_khr_gl_event;
      log(
          syncCLtoGL
              ? "CL to GL sync: Using glFinish"
              : "CL to GL sync: Using implicit sync (cl_khr_gl_event)");

      vao = glGenVertexArrays();
      glBindVertexArray(vao);

      vbo = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vbo);

      glBufferData(
          GL_ARRAY_BUFFER,
          stackPush()
              .floats(
                  0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
                  1.0f, 1.0f, 1.0f),
          GL_STATIC_DRAW);
      stackPop();

      vsh = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(
          vsh,
          "#version 150\n"
              + "\n"
              + "uniform mat4 projection;\n"
              + "\n"
              + "uniform vec2 size;\n"
              + "\n"
              + "in vec2 posIN;\n"
              + "in vec2 texIN;\n"
              + "\n"
              + "out vec2 texCoord;\n"
              + "\n"
              + "void main(void) {\n"
              + "\tgl_Position = projection * vec4(posIN * size, 0.0, 1.0);\n"
              + "\ttexCoord = texIN;\n"
              + "}");
      glCompileShader(vsh);
      String log = glGetShaderInfoLog(vsh, glGetShaderi(vsh, GL_INFO_LOG_LENGTH));
      if (!log.isEmpty()) log(String.format("VERTEX SHADER LOG: %s", log));

      fsh = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(
          fsh,
          "#version 150\n"
              + "\n"
              + "uniform isampler2D mandelbrot;\n"
              + "\n"
              + "in vec2 texCoord;\n"
              + "\n"
              + "out vec4 fragColor;\n"
              + "\n"
              + "void main(void) {\n"
              + "\tfragColor = texture(mandelbrot, texCoord) / 255.0;\n"
              + "}");
      glCompileShader(fsh);
      log = glGetShaderInfoLog(fsh, glGetShaderi(fsh, GL_INFO_LOG_LENGTH));
      if (!log.isEmpty()) log(String.format("FRAGMENT SHADER LOG: %s", log));

      glProgram = glCreateProgram();
      glAttachShader(glProgram, vsh);
      glAttachShader(glProgram, fsh);
      glLinkProgram(glProgram);
      log = glGetProgramInfoLog(glProgram, glGetProgrami(glProgram, GL_INFO_LOG_LENGTH));
      if (!log.isEmpty()) log(String.format("PROGRAM LOG: %s", log));

      int posIN = glGetAttribLocation(glProgram, "posIN");
      int texIN = glGetAttribLocation(glProgram, "texIN");

      glVertexAttribPointer(posIN, 2, GL_FLOAT, false, 4 * 4, 0);
      glVertexAttribPointer(texIN, 2, GL_FLOAT, false, 4 * 4, 2 * 4);

      glEnableVertexAttribArray(posIN);
      glEnableVertexAttribArray(texIN);

      projectionUniform = glGetUniformLocation(glProgram, "projection");
      sizeUniform = glGetUniformLocation(glProgram, "size");

      glUseProgram(glProgram);

      glUniform1i(glGetUniformLocation(glProgram, "mandelbrot"), 0);
    } catch (Exception e) {
      // TODO: cleanup
      throw new RuntimeException(e);
    }

    glDisable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    initGLObjects();
    glFinish();

    setKernelConstants();

    glfwSetWindowSizeCallback(
        window.handle,
        window.windowsizefun =
            new GLFWWindowSizeCallback() {
              @Override
              public void invoke(long window, final int width, final int height) {
                if (width == 0 || height == 0) return;

                events.add(
                    new Runnable() {
                      @Override
                      public void run() {
                        Mandelbrot.this.ww = width;
                        Mandelbrot.this.wh = height;

                        shouldInitBuffers = true;
                      }
                    });
              }
            });

    glfwSetFramebufferSizeCallback(
        window.handle,
        window.framebuffersizefun =
            new GLFWFramebufferSizeCallback() {
              @Override
              public void invoke(long window, final int width, final int height) {
                if (width == 0 || height == 0) return;

                events.add(
                    new Runnable() {
                      @Override
                      public void run() {
                        Mandelbrot.this.fbw = width;
                        Mandelbrot.this.fbh = height;

                        shouldInitBuffers = true;
                      }
                    });
              }
            });

    glfwSetKeyCallback(
        window.handle,
        window.keyfun =
            new GLFWKeyCallback() {
              @Override
              public void invoke(long window, int key, int scancode, int action, int mods) {
                switch (key) {
                  case GLFW_KEY_LEFT_CONTROL:
                  case GLFW_KEY_RIGHT_CONTROL:
                    ctrlDown = action == GLFW_PRESS;
                    return;
                }

                if (action != GLFW_PRESS) return;

                switch (key) {
                  case GLFW_KEY_ESCAPE:
                    glfwSetWindowShouldClose(window, GLFW_TRUE);
                    break;
                  case GLFW_KEY_D:
                    events.offer(
                        new Runnable() {
                          @Override
                          public void run() {
                            doublePrecision = !doublePrecision;
                            log("DOUBLE PRECISION IS NOW: " + (doublePrecision ? "ON" : "OFF"));
                            rebuild = true;
                          }
                        });
                    break;
                  case GLFW_KEY_HOME:
                    events.offer(
                        new Runnable() {
                          @Override
                          public void run() {
                            offsetX = -0.5;
                            offsetY = 0.0;
                            zoom = 1.0;
                          }
                        });
                    break;
                }
              }
            });

    glfwSetMouseButtonCallback(
        window.handle,
        window.mousebuttonfun =
            new GLFWMouseButtonCallback() {
              @Override
              public void invoke(long window, int button, int action, int mods) {
                if (button != GLFW_MOUSE_BUTTON_LEFT) return;

                dragging = action == GLFW_PRESS;

                if (dragging) {
                  dragging = true;

                  dragX = mouseX;
                  dragY = mouseY;

                  dragOffsetX = offsetX;
                  dragOffsetY = offsetY;
                }
              }
            });

    glfwSetCursorPosCallback(
        window.handle,
        window.cursorposfun =
            new GLFWCursorPosCallback() {
              @Override
              public void invoke(long window, double xpos, double ypos) {
                mouseX = xpos;
                mouseY = wh - ypos;

                if (dragging) {
                  offsetX = dragOffsetX + transformX(dragX - mouseX);
                  offsetY = dragOffsetY + transformY(dragY - mouseY);
                }
              }
            });

    glfwSetScrollCallback(
        window.handle,
        window.scrollfun =
            new GLFWScrollCallback() {
              @Override
              public void invoke(long window, double xoffset, double yoffset) {
                if (yoffset == 0) return;

                double scrollX = mouseX - ww * 0.5;
                double scrollY = mouseY - wh * 0.5;

                double zoomX = transformX(scrollX);
                double zoomY = transformY(scrollY);

                zoom *= (1.0 - yoffset * (ctrlDown ? 0.25 : 0.05));

                offsetX += zoomX - transformX(scrollX);
                offsetY += zoomY - transformY(scrollY);
              }
            });
  }