PDA

View Full Version : Understanding generic rectangle code

Inbar_xz
07-05-2018, 04:19 AM
i have a code of Maze Game in JOGL, and it's use this code to create the walls in the init method:

//Create a draw list of a generic wall/ceiling/floor rectangle
//that consists of 100x100 smaller vertexes for smooth lignting
rectList = gl.glGenLists(1);
gl.glNewList(rectList, GL2.GL_COMPILE);
gl.glPushMatrix();
gl.glRotatef(90, 1.0f, 0.0f, 0.0f);
gl.glTranslatef(0.0f, -0.475f, -0.475f);
//gl.glNormal3f(0,0,-1);

//create generic wall vertices on the y-z axis
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
gl.glBegin(GL2.GL_POLYGON);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, (i / 20.0f) + (0.5f * 0.05f), (j / 20.0f) + (0.5f * 0.05f));
gl.glVertex3f(-0.5f, (i / 20.0f) + (0.5f * 0.05f), (j / 20.0f) + (0.5f * 0.05f));
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, (i / 20.0f) - (0.5f * 0.05f), (j / 20.0f) + (0.5f * 0.05f));
gl.glVertex3f(-0.5f, (i / 20.0f) - (0.5f * 0.05f), (j / 20.0f) + (0.5f * 0.05f));
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, (i / 20.0f) - (0.5f * 0.05f), (j / 20.0f) - (0.5f * 0.05f));
gl.glVertex3f(-0.5f, (i / 20.0f) - (0.5f * 0.05f), (j / 20.0f) - (0.5f * 0.05f));
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, (i / 20.0f) + (0.5f * 0.05f), (j / 20.0f) - (0.5f * 0.05f));
gl.glVertex3f(-0.5f, (i / 20.0f) + (0.5f * 0.05f), (j / 20.0f) - (0.5f * 0.05f));
gl.glEnd();
}
}

and then in the cell:

//Enable and bind the textures to be used for mapping onto the faces
gl.glActiveTexture(GL2.GL_TEXTURE0);
textures[0].enable(gl);
textures[0].bind(gl);

gl.glColor3f(1.0f, 1.0f, 1.0f);

//If a wall of the current maze cell exists, draw it. Repeat for all 4 walls.
//right wall
if (walls[0]) {
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);

}
//left wall
if (walls[1]) {
gl.glPushMatrix();
gl.glRotatef(180, 0.0f, 1.0f, 0.0f);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);
gl.glPopMatrix();
}

i just didnt understand.. why it use a loop from 0 to 20? every wall have only 4 vertices, so why it's create so many?
thanks!

OceanJeff40
07-06-2018, 02:46 PM
Each wall will have 4 points, so the 4 calls to glMultiTexCoord2f and glVertex3f functions are handling one wall, and it's in a loop, with a nested loop, so it looks like it's a 20x20 maze.

Oh, and it looks like it's just 2D.... at first glance anyways.

Jeff

Inbar_xz
07-10-2018, 03:42 AM
Each wall will have 4 points, so the 4 calls to glMultiTexCoord2f and glVertex3f functions are handling one wall, and it's in a loop, with a nested loop, so it looks like it's a 20x20 maze.

Oh, and it looks like it's just 2D.... at first glance anyways.

Jeff

i still cant understand.. i read that what the code doing is to create Display Lists for each wall. so this loop create stuff for one wall, no?
cause every wall use this Display Lists after rotation. only the floor have diffrent code, and i can see that it have 4 points:

public void draw(Texture[] textures, GL2 gl) {

gl.glPushMatrix();

//Move the current cell of the maze according to its relative position
gl.glTranslatef(i, 0.0f, j);

//Set up the lighting point position
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, new float[]{-0.3f, 0.4f, 0.0f, 1.0f}, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, new float[]{0.3f, 0.4f, 0.0f, 1.0f}, 0);
//gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPOT_DIRECTION, new float [] { 0.0f, -0.5f, 1.0f, 0.0f}, 0);

//Enable and bind the textures to be used for mapping onto the faces
gl.glActiveTexture(GL2.GL_TEXTURE0);
textures[0].enable(gl);
textures[0].bind(gl);

gl.glColor3f(1.0f, 1.0f, 1.0f);

//If a wall of the current maze cell exists, draw it. Repeat for all 4 walls.
//right wall
if (walls[0]) {
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);

}
//left wall
if (walls[1]) {
gl.glPushMatrix();
gl.glRotatef(180, 0.0f, 1.0f, 0.0f);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);
gl.glPopMatrix();
}
//back wall
if (walls[2]) {
gl.glPushMatrix();
gl.glRotatef(270, 0.0f, 1.0f, 0.0f);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);
gl.glPopMatrix();
}
//front wall
if (walls[3]) {
gl.glPushMatrix();
gl.glRotatef(90, 0.0f, 1.0f, 0.0f);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);
gl.glPopMatrix();
}

//Change the textures to be used for next mapping
textures[0].disable(gl);
textures[1].enable(gl);
textures[1].bind(gl);

//The floor
gl.glBegin(GL2.GL_POLYGON);
//setNormal(floor[0], floor[1], floor[2], gl);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0, 0);
gl.glVertex3fv(floor[0], 0);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0, 1);
gl.glVertex3fv(floor[1], 0);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1, 1);
gl.glVertex3fv(floor[2], 0);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1, 0);
gl.glVertex3fv(floor[3], 0);
gl.glEnd();

//Change the textures to be used for next mapping
textures[1].disable(gl);
textures[2].enable(gl);
textures[2].bind(gl);

//The ceiling
gl.glPushMatrix();
gl.glRotatef(90, 0.0f, 0.0f, -1.0f);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glCallList(rectList);
gl.glPopMatrix();

textures[2].disable(gl);

//If the maze's end-point object exists, invoke its draw method
if (obj != null) obj.draw(gl);

gl.glPopMatrix();

}

i'm trying to understand this cause i want to do collision detection..
i want to know the limit of each wall in order to tell if the player get into a wall.

GClements
07-10-2018, 06:20 AM
i just didnt understand.. why it use a loop from 0 to 20? every wall have only 4 vertices, so why it's create so many?
The comment right at the beginning of the code says:

//Create a draw list of a generic wall/ceiling/floor rectangle
//that consists of 100x100 smaller vertexes for smooth lignting

The fixed-function pipeline performs lighting calculations for each vertex, then interpolates the vertex colours for each fragment. This doesn't work well if the primitive is large compared to the distance to the light or (in the case of specular reflection) the distance to the viewpoint. So the program gets around this by subdividing each wall section into a grid of smaller quads (the comment says 100x100, but the code uses 20x20).

With modern OpenGL code, lighting calculations are typically performed per-fragment in the fragment shader, which would eliminate the need for this.