flat assembler
Message board for the users of flat assembler.

flat assembler > Examples and Tutorials > can someone provide a simple opengl shader tutorial

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
mikokawasaki



Joined: 28 Nov 2012
Posts: 12
(*noob) can someone provide a simple opengl shader tutorial Sad
Post 28 May 2013, 10:56
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2792
Location: dank orb
OpenGL is taught in the schools because it also works on Linux. There is a ton of GLSL stuff out there on the web. Most demo coders use OpenGL it seems.

I'm sticking with DirectX though.
Post 28 May 2013, 12:48
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4203
Location: 2018
Post 28 May 2013, 15:12
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1181
Location: Unknown
Stupid post removed.


Last edited by HaHaAnonymous on 28 Feb 2015, 20:17; edited 1 time in total
Post 28 May 2013, 20:22
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2792
Location: dank orb
HaHaAnonymous wrote:
Quote:

OpenGL is taught in the schools because it also works on Linux.

No, it is because OpenGL is decent (unlike DirectX). And secondly, also works on many other systems (unlike DirectX).
Sorry, that is not my political position. Decency because it's more flexible with hardware requirements, or separates the graphics API from the OS? It would be nice to read something objective comparing the most current APIs either in terms of performance or functionality.

_________________
¯\(°_o)/¯ unlicense.org
Post 28 May 2013, 20:55
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1181
Location: Unknown
Stupid post removed.


Last edited by HaHaAnonymous on 28 Feb 2015, 20:17; edited 1 time in total
Post 28 May 2013, 21:40
View user's profile Send private message Reply with quote
mikokawasaki



Joined: 28 Nov 2012
Posts: 12
bitRAKE why do you feel so offended by openGL...
i dont care if its taught in schools or not..
i came here for help, not for someone to tell me to get lost...

im only new to assembley... i understand a little bit though..

i cant get access to functions like glcreateshader for my project thats why im asking for help..
Post 29 May 2013, 12:23
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2792
Location: dank orb
I suggest to search for GLSL and demo scene. All the other bits you assume yourself. No offense is meant to anyone.
Post 29 May 2013, 23:55
View user's profile Send private message Visit poster's website Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
Here is my Mandelbrot OpenGL renderer on Linux:
http://board.flatassembler.net/topic.php?t=14100

I am not using GLSL but NVIDIA assembly shaders. Maybe it will be helpful.
Post 30 May 2013, 10:32
View user's profile Send private message Visit poster's website Reply with quote
mikokawasaki



Joined: 28 Nov 2012
Posts: 12
the code is really long so i'll give you the highlight of my problem....
this code runs fine but the shader part is not working...

format PE64 GUI 5.0
entry start

include 'compiler\include\win64ax.inc'
include 'compiler\include\opengl.inc'

====================================

invoke wglGetProcAddress,"glCreateShader"
mov [glCreateShader],rax
invoke wglGetProcAddress,"glCreateProgram"
mov [glCreateProgram],rax
invoke wglGetProcAddress,"glShaderSource"
mov [glShaderSource],rax
invoke wglGetProcAddress,"glCompileShader"
mov [glCompileShader],rax
invoke wglGetProcAddress,"glAttachShader"
mov [glAttachShader],rax
invoke wglGetProcAddress,"glLinkProgram"
mov [glLinkProgram],rax
invoke wglGetProcAddress,"glUseProgram"
mov [glUseProgram],rax


invoke glCreateShader, GL_FRAGMENT_SHADER
mov [vs],rax
invoke glCreateProgram
mov [prg],rax
invoke glShaderSource,vs,1,[_fshader],0
invoke glCompileShader,vs
invoke glAttachShader,prg,vs
invoke glLinkProgram,prg
invoke glUseProgram,prg




================================



;glGenBuffers dq ?
;glCreateShader dq ?
;glCreateProgram dq ?
;glShaderSource dq ?
;glCompileShader dq ?
;glAttachShader dq ?
;glLinkProgram dq ?
;glUseProgram dq ?
;vs dq ?
;prg dq ?


_fshader db 'void main(){ gl_FragColor = vec4(0.0,0.0,1.0,1.0); } ',0
Post 02 Jun 2013, 12:57
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 721
If you get something working in windows please post it here. Smile
Post 02 Jun 2013, 23:07
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
bitRAKE wrote:
I suggest to search for GLSL and demo scene. All the other bits you assume yourself. No offense is meant to anyone.

Hey aren't you the one that made an FPS world in Assembly using OpenGL ?

I remember sometime back someone made one. Smile
Post 03 Jun 2013, 04:11
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 721
Yes, he was, but that didn't make use of custom shaders. I'm sure that it was textures with the ordinary opengl pipeline.
Post 03 Jun 2013, 04:41
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2792
Location: dank orb
Nah, you are probably thinking about r22,
http://board.flatassembler.net/topic.php?t=8381

I read a book about OpenGL back in the 1.0 days, but have never coded anything using it. If someone would have listened to me and bought Pixar stock back then...

_________________
¯\(°_o)/¯ unlicense.org
Post 03 Jun 2013, 06:45
View user's profile Send private message Visit poster's website Reply with quote
dancho



