Sign in to follow this  
DarkDemon

OpenGL OpenGL Texture IDs.... help please :(

Recommended Posts

DarkDemon    122
Hello, I'm still rather at a novice level with Opengl and I've been hitting the same problem for awhile now. I have a proprietary 3Ds loader, and framework. Each of the objects in the scene have their own struct with information about it's structure and texture ID. I've been playing around / debugging for awhile and the texture I load is in never binded to the object and I'm not sure why. When I add break points I can see that glBindTexture(GL_TEXTURE_2D, contains the correct address for this texture ); The problem is it's never drawn, the raw texture is loaded and 2d textures are enabled. I'm using a set of arbitrary values for the tex co ords for now to ensure it's correctly displaying. The framework does read in the tex co ords but for the purposes of testing I've no need to use them yet. So the main problem is the texture is never loaded onto the model I select. I'm not entirely sure why either... I would appreciate any assistance. The project can be downloaded here: http://darkdemon.org/gl/spaceships.zip These are the offending classes, Ball.cpp, where objects are drawn and textures for that object are assigned
#include "ball.h"



void Ball::draw(){
    int l_index;
	glPushMatrix();
  	glTranslatef(x,y,z);

  	if (fileloaded){
    glScalef(0.01f,0.01f,0.01f);
    glRotatef(theta,1.0f,0.0f,0.0f);



	//Whether the object has a defined texture
	if(object.texture != NULL)
	{
	  glEnable(GL_TEXTURE_2D);
	  glBindTexture(GL_TEXTURE_2D,object.texture);
	}
	else
	{
      glDisable(GL_TEXTURE_2D);
	}

	glBegin(GL_TRIANGLES);


    for (l_index=0;l_index<object.polygons_qty;l_index++)
	{

        //----------------- FIRST VERTEX -----------------
        //TEX

		glTexCoord2f(-0.0f,0.0f); // -> arbitrary values to test to see if the texture is loading in

		// Coordinates of the first vertex
        glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
                    object.vertex[ object.polygon[l_index].a ].y,
                    object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

        //----------------- SECOND VERTEX -----------------
		//TEX

		glTexCoord2f(1.0f,0.0f); //-> arbitrary values to test to see if the texture is loading in

        // Coordinates of the second vertex
        glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
                    object.vertex[ object.polygon[l_index].b ].y,
                    object.vertex[ object.polygon[l_index].b ].z);
        
        //----------------- THIRD VERTEX -----------------
		//TEX

		glTexCoord2f(1.0f,1.0f); //-> arbitrary values to test to see if the texture is loading in

        // Coordinates of the Third vertex
        glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
                    object.vertex[ object.polygon[l_index].c ].y,
                    object.vertex[ object.polygon[l_index].c ].z);


		 
    }
	glEnd(); 
   
	//glutSwapBuffers();
  }

  glPopMatrix();
}


And Scene.cpp Where new objects are created and the texture id for an object generated
#include "scene.h"
#include "ball.h"

void Scene::initialise(){


    float startarray[4][6]={{9.0f,9.0f,0.0f,0.1f,-0.25f,0.0f},
                            {7.0f,9.0f,0.0f,0.05,-0.05f,0.0f},
                            {3.0f,9.0f,0.0f,0.01f,-0.02f,0.0f},
                            {0.0f,9.0f,0.0f,0.03f,-0.076f,0.0f}
    };
    nBalls=5;
    myBall=new Ball[nBalls];

	for(int i=0;i<nBalls;i++){
		myBall[i].initialise(0.0f,0.0f,0.0f);
	
	}

    for(int i=0;i<nBalls;i++){
        myBall[i].setx(startarray[i][0]);
        myBall[i].sety( startarray[i][1] );
		myBall[i].setz(startarray[i][2]  );

        myBall[i].setvx(startarray[i][3] );
        myBall[i].setvy(startarray[i][4] );
        myBall[i].setvz(startarray[i][5] );
       
		myBall[i].loadfile();
        myBall[i].setangle();
    }
  
	//setup a particular objects texture
	//generate new glutTextureID ready for binding for this object
	myBall[0].loadTexture("blue.raw");


}
 
void Scene::spinballs(){
  for(int i=0;i<nBalls;i++){
      myBall[i].spin();
    }
}

void Scene::moveobjects(){
    for(int i=0;i<nBalls;i++){
      myBall[i].move();
    }
}

void Scene::render(){
  //glColor3f(1.0f,0.0f,0.0f);
//  glTranslatef(x,y,z);
  // glScalef(10.0f,10.0f,10.0f);
  //glutSolidCube(5.0);
  for(int i=0;i<nBalls;i++)
	  myBall[i].draw();

}

