Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Switching from 2D to 3D for drawing object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 29 December 2011 - 12:42 PM

So for my inventory system, i want users to be able to see what the item looks like in the game. i tried using:

glMatrixMode(GL_PROJECTION);
			    glMatrixMode(GL_MODELVIEW);
			    glDepthFunc(GL_LEQUAL);
			    glDepthMask(GL_TRUE);
			    glClear(GL_DEPTH_BUFFER_BIT);

			    MainScreen.ShowItem3D.rots[0].ry=MainScreen.ShowRotY;
			    MainScreen.ShowItem3D.Draw(0,0,0);

                glMatrixMode(GL_PROJECTION);
			    glLoadIdentity();
			    glOrtho(0.0, ScreenW, ScreenH, 0.0, -1.0, 10.0);
			    glMatrixMode(GL_MODELVIEW);
			    glDisable(GL_CULL_FACE);
			    glClear(GL_DEPTH_BUFFER_BIT);
			    glEnable(GL_TEXTURE_2D);

Which the .Draw() would work where im usually drawing the in-game stuff, but for some reason it wont draw here. What might i be doing wrong?

Sponsor:

#2 Alejandro   Members   -  Reputation: 142

Like
0Likes
Like

Posted 29 December 2011 - 03:48 PM

Hi,

A few weeks ago I was messing with the same stuff, you can get a working example in here(sorry for all the messy code, hope it's useful). Basically your display function will look something like this.

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// you can get the width and height using glutGet(GLUT_WINDOW_WIDTH) or HEIGHT
height = height==0 ? 1 : height;
float ratio = width * 1.0 / height;
// dibujado en 3D
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, ratio, .1f, 1000.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

gluLookAt( eyex, eyex, eyez,
    centerx, centery, centerz,
    upx, upy, upz );
glPushMatrix();
{
  // draw 3D things in here
}
glPopMatrix();
// 2D drawing
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
{
  // draw 2D things in here
}
glPopMatrix();
glutSwapBuffers();
}


#3 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 29 December 2011 - 06:08 PM

Actually, i know what i need to do now. its the 2D Quads that get in the way. im trying to draw the 3D models, but the GUI quads are in front of them. How would i draw the depth-tested model infront of the quads?

#4 rdragon1   Crossbones+   -  Reputation: 1200

Like
0Likes
Like

Posted 29 December 2011 - 06:12 PM

You could draw the model to a texture, and then render that texture as a 2D quad in your GUI. Or you could position the model so it's actually in front of the quad.

#5 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 29 December 2011 - 06:20 PM

How would i draw it to a quad then draw that texture? i think it would be easier to position it, if somehow the gui were to become just a color buffer, and the model uses both color and z buffer...

#6 rdragon1   Crossbones+   -  Reputation: 1200

Like
0Likes
Like

Posted 29 December 2011 - 06:50 PM

Research 'render to texture' - the idea is simple: create a render target, draw to it, then bind that target as a texture when drawing to your main render target

#7 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 29 December 2011 - 07:34 PM

I dont think the idea needs to be that complicated. i just need to have the gui become a color only buffer so i can use the depth buffer for the model.

#8 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 29 December 2011 - 11:18 PM

unless you can post some code on how to do that, then i will try to do that. :P

#9 rdragon1   Crossbones+   -  Reputation: 1200

Like
0Likes
Like

Posted 29 December 2011 - 11:32 PM

Can you elaborate on what this means?
"

i just need to have the gui become a color only buffer"




What is it, if not a color buffer?



opengl render to texture (I don't see how this is "that complicated" - it's just simple compositing):

http://www.opengl.or...ebuffer_Objects

#10 gdmarkou   Members   -  Reputation: 332

Like
0Likes
Like

Posted 30 December 2011 - 03:29 AM

i just need to have the gui become a color only buffer so i can use the depth buffer for the model.


Then just disable depth write while drawing gui ( glDepthMask(GL_FALSE); )

#11 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 30 December 2011 - 10:11 AM

i already tried glDepthMask(GL_FALSE); and it still doesnt works.... i tested the model and how it dissapears, it moves backwards, where the gui isnt even there, and looks like something flat is covering it up... weird... ill post most of the code here, alot of other stuff will be removed.

