Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Fecotourist posted:

2. Does it matter whether
a: the vertices, normals, and texture coordinates are packed into an array of structs
b: there are separate vertex array, normal array, TC array
Both ways are pretty common, and it probably doesn't matter especially unless you already know it matters.


Fecotourist posted:

3. How much does spatial locality matter to performance on modern hardware?
glDrawElements() takes an array of indices, is it faster if the used vertices are
close together in memory than if the indices (uint) jump all over the place?

Why are you worrying about locality when you're operating on sets of 8 verts?

Also: why do you always hit "enter" after a few words? It's really annoying.


Also also: why do people (in general, not just you) always ask for help before they've even started on a project? You'll learn much better if you work on things yourself and maybe consult a book. :confused:

Adbot
ADBOT LOVES YOU

Fecotourist
Nov 1, 2008

Avenging Dentist posted:

Both ways are pretty common, and it probably doesn't matter especially unless you already know it matters.
Thanks, exactly what I was looking for.

Avenging Dentist posted:

Why are you worrying about locality when you're operating on sets of 8 verts?

Let me rephrase the question. Given a big vertex array and two possible index arrays, A and B:
code:
A = { 0, 1, 2, 3, 4, 5, 6, 1}
B = { 995, 50, 50210, 3323, 472, 100205, 3, 50}
Is A faster because the vertices it references are close in memory? Is it worth shuffling the vertex array to make index arrays like A more common? I want to draw lots and lots of these 8-vert primitive sets every frame, as many as can be rendered at a given frame rate.

Avenging Dentist posted:

Also: why do you always hit "enter" after a few words? It's really annoying.

Is it more annoying than a single-line post that is stretched to 50 line equivalents by a big dumb avatar?

Avenging Dentist posted:

Also also: why do people (in general, not just you) always ask for help before they've even started on a project? You'll learn much better if you work on things yourself and maybe consult a book. :confused:

