PDA

View Full Version : Code Help - A lot of help!



05-08-2003, 08:58 PM
As you can probably tell i'm new to java and even newer to opengl, i have to make some basic shapes but was having troble compiling these, i wanted to have end up with a screen divided in two (using rectangles) and a 3d cube in the middle but am having problems and due to time i dont really have time to start from the VERY basics, my complete code is below. Thanks for any help


// Java classes
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

// GL4Java classes
import gl4java.GLContext;
import gl4java.awt.GLAnimCanvas;
import gl4java.utils.textures.*;

public class Test extends Applet
{


public void reshape(int width, int height)
{
if(height==0)height=1;
gl.glViewport(0, 0, width, height); // Reset The Current Viewport And Perspective Transformation
gl.glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
gl.glLoadIdentity(); // Reset The Projection Matrix
glu.gluPerspective(45.0f, width / height, 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
gl.glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity(); // Reset The ModalView Matrix

}


public void init()
{
float width = (float)getSize().width;
float height = (float)getSize().height;

gl.glShadeModel(GL_SMOOTH); //Enables Smooth Color Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //This Will Clear The Background Color To Black
gl.glClearDepth(1.0); //Enables Clearing Of The Depth Buffer
gl.glEnable(GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL_LEQUAL); //The Type Of Depth Test To Do
gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
}


public void DrawGLScene()
{
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear The Screen And The Depth Buffer
gl.glLoadIdentity(); //Reset The View

}


public void DrawGLScene()
{
//House
gl.glLoadIdentity(); // Reset The Current Modelview Matrix
gl.glTranslatef(1.5f,0.0f,-9.0f); // Move Right 1.5 Units And Into The Screen 7.0
gl.glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Quad On The X axis ( NEW )
gl.glBegin(GL_QUADS); // Draw A Quad
gl.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Blue
gl.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
gl.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
gl.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
gl.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
gl.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
gl.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
gl.glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
gl.glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
gl.glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
gl.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
gl.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
gl.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
gl.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
gl.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
gl.glEnd(); // Done Drawing The Quad


//Grass
gl.glBegin(GL_QUADS); // Draw A Quad
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
gl.glEnd(); // Done Drawing The Quad

//Sky
gl.glBegin(GL_QUADS); // Draw A Quad
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
gl.glEnd(); // Done Drawing The Quad
}


public void display()
{
for(int i=0;i<3;i++){
glj.gljMakeCurrent(); //Ensure GL is initialised correctly
DrawGLScene();
glj.gljSwap(); //Swap buffers
glj.gljFree(); // release GL
}
}

//Cube
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_Polygon);
glVertex2f(0.0,0.0);
glVertex2f(0.0,3.0);
glVertex2f(4.0,3.0);
glVertex2f(6.0,1.5);
glVertex2f(4.0,0.0);

glEnd ();


// Main method (This is what gets run when you run as an application)
public static void main( String args[] )
{
House applet = new House();
applet.setSize(640,480);
applet.init();
applet.start();


Frame f = new Frame("Colins House - OpenGL and Java");


f.add(applet);
f.pack();// make the frame the size of the Applet
f.setVisible(true);

}
}

}