org.lwjgl.util.glu.GLU Java Examples

The following examples show how to use org.lwjgl.util.glu.GLU. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LwJglRenderingEngine.java    From Gaalop with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * Starts the lwjgl engine and shows a window, where the point clouds are rendered
 */
public void startEngine() {
    int width = 800;
    int height = 600;
    
    try {
        Display.setDisplayMode(new DisplayMode(width, height));
        Display.setFullscreen(false);
        Display.setTitle("Gaalop Visualization Window");

        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
    }
    
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glShadeModel(GL11.GL_SMOOTH);
    changeSize(width, height);
    GL11.glDisable(GL11.GL_LIGHTING);


    // init OpenGL
    GL11.glViewport(0, 0, width, height);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective((float) 65.0, (float) width / (float) height, (float) 0.1, 100);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    
    
}
 
Example #2
Source File: LwJglRenderingEngine.java    From Gaalop with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * Changes the size of the lwjgl window
 * @param w The new width of the lwjgl window
 * @param h The new height of the lwjgl window
 */
private void changeSize(float w, float h) {
    // Prevent a division by zero, when window is too short
    if (h == 0) {
        h = 1;
    }
    float wRatio = 1.0f * w / h;
    // Reset the coordinate system before modifying
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    // Set the viewport to be the entire window
    GL11.glViewport(0, 0, (int) w, (int) h);
    // Set the correct perspective.
    GLU.gluPerspective(45.0f, wRatio, (float) near, (float) far);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GLU.gluLookAt(camPos.x, camPos.y, camPos.z, // Position
            camPos.x + camDir.x, camPos.y + camDir.y, camPos.z + camDir.z, // Lookat
            camUp.x, camUp.y, camUp.z);               // Up-direction}
}
 
Example #3
Source File: ClientProxy.java    From CommunityMod with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void init() {
	Sphere sphere = new Sphere();
	
	sphere.setDrawStyle(GLU.GLU_FILL);
	sphere.setNormals(GLU.GLU_SMOOTH);
	
	sphere.setOrientation(GLU.GLU_OUTSIDE);
	sphereOutId = GlStateManager.glGenLists(1);
	GlStateManager.glNewList(sphereOutId, GL11.GL_COMPILE);
	sphere.draw(0.5F, 32, 32);
	GlStateManager.glEndList();
	
	sphere.setOrientation(GLU.GLU_INSIDE);
	sphereInId = GlStateManager.glGenLists(1);
	GlStateManager.glNewList(sphereInId, GL11.GL_COMPILE);
	sphere.draw(0.5F, 32, 32);
	sphere.draw(0.5F, 32, 32);
}
 
Example #4
Source File: MatrixUtil.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void testLookAtMatrix(Vector3f eye, Vector3f center, Vector3f up)
{
	// Make a lookat matrix in opengl and pull it out into a Matrix4f
	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glLoadIdentity();
	GLU.gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z);
	
	FloatBuffer fromGlBuffer = BufferUtils.createFloatBuffer(16);
	GL11.glGetFloat(GL11.GL_MODELVIEW, fromGlBuffer);
	Matrix4f fromGl = new Matrix4f();
	fromGl.load(fromGlBuffer);
	
	Matrix4f manual = createLookAt(eye, center, up);
	
	compare(fromGl, manual);
}
 