I don't think that's the case here. My application already runs ok using OpenSceneGraph, [which seems to be using the glDrawElements() path the way I've been doing it]. For various reasons, I'm switching from OSG to SDL plus explicit OpenGL. If I'm doing that work, I just want to follow a good path.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Fecotourist posted:

Let me rephrase the question. Given a big vertex array and two possible index arrays, A and B:
code:
A = { 0, 1, 2, 3, 4, 5, 6, 1}
B = { 995, 50, 50210, 3323, 472, 100205, 3, 50}
Is A faster because the vertices it references are close in memory? Is it worth shuffling the vertex array to make index arrays like A more common? I want to draw lots and lots of these 8-vert primitive sets every frame, as many as can be rendered at a given frame rate.

It'll probably be a little bit faster, but seriously, it sounds like you're trying to work on micro-optimizations before you even have a working (re)implementation. If it takes zero extra work to maintain locality, do it, but you're seriously overthinking things.


Fecotourist posted:

Is it more annoying than a single-line post that is stretched to 50 line equivalents by a big dumb avatar?

Far more. If I want shorter columns I'll resize my browser window. :)

Fecotourist posted:

I don't think that's the case here. My application already runs ok using OpenSceneGraph, [which seems to be using the glDrawElements() path the way I've been doing it]. For various reasons, I'm switching from OSG to SDL plus explicit OpenGL. If I'm doing that work, I just want to follow a good path.

Have you run performance tests to determine where your bottlenecks are? Those would give you way more information than some random internet people who don't know the specifics of your code.

Fecotourist
Nov 1, 2008

Avenging Dentist posted:


Have you run performance tests to determine where your bottlenecks are? Those would give you way more information than some random internet people who don't know the specifics of your code.

My meshes are LOD'ed, and the global detail level is adjusted continuously to maintain a target frame rate, so I can assert pretty confidently that rendering will often be a bottleneck. More detail than that, I don't know. With per-vertex normals and a single texture (cheap per pixel, right?), I have the impression that it's worth a little effort up front to make the geometry expression efficient.

But my take home seems to be that glDrawElements() is not a totally stupid way to go, that packed/separate doesn't matter a priori, and that it's probably not a huge deal that my index arrays jump around in memory, so thanks for that confirmation.

heeen
May 14, 2005

CAT NEVER STOPS
I'd say store it as a VBO on the gpu and only call it by id.
Also 8 vertexes easily fit into the GPU transformation cache so nothing to worry about there.
If you have meshes of several hundreds of thousands of vertices you want to make sure your geometry is reusing indexes as efficiently as possible.

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!
You can also take advantage of D3DX's ID3DXMesh::OptimizeInPlace function, which will handle index optimization. It doesn't depend on using D3D for rendering, and you can do it either during load or in the content pipeline (the latter being preferred).

heeen
May 14, 2005

CAT NEVER STOPS
the glVertexAttrib family of functions allows to add generic attributes to each vertex.
You can set a index that the data will be associated with. However, you can't choose the index arbitrarily, since I discovered that using indices lower than 4 will break standard (Normal/TexCoord) attributes, plus 0 is the vertex position.
How can I find out, which will be the first real free index to use for custom attributes?

shodanjr_gr
Nov 20, 2007

heeen posted:

How can I find out, which will be the first real free index to use for custom attributes?

http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/attributes.php

bottom of the page, there is a table.

heeen
May 14, 2005

CAT NEVER STOPS
I seem to have a bug in my ppl shader, but I can't wrap my head around it:
code:
	mat3 tbn=mat3(tvec, svec, ws_normal);
	vec3 normalmap=texture2D(normalmap, gl_TexCoord[0].xy).rgb*2.0-1.0;		

	vec3 lightcolor=vec3(0.0,0.0,0.0);
	float NdotL, NdotHV;
	vec3 lightdir = gl_LightSource[0].position.xyz - ws_pos.xyz;
	float dist= length(lightdir);
	lightdir= normalize(lightdir)*tbn;
	
	NdotL = max(dot(normalmap, lightdir), 0.0);

	gl_FragColor.rgb=NdotL;
	gl_FragColor.a=1.0;
the source seems to be the NdotL value:

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!
mat3 tbn=mat3(svec, tvec, ws_normal);

Try that.

heeen
May 14, 2005

CAT NEVER STOPS

OneEightHundred posted:

mat3 tbn=mat3(svec, tvec, ws_normal);

Try that.

thanks, although I found it myself, that was exactly the error :)

brian
Sep 11, 2001
I obtained this title through beard tax.

How do I make a smaller texture out of an existing loaded texture in openGL? I want to be able to turn a frame in a sprite sheet into a whole texture so I can repeat it over an area, I know it's fairly simple if it's just a single texture so i've been trying to work out how to seperate a section of an existing texture into a new texture handle. I've looked at glCopyTexSubImage2D but it seems to act on the read buffer, is there any way I can do this other than seperating the sprites in the spritesheet into seperate textures at load time?

haveblue
Aug 15, 2005



Toilet Rascal

brian posted:

How do I make a smaller texture out of an existing loaded texture in openGL? I want to be able to turn a frame in a sprite sheet into a whole texture so I can repeat it over an area, I know it's fairly simple if it's just a single texture so i've been trying to work out how to seperate a section of an existing texture into a new texture handle. I've looked at glCopyTexSubImage2D but it seems to act on the read buffer, is there any way I can do this other than seperating the sprites in the spritesheet into seperate textures at load time?

You can change the read buffer to the source texture with the framebuffer object API.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
I've got a model that rotates along the Y axis and that model has a bounding box defined by a maximum and minimum point that I would like to follow it. I thought that simply transforming the two points by the same rotation matrix would do it but apparently it doesn't work that way.

Bounding box without rotation:

After rotation:


Any ideas on what I actually need to transform it by to keep it inline?

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!
You can't really do that because any point on the model can affect the bounds. i.e. a sphere will always have the same box but a diagonal line won't even though they may have the same bounding box at given configurations.

Best thing to do would be either precompute the bounding box on given animation frames and expand it a bit if you're compositing animations, or use various extent points (i.e. hands, elbows, head, gut, back, knees, feet) to determine it.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
The bounding box is already computed per-frame and is interpolated inbetween. I'm simply performing a rotation about the Y axis on the model's world matrix. In fact, this method does in fact work when I transform the bounding box's physical geometry, but I need to get the actual values for my collision testing.

edit: I suppose I might as well provide some code. This is the code that sets the position of each of the drawn bounding box's positions:
code:
            bounds = animationController.GetMaximumBounds();

            //bounds.Minimum = Vector3.Transform(bounds.Minimum, Matrix.CreateRotationY(rotation));
            //bounds.Maximum = Vector3.Transform(bounds.Maximum, Matrix.CreateRotationY(rotation));

            boundingBoxVertices[0].Position = bounds.Maximum;
            boundingBoxVertices[1].Position = new Vector3(bounds.Maximum.X, bounds.Maximum.Y, bounds.Minimum.Z);
            boundingBoxVertices[2].Position = new Vector3(bounds.Minimum.X, bounds.Maximum.Y, bounds.Minimum.Z);
            boundingBoxVertices[3].Position = new Vector3(bounds.Minimum.X, bounds.Maximum.Y, bounds.Maximum.Z);

            boundingBoxVertices[4].Position = new Vector3(bounds.Maximum.X, bounds.Minimum.Y, bounds.Maximum.Z);
            boundingBoxVertices[5].Position = new Vector3(bounds.Maximum.X, bounds.Minimum.Y, bounds.Minimum.Z);
            boundingBoxVertices[6].Position = bounds.Minimum;
            boundingBoxVertices[7].Position = new Vector3(bounds.Minimum.X, bounds.Minimum.Y, bounds.Maximum.Z);

            boundingBoxVertexBuffer.SetData<VertexPositionColor>(boundingBoxVertices);
And the code where I set the world transform in the draw stage:
code:
boundingBoxEffect.Parameters["World"].SetValue(Matrix.CreateRotationY(rotation)*Matrix.CreateTranslation(worldPosition));
//boundingBoxEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(worldPosition));
bounds is just a struct that contains two 3D vectors. The way the code is written now, the bounding box looks fine. Uncommenting the commented out code in both sections gives me the problem. Normally it would be silly to transform it part way like that, but I need to do this in another section where I determine the collision boundaries. The collision boundries have to be a box in object space.

MasterSlowPoke fucked around with this message at 02:04 on Feb 13, 2009

zzz
May 10, 2008

MasterSlowPoke posted:

The bounding box is already computed per-frame and is interpolated inbetween. I'm simply performing a rotation about the Y axis on the model's world matrix. In fact, this method does in fact work when I transform the bounding box's physical geometry, but I need to get the actual values for my collision testing.

edit: I suppose I might as well provide some code. This is the code that sets the position of each of the drawn bounding box's positions:
code:
            bounds = animationController.GetMaximumBounds();

            //bounds.Minimum = Vector3.Transform(bounds.Minimum, Matrix.CreateRotationY(rotation));
            //bounds.Maximum = Vector3.Transform(bounds.Maximum, Matrix.CreateRotationY(rotation));

            boundingBoxVertices[0].Position = bounds.Maximum;
            boundingBoxVertices[1].Position = new Vector3(bounds.Maximum.X, bounds.Maximum.Y, bounds.Minimum.Z);
            boundingBoxVertices[2].Position = new Vector3(bounds.Minimum.X, bounds.Maximum.Y, bounds.Minimum.Z);
            boundingBoxVertices[3].Position = new Vector3(bounds.Minimum.X, bounds.Maximum.Y, bounds.Maximum.Z);

            boundingBoxVertices[4].Position = new Vector3(bounds.Maximum.X, bounds.Minimum.Y, bounds.Maximum.Z);
            boundingBoxVertices[5].Position = new Vector3(bounds.Maximum.X, bounds.Minimum.Y, bounds.Minimum.Z);
            boundingBoxVertices[6].Position = bounds.Minimum;
            boundingBoxVertices[7].Position = new Vector3(bounds.Minimum.X, bounds.Minimum.Y, bounds.Maximum.Z);

            boundingBoxVertexBuffer.SetData<VertexPositionColor>(boundingBoxVertices);
And the code where I set the world transform in the draw stage:
code:
boundingBoxEffect.Parameters["World"].SetValue(Matrix.CreateRotationY(rotation)*Matrix.CreateTranslation(worldPosition));
//boundingBoxEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(worldPosition));
bounds is just a struct that contains two 3D vectors. The way the code is written now, the bounding box looks fine. Uncommenting the commented out code in both sections gives me the problem. Normally it would be silly to transform it part way like that, but I need to do this in another section where I determine the collision boundaries. The collision boundries have to be a box in object space.

Well once you rotate the AA bounding box it's obviously not going to be axis-aligned anymore, so you can't really use it for collisions like that.
I guess one cheapo solution would be to take the maximum and minimum of the new and old corner coordinates, per axis, to get a slightly larger AABB of both the original and the rotated BB. Or switch over to something rotation-invariant like bounding spheres...

If you actually need the rotated positions of the BB vertices, like what you're calculating in that source snippet, you need to work out the coordinates from min/max first and then transform each one separately. Of course the result is not an AABB so it's useless for cheap collision detection.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
Ah, of course. Rotating two points won't give me an OBB, and that wouldn't work for my collision detection regardless. Thanks.

efficiency

MasterSlowPoke fucked around with this message at 13:40 on Feb 13, 2009

redphoenix11
May 9, 2007

I'm trying to do a basic mouse look in DX9, and I'm getting the cursor position in without any problems, but it always rotates about the z-axis no matter what I do. The actual code where I create the transform matrix is this:

code:
    // Set view matrix 
    D3DXVECTOR3 vEyePt = g_CameraPos;
    D3DXVECTOR3 vLookatPt = g_LookAt;
    D3DXVECTOR3 vUpVec = g_Up;
    
    D3DXMATRIX matRotation;
      
    if(g_fCurrY != 0){

	D3DXMatrixRotationAxis(&matRotation, &g_Right, D3DXToRadian((float)g_fCurrY / 3.0f));
	D3DXVec3TransformCoord(&vLookatPt, &vLookatPt, &matRotation);
	D3DXVec3TransformCoord(&vUpVec, &vUpVec, &matRotation);
    }

    if(g_fCurrX != 0){

	D3DXMatrixRotationAxis(&matRotation, &D3DXVECTOR3(0,1,0), D3DXToRadian((float)g_fCurrX /3.0f));
	D3DXVec3TransformCoord(&vLookatPt, &vLookatPt, &matRotation);
	D3DXVec3TransformCoord(&vUpVec, &vUpVec, &matRotation);
    }


    g_LookAt = vLookatPt;
    g_Up = vUpVec; 

    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &g_Up);
    g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);

    // Set projection matrix
    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/2, 1.0f, 1.0f, 100.0f);
    g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