void myApp::Pause()
{
    MainScreen.LoadScreen("pause");
    myApp::Playing=false;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, myApp::Screenw, myApp::Screenh, 0.0, 0, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0,0,0,0);
    glPushAttrib(GL_ENABLE_BIT);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glDisable(GL_CULL_FACE);
    glDepthMask(GL_FALSE);
    SetCursorPos(myApp::MouseX, myApp::MouseY);
}

//ABOVE IS CALLED BEFORE SWITCHING TO USE THE FUNCTION
#include "Functions.h"
extern DrawFunctions DrawFunc;
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "App.h"
extern myApp APP;
#include "ImageHolder.h"
extern ImageHolder Images;
#include "ScreenClass.h"
extern Screen MainScreen;
#include "GameStats.h"
extern GameStats Game;
void ScreenDraw()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glClearColor(0,0,0,0);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glDepthMask(GL_FALSE);
    glPushMatrix();
    const int ScreenW=APP.Screenw,ScreenH=APP.Screenh;
    glBindTexture(GL_TEXTURE_2D, Images.SCREEN_BACKGROUND);
    glBegin(GL_QUADS);
	    glTexCoord2f(0, 0);glVertex2f(ScreenW/2-320,ScreenH/2-240);
	    glTexCoord2f(1, 0);glVertex2f(ScreenW/2+320,ScreenH/2-240);
	    glTexCoord2f(1, 1);glVertex2f(ScreenW/2+320,ScreenH/2+240);
	    glTexCoord2f(0, 1);glVertex2f(ScreenW/2-320,ScreenH/2+240);
    glEnd();
  
    if(Game.BACKPACK)
    {
	    if(MainScreen.HoldingItem)
	    {
		    float sAd=1.0f/8.0f;
		    POINT Sh;
		    if(MainScreen.Show3DItem && MainScreen.invholding)
		    {
			    glPopMatrix();
			    glDepthMask(GL_TRUE);
			    glClear(GL_DEPTH_BUFFER_BIT);
			    glEnable(GL_DEPTH_TEST);
			    glMatrixMode(GL_PROJECTION);
			    glLoadIdentity();
			    glMatrixMode(GL_MODELVIEW);
			    gluPerspective(45.0f,APP.Screenw * (1.0f/APP.Screenh),0.1,100.0f);
			    glLoadIdentity();
			    glPushMatrix();
			    glRotatef(180,0,0,1);
			    glRotatef(-45,0,1,0);
			    //glTranslatef(0,0,)
			    //if(MainScreen.ShowItem3D.rots.size()!=0){MainScreen.ShowItem3D.rots[0].ry=MainScreen.ShowRotY;}
			    MainScreen.ShowItem3D.Draw(MainScreen.ShowRotY*0.01f,-3,0);
			    glRotatef(-45,0,1,0);
			    glPopMatrix();
			    glMatrixMode(GL_PROJECTION);
			    glLoadIdentity();
			    glOrtho(0.0, ScreenW, ScreenH, 0.0, 0, 100.0);
			    glMatrixMode(GL_MODELVIEW);
			    glLoadIdentity();
			    glDisable(GL_CULL_FACE);glDisable(GL_DEPTH_TEST);
			    glClear(GL_DEPTH_BUFFER_BIT);
			    glEnable(GL_TEXTURE_2D);
			    glDepthMask(GL_FALSE);
			    glLoadIdentity();
			    glPushMatrix();
		    }
		    else
		    {
			    std::vector<Attribute>nAtts;
			    if(MainScreen.invholding){nAtts=Game.myStuff.Holder[MainScreen.ItemHolding.x][MainScreen.ItemHolding.y].Atts;}
			    else{nAtts=Game.myStuff.Hotbar[MainScreen.ItemHolding.x].Atts;}
		    }
	    }
    }
    glPopMatrix();
    glFlush();
}


#12 Alejandro   Members   -  Reputation: 142

Like
0Likes
Like

Posted 30 December 2011 - 02:08 PM

You could try using fbo's, here's a good tutorial with a working example

#13 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 30 December 2011 - 03:12 PM

Well id rather use the easiest way first, and im sure theres a error i made in drawing stuff over the gui. if anyone could point it out if they can find it, that would be nice. if not, i dont think i will do this just yet...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS