FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Issue with textures not showing

 
Post new topic   Reply to topic     Forum Index -> Derelict
View previous topic :: View next topic  
Author Message
ste3e



Joined: 08 Apr 2012
Posts: 17
Location: New Zealand

PostPosted: Mon Apr 30, 2012 8:58 pm    Post subject: Issue with textures not showing Reply with quote

My apologies aldacron, you must be getting sick of seeing my postings. At least they provide documentation for using Derelict3 as the issues are sorted. Just to scare you, I have a working version of c# mono doing deferred shading that I am converting over to D. Part of the problem with working through the issues is that the c# program is divided into classes, which while making the program coherent to me adds a layer of indirection when trying to post the code that should be working. For this reason I have created a basic c style main and functions (below) program that gets to the point.

Anyway. The problem I am currently having is that the sampler2D is not drawing to the triangle on screen. There are no warnings and everything seems to be fine except for the fact that the image that should appear does not. Is there a check to see if data presumably got by DevIl has been handed off to GL?

Code:
import std.stdio;
import std.string;
import std.conv;
import std.path;
import std.file;
import derelict.sdl2.sdl;
import derelict.devil.il;
import derelict.opengl3.gl3;

pragma(lib, "DerelictUtil.lib");
pragma(lib, "DerelictSDL2.lib");
pragma(lib, "DerelictGL3.lib");
pragma(lib, "DerelictIL.lib");

SDL_Window *win;
SDL_GLContext context;
int w=800, h=600;
bool running=true;
uint shader = 0, vao = 0, tid = 0, colLoc = 0;
int flags=SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_SHOWN;

bool initSDL_GL(){
   if(SDL_Init(SDL_INIT_VIDEO) < 0){
      writefln("Error initializing SDL");
      return false;
   }
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

   win=SDL_CreateWindow("3Doodle", SDL_WINDOWPOS_CENTERED,
   SDL_WINDOWPOS_CENTERED, w, h, flags);
    if(!win){
        writefln("Error creating SDL window");
      SDL_Quit();
      return false;
     }

    context=SDL_GL_CreateContext(win);
    SDL_GL_SetSwapInterval(1);
   
   glClearColor(0.0, 0.0, 0.0, 1.0);
   glViewport(0, 0, w, h);

    DerelictGL3.reload();

    return true;
}


bool initShaders(){
   const string vshader="
   #version 330
   layout(location = 0) in vec3 pos;
   layout(location = 1) in vec2 texCoords;

   out vec2 coords;

   void main(void)
   {
      coords=texCoords.st;

       gl_Position = vec4(pos, 1.0);
   }
   ";
   const string fshader="
   #version 330

   uniform sampler2D colMap;

   in vec2 coords;

   void main(void)
   {
      vec3 col=texture2D(colMap, coords.st).xyz;

       gl_FragColor = vec4((coords.yyx + col), 1.0);//color with coord vals. col has no influence
   }
   ";

   shader=glCreateProgram();
   if(shader == 0){
      writeln("Error: GL did not assigh main shader program id");
      return false;
   }
   int vshad=glCreateShader(GL_VERTEX_SHADER);
   const char *vptr=toStringz(vshader);
   glShaderSource(vshad, 1, &vptr, null);
   glCompileShader(vshad);   
   int status, len;
   glGetShaderiv(vshad, GL_COMPILE_STATUS, &status);
   if(status==GL_FALSE){
      glGetShaderiv(vshad, GL_INFO_LOG_LENGTH, &len);
      char[] error=new char[len];
      glGetShaderInfoLog(vshad, len, null, cast(char*)error);
      writeln(error);
      return false;
   }
   int fshad=glCreateShader(GL_FRAGMENT_SHADER);
   const char *fptr=toStringz(fshader);
   glShaderSource(fshad, 1, &fptr, null);
   glCompileShader(fshad);   
   glGetShaderiv(vshad, GL_COMPILE_STATUS, &status);
   if(status==GL_FALSE){
      glGetShaderiv(fshad, GL_INFO_LOG_LENGTH, &len);
      char[] error=new char[len];
      glGetShaderInfoLog(fshad, len, null, cast(char*)error);
      writeln(error);
      return false;
   }
   glAttachShader(shader, vshad);
   glAttachShader(shader, fshad);
   glLinkProgram(shader);
   glGetShaderiv(shader, GL_LINK_STATUS, &status);
   if(status==GL_FALSE){
      glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
      char[] error=new char[len];
      glGetShaderInfoLog(shader, len, null, cast(char*)error);
      writeln(error);
      return false;
   }


   return true;
}