Example #5
Source File: WorldSceneRenderer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Vec2f setupCamera(int x, int y, int width, int height, int skyColor) {
    Minecraft mc = Minecraft.getMinecraft();
    ScaledResolution resolution = new ScaledResolution(mc);

    GlStateManager.pushAttrib();
    mc.entityRenderer.disableLightmap();
    GlStateManager.disableLighting();
    GlStateManager.enableDepth();
    GlStateManager.enableBlend();

    //compute window size from scaled width & height
    int windowWidth = (int) (width / (resolution.getScaledWidth() * 1.0) * mc.displayWidth);
    int windowHeight = (int) (height / (resolution.getScaledHeight() * 1.0) * mc.displayHeight);

    //translate gui coordinates to window's ones (y is inverted)
    int windowX = (int) (x / (resolution.getScaledWidth() * 1.0) * mc.displayWidth);
    int windowY = mc.displayHeight - (int) (y / (resolution.getScaledHeight() * 1.0) * mc.displayHeight) - windowHeight;

    int mouseX = Mouse.getX();
    int mouseY = Mouse.getY();
    Vec2f mousePosition = null;
    //compute mouse position only if inside viewport
    if (mouseX >= windowX && mouseY >= windowY && mouseX - windowX < windowWidth && mouseY - windowY < windowHeight) {
        mousePosition = new Vec2f(mouseX, mouseY);
    }

    //setup viewport and clear GL buffers
    GlStateManager.viewport(windowX, windowY, windowWidth, windowHeight);

    if (skyColor >= 0) {
        GL11.glEnable(GL11.GL_SCISSOR_TEST);
        GL11.glScissor(windowX, windowY, windowWidth, windowHeight);
        RenderUtil.setGlClearColorFromInt(skyColor, 255);
        GlStateManager.clear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glDisable(GL11.GL_SCISSOR_TEST);
    }

    //setup projection matrix to perspective
    GlStateManager.matrixMode(GL11.GL_PROJECTION);
    GlStateManager.pushMatrix();
    GlStateManager.loadIdentity();

    float aspectRatio = width / (height * 1.0f);
    GLU.gluPerspective(60.0f, aspectRatio, 0.1f, 10000.0f);

    //setup modelview matrix
    GlStateManager.matrixMode(GL11.GL_MODELVIEW);
    GlStateManager.pushMatrix();
    GlStateManager.loadIdentity();
    GLU.gluLookAt(0.0f, 0.0f, -10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    return mousePosition;
}
 
Example #6
Source File: OpenGL3_TheQuadTextured.java    From ldparteditor with MIT License 5 votes vote down vote up
private void exitOnGLError(String errorMessage) {
    int errorValue = GL11.glGetError();
     
    if (errorValue != GL11.GL_NO_ERROR) {
        String errorString = GLU.gluErrorString(errorValue);
        System.err.println("ERROR - " + errorMessage + ": " + errorString);
         
        if (Display.isCreated()) Display.destroy();
        System.exit(-1);
    }
}
 
Example #7
Source File: PolygonBuilderBase.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
public PolygonBuilderBase() {
	tesselator.gluBeginPolygon();
	tesselator.gluTessProperty(GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_NONZERO);

	started = true;

	tesselator.gluTessCallback(GLU.GLU_TESS_BEGIN, collector);
	tesselator.gluTessCallback(GLU.GLU_TESS_END, collector);
	tesselator.gluTessCallback(GLU.GLU_TESS_VERTEX, collector);
	tesselator.gluTessCallback(GLU.GLU_TESS_COMBINE, collector);
	tesselator.gluTessCallback(GLU.GLU_TESS_ERROR, collector);
}
 
Example #8
Source File: ProjectionHelper.java    From OpenModsLib with MIT License 5 votes vote down vote up
public Vec3d unproject(float winX, float winY, float winZ) {
	GLU.gluUnProject(winX, winY, winZ, modelview, projection, viewport, objectCoords);

	float objectX = objectCoords.get(0);
	float objectY = objectCoords.get(1);
	float objectZ = objectCoords.get(2);

	return new Vec3d(objectX, objectY, objectZ);
}
 
Example #9
Source File: ProjectionHelper.java    From OpenModsLib with MIT License 5 votes vote down vote up
public Vec3d project(float objX, float objY, float objZ) {
	GLU.gluProject(objX, objY, objZ, modelview, projection, viewport, winCoords);

	float winX = winCoords.get(0);
	float winY = winCoords.get(1);
	float winZ = winCoords.get(2);

	return new Vec3d(winX, winY, winZ);
}
 
Example #10
Source File: WorldSceneRenderer.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
private BlockPos handleMouseHit(Vec2f mousePosition) {
    //read depth of pixel under mouse
    GL11.glReadPixels((int) mousePosition.x, (int) mousePosition.y, 1, 1,
        GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, PIXEL_DEPTH_BUFFER);

    //rewind buffer after write by glReadPixels
    PIXEL_DEPTH_BUFFER.rewind();

    //retrieve depth from buffer (0.0-1.0f)
    float pixelDepth = PIXEL_DEPTH_BUFFER.get();

    //rewind buffer after read
    PIXEL_DEPTH_BUFFER.rewind();

    //read current rendering parameters
    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, MODELVIEW_MATRIX_BUFFER);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, PROJECTION_MATRIX_BUFFER);
    GL11.glGetInteger(GL11.GL_VIEWPORT, VIEWPORT_BUFFER);

    //rewind buffers after write by OpenGL glGet calls
    MODELVIEW_MATRIX_BUFFER.rewind();
    PROJECTION_MATRIX_BUFFER.rewind();
    VIEWPORT_BUFFER.rewind();

    //call gluUnProject with retrieved parameters
    GLU.gluUnProject(mousePosition.x, mousePosition.y, pixelDepth,
        MODELVIEW_MATRIX_BUFFER, PROJECTION_MATRIX_BUFFER, VIEWPORT_BUFFER, OBJECT_POS_BUFFER);

    //rewind buffers after read by gluUnProject
    VIEWPORT_BUFFER.rewind();
    PROJECTION_MATRIX_BUFFER.rewind();
    MODELVIEW_MATRIX_BUFFER.rewind();

    //rewind buffer after write by gluUnProject
    OBJECT_POS_BUFFER.rewind();

    //obtain absolute position in world
    float posX = OBJECT_POS_BUFFER.get();
    float posY = OBJECT_POS_BUFFER.get();
    float posZ = OBJECT_POS_BUFFER.get();

    //rewind buffer after read
    OBJECT_POS_BUFFER.rewind();

    //System.out.println(String.format("%f %f %f %f", pixelDepth, posX, posY, posZ));
    //if we didn't hit anything, just return null
    //also return null if hit is too far from us
    if (posY < -100.0f) {
        return null; //stop execution at that point
    }

    BlockPos pos = new BlockPos(posX, posY, posZ);
    if (world.isAirBlock(pos)) {
        //if block is air, then search for nearest adjacent block
        //this can happen under extreme rotation angles
        for (EnumFacing offset : EnumFacing.VALUES) {
            BlockPos relative = pos.offset(offset);
            if (world.isAirBlock(relative)) continue;
            pos = relative;
            break;
        }
    }
    if (world.isAirBlock(pos)) {
        //if we didn't found any other block, return null
        return null;
    }
    return pos;
}
 
