News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Can't get a rendering context, wglCreateContext fails

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

Previous topic - Next topic

GoneFishing

Hi Jochen,
These problems with OpenGL on different machines seem never stop if not been stopped
it's just like beer which is never enough ...
but I must go and buy some more now  ;)

dedndave

ok Jochen
i figured you might have the PSDK or maybe VS installed

we need to figure out what's up with Gunther's machine so we can avoid it in the future
maybe it has to do with the message loop calls

let me finish my "cleaned-up" version
in the mean time, Gunther can test Jochen's latest post and qWord's "foo" on his win7-64 machine   :biggrin:

hamper

Well, for what it's worth here's my final, finished, complete, working program!
It's taken a long time and much heartache, but it diplays a multi-coloured revolving triangle inside a little window.
Thanks to everyone (esp. dave) for all your help, and my sincere apologies for throwing a bit of a wobbly here and there (just frustration -- as it turns out it was the library files not performing as they should).
Enjoy!
After all, we all need a good spinning triangle now and then.
Regards,
Phil

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

.686
.model flat,stdcall
option casemap:none

includelib gdi32
includelib kernel32
includelib opengl32
includelib user32

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

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
PostQuitMessage   proto :dword
RegisterClassExA  proto :dword
windowproc        proto :dword,:dword,:dword,:dword

ChoosePixelFormat proto :dword,:dword
GetDC             proto :dword
glBegin           proto :dword
glClear           proto :dword
glClearColor      proto :real4,:real4,:real4,:real4
glColor3fv        proto :dword
glEnd             proto
glMatrixMode      proto :dword
glOrtho           proto :real8,:real8,:real8,:real8,:real8,:real8
glPopMatrix       proto
glPushMatrix      proto
glRotatef         proto :real4,:real4,:real4,:real4
glVertex3fv       proto :dword
glViewport        proto :sdword,:sdword,:sdword,:sdword
PeekMessageA      proto :dword,:dword,:dword,:dword,:dword
ReleaseDC         proto :dword,:dword
rendernextframe   proto
SetPixelFormat    proto :dword,:sdword,:dword
SwapBuffers       proto :dword
wglCreateContext  proto :dword
wglDeleteContext  proto :dword
wglMakeCurrent    proto :dword,:dword

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
mybgcolour  real4 0.0,0.0,0.0,1.0   ; black
mycolours   real4 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0
myortho     real8 1.0,1.0,-1.0,1.3,0.5,1.5
myrot       real4 0.0,0.0,0.0,1.0
myrotsdw    sdword 0
pfd         pfdtype <>
myvertices  real4 0.0,1.0,1.0,0.87,-0.5,1.0,-0.87,-0.5,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

;---------- initialise OpenGL

push winhandle
call GetDC
mov  dchandle,eax
push offset pfd
push dchandle
call ChoosePixelFormat
push offset pfd
push eax
push dchandle
call SetPixelFormat
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              ; set viewport
push dword ptr myortho[44]
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       ; orthographic projection
call glOrtho
push 5888
call glMatrixMode            ; modelview matrix stack
push mybgcolour[12]
push mybgcolour[8]
push mybgcolour[4]
push mybgcolour
call glClearColor            ; background colour to clear to

;---------- 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,16 ; WM_CLOSE
je its16

; default processing
push lparam
push wparam
push message
push hwnd
call DefWindowProcA
ret

its16:
push 0
call PostQuitMessage
ret

windowproc endp


;---------- rendering the next frame

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
   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 glPopMatrix             ; pop the matrix stack
   push dchandle
   call SwapBuffers             ; swap buffers (with automatic flush)
ret

rendernextframe endp

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

end start


dedndave

are you Australian ?
not the only one on board, in fact the guy that runs the place (Hutch) is an Aussie   :P

hamper

No mate, English. Up in the Yorkshire Dales actually. Not rich enough to be an Aussie. But well versed enough on bloody sheep to be one.

dedndave