void Scene::cdetect(){
   
    for(int i=0;i<nBalls;i++){
      if((myBall[i].getx()<-10.0)||(myBall[i].getx()>10.0))
        myBall[i].setvx(myBall[i].getvx()*-1.0f);

      if((myBall[i].gety()<-10.0)||(myBall[i].gety()>10.0))
        myBall[i].setvy(myBall[i].getvy()*-1.0f);
    }

    for(int i=0;i<nBalls;i++){
	for(int j=i+1;j<nBalls;j++){
	    float tx=myBall[i].getx()-myBall[j].getx();
	    float ty=myBall[i].gety()-myBall[j].gety();
	    float tz=myBall[i].getz()-myBall[j].getz();

            float dist=sqrt(tx*tx+ty*ty+tz*tz);
            if(dist<0.9f){
                float temp=myBall[i].getvx();
                myBall[i].setvx(myBall[j].getvx());
                myBall[j].setvx(temp);
                temp=myBall[i].getvy();
                myBall[i].setvy(myBall[j].getvy());
                myBall[j].setvy(temp);
                temp=myBall[i].getvz();
                myBall[i].setvz(myBall[j].getvz());
                myBall[j].setvz(temp);
	    }
	}
    }
}


[Edited by - DarkDemon on December 16, 2008 8:40:15 AM]

Share this post


Link to post
Share on other sites
David_pb    1046
You made a few mistakes. First you need a valid render context when creating your textures. The second thing is, your data is stored as 24 bit RGB. So you need to change the lines:


unsigned char * data = (unsigned char*) malloc(512*512*3); // <--
fread(data,1,512*512*3,infile); // <-- raw file contains RGB data, not RGBA
fclose(infile);

// ...

glTexImage2D(GL_TEXTURE_2D,0,3,512,512,0,GL_RGB,GL_UNSIGNED_BYTE,data); // <-- changed 3 and GL_RGB


And one last thing. This'll not work...

if(object.texture != NULL)


...because most of your objects texturemembers aren't initialized correctly.

Share this post


Link to post
Share on other sites
DarkDemon    122
Thanks David,

I have made those revisions so I can only assume the problem now is with the render context as the texture still doesn't get drawn, I'm not too sure on these though.. I assume that was setup in the viewer class...

Revised source

#include "ball.h"



void Ball::draw(){
int l_index;
glPushMatrix();
glTranslatef(x,y,z);

if (fileloaded){
glScalef(0.01f,0.01f,0.01f);
glRotatef(theta,1.0f,0.0f,0.0f);


glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,object.texture);

glBegin(GL_TRIANGLES);


for (l_index=0;l_index<object.polygons_qty;l_index++)
{

//----------------- FIRST VERTEX -----------------
//TEX

glTexCoord2f(0.0f,1.0f); // -> arbitrary values to test to see if the texture is loading in

// Coordinates of the first vertex
glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
object.vertex[ object.polygon[l_index].a ].y,
object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

//----------------- SECOND VERTEX -----------------
//TEX

glTexCoord2f(1.0f,1.0f); //-> arbitrary values to test to see if the texture is loading in

// Coordinates of the second vertex
glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
object.vertex[ object.polygon[l_index].b ].y,
object.vertex[ object.polygon[l_index].b ].z);

//----------------- THIRD VERTEX -----------------
//TEX

glTexCoord2f(0.0f,1.0f); //-> arbitrary values to test to see if the texture is loading in

// Coordinates of the Third vertex
glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
object.vertex[ object.polygon[l_index].c ].y,
object.vertex[ object.polygon[l_index].c ].z);
}
glEnd();

}

glPopMatrix();
}






Viewer class

#include "viewer.h"


Viewer::Viewer(){


WinName="";
BufType=GLUT_SINGLE;

for(int i=0;i<3;i++){
background[i]=0.0f;
}

}

Viewer::~Viewer(){
return;
}

void Viewer::Init(int argc,char **argv){

eye[0]=0.0f;
eye[1]=0.5;
eye[2]=25.0;

aim[0]=0.0f;
aim[1]=0.0f;
aim[2]=0.0f;

upright[0]=0.0f;
upright[1]=1.0f;
upright[2]=0.0f;

glutInit(&argc,argv);

}

void Viewer::CreateWin(char *Name,int Width,int Height){
WinName=Name;
WinWidth=Width;
WinHeight=Height;
}

void Viewer::SetValue(myEnum PName,myEnum Type){

switch(PName){
case BUFFER:
if(Type==MDOUBLE)
BufType=GLUT_DOUBLE;
else
if(Type==SINGLE)
BufType=GLUT_SINGLE;
break;
case BACKCOLOUR:
break;
default:
break;
}
}