And my initial values for eye, lookat, right, and up are:

code:
D3DXVECTOR3				g_CameraPos(5.0f, 5.0f, -15.0f);
D3DXVECTOR3				g_LookAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3				g_Up(0.0f, 1.0f, 0.0f);
D3DXVECTOR3				g_Right(1.0f, 0.0f, 0.0f);
And g_fCurrX and g_fCurrY are just the current positions of the mouse cursor, which I get from DirectInput, so it's already offset from the last position. My problem is probably something stupid, but I do more work with shaders than other stuff in DirectX so I'm somewhat lost.

EDIT: I should also mention that rotating doesn't work at all when I'm moving the mouse along the x-axis.

redphoenix11 fucked around with this message at 04:15 on Feb 17, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I'm pretty sure you don't want to be changing the "up" vector, for one thing.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
This is how I do a FPS camera. It's in C# but you should be able to translate it.
code:
// Determine change in camera angle this frame 
cameraAngle.X += MathHelper.ToRadians((mouseState.Y - this.Window.ClientBounds.Y / 2) * rotSpeed * 0.01f); // pitch 
cameraAngle.Y += MathHelper.ToRadians((mouseState.X - this.Window.ClientBounds.X / 2) * rotSpeed * 0.01f); // yaw 
 
// Calculate what direction the camera is facing and what direction is its left for movement 
cameraForward = Vector3.Normalize(new Vector3((float)Math.Sin(-cameraAngle.Y), (float)Math.Sin(cameraAngle.X), (float)Math.Cos(-cameraAngle.Y))); 
cameraLeft = Vector3.Normalize(new Vector3((float)Math.Cos(cameraAngle.Y), 0f, (float)Math.Sin(cameraAngle.Y))); 
 
