flat assembler
Message board for the users of flat assembler.

Index > Windows > Translating OpenGL code

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 843
Location: Jakarta, Indonesia
TmX 17 Dec 2009, 08:19
I have this following code
Code:
#include <windows.h>
#include <GL/glut.h>

static GLfloat spin=0.0;

void myInit(void);
void myDisplay(void);
void spinDisplay(void);
void spinCDisplay(void);
void myReshape(int, int); 
void myMouse(int, int, int, int);

int main(int argc, char *argv[]) {
    glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowPosition(100,100);
    glutCreateWindow("My Animation");
 glutDisplayFunc(myDisplay);
 glutReshapeFunc(myReshape);
 glutMouseFunc(myMouse);
     myInit();
   glutMainLoop();
     return 0;
}

void myInit() {
     glClearColor(0.0,0.0,0.0,0.0);
}

void myDisplay() {
     glClear(GL_COLOR_BUFFER_BIT);
       glPushMatrix();
     glTranslated(0.0,20.0,0.0);
 glRotatef(spin,0.0,0.0,1.0);
        glColor3f(1.0,0.0,0.0);
     glRectf(-10.0,35.0,10.0,15.0);
      glPopMatrix();
      glPushMatrix();
     glTranslated(0.0,-20.0,0.0);
        glRotatef(spin,0.0,0.0,-1.0);
       glColor3f(0.0,1.0,0.0);
     glRectf(-10.0,-15.0,10.0,-35.0);
    glPopMatrix();
      glutSwapBuffers();
}

void spinDisplay() {
       spin=spin+12;
       if(spin>360.0) spin=spin-360.0;
  glutPostRedisplay();
}

void spinCDisplay() {
    spin=spin-11;
       if(spin<360.0) spin=spin+360.0;
  glutPostRedisplay();
}

void myReshape(int w, int h) {
   glViewport(0,0,w,h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
   glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}

void myMouse(int button, int state, int x, int y) {
 switch(button) {
               case GLUT_LEFT_BUTTON : 
                        if(state==GLUT_DOWN) glutIdleFunc(spinDisplay);
                     break;
              case GLUT_MIDDLE_BUTTON:
                        if(state==GLUT_DOWN) glutIdleFunc(NULL);
                    break;
              case GLUT_RIGHT_BUTTON: 
                        if(state==GLUT_DOWN) glutIdleFunc(spinCDisplay);
                    break;
              default: 
                       break;
      }
}
    


Several parts of them are not that hard to translate in assembly, such as:
Code:
myDisplay:
 glClear,GL_COLOR_BUFFER_BIT
 glPushMatrix
        glTranslated,0.0,20.0,0.0
   glRotatef,spin,0.0,0.0,1.0
  glColor3f,1.0,0.0,0.0
       glRectf,-10.0,35.0,10.0,15.0
        glPopMatrix
 glPushMatrix
        glTranslated,0.0,-20.0,0.0
  glRotatef,spin,0.0,0.0,-1.0)
    glColor3f,0.0,1.0,0.0
       glRectf,-10.0,-15.0,10.0,-35.0
      glPopMatrix
 glutSwapBuffers    


My question is: how to translate glutDisplayFunc, glutReshapeFunc, or
glutMouseFunc?

I don't know how to pass function/procedure as argument in assembly, yet.
Post 17 Dec 2009, 08:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 17 Dec 2009, 08:38
Code:
push myDisplay
call glutDisplayFunc    
Post 17 Dec 2009, 08:38
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 843
Location: Jakarta, Indonesia
TmX 17 Dec 2009, 14:57
Ah I see.

So here's my another attempt:
Code:
format PE GUI 4.0

entry start

include 'win32ax.inc'
include 'opengl.inc'
include 'glut32.inc'
include 'macro\if.inc'

section '.text' code readable executable
start:
  ;glutInit(&argc, argv);
 ;invoke glutInitDisplayMode, GLUT_DOUBLE | GLUT_RGB
 invoke glutInitWindowPosition,100,100
       invoke glutCreateWindow,"My Animation"
    push myDisplay
      call glutDisplayFunc
        push myReshape
      call glutReshapeFunc
        push myMouse
        call glutMouseFunc
  call myInit
 invoke glutMainLoop
 
myInit:
     invoke glClearColor,0.0,0.0,0.0,0.0
 
myDisplay:
  invoke glClear,GL_COLOR_BUFFER_BIT
  invoke glPushMatrix
 invoke glTranslated,0.0,20.0,0.0
    invoke glRotatef,[spin],0.0,0.0,1.0
 invoke glColor3f,1.0,0.0,0.0
        invoke glRectf,-10.0,35.0,10.0,15.0
 invoke glPopMatrix
  invoke glPushMatrix
 invoke glTranslated,0.0,-20.0,0.0
   invoke glRotatef,[spin],0.0,0.0,-1.0
        invoke glColor3f,0.0,1.0,0.0
        invoke glRectf,-10.0,-15.0,10.0,-35.0
       invoke glPopMatrix
  invoke glutSwapBuffers
      
spinDisplay:
        add spin, 12.0
      .if (spin > 360.0) 
              add spin,-360.0;
    .endif
      invoke glutPostRedisplay
    
spinCDisplay:
       add spin,-11.0
      .if (spin < 360.0) 
              add spin,360.0
      .endif
      invoke glutPostRedisplay
    
proc myReshape, w,h 
    push ebx esi edi
    invoke glViewport,0,0,w,h
   invoke glMatrixMode,GL_PROJECTION
   invoke glLoadIdentity
       invoke glOrtho,-50.0,50.0,-50.0,50.0,-1.0,1.0
       invoke glMatrixMode,GL_MODELVIEW
    invoke glLoadIdentity
       pop edi esi ebx
     ret
endp

proc myMouse, button, state, x, y
        push ebx esi edi
    .if (button = GLUT_LEFT_BUTTON)
             .if (state = GLUT_DOWN) 
                    push spinDisplay
                    call glutIdleFunc
           .endif
      .endif
      .elseif (button = GLUT_MIDDLE_BUTTON)
               .if (state =GLUT_DOWN) 
                     push NULL       
                    call glutIdleFunc
           .endif
      .endif
      .elseif (button = GLUT_RIGHT_BUTTON) 
               . if (state = GLUT_DOWN) 
                   push spinCDisplay
                   call glutIdleFunc
                   break;
              .endif
      .endif
      pop edi esi ebx
     ret
endp
 
section '.data' data readable writeable
spin GLfloat 0.0
    


Quote:

C:\Codes>fasm gltest.asm
flat assembler version 1.69.10 (1572863 kilobytes memory)
gltest.asm [45]:
add spin, 12.0
error: invalid operand.


I'm sure more error will be revealed.

BTW, I attach the glut32.inc


Description:
Download
Filename: glut32.inc
Filesize: 4.83 KB
Downloaded: 228 Time(s)

Post 17 Dec 2009, 14:57
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 17 Dec 2009, 15:08
You must use FPU or SSE instructions to perform the addition because ADD instruction works with integers only (using just "add [spin], 12" won't do the trick because ADD won't follow the IEEE-754 format).

Example using FPU:
Code:
finit ; use this at the beginning of the program
.
.
.
fld [twelve] ; You can't use constants in FPU instructions Sad
fld [spin]
faddp
fstp [spin]
.
.
.
spin GLfloat 0.0 
twelve GLfloat 12.0    
Post 17 Dec 2009, 15:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 17 Dec 2009, 15:32
Since you are using the macros then you can simply do this:
Code:
invoke glutDisplayFunc,myDisplay    
Post 17 Dec 2009, 15:32
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 20 Dec 2009, 09:44
Hello, TmX
For some functions, like this one:
Code:
invoke glOrtho,-50.0,50.0,-50.0,50.0,-1.0,1.0    

will require 'double floats' for parameters so you'll need to do this:
Code:
invoke glOrtho, double (-50.0), double 50.0, double (-50.0), double 50.0, double (-1.0), double 1.0    

negative values have to be put in parenthesis (-1.0).
Post 20 Dec 2009, 09:44
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 20 Dec 2009, 17:41
Hello
Im glad to see an OpenGL enthusiast!
This was a demo i wrote a while back...
http://board.flatassembler.net/topic.php?t=9262
Maybe it can help you with learning gl and fpu...
And if you are targeting Win32, then drop glut, it sucks...
Post 20 Dec 2009, 17:41
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.