void Viewer::InitWorld(){
vrman=new WorldManager;
vrman->initialise();

}
void Viewer::InitCamera(){
mycamera=new camera(PERSPECTIVE);
mycamera->SetValue(MNEAR,5.0f);
mycamera->SetValue(YANGLE,45.0f);


mycamera->SetValuev(AIMAT, aim);
mycamera->SetValuev(UPDIRECTION, upright);
mycamera->SetValuev(POSITION, eye);

mycamera->SetValue(MFAR,33.0f);
mycamera->SetValue(HEIGHT,WinHeight);
mycamera->SetValue(ASPECT,1.0f);
}

void Viewer::SetCamera(float nvalue,float viewangle){
mycamera->SetValue(MNEAR, nvalue);
mycamera->SetValue(YANGLE, viewangle);

}

void Viewer::Show(){

GLInit();
glutMainLoop();

}


void Viewer::GLInit(){


glutInitDisplayMode(BufType |GLUT_RGB |GLUT_DEPTH);
glutInitWindowSize(WinWidth, WinHeight);
glutCreateWindow(WinName);
glutReshapeFunc(Reshape);
glutDisplayFunc(Display);
glutMouseFunc(Mouse);
glutIdleFunc(Idle);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(SpecialKey);
// glEnable(GL_DEPTH_TEST);
glClearColor(0.0f,0.0f,0.0f,1.0f);

}


void Viewer::Display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

vrman->exec_loop();

glFlush();
if(BufType==GLUT_DOUBLE){

glutSwapBuffers();
}


}

void Viewer::Reshape(int w,int h){

glViewport(0,0,(GLsizei)w,(GLsizei)h);
WinWidth=w;
WinHeight=h;
mycamera->SetValuev(POSITION,eye);
mycamera->SetValuev(AIMAT,aim);
mycamera->Render();

}

void Viewer::Mouse(int button,int state,int x,int y){

vrman->mouse(button,state,x,y);
Display();
}

void Viewer::Idle(){
vrman->idle();
Display();
}

void Viewer::Keyboard(unsigned char key,int x,int y){

switch(key){
case 0x14b:
case GLUT_KEY_UP:
eye[2]-=0.5;
aim[2]-=0.5;
break;
case GLUT_KEY_DOWN:
eye[2]+=0.5;
aim[2]+=0.5;
break;
case GLUT_KEY_LEFT:
eye[0]-=0.5;
aim[0]-=0.5;
break;
case GLUT_KEY_RIGHT:
eye[0]+=0.5;
aim[0]+=0.5;
break;
default:
vrman->keyboard((unsigned char)key,x,y);
break;
}

Display();
}

void Viewer::SpecialKey(int key, int x, int y){
switch(key){
case GLUT_KEY_F1:
upright[0]=0.0f;
upright[1]=0.0f;
upright[2]=-1.0f;
aim[0]=0.0f;
aim[1]=0.0f;
aim[2]=0.0f;
eye[0]=0.0f;
eye[1]=25.0f;
eye[2]=0.0f;
break;
case GLUT_KEY_F2:
upright[0]=0.0f;
upright[1]=1.0f;
upright[2]=0.0f;
aim[0]=0.0f;
aim[1]=0.0f;
aim[2]=0.0f;
eye[0]=0.0f;
eye[1]=0.5f;
eye[2]=25.0f;
break;
case GLUT_KEY_F12:
vrman->restart();
break;
case GLUT_KEY_UP:
eye[2]-=0.5;
aim[2]-=0.5;
break;
case GLUT_KEY_DOWN:
eye[2]+=0.5;
aim[2]+=0.5;
break;
case GLUT_KEY_LEFT:
eye[0]-=0.5;
aim[0]-=0.5;
break;
case GLUT_KEY_RIGHT:
eye[0]+=0.5;
aim[0]+=0.5;
break;
case GLUT_KEY_PAGE_UP:
eye[1]+=0.5;
aim[1]+=0.5;
break;
case GLUT_KEY_PAGE_DOWN:
eye[1]-=0.5;
aim[1]-=0.5;
break;
default:
vrman->keyboard((unsigned char)key,x,y);
break;
}
mycamera->SetValuev(UPDIRECTION,upright);
mycamera->SetValuev(POSITION,eye);
mycamera->SetValuev(AIMAT,aim);
mycamera->Render();
Display();
}





[Edited by - DarkDemon on December 16, 2008 8:59:39 AM]

Share this post


Link to post
Share on other sites
DarkDemon    122
Thanks David,

I'm taking some guesses here, I tried this

http://msdn.microsoft.com/en-us/library/ms537549(VS.85).aspx


Viewer.cpp

void Viewer::Show(){

// create a rendering context
hglrc = wglCreateContext (hdc);

// make it the calling thread's current rendering context
wglMakeCurrent (hdc, hglrc);

GLInit();
glutMainLoop();
}




