So I have successfully imported textures into my game, but they are upside down, distorted and also off-centre.
The code can also be found here: http://pastebin.com/nvWMZqpX
If anyone could help it would be greatly appreciated.
public class Main {
private Texture sky;
public Main() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Spark");
Display.create();
sky = TextureLoader.getTexture("PNG", new FileInputStream(new File("resources/textures/sky.png")));
while(!Display.isCloseRequested()) {
setCamera();
sky.bind();
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3d(1, 1, 1);
glTexCoord2f(1, 0);
glVertex2i(640, 0);
glTexCoord2f(0, 0);
glVertex2i(0, 0);
glTexCoord2f(0, 1);
glVertex2i(0, 480);
glTexCoord2f(0, 1);
glVertex2i(0, 480);
glTexCoord2f(1, 1);
glVertex2i(640, 640);
glTexCoord2f(1, 0);
glVertex2i(640, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
Display.update();
Display.sync(60);
}
sky.release();
Display.destroy();
} catch (LWJGLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Main();
}
public static void setCamera() {
//Clear Screen
glClear(GL_COLOR_BUFFER_BIT);
//Modifying the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
//Modify modelviewing matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
Is your texture a power of 2 (POT) image? (for example 512x512 pixels). This could be the cause of the distortion.
Also try inverting your texcoord arguments. This would fix the upside down part.
I might also noticed a wrong argument
glTexCoord2f(1, 1);
glVertex2i(640, 0);
glTexCoord2f(0, 1);
glVertex2i(0, 0);
glTexCoord2f(0, 0);
glVertex2i(0, 480);
glTexCoord2f(0, 0);
glVertex2i(0, 480);
glTexCoord2f(1, 0);
glVertex2i(640, 480); // <--- this line was 640,640?
glTexCoord2f(1, 1);
glVertex2i(640, 0);
Texture coordinates in openGL are handled upside down like this:
(source: learnopengles.com)
Related
Everytime I am running this code it will give me a white window with the loading cursor of death. After some seconds the window is marked as non responding.
This is my code that I have written:
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit())
throw new IllegalStateException("Couldn't initialize GLFW");
long window = GLFW.glfwCreateWindow(1200, 750, "WorldCraft", MemoryUtil.NULL, MemoryUtil.NULL);
//GLFW.glfwMakeContextCurrent(window);
// Enable v-sync
Timer timer = new Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (GLFW.glfwGetCurrentContext() == MemoryUtil.NULL) {
GLFW.glfwMakeContextCurrent(window);
GLFW.glfwSwapInterval(1);
GLFW.glfwShowWindow(window);
GL.createCapabilities();
}
System.out.println("Begin Render");
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GLFW.glfwPollEvents();
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(0.65F, 0.34F, 0.15F);
GL11.glVertex3d(8, 0, 8);
GL11.glVertex3d(-8, 0, 8);
GL11.glVertex3d(8, 0, -8);
GL11.glVertex3d(-8, 0, -8);
GL11.glEnd();
GL11.glPopMatrix();
GLFW.glfwSwapBuffers(window);
System.out.println("End Render");
}
});
timer.start();
What am I doing wrong?
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 a rectangle and this will be the base of my moving object.
I`m trying to rotate the object only when the A button is pressed, when is released the object should stop rotating.
package Tanc;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.LWJGLException;
public class Tanc{
public Tanc(){
try{
Display.setDisplayMode(new DisplayMode(640,480));
Display.setTitle("Tanc");
Display.create();
}catch(LWJGLException e){
e.printStackTrace();
}
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glOrtho(1, 1, 1, 1, 1, -1);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glMatrixMode(GL11.GL_PROJECTION);
float y_angle = 0;
boolean aFlag = false;
while(!Display.isCloseRequested()){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glRectd(0, 0, 0.2f, 0.3f);
GL11.glTranslatef(0, 0, 0);
while(Keyboard.next()){
if(Keyboard.getEventKey() == Keyboard.KEY_A){
aFlag = true;
}
}
if(aFlag){
y_angle = 0.1f;
GL11.glRotatef(y_angle, 0, 0, 1);
}
else{
y_angle = 0;
GL11.glRotatef(0, 0, 0, 1);
}
// GL11.glPopMatrix();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
public static void main(String[] args){
new Tanc();
}
}
That is because you never really rotate anything. You y_angle doesn't really change other than from 0.0 to 0.1.
Remember that the angle parameter glRotatef() takes, need to be in degrees and not radians. A full circle in radians goes from 0.0 to ~6.2831 radians. Where a full circle using degress goes from 0.0 to 360.0 degress. So your angle isn't noticeable at all, because your only changing it by such a small amount.
I've changed your code. When you now hold the A button, it will rotate and when you release the button it will stop rotating.
float y_angle = 0;
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glLoadIdentity();
GL11.glRectd(0, 0, 0.2f, 0.3f);
GL11.glTranslatef(0, 0, 0);
while (Keyboard.next()){
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
y_angle += 10f;
}
}
GL11.glRotatef(y_angle, 0, 0, 1);
Display.update();
Display.sync(60);
}
I'm trying to fill a torus texture of wood, but leaves only one color:
img http://dl.dropbox.com/u/70996029/Screenshots/Screenshot-2012-04-12_01.28.44.png
The image is 128x128 png. What am I doing wrong?
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LESS);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glOrtho(-17.0, 17.0, -17.0, 17.0, -17.0, 17.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
public void display(GLAutoDrawable drawable)
{
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
float mat_diffuse[] = { 0.5f, 0.5f, 0.8f, 1.0f };
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
glut.glutSolidTorus(2, 5, 80, 80);
gl.glRotatef(90, 1, 0, 0);
gl.glTranslatef(5, 0, 0);
Texture texture = null;
try {
texture = TextureIO.newTexture(new File("D:\\Program Files\\eclipse\\projects\\rotation OGL\\src\\wood.png"), true);
texture.setTexParameterf(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
texture.setTexParameterf(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
} catch (Exception e) {
e.printStackTrace();
}
gl.glEnable(GL2.GL_TEXTURE_2D);
texture.bind(gl);
texture.enable(gl);
glut.glutSolidTorus(2, 5, 80, 80);
texture.disable(gl);
}
Try generating some texture coordinates for your geometry.
glutSolidTorus() does not generate any for you.