PDA

View Full Version : texturing question



theophage
01-24-2006, 08:05 AM
I'm very new to programming, but I learn from example best. I recently downloaded Euphoria and GLFW and using examples from that I've been experimenting and reading their tuts/user guides to learn, but they dont cover all topics completely. right now I've been working on a cube that rotates with mouse movements, my first challenge was learning to load 2 textures one onto a separate face of, what for now is only 2 sides of a cube. my question is why would one texture overlap the other when it is obviously in front of the overlapping one? so as i rotate it, side number 2 is always on top of side number 1, even though side 2 is further away from the camera.

throw some hints out, or lemme know if you need more info about how I'm coding this or anything else.

thx

Relic
01-24-2006, 08:43 AM
Most likely depth testing is off.
In OpenGL speak (I don't use those tools you mentioned):
Add glEnable(GL_DEPTH_TEST) to your OpenGL initialization routine and make sure you issue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) each time before you render your scene.
Search the web for the OpenGL Programming Guide redbook.pdf and read that.

theophage
01-24-2006, 09:24 AM
hrm, the glEnable(GL_DEPTH_TEST) was missing, but had no effect. I have seen the redbook before, and i read up on it a little more, but i'm unable to find anything in there that resembles my situation. I'll keep reading it, but I have to go to work for now.

Relic
01-24-2006, 09:55 AM
Could still be depth buffer problems.
Check the number of depth bits and the ratio of the zFar to zNear ratio in the projection matrix initialization.
If your zFar/zNear ratio is too big, the depth precision is affected.
Make that ratio as small as possible by shifting zNear out and pull zFar in so that you just don't clip your model's coordinates.

jide
01-25-2006, 01:48 AM
Did you allowed the window to do depth testing ? GLFW must provide something like that in the window initialization routine.

theophage
01-25-2006, 09:36 PM
if the problem is depth buffering, then I need to learn more. the camera is -10(z) units away from 0,0,0, zmin is at 0, zfar is at 20, the parts of the cube are placed at 0,0,0 I tried doing a negative zmin but it just prevented anything from being displayed. So now i guess i need to learn how to place the parts of the cube at -5(z) and rotate it there, but when i do that it rotates it around the 0,0,0 axis so, i cant get a proper angled view before the cube leaves the screen. so looks like i still have lots to learn. still any tips would help, thx for the info so far though.

01-25-2006, 09:59 PM
If I may make a friendly suggestion, I would recommend using GLUT while learning OpenGL. It's a very thin API that handles basic window creation and input, and it's darn near impossible to botch it up. Whereas with more complex APIs you may be wasting time battling the the details of the API rather than learning OpenGL.

On top of its simplicity, there are literally thousands of ready-to-build demos written using GLUT (including a textured cube ;) ). I'd start off with the stuff that works.

Cheers

jide
01-26-2006, 09:24 AM
theophage post your entire code please.

theophage
01-29-2006, 06:58 PM
sorry it took so long to get back

here it is


include msgbox.e
include C:\EUPHORIA\GLFW\Euphoria\gl\gl.e
include C:\EUPHORIA\GLFW\Euphoria\gl\glu.e
include C:\EUPHORIA\GLFW\Euphoria\gl\glfw.e

atom t1, t0, fps, texidone, texidtwo --, texidthree, texidfour, texidfive, texidsix
integer running, frames, width, height, x, y
sequence titlestr, pos, lptex
frames = 0
t0 = glfwGetTime()
lptex = ({0, 0})

---------- setup window

procedure setupwindow()
running = glfwInit()
if not(glfwOpenWindow(1280, 1024, 0,0,0,0, 0,0, GLFW_FULLSCREEN)) then
glfwTerminate()
running = message_box("unable to open an opengl window.", "Error", MB_ICONERROR)
abort(1)
end if
glShadeModel(GL_FLAT)
glEnable(GL_DEPTH_TEST)
glfwSwapInterval(0)
glfwEnable(GLFW_STICKY_KEYS)
glfwSwapInterval(1)
end procedure

---------- setup textures