bool initVAO(){
   uint vbov, vboc;
        //fits in clip space so no projection matrix required
   const float[] v = [   -0.75f, -0.75f, 0.0f,
                  0.75f, 0.75f, 0.0f,
                  -0.75f, 0.75f, 0.0f];
   const float[] c = [   0.0f, 0.0f,
                  1.0f, 1.0f,
                  0.0f, 1.0f];
   glGenVertexArrays(1, &vao);
   assert(vao > 0);

   glBindVertexArray(vao);

   glGenBuffers(1, &vbov);
   assert(vbov > 0);
   glBindBuffer(GL_ARRAY_BUFFER, vbov);
   glBufferData(GL_ARRAY_BUFFER, v.length * GL_FLOAT.sizeof, v.ptr, GL_STATIC_DRAW);
   glEnableVertexAttribArray(0);
   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, null);         
   glBindBuffer(GL_ARRAY_BUFFER, 0);

   glGenBuffers(1, &vboc);
   assert(vboc > 0);
   glBindBuffer(GL_ARRAY_BUFFER, vboc);
   glBufferData(GL_ARRAY_BUFFER, c.length * GL_FLOAT.sizeof, c.ptr, GL_STATIC_DRAW);
   glEnableVertexAttribArray(1);
   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, null);         
   glBindBuffer(GL_ARRAY_BUFFER, 0);

   glBindVertexArray(0);   

   return true;
}

bool initTex(){
   assert(exists("MaoriPartyTransforms.png"));
   ilInit();
   ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
   ilEnable(IL_ORIGIN_SET);
   ilBindImage(0);

   assert(ilLoadImage("MaoriPartyTransforms.png"));
   assert(ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE));

   glGenTextures(1, &tid);
   assert(tid > 0);
   glBindTexture(GL_TEXTURE_2D, tid);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

   glTexImage2D(   GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP),
               ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),
               0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
   glBindTexture(GL_TEXTURE_2D, 0);
   ilDeleteImage(0);

   return true;
}

bool initUniforms(){
   colLoc=glGetUniformLocation(shader, "colMap");
   if(colLoc == -1){writeln("Error: main shader did not assign id to sampler2D colMap"); return false;}

   glUseProgram(shader);
   glUniform1i(colLoc, 0);
   glUseProgram(0);

   return true;
}

int main(){
   try{
        DerelictSDL2.load();
    }catch(Exception e){
        writeln("Error loading SDL2 lib");
      return false;
    }
    try{
        DerelictGL3.load();
    }catch(Exception e){
        writeln("Error loading GL3 lib");
      return false;
    }
   try{
        DerelictIL.load();
    }catch(Exception e){
        writeln("Error loading DevIl image lib ", e);
      return false;
    }

   writeln("Init SDL_GL: ", initSDL_GL());
   writeln("Init shaders: ", initShaders());
   writeln("Init VAO: ", initVAO());
   writeln("Init textures: ", initTex());
   writeln("Init uniforms: ", initUniforms());

   while(running){
      SDL_Event e;
      while(SDL_PollEvent(&e)){
         switch(e.type){
            case SDL_KEYDOWN:
            running=false;
            break;
            default:
            break;
         }
      }
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(shader);      

      glBindVertexArray(vao);

      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, tid);

      glDrawArrays(GL_TRIANGLES, 0, 6);

      glBindTexture(GL_TEXTURE_2D, 0);
      glBindVertexArray(0);
      glUseProgram(0);

      SDL_GL_SwapWindow(win);
   }

   SDL_GL_DeleteContext(context);
   SDL_DestroyWindow(win);
   SDL_Quit();

   return 0;
}
Back to top
View user's profile Send private message
aldacron



Joined: 05 May 2004
Posts: 1322
Location: Seoul, South Korea

PostPosted: Tue May 01, 2012 2:11 am    Post subject: Re: Issue with textures not showing Reply with quote

ste3e wrote:
My apologies aldacron, you must be getting sick of seeing my postings. At least they provide documentation for using Derelict3 as the issues are sorted.


No, not at all. I'm happy to help with any Derelict issues you may have. But, to be honest, this particular problem doesn't appear to be related to Derelict so technically doesn't belong here.

Quote:
Anyway. The problem I am currently having is that the sampler2D is not drawing to the triangle on screen. There are no warnings and everything seems to be fine except for the fact that the image that should appear does not. Is there a check to see if data presumably got by DevIl has been handed off to GL?


It's been years since I used DevIL. I think the last time I bothered with it was when it was still called OpenIL. That said, I don't see that you call ilGenImages anywhere before calling ilBindImage. You are binding to 0, which is the default image and not something you should be doing when loading a new image. And if you read the documentation for ilBindImage, you'll see this:

Quote:
IL_ILLEGAL_OPERATION - ilGenImages was never called beforehand, Image is out of bounds or the image name has been deleted via ilDeleteImages.


And you can know whether or not this error was set by calling ilGetError after ilBindImage. In general, you should be checking for DevIL errors with ilGetError and checking for OpenGL errors with glGetError during development. It will help catch problems like this.
_________________
The One With D | The One With Aldacron | D Bits
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Derelict All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group