// Move along camera's forward direction 
if (keyboardState.IsKeyDown(Keys.W)) 
    cameraPosition -= speed * (float)gameTime.ElapsedGameTime.TotalSeconds * cameraForward; 
if (keyboardState.IsKeyDown(Keys.S)) 
    cameraPosition += speed * (float)gameTime.ElapsedGameTime.TotalSeconds * cameraForward; 
 
// Move along camera's side direction 
if (keyboardState.IsKeyDown(Keys.A)) 
    cameraPosition -= speed * (float)gameTime.ElapsedGameTime.TotalSeconds * cameraLeft; 
if (keyboardState.IsKeyDown(Keys.D)) 
    cameraPosition += speed * (float)gameTime.ElapsedGameTime.TotalSeconds * cameraLeft; 
 
// Move along the world's up direction - moving along camera's up would feel odd 
if (keyboardState.IsKeyDown(Keys.LeftShift)) 
    cameraPosition.Y += speed * (float)gameTime.ElapsedGameTime.TotalSeconds; 
if (keyboardState.IsKeyDown(Keys.LeftControl)) 
    cameraPosition.Y -= speed * (float)gameTime.ElapsedGameTime.TotalSeconds; 
 
// Create the view matrix   
view = Matrix.Identity; 
view *= Matrix.CreateTranslation(-cameraPosition); 
view *= Matrix.CreateRotationY(cameraAngle.Y); 
view *= Matrix.CreateRotationX(cameraAngle.X);    
 