Joined: 06 Mar 2011
Posts: 74
modern opengl 3.3 ver using vertex and fragment shader in C ,
but you will get idea behind it...
using :
1.) glfw opengl framework
http://www.glfw.org/
2.) glew extension
http://glew.sourceforge.net/

main.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <GL/glew.h>
#include <GL/glfw.h>

#include "load_shaders.h"

static const float gVertexBufferData[] = {
        -1.0f,-1.0f,0.0f,
         1.0f,-1.0f,0.0f,
         0.0f, 1.0f,0.0f
};

int main(int argc,char **argv) {
        
        if(glfwInit()==GL_FALSE) {
                fprintf(stderr,"Failed to initialze GLFW");
                return -1;
        }
        
        glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR,3);
        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR,3);
        glfwOpenWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);

        if(glfwOpenWindow(800,600,0,0,0,0,32,0,GLFW_WINDOW)==GL_FALSE) {
                fprintf(stderr,"Failed to open GLFW window");
                glfwTerminate();
                return -1;
        }

        glewExperimental = true;
        if(glewInit()!=GLEW_OK) {
                fprintf(stderr,"Failed to initialize Glew");
                glfwTerminate();
                return -1;
        }

        glfwSetWindowTitle("Simple Shader");

        glfwEnable(GLFW_STICKY_KEYS);

        glClearColor(0.0f,0.0f,0.4f,0.0f);

        // create vertex array object
        GLuint vertexArrayObj;
        glGenVertexArrays(1,&vertexArrayObj);
        glBindVertexArray(vertexArrayObj);

        // create vertex buffer object
        GLuint vertexBufferObj;
        glGenBuffers(1,&vertexBufferObj);
        glBindBuffer(GL_ARRAY_BUFFER,vertexBufferObj);
        glBufferData(GL_ARRAY_BUFFER,sizeof(gVertexBufferData),gVertexBufferData,GL_STATIC_DRAW);
        
        GLuint programID;
        if((programID=LoadShaders("SimpleVertexShader.vertexshader","SimpleFragmentShader.fragmentshader"))==0) {
                fprintf(stderr,"Error at LoadShader func");
                return -1;
        }

        do {
                glClear(GL_COLOR_BUFFER_BIT);
                
                glUseProgram(programID);

                glEnableVertexAttribArray(0);
                glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
                glDrawArrays(GL_TRIANGLES,0,3);
                glDisableVertexAttribArray(0);

                glfwSwapBuffers();
        }
        while(glfwGetKey(GLFW_KEY_ESC)!=GLFW_PRESS && glfwGetWindowParam(GLFW_OPENED));


        glfwTerminate();

        glDeleteBuffers(1,&vertexBufferObj);
        glDeleteVertexArrays(1,&vertexArrayObj);

        return 0;
}

    


load_shaders.h
Code:
#ifndef LOADSHADERS
#define LOADSHADERS

extern GLuint LoadShaders(const char *vertexShaderFile,const char *fragmentShaderFile);

#endif

    


load_shaders.c

Code:

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h>
#include <GL/glfw.h>

#include "load_shaders.h"

static char * ReadShaderFile(const char *fileName) {
        FILE *shaderStream = NULL;

        if((shaderStream = fopen(fileName,"r"))==NULL) {
                fprintf(stderr,"Can't open shader file");
                return NULL;
        }
        long int pos = ftell(shaderStream);
        fseek(shaderStream,pos,SEEK_END);
        long int len = ftell(shaderStream);
        fseek(shaderStream,pos,SEEK_SET);
        
        char *shaderCode = NULL;
        if((shaderCode = malloc(len*sizeof(char)))==NULL) {
                fprintf(stderr,"Can't allocate memory for the shader code");
                fclose(shaderStream);
                return NULL;
        }
        
        unsigned int i = 0;
        char c;
        while((c = (char)fgetc(shaderStream))!=EOF) {
                shaderCode[i] = c;
                i++;
        }
        shaderCode[i] = 0;
        
        fclose(shaderStream);

        return shaderCode;
}

