News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Can't get a rendering context, wglCreateContext fails

Started by hamper, November 11, 2013, 10:30:52 PM

Previous topic - Next topic

Farabi

Quote from: vertograd on November 12, 2013, 08:11:58 AM
intriguing ...
I will try but please clearify several things :
hRC - a handle to window rectangle
hDC -               to device context (but you're calling GetDC inside your proc? )
and what is a macro CADD? EDIT: OK, I've found it !
anyway you'd better to give us (beginners) the whole application example  ;)

Use a blank variable. That variable will be filled with a new hDC, you dont need to create one. It will creating it for you.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

Quote from: vertograd on November 12, 2013, 08:11:58 AM
intriguing ...
I will try but please clearify several things :
hRC - a handle to window rectangle
hDC -               to device context (but you're calling GetDC inside your proc? )
and what is a macro CADD? EDIT: OK, I've found it !
anyway you'd better to give us (beginners) the whole application example  ;)

This is the whole applications https://www.asuswebstorage.com/navigate/s/04AAECE4836447E29B9E0A0AAA5D13DCY but it wont worked on yours because MASM changed the inc file structure, that is why you need to use my tools. Im still synching my tools to the server. After it done, I'll give it to you. You may use it for commercial or non commercial purpose, I'll tell you everything. I guess I need to pass my legacy.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

GoneFishing

Here it is  :icon_exclaim:

;--------------------------------------------------------------------------------------
; A simple OpenGL example                                                   ogltest.asm
;--------------------------------------------------------------------------------------

include \masm32\include\masm32rt.inc

includelib \masm32\lib\opengl32.lib

;--------------------------------------------------------------------------------------

CreateWindowExA      proto :dword,:dword,:dword,:dword,:dword,:dword,
                           :dword,:dword,:dword,:dword,:dword,:dword
DefWindowProcA       proto :dword,:dword,:dword,:dword
DispatchMessageA     proto :dword
ExitProcess          proto :dword
GetMessageA          proto :dword,:dword,:dword,:dword
GetModuleHandleA     proto :dword
LoadCursorA          proto :dword,:dword
LoadIconA            proto :dword,:dword
PostQuitMessage      proto :dword
RegisterClassExA     proto :dword
windowproc           proto :dword,:dword,:dword,:dword
GetDC                proto :dword
wglChoosePixelFormat proto :dword,:dword
wglSetPixelFormat    proto :dword,:sdword,:dword
wglCreateContext     proto :dword
wglMakeCurrent       proto :dword,:dword
wglDeleteContext     proto :dword
ReleaseDC            proto :dword,:dword
glBegin              proto :dword
glEnd                proto
glVertex2fv          proto :dword
MessageBoxA          proto :dword,:dword,:dword,:dword
DrawScene            proto
point struct
   x dword ?
   y dword ?
point ends

msgst struct
   hwnd    dword ?
   message dword ?
   wparam  dword ?
   lparam  dword ?
   time    dword ?
   pt      point <>
msgst ends

windowclasstype struct
   wcsize        dword ?
   style         dword ?
   addrwp        dword ?
   classeb       dword ?
   windoweb      dword ?
   proghandle    dword ?
   liconhandle   dword ?
   cursorhandle  dword ?
   colour        dword ?
   menuhandle    dword ?
   addrclassname dword ?
   siconhandle   dword ?
windowclasstype ends

;--------------------------------------------------------------------------------------

.const
classname   byte "SimpleWinClass",0
windowtitle byte "The window title",0
mymbtitle   byte "Alert",0
mymbtext    byte "Could not get rendering context",0

.data?
msg       msgst <>
mywc      windowclasstype <>
winhandle dword ?
dchandle  dword ?
rchandle  dword ?

.data

PixFrm PIXELFORMATDESCRIPTOR <>
myvertex1 real4 0.0,0.0
myvertex2 real4 1.0,0.0
myvertex3 real4 0.5,0.87

.code
start:

;######################################################################################

;---------- fill up the window class structure variable and get the program handle etc.

mov mywc.wcsize,48
mov mywc.style,35                    ; CS_VREDRAW + CS_HREDRAW + CS_OWNDC
mov mywc.addrwp,offset windowproc
mov mywc.classeb,0
mov mywc.windoweb,0
push 0
call GetModuleHandleA
mov mywc.proghandle,eax
push 32515                           ; A black ! in a yellow triangle
push 0
call LoadIconA
mov mywc.liconhandle,eax
mov mywc.siconhandle,eax
push 32515                           ; A crosshair
push 0
call LoadCursorA
mov mywc.cursorhandle,eax
mov mywc.colour,2                    ; desktop colour
mov mywc.menuhandle,0
mov mywc.addrclassname,offset classname

;---------- register the window class

push offset mywc
call RegisterClassExA

;---------- create and display the window

push 0
push mywc.proghandle
push 0
push 0
push 340                           ; window height
push 320                           ; window width
push 230                           ; window top position
push 480                           ; window left position
push 269418496                     ; WS_VISIBLE + title bar, title, icon, icon menu,
                                   ;    min, max and close buttons, sizing borders
push offset windowtitle
push mywc.addrclassname
push 0                             ; default
call CreateWindowExA
mov winhandle,eax

invoke ShowWindow,winhandle,SW_SHOWNORMAL
invoke UpdateWindow,winhandle
call       DoEvents
ret



;######################################################################################

DoEvents PROC
LOCAL m:MSG
StartLoop: ; Check for waiting messages
invoke PeekMessage,ADDR m,0,0,0,PM_NOREMOVE
or eax,eax
jz NoMsg
invoke GetMessage,ADDR m,NULL,0,0
or eax,eax
jz ExitLoop
invoke TranslateMessage,ADDR m
invoke DispatchMessage,ADDR m
jmp StartLoop
NoMsg: ; No pending messages: draw the scene
invoke DrawScene
jmp StartLoop
ExitLoop: mov eax,m.wParam
ret
DoEvents ENDP

;----------

;comment *

DrawScene proc

push rchandle
push dchandle
call wglMakeCurrent

push 4                  ; triangles
call glBegin
push offset myvertex1
call glVertex2fv
push offset myvertex2
call glVertex2fv
push offset myvertex3
call glVertex2fv
call glEnd
ret
DrawScene endp

;*

;---------- the window procedure

windowproc  PROC hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
LOCAL WINRect:RECT
LOCAL PixFormat:DWORD

.if uMsg == WM_CREATE
invoke GetDC,hWin
mov dchandle,eax
mov ax,SIZEOF PixFrm
mov PixFrm.nSize,ax
mov PixFrm.nVersion,1
mov PixFrm.dwFlags,36
;mov PixFrm.dwLayerMask,PFD_MAIN_PLANE
mov PixFrm.iPixelType,0
mov PixFrm.cColorBits,32
mov PixFrm.cDepthBits,32
mov PixFrm.cAccumBits,0
mov PixFrm.cStencilBits,0
invoke ChoosePixelFormat,dchandle,ADDR PixFrm
mov PixFormat,eax
invoke SetPixelFormat,dchandle,PixFormat,ADDR PixFrm
or eax,eax
jz NoPixelFmt
                       ; int 3
invoke wglCreateContext,dchandle
mov rchandle,eax
                       ; int 3
                        fn MessageBox,0,hex$(eax),"Title",MB_OK

                        fn MessageBox,0,LastError$(),"Last Error Text",MB_OK


NoPixelFmt:
return 0

                  .elseif uMsg == WM_PAINT

                        invoke DrawScene
                       
                        return 0

.elseif uMsg == WM_SIZE
invoke GetClientRect,hWin,ADDR WINRect

return 0

.elseif uMsg == WM_CLOSE

mov eax,rchandle
or eax,eax
jz NoGlDC
; Delete our objects
invoke  wglDeleteContext,rchandle
                        invoke  ExitProcess,0
NoGlDC:
      invoke ReleaseDC,hWin,dchandle
      invoke DestroyWindow,hWin
return 0
.elseif uMsg == WM_DESTROY
invoke PostQuitMessage,NULL
return 0
.endif
invoke DefWindowProc,hWin,uMsg,wParam,lParam
ret

windowproc endp

;--------------------------------------------------------------------------------------

end start


Farabi

 :t congrats that is the first step. Next is model loader.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

GoneFishing

Quote from: Farabi on November 12, 2013, 08:02:02 AM
Use this as a framework https://www.asuswebstorage.com/navigate/s/04AAECE4836447E29B9E0A0AAA5D13DCY
Use RadAsm and edit the MainSection procedure to start making a rendering engine, it can load a 3D models called TRI Format. I'll upload the exporter for the tri format tools.

Thank you for the link. I've already downloaded it . I have no RadAsm installed. Can I do it in QEDITOR or VS?

Quote from: Farabi on November 12, 2013, 08:53:25 AM
:t congrats that is the first step. Next is model loader.

Thanks
You're too quick  :biggrin:
I think next step would be to change the color of triangle, then  to draw a multi-colored one 

drizz

Quote from: hamper on November 12, 2013, 07:32:10 AM
No definitive answer or analysis so far as to why this happens.

You need to use functions from gdi32.dll ChoosePixelformat() SetPixelformat() instead of wgl prefixed ones.

http://www.opengl.org/archives/resources/faq/technical/mswindows.htm
Read 5.190 - 5.210

Farabi

Quote from: vertograd on November 12, 2013, 09:02:55 AM
Quote from: Farabi on November 12, 2013, 08:02:02 AM
Use this as a framework https://www.asuswebstorage.com/navigate/s/04AAECE4836447E29B9E0A0AAA5D13DCY
Use RadAsm and edit the MainSection procedure to start making a rendering engine, it can load a 3D models called TRI Format. I'll upload the exporter for the tri format tools.

Thank you for the link. I've already downloaded it . I have no RadAsm installed. Can I do it in QEDITOR or VS?

Quote from: Farabi on November 12, 2013, 08:53:25 AM
:t congrats that is the first step. Next is model loader.

Thanks
You're too quick  :biggrin:
I think next step would be to change the color of triangle, then  to draw a multi-colored one

Here is RadAsm. Changing the color of triangle is simple, use

invoke glcolor4f,FP4(1.),FP4(1.),FP4(1.)   ; white
invoke glcolor4f,FP4(1.),FP4(0.),FP4(0.)   ; Red
etc...etc...etc


You can also use an array of real4 or real8 as the color data.


RadAsm: https://www.asuswebstorage.com/navigate/s/3DA934576973451996A474B01BE762C7Y
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

GoneFishing

#37
...

Siekmanski

This is a basic Ogl example I did for my little nephew some years ago. He wanted to become a game programmer.
It has some basic stuff as background image, 2D and 3D drawing, line drawing, textures, transparency and an image loader (bmp gif jpeg) to load textures from file or from the resource to get him started.
Creative coders use backward thinking techniques as a strategy.

dedndave


Siekmanski

Creative coders use backward thinking techniques as a strategy.

dedndave

doing some reading.....

i learned that i can use glGetString(GL_VERSION) to get my version level
on my old XP machine, i get 1.4.0, a far cry from the 3+ or whatever is current
at any rate, the version may account for varying results we are seeing

also, found some nice tutorials
there seems to be many of them out there

http://www.mbsoftworks.sk/

a subject that i am particularly interested in...
http://www.lighthouse3d.com/opengl/terrain/

GoneFishing

#42
...

hamper

Well, I've re-written my program to do all the global OpenGL initialisation stuff in the window procedure in response to receiving a WM_CREATE message. Also to use PeekMessageA and to do the frame rendering if there's no message pending. I've also tried using the gdi32.lib versions ChoosePixelFormat and SetPixelFormat instead of the opengl32.lib "wgl" prefix versions...

All to no avail. Still can't get a rendering context. And still can't see what's wrong with my program. But here is my latest version, just in case anyone is actually interested in trying to help me find out where the problem is, as opposed to just berating my programming style, trying to be slick and funny, or promoting software that's nothing to do with the subject of this thread. But I'm not holding my breath. I'll give it a couple of days to see if this forum is actually capable of helping solve my problem. If not, then my assembly notes, C notes, API notes and OpenGL notes are all going on the fire, condemned to the "what a complete waste of time all that was" tray.

;--------------------------------------------------------------------------------------
; An OpenGL example                                                      opengltest.asm
;--------------------------------------------------------------------------------------

.686
.model flat,stdcall
option casemap:none

includelib user32
includelib kernel32
includelib opengl32

;--------------------------------------------------------------------------------------

CreateWindowExA      proto :dword,:dword,:dword,:dword,:dword,:dword,
                           :dword,:dword,:dword,:dword,:dword,:dword
DefWindowProcA       proto :dword,:dword,:dword,:dword
DispatchMessageA     proto :dword
ExitProcess          proto :dword
GetModuleHandleA     proto :dword
LoadCursorA          proto :dword,:dword
LoadIconA            proto :dword,:dword
PeekMessageA         proto :dword,:dword,:dword,:dword,:dword
PostQuitMessage      proto :dword
RegisterClassExA     proto :dword
windowproc           proto :dword,:dword,:dword,:dword
GetDC                proto :dword
wglChoosePixelFormat proto :dword,:dword
wglSetPixelFormat    proto :dword,:sdword,:dword
wglCreateContext     proto :dword
wglMakeCurrent       proto :dword,:dword
wglDeleteContext     proto :dword
ReleaseDC            proto :dword,:dword
glViewport           proto :sdword,:sdword,:sdword,:sdword
glMatrixMode         proto :dword
glOrtho              proto :real8,:real8,:real8,:real8,:real8,:real8
glClearColor         proto :real4,:real4,:real4,:real4
glClear              proto :dword
glPushMatrix         proto
glPopMatrix          proto
glRotatef            proto :real4,:real4,:real4,:real4
glBegin              proto :dword
glEnd                proto
glColor3fv           proto :dword
glVertex3fv          proto :dword
glFlush              proto
wglSwapBuffers       proto :dword
rendernextframe      proto

point struct
   x dword ?
   y dword ?
point ends

msgst struct
   hwnd    dword ?
   message dword ?
   wparam  dword ?
   lparam  dword ?
   time    dword ?
   pt      point <>
msgst ends

windowclasstype struct
   wcsize        dword ?
   style         dword ?
   addrwp        dword ?
   classeb       dword ?
   windoweb      dword ?
   proghandle    dword ?
   liconhandle   dword ?
   cursorhandle  dword ?
   colour        dword ?
   menuhandle    dword ?
   addrclassname dword ?
   siconhandle   dword ?
windowclasstype ends

pfdtype struct
   nsize           word  40
   nversion        word  1
   dwflags         dword 4133
   ipixeltype      byte  0
   ccolorbits      byte  32
   credbits        byte  0
   credshift       byte  0
   cgreenbits      byte  0
   cgreenshift     byte  0
   cbluebits       byte  0
   cblueshift      byte  0
   calphabits      byte  0
   calphashift     byte  0
   caccumbits      byte  0
   caccumredbits   byte  0
   caccumgreenbits byte  0
   caccumbluebits  byte  0
   caccumalphabits byte  0
   cdepthbits      byte  32
   cstencilbits    byte  0
   cauxbuffers     byte  0
   ilayertype      byte  0
   breserved       byte  0
   dwlayermask     dword 0
   dwvisiblemask   dword 0
   dwdamagemask    dword 0
pfdtype ends

;--------------------------------------------------------------------------------------

.const
classname   byte "SimpleWinClass",0
windowtitle byte "The window title",0

.data?
msg        msgst <>
mywc       windowclasstype <>
winhandle  dword ?
dchandle   dword ?
rchandle   dword ?

.data
pfd        pfdtype <>
myortho    real8 1.0,1.0,-1.0,1.3,0.5,1.5
mybgcolour real4 0.0,0.0,0.0,1.0
myrot      real4 0.0,0.0,0.0,1.0
myrotsdw   sdword 0
myvertices real4 0.0,1.0,1.0,0.87,-0.5,1.0,-0.87,-0.5,1.0
mycolours  real4 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0

.code
start:

;######################################################################################

;---------- fill up the window class structure variable and get the program handle etc.

mov mywc.wcsize,48
mov mywc.style,35
mov mywc.addrwp,offset windowproc
mov mywc.classeb,0
mov mywc.windoweb,0
push 0
call GetModuleHandleA
mov mywc.proghandle,eax
push 32515
push 0
call LoadIconA
mov mywc.liconhandle,eax
mov mywc.siconhandle,eax
push 32515
push 0
call LoadCursorA
mov mywc.cursorhandle,eax
mov mywc.colour,2
mov mywc.menuhandle,0
mov mywc.addrclassname,offset classname

;---------- register the window class

push offset mywc
call RegisterClassExA

;---------- create and display the window

push 0
push mywc.proghandle
push 0
push 0
push 340         ; window height
push 320         ; window width
push 230         ; window top position
push 480         ; window left position
push 269418496
push offset windowtitle
push mywc.addrclassname
push 0
call CreateWindowExA
mov winhandle,eax

;---------- the message loop

msgloopstart:
   push 1                 ; messages will be retrieved
   push 0
   push 0
   push winhandle
   push offset msg
   call PeekMessageA
   cmp eax,0
   jne messageretrieved
   call rendernextframe   ; no message pending, so render next frame
   jmp msgloopstart
messageretrieved:
   cmp msg.message,18         ; WM_QUIT
   je msgloopend
   push offset msg
   call DispatchMessageA
   jmp msgloopstart
msgloopend:
   push rchandle
   call wglDeleteContext
   push dchandle
   push winhandle
   call ReleaseDC
   push 0
   call ExitProcess

;######################################################################################

;---------- the window procedure

windowproc proc hwnd:dword,message:dword,wparam:dword,lparam:dword

      cmp message,1           ; WM_CREATE
      je initialiseopengl
      cmp message,16          ; WM_CLOSE
      je closeprogram
   ; default processing
      push lparam
      push wparam
      push message
      push hwnd
      call DefWindowProcA
      ret
   initialiseopengl:
      push winhandle
      call GetDC
      mov dchandle,eax
      push offset pfd
      push dchandle
      call wglChoosePixelFormat
      push offset pfd
      push eax
      push dchandle
      call wglSetPixelFormat
      push dchandle
      call wglCreateContext
      mov rchandle,eax
      push rchandle
      push dchandle
      call wglMakeCurrent
      push 5889
      call glMatrixMode     ; projection matrix stack
      push 300              ; height
      push 300              ; width
      push 10               ; y
      push 10               ; x
      call glViewport
      push dword ptr myortho[44]   ; myortho real8 1.0,1.0,-1.0,1.3,0.5,1.5
      push dword ptr myortho[40]
      push dword ptr myortho[36]
      push dword ptr myortho[32]
      push dword ptr myortho[28]
      push dword ptr myortho[24]
      push dword ptr myortho[20]
      push dword ptr myortho[16]
      push dword ptr myortho[12]
      push dword ptr myortho[8]
      push dword ptr myortho[4]
      push dword ptr myortho
      call glOrtho
      push 5888
      call glMatrixMode     ; modelview matrix stack
      push mybgcolour[12]   ; mybgcolour real4 0.0,0.0,0.0,1.0   ; black
      push mybgcolour[8]
      push mybgcolour[4]
      push mybgcolour
      call glClearColor
      ret
   closeprogram:
      push 0
      call PostQuitMessage
      ret

windowproc endp

;---------- the rendering procedure

rendernextframe proc

      push 16384
      call glClear                 ; clear window to background colour
      call glPushMatrix            ; push the matrix stack
      inc myrotsdw                 ; perform any translations, scales, rotations etc.
      cmp myrotsdw,360
      jne carryon
      mov myrotsdw,0
   carryon:
      finit
      fild myrotsdw
      fst myrot
      push myrot[12]
      push myrot[8]
      push myrot[4]
      push myrot
      call glRotatef
      push 4
      call glBegin                 ; draw objects anew
      push offset mycolours
      call glColor3fv
      push offset myvertices
      call glVertex3fv
      push offset mycolours[12]
      call glColor3fv
      push offset myvertices[12]
      call glVertex3fv
      push offset mycolours[24]
      call glColor3fv
      push offset myvertices[24]
      call glVertex3fv
      call glEnd
      call glFlush
      call glPopMatrix             ; pop the matrix stack
      push dchandle
      call wglSwapBuffers          ; swap buffers
      ret

rendernextframe endp

;--------------------------------------------------------------------------------------

end start



jj2007

hi hamper,

Your frustration is understandable, this has happened to all of us before ;-)

There is one obvious problem:
   initialiseopengl:
      push winhandle
      call GetDC

You are pushing zero here - and you do get a handle, strangely enough. But it's not the right one...
This triggers the following errors:

DC
eax             956367609
$Err$()         Operazione completata.

wglChoosePixelFormat
eax             4
$Err$()         Can't find the specified procedure

wglSetPixelFormat
eax             1
$Err$()         Can't find the specified file

wglCreateContext
hwnd            393264
dchandle        956367609
eax             65536
$Err$()         Can't find the specified file


If instead you use the valid hwnd:
      push hwnd
      call GetDC


... then you get less errors:
DC
eax             503384091
$Err$()         Operazione completata.

wglChoosePixelFormat
eax             4
$Err$()         Can't find the specified procedure <<< this is the remaining problem <<<

wglSetPixelFormat
eax             1
$Err$()         Operazione completata.

wglCreateContext
hwnd            592362
dchandle        503384091
eax             65536
$Err$()         Operazione completata.

wglMakeCurrent
eax             1
$Err$()         Operazione completata.

glMatrixMode
eax             1704576
$Err$()         Operazione completata.

glViewport
eax             1096356113
$Err$()         Operazione completata.

glOrtho
eax             1281
$Err$()         Operazione completata.

glClearColor
eax             255
$Err$()         Operazione completata.


By the way, after two BSODs and complete reboots (Win XP SP3), I decided to ExitProcess directly after call wglCreateContext ;-)

In the meantime, however, it seems that pushing a valid handle for GetDC solves the BSOD problem (I had not seen one for several years, wow...).

Hope this helps you continue your fight :icon14:

P.S.: The result looks cute - see attachment.
P.P.S.: Your message handling is unorthodox. To allow exiting the program with less acrobatics, I would suggest this hack (tested, it works):

messageretrieved:
   cmp msg.message,18         ; WM_QUIT
   je msgloopend

      invoke GetKeyState, VK_SHIFT
      test ah, ah
      js msgloopend

   push offset msg
   call DispatchMessageA