// Reset the mouse to the center of the window so movement is never constrained 
if (this.IsActive) 
{ 
    Mouse.SetPosition(this.Window.ClientBounds.X / 2, this.Window.ClientBounds.Y / 2); 
} 

redphoenix11
May 9, 2007

I wasn't setting the view matrix properly and there was some other little problems, I've got this now:

code:
	// Set view matrix 
    D3DXVECTOR3 vEyePt = g_CameraPos;
    D3DXVECTOR3 vLookatPt = g_LookAt;
	D3DXVECTOR3 vUpVec = g_Up;
	D3DXVECTOR3 vRightVec = g_Right;
    
	// Calculate the yaw
	D3DXMATRIX matYaw;
	D3DXMatrixRotationAxis(&matYaw, &vUpVec, D3DXToRadian(g_fCurrX));
	D3DXVec3TransformCoord(&vLookatPt, &vLookatPt, &matYaw);
	D3DXVec3TransformCoord(&vRightVec, &vRightVec, &matYaw); 

	// Calculate the pitch
	D3DXMATRIX matPitch;
	D3DXMatrixRotationAxis(&matPitch, &vRightVec, D3DXToRadian(g_fCurrY)); 
	D3DXVec3TransformCoord(&vLookatPt, &vLookatPt, &matPitch);
	D3DXVec3TransformCoord(&vUpVec, &vUpVec, &matPitch);


	D3DXMATRIX viewMatrix;
	D3DXMatrixIdentity(&viewMatrix);

	viewMatrix._11 = vRightVec.x; viewMatrix._12 = vUpVec.x; viewMatrix._13 = vLookatPt.x;
	viewMatrix._21 = vRightVec.y; viewMatrix._22 = vUpVec.y; viewMatrix._23 = vLookatPt.y;
	viewMatrix._31 = vRightVec.z; viewMatrix._32 = vUpVec.z; viewMatrix._33 = vLookatPt.z;

	viewMatrix._41 = -D3DXVec3Dot(&vEyePt, &vRightVec);
	viewMatrix._42 = -D3DXVec3Dot(&vEyePt, &vUpVec);
	viewMatrix._43 = -D3DXVec3Dot(&vEyePt, &vLookatPt);

    g_pd3dDevice->SetTransform(D3DTS_VIEW, &viewMatrix);

	//g_Up = vUpVec;
	g_LookAt = vLookatPt;
	g_Right = vRightVec;

	// Set projection matrix
    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/2, 1.0f, 1.0f, 100.0f);
    g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