Example #11
Source File: LwJglRenderingEngine.java    From Gaalop with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    startEngine();
    //long start = System.currentTimeMillis();
    while (!Display.isCloseRequested()) {
        //System.out.println(System.currentTimeMillis()-start);
        //start = System.currentTimeMillis();
        
        if (rendering.isNewDataSetAvailable()) {
            if (list != -1) GL11.glDeleteLists(list, 1);
            list = GL11.glGenLists(1);
            GL11.glNewList(list, GL11.GL_COMPILE);
            draw(rendering.getDataSet(), rendering.getVisibleObjects(), rendering.getLoadedPointClouds());
            GL11.glEndList();
            changed = true;
        }
        
        
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // clear the screen
        GL11.glLoadIdentity(); // apply camPos before rotation

        GL11.glTranslatef(0.0f, 0.0f, -5.0f);
        // draw
        GLU.gluLookAt(camPos.x, camPos.y, camPos.z, // Position
                camPos.x + camDir.x, camPos.y + camDir.y, camPos.z + camDir.z, // Lookat
                camUp.x, camUp.y, camUp.z);               // Up-direction
        // apply rotation
        GL11.glRotatef(camAngleX, 0, 1, 0); // window x axis rotates around up vector
        GL11.glRotatef(camAngleY, 1, 0, 0); // window y axis rotates around x

        //Render the scene
        if (list != -1) GL11.glCallList(list);

        pollInput();
        Display.update();
        if (recorder != null) {
            if (changed || firstFrame) {
                recorder.makeScreenshot();
                changed = false;
            }
            firstFrame = false;
            Display.sync(25); // cap fps to 60fps
        }
        else 
            Display.sync(60); 
    }

    Display.destroy();
}
 