Still no texture is rendered, I'm probably doing it complely wrong I also read this http://www.nullterminator.net/opengl32.html but I couldn't implement it without messing it up. I've been unable to find any clear simple way to construct one.

Share this post


Link to post
Share on other sites
DarkDemon    122
Thanks David,

I've tried to move it.. still no luck.


Test.cpp
I tried moving ->Show which calls glutInt and the update but I got memory errors.


Viewer *myViewer=new Viewer;
myViewer->Init(argc,argv);
myViewer->InitWorld();
myViewer->SetValue(BUFFER,MDOUBLE);
myViewer->CreateWin("Further Programming 3D",1000,800);
myViewer->InitCamera();
myViewer->Show();







Viewer.cpp I got rid of the GLInit call from the show method, and moved it to InitWorld before everything is created. I get no errors but everything moves slow, and flashes.....

void Viewer::InitWorld(){
GLInit();
vrman=new WorldManager;
vrman->initialise();

}





I've also tried moving myViewer->InitWorld(); below myViewer->Show(); again I get memory problems.


I've also made GLInt public and called it like this


Viewer *myViewer=new Viewer;
myViewer->Init(argc,argv);

myViewer->SetValue(BUFFER,MDOUBLE);
myViewer->CreateWin("Further Programming 3D",1000,800);
myViewer->GLInit();
myViewer->InitWorld();
myViewer->InitCamera();
myViewer->Show();



Everything loaded correctly, but still no texture.

Share this post


Link to post
Share on other sites
David_pb    1046
The flashes are probably becauses you just use a single buffer. Ok... let's see what you can do:


Viewer *myViewer=new Viewer;
myViewer->Init(argc,argv);
myViewer->SetValue(BUFFER,MDOUBLE);
myViewer->CreateWin("Further Programming 3D Apps Assignment: By Adam Davis",1000,800);
myViewer->InitWorld(); // <-- Init the world AFTER creating the window + gl initialization
myViewer->InitCamera();

yViewer->Show();




void Viewer::CreateWin(char *Name,int Width,int Height){
WinName=Name;
WinWidth=Width;
WinHeight=Height;
GLInit(); // <-- Initialize GL after the windows properties are set
}




void Viewer::Show(){

//GLInit(); // <-- Don't initialize GL twice
glutMainLoop();

}



And the other stuff I mentioned before. Have fun! :)

Share this post


Link to post
Share on other sites
DarkDemon    122
Thanks David for the help,

That stopped the flicker and the models move fine. Sadly though that annoying issue of the texture not being applied at all seems apparent.

As a test in scene.cpp I call

myBall[0].loadTexture("blue.raw");

This should map the first object with a blue image but it doesn't work.

I did some debugging by adding break points to ensure that the image does get loaded. I also tested to ensure that inside draw glBindTexture(GL_TEXTURE_2D,object.texture); contains the correct address for the image, and it does which is something I can't understand. Everything is loaded in but it doesn't appear to be applied, I know the text co ords are just arb values but I would aspect to see least some of the blue from the image on the object.

- Again thanks again for the help it's been like hitting a brick wall.


#include "ball.h"



void Ball::draw(){
int l_index;
glPushMatrix();
glTranslatef(x,y,z);

if (fileloaded){
glScalef(0.01f,0.01f,0.01f);
glRotatef(theta,1.0f,0.0f,0.0f);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,object.texture);
glBegin(GL_TRIANGLES);


for (l_index=0;l_index<object.polygons_qty;l_index++)
{

//----------------- FIRST VERTEX -----------------
//TEX

glTexCoord2f(0.0f,1.0f); // -> arbitrary values to test to see if the texture is loading in

// Coordinates of the first vertex
glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
object.vertex[ object.polygon[l_index].a ].y,
object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

//----------------- SECOND VERTEX -----------------
//TEX

glTexCoord2f(1.0f,1.0f); //-> arbitrary values to test to see if the texture is loading in

// Coordinates of the second vertex
glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
object.vertex[ object.polygon[l_index].b ].y,
object.vertex[ object.polygon[l_index].b ].z);

//----------------- THIRD VERTEX -----------------
//TEX

glTexCoord2f(0.0f,1.0f); //-> arbitrary values to test to see if the texture is loading in

// Coordinates of the Third vertex
glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
object.vertex[ object.polygon[l_index].c ].y,
object.vertex[ object.polygon[l_index].c ].z);
}
glEnd();

}

glPopMatrix();
}





[Edited by - DarkDemon on December 16, 2008 8:52:05 AM]

Share this post


Link to post
Share on other sites
David_pb    1046
You forgot to set the minifying function. The GL expects GL_LINEAR_MIPMAP_NEAREST as default, and you end up with an uncomplete textureobject.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now