It looks pretty good, so unless there's another glaring error I need to fix, I'm gonna stick with it.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
Looks fine to me. Why are you creating the projection matrix every frame, though?

Scarboy
Jan 31, 2001

Good Luck!
I have a camera in OpenGL using the gluLookAt function that is working correctly. The camera rotates around a fixed point at the center of the screen. Is there any way i can lower the center point of the camera on the screen/viewport/window?

I don't want to put the center of the camera a few units further on the z-axis because then my object moves when the camera rotates around it. I want the object to always be in the same place, have the camera rotating around it, and to not be at the center of the screen (somewhere in the lower 1/3 of the screen).

Any way to do this?

Spite
Jul 27, 2001

Small chance of that...

Scarboy posted:

I have a camera in OpenGL using the gluLookAt function that is working correctly. The camera rotates around a fixed point at the center of the screen. Is there any way i can lower the center point of the camera on the screen/viewport/window?

I don't want to put the center of the camera a few units further on the z-axis because then my object moves when the camera rotates around it. I want the object to always be in the same place, have the camera rotating around it, and to not be at the center of the screen (somewhere in the lower 1/3 of the screen).

Any way to do this?

You can just translate up and down to make it seem like the camera is higher or lower.

I think you're misinterpreting how the math works out. In the end, after your transformations are applied, the camera is at 0,0,0 looking down the -z axis and everything else has been transformed relative to that. Try thinking about it as if the world is moving around the camera, instead of the camera moving through the world.

EDIT: Alternately, you could adjust your viewport - but that might be weird.

Spite fucked around with this message at 02:55 on Feb 25, 2009

zzz
May 10, 2008

Scarboy posted:

I have a camera in OpenGL using the gluLookAt function that is working correctly. The camera rotates around a fixed point at the center of the screen. Is there any way i can lower the center point of the camera on the screen/viewport/window?

I don't want to put the center of the camera a few units further on the z-axis because then my object moves when the camera rotates around it. I want the object to always be in the same place, have the camera rotating around it, and to not be at the center of the screen (somewhere in the lower 1/3 of the screen).

Any way to do this?

Not sure what it is that you want (why not just mve the camera slightly up?), but it sounds like an off-center projection matrix.

