LWJGL Forum

Programming => OpenGL => Topic started by: bobleny on April 03, 2013, 16:40:27

Title: [SOLVED] glTranslatef doesn't seem to be affecting glDrawElements
Post by: bobleny on April 03, 2013, 16:40:27
Hi,
I am having some issues getting a quad drawn with glDrawElements to translate. I am more or less working with the example found here: http://www.lwjgl.org/wiki/index.php?title=The_Quad_colored

With few modifications, a direct copy and paste of their example works great.
However, I am not a fan of (0, 0, 0) being at the center of the screen, and I don't like how it's size is determined using coordinates between 0 and 1. Since I am focusing on making a 2d game right now, I've implemented glOrtho and glTranslatef, but I cannot get the quad's origin to translate.

This is my main class:

package shadedquad;

import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

public class Main
{
// Setup variables
private final int WIDTH = 640;
private final int HEIGHT = 480;

// Quad variables
private int _vaoId = 0;
private int _vboId = 0;
private int _vbocId = 0;
private int _vboiId = 0;
private int _indicesCount = 0;

// Shader variables
private int _vsId = 0;
private int _fsId = 0;
private int _pId = 0;

public Main() throws Exception
{
// Initialize OpenGL (Display)
InitGL(WIDTH, HEIGHT);
System.out.println(glGetString(GL_VERSION));

this.setupQuad();
this.setupShaders();

while (!Display.isCloseRequested())
{
// Do a single loop (logic/render)
this.loopCycle();

// Force a maximum FPS of about 60
Display.sync(60);
// Let the CPU synchronize with the GPU if GPU is tagging behind
Display.update();
}

// Destroy OpenGL (Display)
this.destroyOpenGL();
}

private void InitGL(int width, int height) throws Exception
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.setVSyncEnabled(true);
Display.setFullscreen(false);
Display.create();

glClearColor(0.2f, 0.0f, 0.5f, 0.0f);
// glDisable(GL_LIGHTING);
// glDisable(GL_DEPTH_TEST);
//
// glEnable(GL_BLEND);
// glEnable(GL_TEXTURE_2D);
// glShadeModel(GL_SMOOTH);
//
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, width, height, 0, 1, -1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

public void setupQuad()
{
float[] vertices =
{
0, 0, 0, 1,
1, 0, 0, 1,
1, 1, 0, 1,
0, 1, 0, 1
};

FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
verticesBuffer.put(vertices);
verticesBuffer.flip();

float[] colors =
{
1f, 0f, 0f, 1f,
0f, 1f, 0f, 1f,
0f, 0f, 1f, 1f,
1f, 1f, 1f, 1f
};

FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length);
colorsBuffer.put(colors);
colorsBuffer.flip();

// OpenGL expects to draw vertices in counter clockwise order by default
byte[] indices =
{
0, 1, 2,
0, 2, 3
};

_indicesCount = indices.length;
ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(_indicesCount);
indicesBuffer.put(indices);
indicesBuffer.flip();

// Create a new Vertex Array Object in memory and select it (bind)
_vaoId = glGenVertexArrays();
glBindVertexArray(_vaoId);

// Create a new Vertex Buffer Object in memory and select it (bind) - VERTICES
_vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, _vboId);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Create a new VBO for the indices and select it (bind) - COLORS
_vbocId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, _vbocId);
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Deselect (bind to 0) the VAO
glBindVertexArray(0);

// Create a new VBO for the indices and select it (bind) - INDICES
_vboiId = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboiId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

private void setupShaders() throws Exception
{
// Load the vertex shader
_vsId = LoadShader("src/Shaders/Vertex.glsl", GL_VERTEX_SHADER);
// Load the fragment shader
_fsId = LoadShader("src/Shaders/Fragment.glsl", GL_FRAGMENT_SHADER);

// Create a new shader program that links both shaders
_pId = glCreateProgram();
glAttachShader(_pId, _vsId);
glAttachShader(_pId, _fsId);
glLinkProgram(_pId);

// Position information will be attribute 0
glBindAttribLocation(_pId, 0, "in_Position");
// Color information will be attribute 1
glBindAttribLocation(_pId, 1, "in_Color");

glValidateProgram(_pId);
}

public int LoadShader(String filename, int type) throws Exception
{
StringBuilder shaderSource = new StringBuilder();
int shaderID = 0;

BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null)
{
shaderSource.append(line).append("\n");
}
reader.close();

shaderID = glCreateShader(type);
glShaderSource(shaderID, shaderSource);
glCompileShader(shaderID);

return shaderID;
}

public void loopCycle()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glColor3f(1F, 1F, 1F);

glPushMatrix();
glTranslatef(100, 200, 0);

glUseProgram(_pId);

// Bind to the VAO that has all the information about the vertices
glBindVertexArray(_vaoId);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

// Bind to the index VBO that has all the information about the order of the vertices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboiId);

// Draw the vertices
glDrawElements(GL_TRIANGLES, _indicesCount, GL_UNSIGNED_BYTE, 0);

// Put everything back to default (deselect)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
glUseProgram(0);

glBegin(GL_LINES);
glVertex2i(0, 0);
glVertex2i(100, 100);
glEnd();
glPopMatrix();
}

public void destroyOpenGL()
{
// Delete the shaders
glUseProgram(0);
glDetachShader(_pId, _vsId);
glDetachShader(_pId, _fsId);

glDeleteShader(_vsId);
glDeleteShader(_fsId);
glDeleteProgram(_pId);

// Select the VAO
glBindVertexArray(_vaoId);

// Disable the VBO index from the VAO attributes list
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

// Delete the vertex VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(_vboId);

// Delete the color VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(_vbocId);

// Delete the index VBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(_vboiId);

// Delete the VAO
glBindVertexArray(0);
glDeleteVertexArrays(_vaoId);

Display.destroy();
}

public static void main(String[] args)
{
try
{
new Main();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


This is the vertex shader:

#version 150 core

in vec4 in_Position;
in vec4 in_Color;

out vec4 pass_Color;

void main(void) {
gl_Position = in_Position;
pass_Color = in_Color;
}


This is the fragment shader:

#version 150 core

in vec4 pass_Color;

out vec4 out_Color;

void main(void) {
out_Color = pass_Color;
}


When ran, I get a dark blue background with a white diagonal line stating at (100, 200, 0), from the top left corner of the window, and ending at (200, 300, 0). I also get a multi-colored quad that takes up the entire top right corner of the window with it's origin apparently at the center of the window.

I would think the quad would be a 1 pixel square appearing at (100, 200, 0);

Any ideas what the problem is?

Thanks!
Title: Re: glTranslatef doesn't seem to be affecting glDrawElements
Post by: quew8 on April 08, 2013, 08:01:58
Your using shaders now, so you have to setup all of the matrix stuff yourself in your vertex shader. Any call like glOrtho glTranslate, glLoadMatrix, all will do absolutely nothing. I'm pretty sure the next tutorial deals with transformations though. Take a look.
Title: Re: glTranslatef doesn't seem to be affecting glDrawElements
Post by: bobleny on April 08, 2013, 16:34:59
Thanks for the response.

That is what I ended up having to do and it all makes sense now.