procedure setuptexture()
lptex[1] = allocate(4)
lptex[2] = allocate(4)
glGenTextures(1, lptex[1])
texidone = peek4u(lptex[1])
free(lptex[1])
glBindTexture(GL_TEXTURE_2D, texidone)
if not(glfwLoadTexture2D("imgs\\one.tga", GLFW_ORIGIN_UL_BIT)) then
glfwTerminate()
running = message_box("Unable to Open imgs\\one.tga.", "Error", MB_ICONERROR)
abort(1)
end if
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glGenTextures(1, lptex[2])
texidtwo = peek4u(lptex[2])
free(lptex[2])
glBindTexture(GL_TEXTURE_2D, texidtwo)
if not(glfwLoadTexture2D("imgs\\two.tga", GLFW_ORIGIN_UL_BIT)) then
glfwTerminate()
running = message_box("Unable to Open imgs\\two.tga.", "Error", MB_ICONERROR)
abort(1)
end if
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
end procedure

---------- call setups

setupwindow()
setuptexture()

---------- main loop

while running do

----- determine fps

t1 = glfwGetTime()
if (t1-t0) > 1.0 then
fps = frames / (t1 - t0)
frames = 0
t0 = t1
titlestr = sprintf("FPS: %.1f", {fps})
glfwSetWindowTitle(titlestr)
end if

----- get mouse position

pos = glfwGetMousePos()
x = pos[1]
y = pos[2]

----- determine actual screen size

pos = glfwGetWindowSize()
width = pos[1]
height = pos[2]
if height <= 0 then
height = 1
end if
glViewport(0, 0, width, height)

----- clear buffer

glClearColor(0, 0, 0, 0)
glClear(GL_COLOR_BUFFER_BIT)
glClear(GL_DEPTH_BUFFER_BIT)

----- setup viewing angles

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(65, width / height, 0.0, 10.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt({0.0, 0.0, -10.0}, -- Eye-position
{0.0, 0.0, 0.0}, -- View-point
{0.0, 1.0, 0.0}) -- Up-vector

----- Draw a textured quad
glPushMatrix()
glTranslatef(({0, 0, 5}))
glRotatef(0.05 * x, {0.0, 1.0, 0.0})
glRotatef(0.05 * y, {1.0, 0.0, 0.0})

glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texidtwo)
glBegin(GL_QUADS)

glTexCoord2f({1, 0})
glVertex3f({1, 1, 0})

glTexCoord2f({0, 0})
glVertex3f({1, 1, 2})

glTexCoord2f({0, 1})
glVertex3f({1, -1, 2})

glTexCoord2f({1, 1})
glVertex3f({1, -1, 0})

glEnd()

glBindTexture(GL_TEXTURE_2D, texidone)
glBegin(GL_QUADS)

glTexCoord2f({1, 0})
glVertex3f({-1, 1, 0})

glTexCoord2f({0, 0})
glVertex3f({1, 1, 0})

glTexCoord2f({0, 1})
glVertex3f({1, -1, 0})

glTexCoord2f({1, 1})
glVertex3f({-1, -1, 0})
glEnd()
glFlush()
glDisable(GL_TEXTURE_2D)

----- swap buffer and add frame

glfwSwapBuffers()
frames += 1

----- check for key presses and continue

running = not(glfwGetKey(GLFW_KEY_ESC)) and glfwGetWindowParam(GLFW_OPENED)
end while

glfwTerminate()

Bob
01-30-2006, 12:22 AM
Originally posted by theophage:


if not(glfwOpenWindow(1280, 1024, 0,0,0,0, --> 0 <-- , 0, GLFW_FULLSCREEN))Zero depth bits means you don't want a depth buffer. Put 24 or something there so the window manager knows you want a depth buffer. Without depth buffer, depth testing doesn't work.

And by the way, what language is that?

theophage
01-30-2006, 10:21 PM
thx for pointing that out

euphoria, its a free compiler i came across, allows you to make stand alone .exe files you can freely distribute

and it has everything i ever wanted, free free free ;) just so long i can learn to use gl... just wish qbasic was still the happenin thing :p

theophage
01-30-2006, 10:26 PM
yay, it works now

i also had to change glPerspective to:

gluPerspective(65, width / height, 1, 20.0)or else it still did the same thing just with more flickering, but now its solid.