Related
I have an issue in my project where I am trying to draw a pyramid on screen but the depth is not being displayed.
public void create(float[] vertices, int[] indices, int numberOfVertices, int numberOfIndices) {
indexCount = numberOfIndices;
vao = gl.genVertexArrays();
gl.bindVertexArray(vao);
IntBuffer indicesBuffer = factory.create(indices);
ibo = gl.genBuffers();
gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
FloatBuffer verticesBuffer = factory.create(vertices);
vbo = gl.genBuffers();
gl.bindBuffer(GL_ARRAY_BUFFER, vbo);
gl.bufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
gl.vertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
gl.bindBuffer(GL_ARRAY_BUFFER, 0);
gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
gl.bindVertexArray(0);
}
I create my mesh object with following code where Vertices and Indices are defined as :
private void createTriangles() {
float[] vertices = new float[] {
//x y z
-1.0f, -1.0f, 0.0f, //0
0.0f, 0.0f, 1.0f, //1
1.0f, -1.0f, 0.0f, //2
0.0f, 1.0f, 0.0f //3
};
int[] indices = new int[] {
0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2
};
mesh.create(vertices, indices, 12, 12);
}
In order to display them to screen I call my render function from my game loop which is defined as.
public void render() {
gl.bindVertexArray(vao);
gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
gl.drawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
gl.bindVertexArray(0);
}
To create my FloatBuffer and IntBuffer I have a factory class defined as.
public class BufferFactory {
public IntBuffer create(int[] indices) {
IntBuffer buffer = BufferUtils.createIntBuffer(indices.length);
buffer.put(indices);
buffer.flip();
return buffer;
}
public FloatBuffer create(float[] vertices) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length);
buffer.put(vertices);
buffer.flip();
return buffer;
}
}
So the issue i'm getting is that it should have drawn four triangles to screen to form a pyramid, however my output looks like this.
I have rotated the image to try and see the depth but it is a flat object.
I have tried to identify where the issue may be coming from by attempting to draw each triangle individually by changing my render method to gl.drawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); So that it only draws one triangle. I have tried to draw all four faces individually and all are drawn to screen.
I found the issue. I was originally scaling the triangle such that
model.scale(new Vector3f(0.2f, 0.2f, 0.0f));
As a result the z axis was being multiplied by 0. Silly mistake, hope this helps someone in the future.
even if this question was ask multiple times (i readed all of that and no solution worked for me), I am trying to model a rectangle with LWJGL and OpenGL
, but it crashes every time. Here my PC-Stats:
AMD-Ryzen 1600x |
MSI Nvidia GTX 1060 (6GB) |
MSI x370 Carbon Pro Motherboard
I also tried this on an Intel-Setup, with an i7 Processor and a Nvidia Quadro K 1000M setup, but same Error you can see in the following:
https://hastebin.com/ayiqiritov.makefile
My Drawing Method:
public void render(RawModel model){
GL30.glBindVertexArray(model.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
In this class I create VAOs and the VBOs and store data into those:
private List<Integer> vaos = new ArrayList<Integer>();
private List<Integer> vbos = new ArrayList<Integer>();
public RawModel loadToVAO(float[] positions) {
int vaoID = createVAO();
storeDataInAttributeList(0, positions);
unbindVAO();
return new RawModel(vaoID, positions.length / 3);
}
public void cleanUp() {
for (int vao : vaos) {
GL30.glDeleteVertexArrays(vao);
}
for (int vbo : vbos) {
GL15.glDeleteBuffers(vbo);
}
}
private int createVAO() {
int vaoID = GL30.glGenVertexArrays();
vaos.add(vaoID);
GL30.glBindVertexArray(vaoID);
return vaoID;
}
private void storeDataInAttributeList(int attributeNumber, float[] data) {
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
FloatBuffer buffer = storeDataInFloatBuffer(data);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void unbindVAO() {
GL30.glBindVertexArray(0);
}
private FloatBuffer storeDataInFloatBuffer(float[] data) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data).position(0);
buffer.flip();
return buffer;
}
And my main Method:
public static void main(String[] args){
if(!glfwInit()){
throw new IllegalStateException("Failed");
}
System.out.println(GL11.glGetString(GL11.GL_VERSION));
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 4);
long window = GLFW.glfwCreateWindow(640, 480, "Hello World", 0, 0);
if(window == 0){
throw new IllegalStateException("Failed to create Window");
}
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - 640) / 2, (vidmode.height() - 480) / 2);
glfwShowWindow(window);
Loader loader = new Loader();
Renderer renderer = new Renderer();
float[] vertices = {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
-0.5f, 0.5f, 0f
};
RawModel model = loader.loadToVAO(vertices);
while(!glfwWindowShouldClose(window)){
renderer.prepare();
renderer.render(model);
glfwPollEvents();
}
loader.cleanUp();
GLFW.glfwTerminate();
}
So I have already tried:
Update drivers for Graphic-card, update java, update Windows, setting up a new eclipse, reinstall java and deleting .metadata in eclipse.
Can anyone pls help me?
According to the comment
I dont have implemented a shader yet
The state of the art way of rendering in OpenGL, would be to use a Shader.
If you don't use a shader, than you have to define the array of vertex data by glVertexPointer. glVertexPointer specifies a array for Fixed-Function vertex coordinate attribute. If you don't have a shader program, then you have to use the Fixed Function Pipeline.
private void storeDataInAttributeList(int attributeNumber, float[] data) {
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
FloatBuffer buffer = storeDataInFloatBuffer(data);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL11.glVertexPointer( 3, GL11.GL_FLOAT, 0, 0 ); // <---------
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
Further you have to enable the client-side capability for vertex coordinates by glEnableClientState( GL_VERTEX_ARRAY ):
public void render(RawModel model){
GL30.glBindVertexArray(model.getVaoID());
GL11.glEnableClientState( GL11.GL_VERTEX_ARRAY ); // <---------
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());
GL11.glDisableClientState( GL11.GL_VERTEX_ARRAY ); // <---------
GL30.glBindVertexArray(0);
}
Further note, that you have to creates the "GLCapabilities" instance and makes the OpenGL bindings available for use before you use an OpenGL instruction like GL30.glGenVertexArrays() and you have to ensure the the OpenGL context is current.
Call glfwMakeContextCurrent(window) and GL.createCapabilities() after creating the window and before any OpenGL instruction:
long window = GLFW.glfwCreateWindow(640, 480, "Hello World", 0, 0);
if(window == 0){
throw new IllegalStateException("Failed to create Window");
}
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - 640) / 2, (vidmode.height() - 480) / 2);
glfwMakeContextCurrent(window); // <-----
glfwShowWindow(window);
GL.createCapabilities(); // <-----
.....
Finally you are missing glfwSwapBuffers int the render loop. glfwSwapBuffers Swaps the front and back buffers of the specified window. In other, simple words it binges the buffer where you rendered to, onto the screen:
while(!glfwWindowShouldClose(window)){
renderer.prepare();
renderer.render(model);
glfwSwapBuffers(window); // <-----
glfwPollEvents();
}
See also LWJGL 3 Guide - Getting Started
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 am beginner in openGL.
i am created a prism ( each face is equilateral triangle ) in android using openGL library and i am able to rotate the prism successfully.
but my requirment is to put three different images in each face of the prism and i am not able to put the images. when i am putting the image it is scaling and mapping to all faces.
MyRenderer Class
public class MyRenderer implements Renderer {
/** Cube instance */
/* Rotation values for all axis */
private float xrot; //X Rotation ( NEW )
private float yrot; //Y Rotation ( NEW )
private float zrot; //Z Rotation ( NEW )
/** The Activity Context ( NEW ) */
private Context context;
private Pyramid pyramid;
/**
* Instance the Cube object and set
* the Activity Context handed over
*/
public MyRenderer(Context context) {
this.context = context;
pyramid = new Pyramid(this.context);
}
/**
* The Surface is created/init()
*/
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//Load the texture for the cube once during Surface creation
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
/**
* Here we do our drawing
*/
public void onDrawFrame(GL10 gl) {
//Clear Screen And Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); //Reset The Current Modelview Matrix
//Drawing
gl.glTranslatef(0.0f, -1.0f, -5.0f); //Move 5 units into the screen
gl.glScalef(1.0f, 1.0f, 1.0f); //Scale the Cube to 80 percent, otherwise it would be too large for the screen
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(yrot, 0.0f, 1.65f, 0.0f); //X
pyramid.draw(gl, context);
yrot += 1.0f;
}
/**
* If the surface changes, reset the view
*/
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
gl.glViewport(0, 0, width, height); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
}
MyPyramid class
public class Pyramid {
/** The buffer holding the vertices */
private FloatBuffer vertexBuffer;
/** The buffer holding the color values */
private FloatBuffer colorBuffer;
private ByteBuffer indexBuffer;
private FloatBuffer textureBuffer;
private int noOfFaces = 3;
private int[] texturesID = new int[3];
private float PyramidVertices [] = {
0.0f, 1.65f, 0.0f,
-1.3f, 0.0f, 1.0f,
1.3f, 0.0f, 1.0f,
0.0f, 0.0f, -1.65f,
};
private float textures[] = {
//Mapping coordinates for the vertices
0.0f, 1.65f,
0.0f, 1.65f,
-1.3f, 0.0f,
1.3f, 0.0f,
};
private float colors[] = {
1.0f, 0.0f, 0.0f, 1.0f, //Red
0.0f, 1.0f, 0.0f, 1.0f, //Green
0.0f, 0.0f, 1.0f, 1.0f, //Blue
1.0f, 0.0f, 0.0f, 1.0f, //Red
};
private byte indices [] = { 0, 2, 1,
0, 2, 3,
0, 1, 3,
};
/**
* The Pyramid constructor.
*
* Initiate the buffers.
*/
public Pyramid( Context context) {
//
ByteBuffer byteBuf = ByteBuffer.allocateDirect(PyramidVertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(PyramidVertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
colorBuffer = byteBuf.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(textures.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(textures);
textureBuffer.position(0);
}
/**
* The object own drawing function.
* Called from the renderer to redraw this instance
* with possible changes in values.
*
* #param gl - The GL Context
*/
public void draw(GL10 gl, Context context) {
//Set the face rotation
// gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_CCW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
loadTexture(gl, context);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadTexture(GL10 gl, Context context) {
Bitmap bitmap;
gl.glGenTextures(3, texturesID, 0); // Generate texture-ID array for 6 IDs
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesID[2]);
InputStream is = context.getResources().openRawResource(R.drawable.forward);
try {
//BitmapFactory is an Android graphics utility for images
bitmap = BitmapFactory.decodeStream(is);
} finally {
//Always clear and close
try {
is.close();
is = null;
} catch (IOException e) {
}
}
// Generate OpenGL texture images
// Create Nearest Filtered Texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// Build Texture from loaded bitmap for the currently-bind texture ID
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
and i took the help from
http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html
http://nehe.gamedev.net/
How to give different images on each face?
There are few ways of doing this but here are the simplest two for you to try:
Modify object to your needs
It's quite tricky to use few textures at a time and select them per-face, but the simple solution is to divide your pyramid into few objects. Then, you can assign different texture for each of your objects as you like.
Modify texture to your needs
You can use a technique known as Texture Atlas. In this solution you can take few textures and stitch them together into one, bigger bitmap. Then you use this bitmap as your main texture. You also need to modify your vertices UVs, so one of your triangles uses some part of your bigger texture, while another triangle uses different part of your texture. Using this technique you can get an appearance that different triangles use totally different images as their texture (even that there's just one real texture during rendering).
Let us know if you need more details about it.
Thanks for response.
I found the solution for my problem. I created a equilateral triangle and then by giving the proper rotation angle i am able to rotate it as well as i put the different texture in each face.
MyPyramid class
public class PyramidNew {
int []texturesID = new int[3];
Bitmap []bitmap = new Bitmap[3];
private FloatBuffer textureBuffer;
private FloatBuffer vertexBuffer; // Buffer for vertex-array
private float[][] colors = { // Colors of the 6 faces
{1.0f, 0.5f, 0.0f, 1.0f}, // 0. orange
{1.0f, 0.0f, 1.0f, 1.0f}, // 1. violet
{0.0f, 1.0f, 0.0f, 1.0f}, // 2. green
{0.0f, 0.0f, 1.0f, 1.0f}, // 3. blue
{1.0f, 0.0f, 0.0f, 1.0f}, // 4. red
{1.0f, 1.0f, 0.0f, 1.0f} // 5. yellow
};
/* private float[] vertices = { // Vertices for the front face
-1.5f, 0.0f, 0.86f, // 0. left-bottom-front
1.5f, 0.0f, 0.86f, // 1. right-bottom-front
0.0f, 1.86f, 0.0f, // 2. left-top-front
// 3. right-top-front
};*/
private float[] vertices = { // Vertices for the front face
-1.0f, 0.0f, 0.86f, // 0. left-bottom-front
1.0f, 0.0f, 0.86f, // 1. right-bottom-front
0.0f, 1.86f, 0.0f, // 2. left-top-front
// 3. right-top-front
};
private float textures[] = {
//Mapping coordinates for the vertices
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
// Constructor - Set up the buffers
public PyramidNew( Context context) {
// Setup vertex-array buffer. Vertices in float. An float has 4 bytes
System.out.println("calling Pyramid:::::::::");
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind
ByteBuffer byteBuf = ByteBuffer.allocateDirect(textures.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(textures);
textureBuffer.position(0);
InputStream stream1 = context.getResources().openRawResource(R.drawable.splash_screen);
InputStream stream2 = context.getResources().openRawResource(R.drawable.bg);
InputStream stream3 = context.getResources().openRawResource(R.drawable.bg1);
try {
//BitmapFactory is an Android graphics utility for images
bitmap[0] = BitmapFactory.decodeStream(stream1);
bitmap[1] = BitmapFactory.decodeStream(stream2);
bitmap[2] = BitmapFactory.decodeStream(stream3);
} finally {
//Always clear and close
try {
stream1.close();
stream2.close();
stream3.close();
stream1 = stream2 = stream3 = null;
} catch (IOException e) {
}
}
}
// Draw the color cube
public void draw(GL10 gl, Context context) {
gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//loading the first texture in first face
loadTexture(gl, context, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
// rotating the face and puting the second texture in face
gl.glRotatef(120.0f, 0.0f, 1.0f, 0.0f);
//gl.glColor4f(colors[1][0], colors[1][1], colors[1][2], colors[1][3]);
loadTexture(gl, context, 1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
// Back - Rotate another 120 degree about y-axis and then put different texture
gl.glRotatef(120.0f, 0.0f, 1.0f, 0.0f);
//gl.glColor4f(colors[2][0], colors[2][1], colors[2][2], colors[2][3]);
loadTexture(gl, context, 2);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
public void loadTexture(GL10 gl, Context context, int currentImage) {
// Bitmap []bitmap = new Bitmap[3];
gl.glGenTextures(3, texturesID, 0); // Generate texture-ID array for 6 IDs
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesID[currentImage]);
// Generate OpenGL texture images
// Create Nearest Filtered Texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// Build Texture from loaded bitmap for the currently-bind texture ID
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[currentImage], 0);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
}
}
I have come across some problems I'm not able to explain very well without you guys trying it out.
I'm unable to get a cube to load correctly. I was able to make it rotate nicely on all axes, though. (plural of "axis" is "axes"?)
I haven't ventured on with lighting and textures, so I'm sorry if you can't seem to make out the model yet.
This is what it looks like right now (snapshot of a freely-spinning model):
This is the expected outcome:
This is the code for my GLSurfaceView.Renderer:
package dd.ww;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.Matrix;
public class Render implements Renderer {
private Context context;
private Cube cube;
private float[] modelViewProjectionMatrix = new float[16];
private float[] projectionMatrix = new float[16];
private float[] viewMatrix = new float[16];
private float[] rotationMatrix = new float[16];
private float angle = 0f;
public Render(Context context) {
this.context = context;
}
#Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
GLES20.glClearColor(1f, 1f, 1f, 1f);
cube = new Cube(context);
}
#Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / (float) height;
Matrix.frustumM(projectionMatrix, 0, -3f, 3f, -3f, 3f, 1f, 10f);
}
#Override
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
//Camera position
Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, -4f, 0f, 0f, 0f, 0f, 1f, 0f);
// projection x view = modelView
Matrix.multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
//Creating rotation matrix
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1f);
//rotation x camera = modelView
Matrix.multiplyMM(modelViewProjectionMatrix, 0, rotationMatrix, 0, modelViewProjectionMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, -1f, 0f);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, rotationMatrix, 0, modelViewProjectionMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, angle, -1f, 0f, 0f);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, rotationMatrix, 0, modelViewProjectionMatrix, 0);
cube.draw(modelViewProjectionMatrix);
angle += 0.7f;
if (angle > 360f)
angle = 0f;
}
}
This is the code for the Cube class, along with its OBJ loader. The OBJ Loader is used for loading the OBJ model that was exported from Blender (which is the expected outcome of the Cube, shown in Blender.):
package dd.ww;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.AssetManager;
import android.opengl.GLES20;
import android.util.Log;
public class Cube {
private Context context;
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private int shaderProgram;
//TODO: Go to Google Code, find OpenGL ES 2.0 Programming Guide source code, Android,
//check in the ESShapes.java, and study the FloatBuffers...
public Cube(Context c) {
context = c;
loadCube("cube/cube.obj");
}
private void loadCube(String filename) {
ArrayList<Float> tempVertices = new ArrayList<Float>();
//ArrayList<Float> tempNormals = new ArrayList<Float>();
ArrayList<Short> vertexIndices = new ArrayList<Short>();
//ArrayList<Short> normalIndices = new ArrayList<Short>();
try {
AssetManager manager = context.getAssets();
BufferedReader reader = new BufferedReader(new InputStreamReader(manager.open(filename)));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("v")) {
tempVertices.add(Float.valueOf(line.split(" ")[1])); //vx
tempVertices.add(Float.valueOf(line.split(" ")[2])); //vy
tempVertices.add(Float.valueOf(line.split(" ")[3])); //vz
}
// else if (line.startsWith("vn")) {
// tempNormals.add(Float.valueOf(line.split(" ")[1])); //nx
// tempNormals.add(Float.valueOf(line.split(" ")[2])); //ny
// tempNormals.add(Float.valueOf(line.split(" ")[3])); //nz
// }
else if (line.startsWith("f")) {
/*
vertexIndices.add(Short.valueOf(tokens[1].split("/")[0])); //first point of a face
vertexIndices.add(Short.valueOf(tokens[2].split("/")[0])); //second point
vertexIndices.add(Short.valueOf(tokens[3].split("/")[0])); //third point
normalIndices.add(Short.valueOf(tokens[1].split("/")[2])); //first normal
normalIndices.add(Short.valueOf(tokens[2].split("/")[2])); //second normal
normalIndices.add(Short.valueOf(tokens[3].split("/")[2])); //third
*/
// for (int i = 1; i <= 3; i++) {
// //String[] s = tokens[i].split("/");
// vertexIndices.add(Short.valueOf());
// //normalIndices.add(Short.valueOf(s[2]));
// }
vertexIndices.add(Short.valueOf(line.split(" ")[1]));
vertexIndices.add(Short.valueOf(line.split(" ")[2]));
vertexIndices.add(Short.valueOf(line.split(" ")[3]));
}
}
float[] vertices = new float[tempVertices.size()];
for (int i = 0; i < tempVertices.size(); i++) {
Float f = tempVertices.get(i);
vertices[i] = (f != null ? f : Float.NaN);
}
short[] indices = new short[vertexIndices.size()];
for (int i = 0; i < vertexIndices.size(); i++) {
Short s = vertexIndices.get(i);
indices[i] = (s != null ? s : 1);
}
vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(vertices).position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
indexBuffer.put(indices).position(0);
int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader, vertexCode);
GLES20.glCompileShader(vertexShader);
int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader, fragmentCode);
GLES20.glCompileShader(fragmentShader);
shaderProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(shaderProgram, vertexShader);
GLES20.glAttachShader(shaderProgram, fragmentShader);
GLES20.glLinkProgram(shaderProgram);
int[] linked = new int[1];
GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, linked, 0);
if (linked[0] == 0){
Log.d("DEBUG", "Shader code error.");
Log.d("DEBUG", GLES20.glGetProgramInfoLog(shaderProgram));
GLES20.glDeleteProgram(shaderProgram);
return;
}
GLES20.glDeleteShader(vertexShader);
GLES20.glDeleteShader(fragmentShader);
}
catch (Exception e) {
Log.d("DEBUG", "Error.", e);
}
}
private String vertexCode = "" +
"attribute vec4 a_position; \n" +
"uniform mat4 mvpMatrix; \n" +
"void main() { \n" +
" gl_Position = a_position * mvpMatrix;\n" +
"} \n";
private String fragmentCode = "" +
"precision mediump float; \n" +
"void main() { \n" +
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" +
"} \n";
private int attribute_Position;
private int uniform_mvpMatrix;
public void draw(float[] mvpMatrix){
GLES20.glUseProgram(shaderProgram);
attribute_Position = GLES20.glGetAttribLocation(shaderProgram, "a_position");
GLES20.glVertexAttribPointer(attribute_Position, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
GLES20.glEnableVertexAttribArray(attribute_Position);
uniform_mvpMatrix = GLES20.glGetUniformLocation(shaderProgram, "mvpMatrix");
GLES20.glUniformMatrix4fv(uniform_mvpMatrix, 1, false, mvpMatrix, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, indexBuffer);
GLES20.glDisableVertexAttribArray(attribute_Position);
}
}
And finally, here's the APK attachment (Uploaded to Mediafire, should not get removed. It's non-licensed freeware). The attached APK file is signed, exported straight from my project, and can only be run on Gingerbread or above. (This is what OpenGL ES 2.0 is for...):
Mediafire download link to the APK file.
If anyone is willing to help me realize what I'm doing wrong, I'll be glad for the rest of my life. This question here is the closest that I find when searching on SO that has a 40% chance my problem is related to. Unfortunately, he still has his model distorted. All the rest of the questions I found seems to be about textures not rendering correctly, translating the model around, etc. But I will try my best to find questions with similar problems as mine.
Holy Cow...
I finally got it to work.
The problem is how OpenGL ES 2.0 matrices work.
Quote from SO user, Tim:
I believe it should be mvpMatrix * mRotationMatrix, but you're not supposed to use the same matrix as the input and output to that function, you need to use a temporary matrix. Android.opengl.Matrix " The same float array may be passed for result, lhs, and/or rhs. However, the result element values are undefined if the result elements overlap either the lhs or rhs elements." If that doesn't help then post your entire code.
The bolded text means that if you do this:
//Creating rotation matrix
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1f);
//rotation x camera = modelView
Matrix.multiplyMM(modelViewProjectionMatrix, 0, modelViewProjectionMatrix, 0, rotationMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, -1f, 0f);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, modelViewProjectionMatrix, 0, rotationMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, angle, -1f, 0f, 0f);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, modelViewProjectionMatrix, 0, rotationMatrix, 0);
cube.draw(modelViewProjectionMatrix);
Which seems like normal, the cube will look skewed. Reason is, lhs and rhs shouldn't be the same as your resulting matrix.
But, if you do this:
//Creating rotation matrix
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1f);
//rotation x camera = modelView
float[] duplicateMatrix = Arrays.copyOf(modelViewProjectionMatrix, 16);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, duplicateMatrix, 0, rotationMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, -1f, 0f);
duplicateMatrix = Arrays.copyOf(modelViewProjectionMatrix, 16);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, duplicateMatrix, 0, rotationMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, angle, -1f, 0f, 0f);
duplicateMatrix = Arrays.copyOf(modelViewProjectionMatrix, 16);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, duplicateMatrix, 0, rotationMatrix, 0);
cube.draw(modelViewProjectionMatrix);
It will display correctly.
Where I found the helpful hint from.
And this is where I realized why 3 people voted to close this question off. They must've known the answer to this question in the first place, and wanted me to find the solution by myself. Hats off to them...
I swear to God, this is a hard problem I have finally conquered. There goes 2 years of wasted life down the drain...