Related
I am modeling a 3D maze in Java OpenGL (JOGL) and I am facing an issue about lights. No matter how hard I try lights don't act the way I want to.
This is setup in init method (which is called when the window is created):
// nastaveni materialu - difusni slozka
float[] mat_dif = new float[] { 0.3f, 0.3f, 0.3f, 0.3f };
// nastaveni materialu - zrcadlova slozka
float[] mat_spec = new float[] { 1, 1, 1, 1 };
// nastaveni materialu - ambientni slozka
float[] mat_amb = new float[] { 0.3f, 0.3f, 0.3f, 0.3f };
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, mat_amb, 0);
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_DIFFUSE, mat_dif, 0);
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, mat_spec, 0);
//nastaveni bileho svetla
float[] lightWhite = new float[] { 1, 1, 1, 1 };
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT_AND_DIFFUSE, lightWhite, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, lightWhite, 0);
//nastaveni cerveneho
float[] lightRed = new float[] { 1, 0, 0, 1 };
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT_AND_DIFFUSE, lightRed, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightRed, 0);
//nastaveni modreho
float[] lightBlue = new float[] { 0, 0, 1, 1 };
gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_AMBIENT_AND_DIFFUSE, lightBlue, 0);
gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_SPECULAR, lightBlue, 0);
and code in display method (called when each frame is rendered)
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
light_position = new float[] {6.0f, 2.0f, -6.0f, 1.0f};// bod v prostoru
light_direction = new float[] {0.0f, 0.0f, -6.0f, 0.0f};
light_position2 = new float[] {8.0f, 2.0f, -7.0f, 1.0f};// bod v prostoru
light_direction2 = new float[] {15.0f, 0.0f, -7.0f, 0.0f};
light_position3 = new float[] {8.0f, 2.0f, -4.0f, 1.0f};// bod v prostoru
light_direction3 = new float[] {8.0f, 0.0f, 0.0f, 0.0f};
gl.glPushMatrix();
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
gl.glLightf(GL2.GL_LIGHT0,GL2.GL_SPOT_CUTOFF,60.0f);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPOT_DIRECTION, light_direction, 0);
gl.glLightf(GL2.GL_LIGHT0,GL2.GL_SPOT_EXPONENT,10.0f);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, light_position2, 0);
gl.glLightf(GL2.GL_LIGHT1,GL2.GL_SPOT_CUTOFF,60.0f);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPOT_DIRECTION, light_direction2, 0);
gl.glLightf(GL2.GL_LIGHT1,GL2.GL_SPOT_EXPONENT,10.0f);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_POSITION, light_position3, 0);
gl.glLightf(GL2.GL_LIGHT2,GL2.GL_SPOT_CUTOFF,35.0f);
gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_SPOT_DIRECTION, light_direction3, 0);
gl.glLightf(GL2.GL_LIGHT2,GL2.GL_SPOT_EXPONENT,10.0f);
gl.glPopMatrix();
gl.glColorMaterial (GL2.GL_FRONT_AND_BACK,
GL2.GL_AMBIENT_AND_DIFFUSE ) ;
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_LIGHT1);
gl.glEnable(GL2.GL_LIGHT2);
gl.glShadeModel(GL2.GL_SMOOTH);
for better understending, there is a picture
after this code everything is dark and there is no light.
thank you so much for your time. :)
I have been trying for hours to get a Texture in LWJGL to stretch to a quad.
Here is the code I am using for the quad:
private static void renderLoad() {
glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
texture.bind();
glPushMatrix();{
glBegin(GL_QUADS);
{
glTexCoord2f(0, 1);
glVertex2f(0, 0); //Upper-left
glTexCoord2f(1, 1);
glVertex2f(Display.getWidth(), 0); //Upper-right
glTexCoord2f(1, 0);
glVertex2f(Display.getWidth(), Display.getHeight()); //Bottom-right
glTexCoord2f(0, 0);
glVertex2f(0, Display.getHeight()); //Bottom-left
}
glEnd();
}glPopMatrix();
}
This is what the display looks like when I run it:
http://gyazo.com/376ddb0979c55226d2f63c26215a1e12
I am trying to make the image expand to the size of the window. The Quad is at the size of the window, but the texture seems to not stretch.
Here is what it looks like if I do not use a texture and I simple make the quad a color:
http://gyazo.com/65f21fe3efa2d3948de69b55d5c85424
If it helps here is my main loop:
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, displaySizeX, displaySizeY);
glLoadIdentity();
glOrtho(0, displaySizeX, 0, displaySizeY, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
texture = loadLoadingImage();
//This is the main loop for the game.
while(!Display.isCloseRequested()){
delta = getDelta();
updateFPS();
if(Display.wasResized()){
displaySizeX = Display.getWidth();
displaySizeY = Display.getHeight();
glViewport(0, 0, displaySizeX, displaySizeY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, displaySizeX, 0, displaySizeY, -1, 1);
}
render();
checkInput();
Display.update();
Display.sync(sync);
}
cleanUp();
return true;
How do I make the image stretch to the quad?
public void stretch() {
Color.white.bind();
texture.bind
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(100,100);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(100+texture.getTextureWidth(),100);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(100+texture.getTextureWidth(),100+character.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(100,100+texture.getTextureHeight());
GL11.glEnd(); // all the 0's were originally 100 but it was off centered
}
texture = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("res/texture.png"));
Try using this. This is usually how I do this.
Perhaps something is modifying the texture matrix. You could try adding a
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
to see if that affects anything.
I'm running this lwjgl application:
Display.setDisplayMode(new DisplayMode(500, 500));
Display.create();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 5, -5, 5, -10, 5);
glMatrixMode(GL_MODELVIEW);
float x = 0;
while (!Display.isCloseRequested()) {
Display.update();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glColor3f(1, 0, 0);
x += 0.01f;
glRotatef(x, x, 3 * x, 0.5f * x);
glBegin(GL_QUADS);
drawCube();
glEnd();
glLoadIdentity();
}
Display.destroy();
Which basically draws a 1x1x1 cube in the window. Method drawCube() is this:
public void drawCube() {
glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
glVertex3f(1.0f, 1.0f, 0f); // Top Right Of The Quad (Top)
glVertex3f(0f, 1.0f, 0f); // Top Left Of The Quad (Top)
glVertex3f(0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
glVertex3f(1.0f, 0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(0f, 0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(0f, 0f, 0f); // Bottom Left Of The Quad (Bottom)
glVertex3f(1.0f, 0f, 0f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(0f, 0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f(1.0f, 0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
glVertex3f(1.0f, 0f, 0f); // Bottom Left Of The Quad (Back)
glVertex3f(0f, 0f, 0f); // Bottom Right Of The Quad (Back)
glVertex3f(0f, 1.0f, 0f); // Top Right Of The Quad (Back)
glVertex3f(1.0f, 1.0f, 0f); // Top Left Of The Quad (Back)
glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
glVertex3f(0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(0f, 1.0f, 0f); // Top Left Of The Quad (Left)
glVertex3f(0f, 0f, 0f); // Bottom Left Of The Quad (Left)
glVertex3f(0f, 0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
glVertex3f(1.0f, 1.0f, 0f); // Top Right Of The Quad (Right)
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f(1.0f, 0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f(1.0f, 0f, 0f); // Bottom Right Of The Quad (Right)
}
It outputs this:
To me lines are pretty horrible, Does lwjgl have anti aliasing? If yes, how can i enable it?
For Anti-Aliasing, you can use multisampling. To enable it, add a PixelFormat parameter to Display.create();. Here's an example:
Display.create(new PixelFormat(/*Alpha Bits*/8, /*Depth bits*/ 8, /*Stencil bits*/ 0, /*samples*/8)); I'm not sure what the first 3 parameters do - these are values I found here. I've also seen people use Display.create(new PixelFormat(8,0,0,8)).
NOTE: If you set the AA too high (or other things an incorrect value) it will throw an LWJGLException.
Hope this helps.
PS apologies for the late answer.
Comments
Antialiasing can be achieved by creating a multisample framebuffer, either in a framebuffer object or through the default framebuffer when the LWJGL window is created for the first time.
If you are learning LWJGL and OpenGL then learn how to use VBOs, because glBegin, glVertex, etc. are removed from the core profile of OpenGL.
VBO Example
Here is a little example of a VBO storing Vertices and Texture Coordinates for two triangles and rendering it!
I assume that you know how to load and bind textures already.
Creating the VBO
This is the code where you create the actual Vertex and Texture Coordinate Buffer and store them onto the GPU.
int vertices = 6;
int vertex_size = 3; // X, Y, Z,
int texture_size = 2; // U, V,
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
FloatBuffer texture_data = BufferUtils.createFloatBuffer(vertices * texture_size);
texture_data.put(new float[] { 0f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
vertex_data.flip();
texture_data.flip();
int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vbo_texture_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glBufferData(GL_ARRAY_BUFFER, texture_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Rendering the VBO
Then when you want to render the VBO, you need to do the following.
texture.bind();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glTexCoordPointer(texture_size, GL_FLOAT, 0, 0l);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices); // The vertices is of course the max vertices count, in this case 6
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
texture.unbind();
Deleting the VBO
Then when you're done with the VBO and you don't need it anymore, you can delete it by doing the following.
glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_texture_handle);
LWJGL/OpenGL - Static Imports
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
Info
You only have to create a VBO once (It is really bad to create one for each frame), so if you used VBO to store your terrain. Then if some changes happens then only at that point you want to update the VBO, else just keep it as it is. If you use the VBO for terrain and the terrain is really huge, then split the terrain into different chunks of terrain with it's own VBO.
Extra
If you have an Wavefront OBJ Model, and you want to render it multiple times, the best way to do that, would be to load the whole model to one VBO. Then you would do some Instancing to render it multiple times in multiple position, etc.
Instancing Tutorial 1
Instancing Tutorial 2
Update
A little example of a VBO storing Vertices and Colors for a Triangle and rendering it!
Creating the VBO.
This is the code where you create the actual Vertex and Color Buffer and bind them to the VBO.
int vertices = 3;
int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });
vertex_data.flip();
FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });
color_data.flip();
int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Rendering the VBO.
This is the code you need to call, to render the VBO.
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
The imports and the code for disposing/deleting the buffers are the same as the previous example!
Update 2 - Complete Example
Okay, so here is a complete example of a VBO containing Vertices and Colors. The example is 100% LWJGL only!
When I run the following code this is the desired result I get.
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class VBOTest
{
public final static void main(String[] args)
{
int width = 1280;
int height = 720;
try
{
Display.setTitle("VBO Test");
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(0);
}
/*
* Initialize OpenGL States
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0f, width, height, 0f, -1f, 1f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/*
* Creating the Vertex & Color VBO
*/
final int VERTEX_SIZE = 3; // X, Y, Z,
final int COLOR_SIZE = 4; // R, G, B, A,
int vertices = 6;
int vbo_vertex_handle;
int vbo_color_handle;
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * VERTEX_SIZE);
float half_width = 200f;
float half_height = 200f;
vertex_data.put(new float[] { -half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.flip();
FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * COLOR_SIZE);
color_data.put(new float[] { 1f, 0f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 0f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.flip();
vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*
* Main Rendering Loop
*/
boolean running = true;
while (running)
{
running = (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
{
glTranslatef(width / 2f, height / 2f, 0f);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(VERTEX_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(COLOR_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
glPopMatrix();
glFlush();
Display.sync(60);
Display.update();
}
/*
* Dispose Elements
*/
glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_color_handle);
Display.destroy();
System.exit(0);
}
}
This has probably something to do with my transformations, but right now I can't figure this out and this is driving me instane. I have wrapped the draw code so that I can easily define new triangles. However, when I put this into a function, it just shows a grey screen. Te function code is as follows:
public void Draw(float[] mViewMatrix, float[] mModelMatrix, float[] mProjectionMatrix, int mPositionHandle, int mColorHandle, int mMVPMatrixHandle)
{
long time = SystemClock.uptimeMillis() % 10000L;
float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
//aBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
aBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
The code which IS working is:
public void onDrawFrame(GL10 glUnused)
{
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
// Do a complete rotation every 10 seconds.
long time = SystemClock.uptimeMillis() % 10000L;
float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
// Draw the triangle facing straight on.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle1Vertices);
// Draw one translated a bit down and rotated to be flat on the ground.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle2Vertices);
// Draw one translated a bit to the right and rotated to be facing to the left.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, 90.0f, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle3Vertices);
*/
/*
for (int x = 0; x < staticHolder.objectList.size(); x++)
{
staticHolder.objectList.get(x).Draw(mViewMatrix, mModelMatrix, mProjectionMatrix, mPositionHandle, mColorHandle, mMVPMatrixHandle);
}
*/
}
/**
* Draws a triangle from the given vertex data.
*
* #param aTriangleBuffer The buffer containing the vertex data.
*/
private void drawTriangle(final FloatBuffer aTriangleBuffer)
{
// Pass in the position information
aTriangleBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
aTriangleBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
I am passing in the same variables and the final variables used here are initialized the same. There is some other work that happens in the function for encapsulation. Any idea why it is refusing to render in the function?
The following code loads the objects in the list:
final float[] triangle1VerticesData = {
// X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.559016994f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f};
final float[] triangle2VerticesData = {
// X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 0.559016994f, 0.0f,
1.0f, 0.0f, 1.0f, 1.0f};
// This triangle is white, gray, and black.
final float[] triangle3VerticesData = {
// X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f,
0.0f, 0.559016994f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
staticHolder.objectList.add(new Triangle(triangle1VerticesData));
staticHolder.objectList.add(new Triangle(triangle2VerticesData));
staticHolder.objectList.add(new Triangle(triangle3VerticesData));
The receiving class is:
public class Triangle extends shape
{
public Triangle(float[] data)
{
verts = data;
}
}
After the following bit of code:
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
You must put the vertices into the buffer (otherwise, it's blank!):
aBuffer.put(verts);
The reason this isn't in the bit of code that works, is because those three sets of vertices' buffers are pre-allocated, and the vertices are put into it then (at initialization). They are simply passed to the method each time, so they don't have to be put() in again.
On that note, you will want to avoid allocations in your Draw method, as it's called many times per frame and could lead to slow rendering. Allocate aBuffer once, and put new vertices into it each time.
I'm trying to render a quad with a texture, but it's not working.
Here is my code:
//Init Code
GL11.glViewport(0, 0, WIDTH, HEIGHT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, ((float) WIDTH / (float) HEIGHT), 0.1f, 100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
GL11.glEnable(GL11.GL_TEXTURE_2D);
//Render Code
BufferedImage textures = null;
try {
textures = ImageIO.read(BlockWorldComponent.class.getResource("/textures.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImage img = textures;
int w = img.getWidth();
int h = img.getHeight();
int imgInt[] = new int[w * h];
byte imgByte[] = new byte[w * h * 4];
img.getRGB(0, 0, w, h, imgInt, 0, w);
for (int i = 0; i < imgInt.length; i++) {
int a = imgInt[i] >> 24 & 0xFF;
int r = imgInt[i] >> 16 & 0xFF;
int g = imgInt[i] >> 8 & 0xFF;
int b = imgInt[i] & 0xFF;
imgByte[i * 4 + 0] = (byte) r;
imgByte[i * 4 + 1] = (byte) g;
imgByte[i * 4 + 2] = (byte) b;
imgByte[i * 4 + 3] = (byte) a;
}
ByteBuffer imageData = ByteBuffer.allocateDirect(imgByte.length);
imageData.order(ByteOrder.nativeOrder());
imageData.clear();
imageData.put(imgByte, 0, imgByte.length);
imageData.flip();
int[] tex = new int[1];
tex[0] = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex[0]);
System.out.println(tex[0]);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, w, h, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageData);
GL11.glScalef(0.25f, 0.25f, 0.25f);
//GL11.glColor3f(0.0f,1.0f,0.0f);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex[0]);
GL11.glBegin(GL11.GL_QUADS);
//Top Left
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, -1.0f);
//Top Right
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f);
//Bottom Left
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
//Bottom Right
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
GL11.glEnd();
I see a couple problems here. First, you don't seem to have a texture bound when you call glTexImage2D. You will need to call glBindTexture(GL_TEXTURE_2D, tex[0]); prior to calling it in order for the texture to be properly set up by OpenGL. And, of course, you will need to have populated tex[0] by means of a call to glGenTextures prior to that.
Secondly, you cannot call glBindTexture between calls to glBegin and glEnd. As a result, the first bind you call in your drawing section, GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); will take effect, but not the second, since the second one is after your call to glBegin. As such, no texture will be used in rendering.
Have you enabled texturing in your project?
glEnable(GL_TEXTURE_2D);
Or perhaps in your case:
GL11.glEnable(GL_11.GL_TEXTURE_2D);