GLuint LoadShaders(const char *vertexShaderFile,const char *fragmentShaderFile) {
        
        // read vertex shader
        char *vertexShaderCode = NULL;
        if((vertexShaderCode = (ReadShaderFile(vertexShaderFile)))==NULL) {
                fprintf(stderr,"Can't read vertex shader file");
                return 0;
        }

        // read fragment shader
        char *fragmentShaderCode = NULL;
        if((fragmentShaderCode = (ReadShaderFile(fragmentShaderFile)))==NULL) {
                fprintf(stderr,"Can't read fragment shader file");
                return 0;
        }
        
        char const *vertexPtr = vertexShaderCode;
        // compile vertex shader
        printf("Compiling shader : %s\n",vertexShaderFile);
        GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShaderID,1,&vertexPtr,NULL);
        glCompileShader(vertexShaderID);
        
        GLint result = GL_FALSE;
        // check vertex shader
        glGetShaderiv(vertexShaderID,GL_COMPILE_STATUS,&result);
        if(result==GL_FALSE) {
                fprintf(stderr,"Error compiling vertex shader\n");
                free(vertexShaderCode);
                return 0;
        }
        
        char const *fragmentPtr = fragmentShaderCode;
        // compile fragment shader
        printf("Compiling shader : %s\n",fragmentShaderFile);
        GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShaderID,1,&fragmentPtr,NULL);
        glCompileShader(fragmentShaderID);

        // check fragment shader
        glGetShaderiv(fragmentShaderID,GL_COMPILE_STATUS,&result);
        if(result==GL_FALSE) {
                fprintf(stderr,"Error compiling fragment shader\n");
                free(fragmentShaderCode);
                return 0;
        }

        // linking program
        printf("Linking program\n");
        GLuint programID = glCreateProgram();
        glAttachShader(programID,vertexShaderID);
        glAttachShader(programID,fragmentShaderID);
        glLinkProgram(programID);
    
        // check program
        glGetProgramiv(programID,GL_LINK_STATUS,&result);
        if(result==GL_FALSE) {
                fprintf(stderr,"Error linkinig shaders\n");
                free(vertexShaderCode);
                free(fragmentShaderCode);
                glDeleteShader(vertexShaderID);
                glDeleteShader(fragmentShaderID);
                return 0;
        }
        
        free(vertexShaderCode);
        free(fragmentShaderCode);

        glDeleteShader(vertexShaderID);
        glDeleteShader(fragmentShaderID);
        
        return programID;
}

    


SimpleVertexShader.vertexshader

Code:

#version 330 core

// Input vertex data
layout(location = 0) in vec3 vertexPosition_modelspace;

void main(){

    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;

}

    


SimpleFragmentShader.fragmentshader

Code:

#version 330 core

// Ouput data
out vec3 color;

void main()
{

        // Output color = red 
        color = vec3(1,0,0);

}

    
Post 03 Jun 2013, 08:05
View user's profile Send private message Reply with quote
mikokawasaki



Joined: 28 Nov 2012
Posts: 12
tthsqe, ill definitally post it if i can get it to work...
im guessing its the _fshader declaration type part idk...
Post 04 Jun 2013, 03:39
View user's profile Send private message Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
Third parameter to glShaderSource is a pointer to pointer so in fasm in 64bits it must be like this:

align 1
shader_source db '<shader source>',0

align 8
shader_source_ptr dq shader_source

invoke glShaderSource,vs,1,shader_source_ptr,0
Post 04 Jun 2013, 07:37
View user's profile Send private message Visit poster's website Reply with quote
mikokawasaki



Joined: 28 Nov 2012
Posts: 12
...


Last edited by mikokawasaki on 04 Jun 2013, 10:46; edited 4 times in total
Post 04 Jun 2013, 10:08
View user's profile Send private message Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
I don't know invoke macro but all these :

Code:
invoke glCompileShader,vs
invoke glAttachShader,prg,vs
invoke glLinkProgram,prg
invoke glUseProgram,prg 
    


Should be:

Code:
invoke glCompileShader,[vs]
invoke glAttachShader,[prg],[vs]
invoke glLinkProgram,[prg]
invoke glUseProgram,[prg]
    


OpenGL returns integer values that are handles to the resources.
Post 04 Jun 2013, 10:29
View user's profile Send private message Visit poster's website Reply with quote
mikokawasaki



Joined: 28 Nov 2012
Posts: 12
I got it working now....

im posting for anyone interested...

thank you randall for "pointing" that out...
i learned something new...

heres the working shader code thing im using...


Code:
invoke wglGetProcAddress,"glCreateShader"
mov [glCreateShader],rax
invoke wglGetProcAddress,"glCreateProgram"
mov [glCreateProgram],rax
invoke wglGetProcAddress,"glShaderSource"
mov [glShaderSource],rax
invoke wglGetProcAddress,"glCompileShader"
mov [glCompileShader],rax
invoke wglGetProcAddress,"glAttachShader" 
mov [glAttachShader],rax
invoke wglGetProcAddress,"glLinkProgram" 
mov [glLinkProgram],rax
invoke wglGetProcAddress,"glUseProgram"
mov [glUseProgram],rax



invoke glCreateShader, GL_FRAGMENT_SHADER
mov [vs],rax
invoke glCreateProgram
mov [prg],rax
invoke glShaderSource,[vs],1,_fshader_ptr,0
invoke glCompileShader,[vs]
invoke glAttachShader,[prg],[vs]
invoke glLinkProgram,[prg]
invoke glUseProgram,[prg]

...


glGenBuffers dq ?
glCreateShader dq ?
glCreateProgram dq ?
glShaderSource dq ?
glCompileShader dq ?
glAttachShader dq ?
glLinkProgram dq ?
glUseProgram dq ?
vs dq ?
prg dq ?


  align 1
  _fshader db 'void main(){ gl_FragColor = vec4(0.0,0.0,1.0,1.0); }  ',0   
  align 8 
  _fshader_ptr dq _fshader



    
Post 04 Jun 2013, 10:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2019, Tomasz Grysztar.

Powered by rwasa.