Example #12
Source File: Snippet195.java    From ldparteditor with MIT License 4 votes vote down vote up
public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    //        try {
    //            GLContext.useContext(canvas);
    //        } catch (LWJGLException e) {
    //            e.printStackTrace();
    //        }

    canvas.addListener(SWT.Resize, event -> {
        Rectangle bounds = canvas.getBounds();
        float fAspect = (float) bounds.width / (float) bounds.height;
        canvas.setCurrent();
        //                try {
        //                    GLContext.useContext(canvas);
        //                } catch (LWJGLException e) {
        //                    e.printStackTrace();
        //                }
        GL11.glViewport(0, 0, bounds.width, bounds.height);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
    });

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glClearDepth(1.0);
    GL11.glLineWidth(2);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    shell.setText("SWT/LWJGL Example"); //$NON-NLS-1$
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        @Override
        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                //                    try {
                //                        GLContext.useContext(canvas);
                //                    } catch (LWJGLException e) {
                //                        e.printStackTrace();
                //                    }
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glLoadIdentity();
                GL11.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
                GL11.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(1, 1.9f + (float) Math.sin(0.004f * frot), 15, 15);
                GL11.glColor3f(0.9f, 0.9f, 0f);
                drawTorus(1.9f, 5.9f + (float) Math.sin(0.004f * frot), 15, 15);
                canvas.swapBuffers();
                display.asyncExec(this);
            }
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
 
Example #13
Source File: TrueTypeFont.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 4 votes vote down vote up
public static int loadImage(BufferedImage bufferedImage) {
    try {
	    short width       = (short)bufferedImage.getWidth();
	    short height      = (short)bufferedImage.getHeight();
	    //textureLoader.bpp = bufferedImage.getColorModel().hasAlpha() ? (byte)32 : (byte)24;
	    int bpp = (byte)bufferedImage.getColorModel().getPixelSize();
	    ByteBuffer byteBuffer;
	    DataBuffer db = bufferedImage.getData().getDataBuffer();
	    if (db instanceof DataBufferInt) {
	    	int intI[] = ((DataBufferInt)(bufferedImage.getData().getDataBuffer())).getData();
	    	byte newI[] = new byte[intI.length * 4];
	    	for (int i = 0; i < intI.length; i++) {
	    		byte b[] = intToByteArray(intI[i]);
	    		int newIndex = i*4;

	    		newI[newIndex]   = b[1];
	    		newI[newIndex+1] = b[2];
	    		newI[newIndex+2] = b[3];
	    		newI[newIndex+3] = b[0];
	    	}

	    	byteBuffer  = ByteBuffer.allocateDirect(
	    			width*height*(bpp/8))
		                           .order(ByteOrder.nativeOrder())
		                            .put(newI);
	    } else {
	    	byteBuffer  = ByteBuffer.allocateDirect(
	    			width*height*(bpp/8))
		                           .order(ByteOrder.nativeOrder())
		                            .put(((DataBufferByte)(bufferedImage.getData().getDataBuffer())).getData());
	    }
	    byteBuffer.flip();


	    int internalFormat = GL11.GL_RGBA8,
		format = GL11.GL_RGBA;
		IntBuffer   textureId =  BufferUtils.createIntBuffer(1);;
		GL11.glGenTextures(textureId);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId.get(0));

		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);


		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
		//GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
		//GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_NEAREST);

		//GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
		//GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_LINEAR);
		//GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_NEAREST);

		GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

		GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, internalFormat, width, height, format, GL11.GL_UNSIGNED_BYTE, byteBuffer);
		return textureId.get(0);

	} catch (Exception e) {
    	e.printStackTrace();
    	System.exit(-1);
    }

	return -1;
}
 
Example #14
Source File: PolygonBuilderBase.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void error(int errorCode) {
	Log.debug("Failed to create polygon, cause %d=%s", errorCode, GLU.gluErrorString(errorCode));
	failed = true;
}