Why does the whole world translate instead of just the object? - directx

I have not been able to translate my object alone, when I try to do that, the whole world would translate instead.
Anyone have any ideas why this is happening?
d3ddev->SetStreamSource(0, v_buffer[2], 0, sizeof(CUSTOMVERTEX));
d3ddev->SetIndices(i_buffer[2]);
d3ddev->SetTexture(0, texture[1]);
D3DXMATRIX matTranslate;
D3DXMatrixTranslation(&matTranslate, 30.0f, 0.0f, 30.0f);
d3ddev->SetTransform(D3DTS_WORLD, &matTranslate);
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);

The D3D device acts as a state machine. Whenever you set some state on the device, like the world transform matrix, it will persist between multiple draw calls. After you draw your object, you must reset the world transform back to the identity matrix before drawing the rest of the scene to prevent it from being translated.
D3DXMATRIX matIdentity;
D3DXMatrixIdentity(&matIdentity);
d3ddev->SetTransform(D3DTS_WORLD, &matIdentity);

Related

GLKit Doesn't draw GL_POINTS or GL_LINES

I am working hard on a new iOS game that is drawn only with procedurally generated lines. All is working well, except for a few strange hiccups with drawing some primitives.
I am at a point where I need to implement text, and the characters are set up to be a series of points in an array. When I go to draw the points (which are CGPoints) some of the drawing modes are working funny.
effect.transform.modelviewMatrix = matrix;
[effect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, 0, 0, &points);
glDrawArrays(GL_POINTS, 0, ccc);
I am using this code to draw from the array, and when the mode is set to GL_LINE_LOOP or GL_LINE_STRIP all works well. But if I set it to GL_POINTS, I get a gpus_ReturnGuiltyForHardwareRestert error. And if I try GL_LINES it just doesn't draw anything.
What could possibly be going on?
When you draw with GL_POINTS in ES2 or ES3, you need to specify gl_PointSize in the vertex shader or you'll get undefined behavior (ugly rendering on device at best, the crash you're seeing at worst). The vertex shader GLKBaseEffect uses doesn't do gl_PointSize, so you can't use it with GL_POINTS. You'll need to implement your own shaders. (For a starting point, try the ones in the "OpenGL Game" template you get when creating a new Xcode project, or using the Xcode Frame Debugger to look at the GLSL that GLKBaseEffect generates.)
GL_LINES should work fine as long as you're setting an appropriate width with glLineWidth() in client code.

C4Shape, setFrame doesn't change the size of the shape (C4Framework)

I'm playing around with C4, and can't seem to figure out why my shapes don't animate. If I create a shape like so:
self.theShape = [C4Shape ellipse:CGRectMake(100, 100, 2, 2)];
... and later call
[theShape setFrame:CGRectMake(200, 200, 50,50)];
The shape doesn't change size. The implementation suggests that it should, but I'm not seeing it. Is there anything I'm doing wrong? Is it because I'm not updating the canvas?
In C4, calling setFrame: on a C4Shape object will not scale it.
The reason being is that changing the frame of a CAShapeLayer's view will not change the underlying bezier shape itself... Unlike calling the same thing on an image. UIKit will scale images, and other content, but will not scale beziers (as far as I know)...
So, if it's a rect, call:
[theShape rect:aNewFrame]
or if its an ellipse, call:
[theShape ellipse:aNewFrame]
This approach constructs a new shape and then leverages Core Animations ability to implicitly animate the transition between the old shape path and the new one.

How to redraw only a portion of the presenting framebuffer?

I want to implement a simple paint feature on iOS.
I have some framebuffers/textures wired up to get the desired final composition.
Redrawing the whole screen is a huge overhead. I can determine a rectangle that has changed.
So what is the best way to draw only that portion?
glScissor(0, 0, 50, 50);
glEnable(GL_SCISSOR_TEST);

How do I draw a rotating prism in OpenGL?

I'm trying to draw a simple crystal that rotates on its axis. I can get the shape right easily enough by drawing a pyramid and then drawing it again upside down, but I've got two problems.
First off, even though I draw everything in the same color, two of the faces come out a different color as the other two.
Second, it's placing a "bottom" on each pyramid that's visible through the translucent walls of the crystal, which ruins the effect. Is there any way to get rid of it?
Here's the code I'm using to set up and draw the GL scene. There's a lot more OpenGL code than this, of course, but this is the relevant part.
procedure Initialize;
begin
glShadeModel(GL_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.5);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
end;
procedure Draw; //gets called in a loop
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5,-0.5,-6.0);
glRotatef(rotation,0.0,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glColor4f(0, 0, 1, 0.2);
glVertex3f(0, 3.4, 0);
glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, -1);
glVertex3f(-1, 0, -1);
glEnd;
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, -3.4, 0);
glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, -1);
glVertex3f(-1, 0, -1);
glEnd;
rotation := rotation + 0.02;
end;
Anyone know what I'm doing wrong and how to fix it?
I'm trying to draw a simple crystal
Stop. Crystals are translucent, and the moment you start drawing translucent objects, you can basically discard any notion of the effect being "simple". Rendering a true prism (which refracts different wavelengths of light differently) is something that requires raytracing of some form to get right. And there are many ray tracers that can't even get it right, since they only trace R, G and B wavelengths, whereas you need to trace many wavelengths to approximate the refraction and light splitting pattern of a prism.
The best you're going to get is on a rasterizer like OpenGL some level of fakery.
I can't explain what's going on with the faces, but the problem with seeing through to the other polygons is simple: you're not using backface culling. Unless you want to see the back faces of transparent objects, you need to make sure that backface culling is active.

iOS - zoom only part of display using openGL ES

I'm working on a level builder app to build content for a game. Part of the iPad screen is a dedicated control panel, while the rest is a graphical representation of the level being built. I need to zoom the level area in and out without affecting the control panel. I'm using openGL ES for rendering. Can anyone give me some pointers here? Can I split the screen with different viewports and so just scale one?
The trick is to understand that OpenGL is a state machine and there is no such thing like "global initialization". As long as you follow the badly written tutorials and have your projection matrix setup in the window resize handler you'll be stuck. What you actually do is something like this:
void render_perspective_scene(void);
void render_ortho_scene(void);
void render_HUD();
void display()
{
float const aspect = (float)win_width/(float)win_height;
glViewport(0,0,win_width,win_height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-aspect*near/lens, aspect*near/lens, -near/lens, near/lens, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render_perspective_scene();
glEnable(GL_SCISSOR_TEST);
// just clear the depth buffer, so that everything that's
// drawn next will overlay the previously rendered scene.
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(ortho_x, ortho_y, ortho_width, ortho_height);
glScissor(ortho_x, ortho_y, ortho_width, ortho_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-aspect*scale, aspect*scale, -scale, scale, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render_ortho_scene();
// Same for the HUD, only that we render
// that one in pixel coordinates.
glViewport(hud_x, hud_y, hud_width, hud_height);
glScissor(hud_x, hud_y, hud_width, hud_height);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, win_width, 0, win_height, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render_HUD();
}
The important part is, that you set the viewport/scissor and the projection within the drawing handler prior to rendering that sub-part.
If you're rendering with OpenGL ES (presumably 2.0), then you have full control over the scaling of what you render. You decide where the scale gets applied, and you decide how things are rendered.
I'd guess your code currently looks a bit like this.
Get view scale
Apply view scale to view matrix
Render level
Render control panel
When it should look like this.
Render control panel
Get view scale
Apply view scale to view matrix
Render level
The matrices (or whatever transformation stuff you have) you use for transforming the level should not be used for transforming the control panel.

Resources