ok - i just noticed some words, is all - lol
wifee is a Brummy   :P

dedndave

well - it works
there are a few things i don't understand, yet
part of the learning curve

i am interested to see if it works on Gunther's win7-64 machine   :P

Gunther

Hi Dave,

Quote from: dedndave on November 14, 2013, 02:52:20 PM
i am interested to see if it works on Gunther's win7-64 machine   :P

I'm interested, too. But at the moment I've to do some lessons (only a short break). I'll download the appropriate archive this evening and test it.

Gunther
You have to know the facts before you can distort them.

FORTRANS

Hi,

   In Reply #49, qWord's foo works on XP and just shows a black
window on Win2k.  In Reply #96, Dave's SmallOgl3 shows a black
window on Win2k.  At first it seemed to just show a black window
on XP, but if I grabbed the title bar and moved the window around,
the triangle shows up.

Cheers,

Steve

dedndave

thanks for the report, Steve
that's a little odd   :P

this one does things a little differently, maybe it will fix the problems you mentioned
i use the multi-media timer so that it updates once every mS
thing about that is, the mm timer runs in it's own thread
OpenGL seems to want everything in one thread
i cheated a little and used PostMessage to send a WM_TIMER message
i guess i could register a user message to be more correct   ;)

but - i also handle the WM_PAINT messages so that the updates are "windows-happy"

EDIT: small RC file change to make the icon appear in the title bar

GoneFishing

nice  :t
runs fine here
with such a speed that I can see much more than just a simple triangle  :biggrin:
one minor problem  - the triangle seems to enjoy itself and doesn't want to stop unless  persuaded with Task Manager
EDIT: one more thing - "Busy" cursor

jj2007

Quote from: vertograd on November 15, 2013, 02:52:38 AM
nice  :t
runs fine here
with such a speed that I can see much more than just a simple triangle  :biggrin:
one minor problem  - the triangle seems to enjoy itself and doesn't want to stop unless  persuaded with Task Manager

See this post. Try pushing 1 or 7 instead of eax=4 before SetPixelFormat.

You don't need Task Manager to stop it. In reply #44 I posted one that ends smoothly by pressing Shift, but all other versions posted here also stop when clicking Close in the system menu. They do not stop when clicking the x in the upper right corner, though.

GoneFishing

Quote from: jj2007 on November 15, 2013, 03:00:38 AM
Quote from: vertograd on November 15, 2013, 02:52:38 AM
nice  :t
runs fine here
with such a speed that I can see much more than just a simple triangle  :biggrin:
one minor problem  - the triangle seems to enjoy itself and doesn't want to stop unless  persuaded with Task Manager

See this post. Try pushing 1 or 7 instead of eax=4 before SetPixelFormat.

You don't need Task Manager to stop it. In reply #44 I posted one that ends smoothly by pressing Shift, but all other versions posted here also stop when clicking Close in the system menu. They do not stop when clicking the x in the upper right corner, though.

Task Manager is the only way to stop it
no system menu , no ALT-F4, no Shift
Just ran your OGL_Test from Reply #44 . It doesn't respond on pressing Shift here

FORTRANS

Hi Dave,

   Tried SmallOgl4a from Reply #99.  Win2k, quite fast triangle so
looks ragged, 100% CPU, and needs task manager to kill it.  On XP
blank window until wiggled, then some rough triangle stuff, can be
closed normally.  When not wiggling, erratic CPU usage, mostly low,
a few percent, but blips of ~30% or ~70%, and can hit 100% right
after a wiggle session.  While moving around CPU seems to mostly
alternate between ~35% and ~65%.  Definitely different behavior.
Numbers are very approximate.

Regards,

Steve N.

jj2007

Quote from: vertograd on November 15, 2013, 03:04:19 AM
Just ran your OGL_Test from Reply #44 . It doesn't respond on pressing Shift here

Oops, that was a version without GetKeyState. This one is better... plain Masm32, 3k exe.