This is my drawRect code:
public static void drawRect(float X, float Y, float WIDTH, float HEIGHT, float RED, float GREEN, float BLUE)
{
// clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(RED, GREEN, BLUE);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(X,Y);
GL11.glVertex2f(X+WIDTH,Y);
GL11.glVertex2f(X+WIDTH,Y+HEIGHT);
GL11.glVertex2f(X,Y+HEIGHT);
GL11.glEnd();
}
This is what i'm doing
Renderer.drawRect(0, 0, Display.getWidth(), Display.getHeight(), 255, 255, 255);
It fills the entire screen (like it should) but the color is always black.
I suggested reading tutorials because glColor3f() expects 3 floats in the 0...1 range for color components, just like most accelerated graphics API-s. And if this one slipped, there may be confusion about other details too. But nevertheless, 255 would be still clamped to 1, so the routine does not draw a black rectangle, something is missing before (in the setup) and/or after (like a call making the drawing actually appear on screen).
LWJGL wiki has a complete example code for exactly what you are trying to do, by the way: http://wiki.lwjgl.org/wiki/LWJGL_Basics_3_(The_Quad).html
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class QuadExample {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException 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);
while (!Display.isCloseRequested()) {
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(0.5f,0.5f,1.0f);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
GL11.glEnd();
Display.update();
}
Display.destroy();
}
public static void main(String[] argv) {
QuadExample quadExample = new QuadExample();
quadExample.start();
}
}
Related
Currently, I am able to move the box to the left and right through the display boundary, but I am unable to figure out how to move the box up and down. I would like the box to navigate in a circle around the display, e.g move right, then down, then to the left, then up, then continuously moving throughout the display. Here is my code for my entity class:
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
class Entity
{
private static enum State { START, LEFT, RIGHT, UP, DOWN};
private Rectangle box;
private State state;
private float speed; // pixels / ms
public Entity(float speed)
{
box = new Rectangle(10, 10, 10, 10);
state = State.START;
this.speed = speed;
}
public void draw()
{
float x = (float)box.getX();
float y = (float)box.getY();
float w = (float)box.getWidth();
float h = (float)box.getWidth();
// draw the square
GL11.glColor3f(0,1,0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y);
GL11.glVertex2f(x+w, y);
GL11.glVertex2f(x+w, y+w);
GL11.glVertex2f(x, y+w);
GL11.glEnd();
}
public void update(int delta)
{
switch (state)
{
case START:
state = State.RIGHT;
case RIGHT:
box.translate((int)(speed*delta), 0);
if (box.getX() >= 800)
{
state = State.LEFT;
}
break;
case LEFT:
box.translate((int)(-speed*delta), 0);
if (box.getX() <= 0)
{
state = State.RIGHT;
}
break;
}
}
}
Here is my code for GameLoop:
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
import org.lwjgl.LWJGLException;
public class GameLoop
{
public static final int TARGET_FPS=100;
public static final int SCR_WIDTH=800;
public static final int SCR_HEIGHT=600;
public static void main(String[] args) throws LWJGLException
{
initGL(SCR_WIDTH, SCR_HEIGHT);
Entity e = new Entity(.1f);
long time = (Sys.getTime()*4000)/Sys.getTimerResolution(); // ms
while (! Display.isCloseRequested())
{
long time2 = (Sys.getTime()*4000)/
Sys.getTimerResolution(); // ms
int delta = (int)(time2-time);
System.out.println(delta);
e.update(delta);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
e.draw();
// UPDATE DISPLAY
Display.update();
Display.sync(TARGET_FPS);
time = time2;
}
Display.destroy();
}
public static void initGL(int width, int height) throws LWJGLException
{
// open window of appropriate size
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
Display.setVSyncEnabled(true);
// enable 2D textures
GL11.glEnable(GL11.GL_TEXTURE_2D);
// set "clear" color to black
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// set viewport to entire window
GL11.glViewport(0,0,width,height);
// set up orthographic projectionr
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
// GLU.gluPerspective(90f, 1.333f, 2f, -2f);
// GL11.glTranslated(0, 0, -500);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
}
Any help would be appreciated. I also need to have the box change colors every time it changes direction.
Correct me if i'm wrong but i think you just need to use box.translate(0,value) to move your Rectangle up or down. Just adapt your left-right movement to the y value of the box. You could also use the State to determine which color it should be drawn in.
I am trying to render a textured quad onto the screen as a start menu button for my new game. But when rendering it just renders as a white quad, i have searched for days over the internet and i havent found a single answer that has fixed the problem.
My texture is wood.png and it is in a "res" folder inside a resources source folder in the project. it is a 128 * 128 pixel image.
The code for rendering textures is as follows:
public static void renderTexture(Texture texture, float width, float height, float x, float y) {
texture.bind();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
texture.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glLoadIdentity();
glEnd();
glDisable(GL_BLEND);
}
The code that i use to load the textures is:
public static Texture loadTexture(String fileName){
try {
Texture texture = TextureLoader.getTexture("PNG",Class.class.getResourceAsStream("/res/"+fileName+".png"));
return texture;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I have a static Texture storing private static Texture startTex = Loader.loadTexture("wood"); and i render it every frame by doing:
RenderSystem.renderTexture(startTex, 200, 200, 0, 0);
The Answer is that i did glBegin(GL_TEXTURE_2D); instead of glEnable(GL_TEXTURE_2D);
Sorry the code for that part wasnt shown.
Hi guys i am trying to draw a 100 by 100 image to my screen. However when i draw it, openGL draws it half the size of my square with a kind of border round the right and bottom edges.
This is my code:
package biz.boulter.lwjglgame;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;
public class Game
{
public static final int WIDTH = 1000;
public static final int HEIGHT = WIDTH / 16 * 9;
private static Texture t;
private static void render()
{
glClear(GL_COLOR_BUFFER_BIT);
t.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(100, 100); // top-left
glTexCoord2f(1, 0);
glVertex2i(200, 100); // top-right
glTexCoord2f(1, 1);
glVertex2i(200, 200); // bottom-right
glTexCoord2f(0, 1);
glVertex2i(100, 200); // bottom-left
glEnd();
}
private static Texture loadTexture(String path) throws Exception{
return TextureLoader.getTexture("PNG", Game.class.getResourceAsStream(path));
}
public static void main(String[] args)
{
try
{
Display.setDisplayMode(new DisplayMode(1000, 1000 / 16*9));
Display.create();
t = loadTexture("/assets/guy.png");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1000, 1000/16*9, 0, 1, -1);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
while(!Display.isCloseRequested())
{
render();
Display.sync(60);
Display.update();
}
}catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
Why is it doing this. Also i am using slick-util.jar for the texture loading.
ScreenShot:
http://oi59.tinypic.com/13zyflk.jpg
I have this code that I understand now:
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class Main {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 0, 800);
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);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(1.0f,1.0f,1.0f);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
GL11.glEnd();
Display.update();
}
Display.destroy();
}
public static void main(String[] argv) {
Main quadExample = new Main();
quadExample.start();
}
}
I want to know how to draw the box so that it is not drawn on the screen, but setback a bit, how?
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(100,100,-100);
GL11.glVertex3f(100+200,100,-100);
GL11.glVertex3f(100+200,100+200,-100);
GL11.glVertex3f(100,100+200,-100);
GL11.glEnd();
Though that won't make any visual difference with a single quad in ortho. You'll have to switch to a perspective projection matrix for it to look like it's "moved back".
I'm trying to get a simple triangle drawn in Java using LWJGL.
I'm trying to get a simple triangle up, each with a corner of one specific color. Right now it is just giving me a blank screen.
Here is my code:
package com.ex;
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 ColoredTriangle {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// Init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 480, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
boolean quit = false;
while (!quit) {
// Clear the screen.
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// Begin drawing
GL11.glBegin(GL11.GL_TRIANGLES);
// Top & Red
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex2f(0.0f, 1.0f);
// Right & Green
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex2f(1.0f, 1.0f);
// Left & Blue
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex2f(1.0f, -1.0f);
GL11.glEnd();
Display.update();
if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
quit = true;
}
Display.destroy();
}
public static void main(String args[]) {
ColoredTriangle ct = new ColoredTriangle();
ct.start();
}
}
It is working perfectly fine, it's just that your triangle is 1 unit high and your window is 480 units high, so it only shows up as one pixel in the corner.
If you replace GL11.glOrtho(0, 640, 480, 0, 1, -1); with GL11.glOrtho(-3.2, 3.2, -2.4, 2.4, -1, 1); then you'll see everything just fine.