flat assembler
Message board for the users of flat assembler.

Index > Windows > *SOLVED* OpenGL Textures - Porting problem.

Author
Thread Post new topic Reply to topic
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004 16 Jan 2008, 17:06
EDIT: SOLVED PROBLEM, SEE END REPLY, EXECUTABLE UPDATED TOO Smile.

recently i decided to start learning how to use opengl. Since i have a module on it it next semester i thought i may as well check it out. It's either that or make a flash game in a newbie WYSIWYG editor *gah*. Well, i've been using NeHe tutorials and i'm currently up to http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06.

At the moment i have a simple 3D map (4 walls and a floor) which you can walk around using the arrows keys. But im stuck getting my texture loader to work. The examples from the website are written in all sorts of languages (at least 20...) and one of them is MASM. Which lucky for me is kind of similar to FASM, but alas im still having problems. What makes things really annoying is that the assembled MASM executable can't be read in a debugger so i can't see exactly whats going on (contains too much embedded data, probably a compressed program).

There is one key difference between mine and the MASM code, and that is that i want to load the image from a file as opposed to from a resource within the program. My LoadImage() returns successful so im assuming this is not the problem.


ORIGINAL MASM CODE:
Code:
invoke glGenTextures, 1, ADDR texture
    invoke LoadImage, hInstance, IDD_BITMAP, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION
    cmp eax, 0
    je end_load
    mov hBMP, eax
    invoke GetObject, hBMP, sizeof BITMAP, ADDR ImgInfo
    invoke glBindTexture, GL_TEXTURE_2D, texture
    invoke glTexImage2D, GL_TEXTURE_2D, 0, 3, ImgInfo.bmWidth, ImgInfo.bmHeight,\
                         0, GL_BGR_EXT, GL_UNSIGNED_BYTE, ImgInfo.bmBits
    invoke glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
    invoke glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
    mov eax, 1
   end_load:
    ret                      
    


My Code:

Code:
invoke glGenTextures,1,texture
invoke LoadImage,NULL,filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE+LR_CREATEDIBSECTION
mov    [hBMP],eax
invoke GetObject,eax,sizeof.BITMAP,[ImgInfo]
invoke glBindTexture, GL_TEXTURE_2D,[texture]
invoke glTexImage2D, GL_TEXTURE_2D,0,3,[ImgInfo.bmWidth],[ImgInfo.bmHeight],0,GL_BGR_EXT,GL_UNSIGNED_BYTE,[ImgInfo.bmBits]
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
invoke glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR   
    


my data declared like so:

Code:
filename                 db                    'walltexture.bmp',0
hBMP                     dd                    NULL
ImgInfo                  BITMAP                ?
texture                  GLuint                NULL ;im pretty sure this is just a dd too...
    


I've checked all the windows functions and they're returning successfully. I can't check the opengl functions since none of the sites explaining functionality seems to tell me anything about them :/.


Can anyone see any faults in my program? If you can't, have you any opengl examples which load textures?

I've included my source with executable, if you want to take a look.


Description:
Download
Filename: Opengl.zip
Filesize: 42.57 KB
Downloaded: 184 Time(s)



Last edited by calpol2004 on 17 Jan 2008, 18:41; edited 2 times in total
Post 16 Jan 2008, 17:06
View user's profile Send private message MSN Messenger Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 16 Jan 2008, 21:08
Easy fix for you (there may be more errors but this one popped out at me).

Remove the square brackets [] on ImgInfo in your GetObject call.
Post 16 Jan 2008, 21:08
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004 16 Jan 2008, 22:12
r22 wrote:
Easy fix for you (there may be more errors but this one popped out at me).

Remove the square brackets [] on ImgInfo in your GetObject call.


That was how it was originally, but GetObject returned an error. And adding the brackets made it return successfully. Which is weird, Makes me wonder if LoadImage on which GetObject depends upon is not working correctly (LoadImage returns successful).

I usually get confused with brackets. My current understanding is that with no brackets you pass a pointer and with brackets you pass the immediate value. Since the name of structure doesn't actually reference any data, i'd assume it's just an address to the first element. And because the function wants the address and the address IS the immediate value you don't need brackets. I think i need to study the FASM doc more thoroughly :p.

I did try porting a different example which loads raw images:

Code:
invoke VirtualAlloc,NULL,256*256*3,MEM_COMMIT        ;save the file to a buffer HERE
mov    [hBMP],eax
invoke CreateFile,filename,GENERIC_READ+GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
mov    [hFile],eax
invoke ReadFile,[hFile],[hBMP],256*256*3,[BytesRead],NULL
invoke CloseHandle,[hFile]
invoke glGenTextures,1,texture
invoke glBindTexture,GL_TEXTURE_2D,texture
invoke glTexEnvf,GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT
invoke gluBuild2DMipmaps,GL_TEXTURE_2D,3,256,256,GL_RGB,GL_UNSIGNED_BYTE,[hBMP]  
    


But that's not working either. Perhaps it's not my initialization at all, but my implementation. The example im porting from is written in C and uses fopen() from stdio so my use of memory allocation file I/O might be wrong. it's gluBuild2DMipMaps thats causing an error (send or don't send report error).

Heres my DrawScene code section (cut down):