Something like calling glFrustum by hand (or D3DXMatrixPerspectiveOffCenterLH, both the GL and DX references have matrices describing what's going on) with the vertical bounds of the viewing frustum skewed as much as you like.

Scarboy
Jan 31, 2001

Good Luck!
I know the math behind what gluLookAt does, that's why I couldn't wrap my head around how to do it. Thankfully, the second you said off center projection I just smacked myself on the head and said, "oooooooooh, that's why my graphics professor said glFrustum is useful for off center projections" oh so long ago.

I've got it working exactly how I want it now, thanks.

Contero
Mar 28, 2004

Is there a straightforward way to draw (rasterized?) 2d quads using pixel coordinates? I'm trying to draw up a menu and some simple 2d graphics on top of a 3d scene.

(edit: openGL)

Contero fucked around with this message at 05:52 on Mar 6, 2009

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
Use an unprojection matrix to get the world space coordinates for your quads.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

MasterSlowPoke posted:

Use an unprojection matrix to get the world space coordinates for your quads.

Wait, you seriously can't use pre-transformed coordinates in OpenGL? It's not even a full line of code to do that in DirectX.

joe87653
Jul 9, 2002

Contero posted:

Is there a straightforward way to draw (rasterized?) 2d quads using pixel coordinates? I'm trying to draw up a menu and some simple 2d graphics on top of a 3d scene.

(edit: openGL)
Switch to orthographic mode. Then the Z is interpreted as Z-order

Here are two basic functions you can use to switch between:
code:
void Go2D(GLdouble w,GLdouble h,GLdouble n,GLdouble f){
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-(w/2),(w/2),-(h/2),(h/2),n,f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void Go3D(GLdouble w,GLdouble h,GLdouble n,GLdouble f){
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45,(w/h),n,f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

Avenging Dentist posted:

Wait, you seriously can't use pre-transformed coordinates in OpenGL? It's not even a full line of code to do that in DirectX.
Durr!!! DirectX is better than OpenGL!111

krysmopompas
Jan 17, 2004
hi

Avenging Dentist posted:

Wait, you seriously can't use pre-transformed coordinates in OpenGL? It's not even a full line of code to do that in DirectX.
Pre-transformed coordinates suck. If you want clipping, not all hardware supports doing the backprojection into clipping space, so it falls back into software and is slow as balls.

You're better off doing it via the unprojection way.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

krysmopompas posted:

Pre-transformed coordinates suck. If you want clipping, not all hardware supports doing the backprojection into clipping space, so it falls back into software and is slow as balls.

You're better off doing it via the unprojection way.

It sounded like he was making a HUD, where that wouldn't be an issue. Though I suppose you could set all the matrices in the pipeline to identity.

Avenging Dentist fucked around with this message at 21:26 on Mar 6, 2009

shodanjr_gr
Nov 20, 2007
Any suggestions on how I can do some profiling on GLSL code? I got an app running various shaders in succession and I want to see how each frame time is divided between those shaders.

I've installed Nvidias Perfkit that comes with the gDEBUGGER app, but I cant say I found out something that helps me out (PerfHUD on the other hand looks awesome...if only I were using DirectX).

numeric_atrophy
Oct 18, 2003

Download Geared - FREE
(17 Million Downloads)

Windows 8
iOS
Android
WinPhone 7
edit: Got it solved. Apparently [UIImage imageNamed:] does too muh caching, and images are not often released.


I'm working on my first openGL game for the iphone. It runs smoothly, and Instrument's leaks application says that I'm not leaking, but watching my app in the Instruments/Activity Monitor, I seem to be using up about two megabytes more of memory every time my level changes. I googled around, and found a suggestion not to use glGenTextures more than once for a texture and to copy the number over and use it in the future, and tried that, but the memory still increases.

This is called after each level. When I comment out the CGImageRef, CGContextRef and CGContextDrawImage the leak is removed, but then of course I only get an all black image. If I only comment out CGContextRef and CGContextDrawImage then the leak is still there, which leads me to believe the leak is CGImageRef not being released, but when I try to release it, the app crashes on the second call of setLevelTextures.
code:
-(void)setLevelTextures:(LevelObject *)level
{	
	NSString *bgString = [NSString stringWithString: [level loBgImage]];
	NSString *pathString = [NSString stringWithString: [level loPathImage]];

	CGImageRef bgImage =		[UIImage imageNamed:bgString].CGImage;
	CGImageRef mapPathImage  =	[UIImage imageNamed:pathString].CGImage;
	
	GLubyte * bgData =		(GLubyte *) calloc(width512 * height512, 4);
	GLubyte * mapPathData =		(GLubyte *) calloc(width512 * height512, 4);
	
	CGContextRef mapPathContext = 
CGBitmapContextCreate(mapPathData, width512, height512, 8, width512 * 4, 
CGImageGetColorSpace(mapPathImage), kCGImageAlphaPremultipliedLast);
	CGContextDrawImage(mapPathContext, CGRectMake(0.0, 0.0, (CGFloat)width512, (CGFloat)height512), mapPathImage);
	
	if (firstTextureA == NO)
	{
		glGenTextures(1, &mapPathTexture);
		pathTexturePointer = mapPathTexture;
		if (mapPathTexture != 0)	{		glDeleteTextures(1, &mapPathTexture);	}
		firstTextureA = YES;
	}
	glBindTexture(GL_TEXTURE_2D, pathTexturePointer);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width512, height512, 0, GL_RGBA, GL_UNSIGNED_BYTE, mapPathData);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	
	CGContextRef bgContext = 
CGBitmapContextCreate(bgData, width512, height512, 8, width512 * 4, 
CGImageGetColorSpace(bgImage), kCGImageAlphaPremultipliedLast);
	CGContextDrawImage(bgContext, CGRectMake(0.0, 0.0, (CGFloat)width512, (CGFloat)height512), bgImage);
	if (firstTextureB == NO)
	{
		glGenTextures(1, &bgTexture);
		mapTexturePointer = bgTexture;
		if (bgTexture != 0)		{		glDeleteTextures(1, &bgTexture);	}
		firstTextureB = YES;
	}
	glBindTexture(GL_TEXTURE_2D, mapTexturePointer);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width512, height512, 0, GL_RGBA, GL_UNSIGNED_BYTE, bgData);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	
	
	if (mapPathContext != nil)	{	CGContextRelease(mapPathContext);	mapPathContext = nil;}
	if (bgContext != nil)		{	CGContextRelease(bgContext);	bgContext = nil;}
	
	if (bgData != nil)		{	free(bgData);	bgData = nil;}
	if (mapPathData != nil)		{	free(mapPathData);	mapPathData = nil;}
	
	[bgString release];
	[pathString release];
}
I've been working on this leak for hours now, trying different things, with no success.



edit: Solved it with
code:
NSString *bgString = [NSString stringWithString: [level loBgImage]];
	NSString *pathString = [NSString stringWithString: [level loPathImage]];
	
	NSString *fileLocation1 = [[NSBundle mainBundle] pathForResource:bgString ofType:@"jpg"];
	NSString *fileLocation2 = [[NSBundle mainBundle] pathForResource:pathString ofType:@"png"];
	
	NSData *imageData1 = [NSData dataWithContentsOfFile:fileLocation1];	
	NSData *imageData2 = [NSData dataWithContentsOfFile:fileLocation2];	
	
	CGImageRef bgImage	=	[[UIImage imageWithData:imageData1] CGImage];
	CGImageRef mapPathImage =	[[UIImage imageWithData:imageData2] CGImage];

numeric_atrophy fucked around with this message at 12:51 on Mar 25, 2009

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
This isn't 3d or using OGL / DX but I think it is the best topic still.

I'm using GDI and a DIB Section to draw on. I thought DIB's are ABGR?

RGB(255, 0, 0) seems to give 0x000000FF. Ok... but 0x000000FF appears to be blue. :confused:

slovach fucked around with this message at 11:01 on Mar 28, 2009

StickGuy
Dec 9, 2000

We are on an expedicion. Find the moon is our mission.

slovach posted:

RGB(255, 0, 0) seems to give 0x000000FF. Ok... but 0x000000FF appears to be blue. :confused:
At least with .NET, bitmaps are ARGB.

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
I figured it out.

I tried to be cool and shift by 8 instead of dividing by 255. It turned out that 'small inaccuracy' was actually doing a pretty good job of butchering results. :downs:

Tim Berners-Lee
Apr 12, 2007
Inventor of the Internet
Can someone explain to me why gluLookAt doesn't act the way I expect it to?

code:
void displayCallback(void) {

  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  gluLookAt(
	    xx,0,10,
	    xx,0,9,
	    xx,1,10
	    );
  drawScene();
  
  glutSwapBuffers();
}
Here xx is just some keyboard controlled parameter, and drawScene just draws a triangle at the origin. As xx ranges over values, I'd expect the scene to shift back and forth from left to right along the x-axis. Instead, it rotates in the xy plane, tracing out some downwards-facing parabola-shaped thing centered at the origin.


edit: the problem is that the UP vector is just that, a vector, not an offset from position. Works fine with 0,1,0. gently caress I spent way too long fixing this.

Tim Berners-Lee fucked around with this message at 06:19 on Mar 30, 2009

Adbot
ADBOT LOVES YOU

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!

StickGuy posted:

At least with .NET, bitmaps are ARGB.
DIB sections depend on the convention you're using, which is mainly because Microsoft specifies them as DWORDS and specifies the bit masks as such as well, despite storing them as little-endian.

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

quote:

The blue mask is 0x000000FF, the green mask is 0x0000FF00, and the red mask is 0x00FF0000
... yet because they're little endian, the byte order is BGRA.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply