LWJGL Forum

Programming => OpenGL => Topic started by: ChrisMarron on December 04, 2011, 12:37:29

Title: Object not showing on screen, please help. SOLVED, FORGOT TO SPECIFY GLORTHO
Post by: ChrisMarron on December 04, 2011, 12:37:29
Ok so I was reading through the red book and I got to the part about drawing an Icosahedron http://fly.cc.fer.hr/~unreal/theredbook/chapter02.html (almost at the bottom just above the picture of them) and decided I'd have a go at drawing one myself. So I took the fullscreen example from the tutorial and tried adding an icosahedron to it. To make it more efficient I calculated the coordinates before I used them and then I expanded my icosahedron to 50 times its usual size as it'd be mini otherwise and moved it's centre to x =300 and y = 300. The shape is made up of 20 triangles but none of them seem to be showing and I don't get why. Code is below... The quad shows perfectly. The icosahedron not at all.

package Tutorials;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.*;

public class Fullscreen {

float rotation = 0; //angle of quad rotation.
long lastFrame= 0; //time at last frame.
int fps = 0; // frames per second.
long lastFPS = 0; // last FPS time.
int width1 = 1920, height1 = 1200;
float x = width1/2, y = height1/2; //position of quad.
boolean vsync = false; //is VSync enabled?


double X = 0.525731112119133606;
double Z = 0.850650808352039932;
double [] [] vdata =
{   {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},    
  {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},    
  {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
int [][] tindices =
{
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},    
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},    
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
};
double coords [] [] = new double [60] [3];

public void start() {

try {
Display.setDisplayMode(new DisplayMode(width1, height1));
Display.create();
}
catch (LWJGLException e){
e.printStackTrace();
System.exit(0);
}//end try catch loop.

initGL(); //init OpenGl.
getDelta(); // call once before loop to initialise lastFrame.
lastFPS = getTime(); //call before loop to initialise fps timer.
calcIcosahedron();

while (!Display.isCloseRequested()) {
int delta = getDelta();

update(delta);
renderGL();
Display.update();
Display.sync(60); // cap fps to 60 fps.
}
Display.destroy(); //kill the display.
}

private void calcIcosahedron() {


for (int i = 0; i<20; i++){
int j = i*3;
coords [j][0] = (vdata[tindices[i][0]][0]*50)+300;
coords [j][1] = (vdata[tindices[i][0]][1]*50)+300;
coords [j][2] = vdata[tindices[i][0]][2]*50;

coords [j+1][0] = (vdata[tindices[i][1]][0]*50)+300;
coords [j+1][1] = (vdata[tindices[i][1]][1]*50)+300;
coords [j+1][2] = vdata[tindices[i][1]][2]*50;

coords [j+2][0] = (vdata[tindices[i][2]][0]*50)+300;
coords [j+2][1] = (vdata[tindices[i][2]][1]*50)+300;
coords [j+2][2] = vdata[tindices[i][2]][2]*50;

System.out.println(coords[j][0] +"," + coords[j][1]+ ","+coords[j][2]);
System.out.println(coords[j+1][0] +"," + coords[j+1][1]+ ","+coords[j+1][2]);
System.out.println(coords[j+2][0] +"," + coords[j+2][1]+ ","+coords[j+2][2]);
}
}

public void update(int delta){
//rotate quad.
rotation += 0.15f * delta;

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f*delta;
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;

if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))y += 0.35f * delta;

while(Keyboard.next()){
if (Keyboard.getEventKeyState()){
if(Keyboard.getEventKey() == Keyboard.KEY_F){
setDisplayMode(width1, height1, !Display.isFullscreen());
}
else if (Keyboard.getEventKey() == Keyboard.KEY_V){
vsync = !vsync;
Display.setVSyncEnabled(vsync);
}
}
}

//Keep Quad on the screen.
if (x<0) x = 0;
if (x>width1) x = width1;
if (y<0) y = 0;
if (y>height1) y = height1;

updateFPS();
}//end update method.

public void setDisplayMode(int width, int height, boolean fullscreen) {
//return if requested DisplayMode is already set.
if ((Display.getDisplayMode().getWidth() == width) && (Display.getDisplayMode().getHeight() == height) && (Display.isFullscreen()== fullscreen)){
return;
}

try {
DisplayMode targetDisplayMode = null;
if (fullscreen){
DisplayMode[] modes = Display.getAvailableDisplayModes();
int freq = 0;

for (int i=0; i<modes.length;i++){
DisplayMode current = modes[i];
if ((current.getWidth() == width) && (current.getHeight()==height)){
if ((targetDisplayMode == null) || (current.getFrequency() >= freq)){
if ((targetDisplayMode == null) || (current.getBitsPerPixel()> targetDisplayMode.getBitsPerPixel())){
targetDisplayMode = current;
freq = targetDisplayMode.getFrequency();
}
}

//if we've found a match for bpp and frequency against the original display mode then it's probably best to go for this one as it's most likely compatible
//with the monitor.
if((current.getBitsPerPixel()== Display.getDesktopDisplayMode().getBitsPerPixel()) && (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())){
targetDisplayMode = current;
break;
}
}
}
}
else {
targetDisplayMode = new DisplayMode (width, height);
}
if (targetDisplayMode == null) {
System.out.println ("Failed to find value mode: " + width + "x" + height + " fs =" +fullscreen);
return;
}

Display.setDisplayMode(targetDisplayMode);
Display.setFullscreen(fullscreen);

} catch (LWJGLException e){
System.out.println("Unable to setup mode " +width+"x"+height+" fullscreen="+fullscreen+ e);
}
}//end setDisplayMode method.


//Calculate how many milliseconds have passed since last frame.
//Return milliseconds passed since last frame.
public int getDelta(){
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}//end getDelta method.

public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution(); //get the system time and display it in milliseconds.
}// end getTime method.

public void updateFPS(){
if (getTime() - lastFPS> 1000){
Display.setTitle("FPS: " + fps);
fps = 0;
lastFPS +=1000;
}
fps++;
}//end updateFPS method.

public void initGL(){
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width1, height1, 0, 1, -1);
glMatrixMode(GL11.GL_MODELVIEW);
}//end initGL

public void renderGL(){
glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); //clear screen and depth buffer.
glColor3f(0.0f,0.0f,1.0f); // set the color to blue one time only.

//Draw Quad.
glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(rotation, 0f, 0f, 1f);
glTranslatef(-x,-y, 0);
glBegin(GL_QUADS);
glVertex3f(x-50, y-50,0);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(x+50, y-50,0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(x+50, y+50,0);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(x-50, y+50, 0);
glEnd();
glPopMatrix();

//draw the icosahedron.
for (int i = 0; i<20; i++){
/*color time*/
int j=i*3;
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3d(coords[j][0], coords[j][1], coords[j][2]);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3d(coords[j+1][0], coords[j+1][1], coords[j+1][2]);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3d(coords[j+2][0], coords[j+2][1], coords[j+2][2]);
glEnd();
System.out.println(coords[j][0] +"," + coords[j][1]+ ","+coords[j][2]);
System.out.println(coords[j+1][0] +"," + coords[j+1][1]+ ","+coords[j+1][2]);
System.out.println(coords[j+2][0] +"," + coords[j+2][1]+ ","+coords[j+2][2]);
}

}//end renderGL method.

public static void main(String[] argv) {
Fullscreen fullscreenExample = new Fullscreen();
fullscreenExample.start();
}//end main

}//end Fullscreen class.