Code:
invoke  glClear,GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT            ;clear buffer
invoke  glLoadIdentity
invoke  glRotatef,[rpos],0.0f,1.0f,0.0f
invoke  glTranslatef,[xpos],0.0,[zpos]            
invoke  glEnable,GL_TEXTURE_2D
invoke  glBindTexture,GL_TEXTURE_2D,texture
invoke  glBegin,GL_QUADS
        invoke  glTexCoord3f,0.0,0.0,0.0
        invoke  glVertex3f,0.0,0.0,0.0
        invoke  glTexCoord3f,1.0,0.0,0.0
        invoke  glVertex3f,1.0,0.0,0.0
        invoke  glTexCoord3f,1.0,1.0,0.0
        invoke  glVertex3f,1.0,1.0,0.0
        invoke  glTexCoord3f,0.0,1.0,0.0
        invoke  glVertex3f,0.0,1.0,0.0
invoke  glEnd
invoke  SwapBuffers,[hdc]     
    


Im totally lost here Sad.
Post 16 Jan 2008, 22:12
View user's profile Send private message MSN Messenger Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 17 Jan 2008, 07:59
Isn't this bad programming?
Code:
        invoke  glTexCoord3f,0.0,0.0,0.0
        invoke  glVertex3f,0.0,0.0,0.0
        invoke  glTexCoord3f,1.0,0.0,0.0     

I haven't done much 3D-programming but I remember D3D had a Vertex buffer ^o) and you just give it a pointer. Looks like here you make a call for every vertex :S.
Post 17 Jan 2008, 07:59
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004 17 Jan 2008, 09:14
Madis731 wrote:
Isn't this bad programming?
Code:
        invoke  glTexCoord3f,0.0,0.0,0.0
        invoke  glVertex3f,0.0,0.0,0.0
        invoke  glTexCoord3f,1.0,0.0,0.0     

I haven't done much 3D-programming but I remember D3D had a Vertex buffer ^o) and you just give it a pointer. Looks like here you make a call for every vertex :S.


Yea, i think they're called display lists. You can store a routine and call it with one line. I haven't learned how to use those yet though.
Post 17 Jan 2008, 09:14
View user's profile Send private message MSN Messenger Reply with quote
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004 17 Jan 2008, 18:45
Turns out that [hBMP] wasn't a valid handle. i was tweaking with VirtualAlloc() and LocalAlloc until instead of displaying the current colour it displayed black. This led me to believe that [hBMP] was now valid but was empty.

So i spent 3 or 4 hours trying to get ReadFile to work, until i decided to use fread() instead. I then swapped VirtualAlloc for malloc() and CreateFile for fopen(). Now fread() actually read the file and the image displayed.

Heres the code:

Code:
cinvoke malloc,256*256*3
mov    [hBmp],eax
cinvoke fopen,filename,access
mov    [hFile],eax
cinvoke fread,[hBmp],256*256*3,1,[hFile]
cinvoke fclose,[hFile]
invoke glGenTextures,1,GLTexture
invoke glBindTexture,GL_TEXTURE_2D,GLTexture
invoke glTexEnvf,GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT
invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT
invoke gluBuild2DMipmaps,GL_TEXTURE_2D,3,256,256,GL_RGB,GL_UNSIGNED_BYTE,[hBmp] 
    


Last edited by calpol2004 on 18 Jan 2008, 14:55; edited 2 times in total
Post 17 Jan 2008, 18:45
View user's profile Send private message MSN Messenger Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 18 Jan 2008, 07:24
Nice - I thought its 2D at first Razz and was a bit disappointed. Then I read the source and saw the keys (L, R, U, D,...) and started using them Wink NICE!

Btw, did you notice how strangely the floor acts when you rotate around. Maybe its my drivers or my videocard if you don't notice it, but I think its about how OGL transfers the colours in a two-colour "environment" (read: on a quad consisting of two tri-vertex planes).
Post 18 Jan 2008, 07:24
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
asmfan



Joined: 11 Aug 2006
Posts: 392
Location: Russian
asmfan 18 Jan 2008, 09:34
malloc, fopen, fread, fclose, free - C calling convention. Use cinvoke rather than invoke.
Post 18 Jan 2008, 09:34
View user's profile Send private message Reply with quote
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004 18 Jan 2008, 14:38
Madis731 wrote:
Nice - I thought its 2D at first Razz and was a bit disappointed. Then I read the source and saw the keys (L, R, U, D,...) and started using them Wink NICE!

Btw, did you notice how strangely the floor acts when you rotate around. Maybe its my drivers or my videocard if you don't notice it, but I think its about how OGL transfers the colours in a two-colour "environment" (read: on a quad consisting of two tri-vertex planes).


Yea i did, it does look a lot better with a texture as the floor (still needs work though). Come to think of it, take a look for yourself (attached).

Now i've got textures down, it's collision detection, looking with mouse (not looking forward to the maths...), Simple timing so it runs at the same speed on all pc's and loading the world from a file. Shocked. I'm hoping to have a VERY simple 3D engine ready for when the module starts Twisted Evil.

asmfan wrote:
malloc, fopen, fread, fclose, free - C calling convention. Use cinvoke rather than invoke.


Thanks :p, i usually forget things like that Wink.


Description:
Download
Filename: Opengl.zip
Filesize: 53.33 KB
Downloaded: 210 Time(s)

Post 18 Jan 2008, 14:38
View user's profile Send private message MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.