Error when using P3D Processing 3 - processing

Could you please help me
I am trying to start drawing 3D shapes using Processing 3
Whenever the renderer P3D is used, I get the following Error
ERROR: 0:2: '' : Declaration must include a precision qualifier or the default precision must have been previously declared.
ERROR: 0:30: '' : Declaration must include a precision qualifier or the default precision must have been previously declared.
The piece of code I am trying to run:
float x,y,z;
void setup() {
size(200,200,P3D);
x = width/2;
y = height/2;
z = 0;
}
void draw() {
translate(x,y,z);
rectMode(CENTER);
rect(0,0,100,100);
z++; // The rectangle moves forward as z increments.
}
Thank you

Related

How to make this pattern to expand and shrink back

i have a task to make a pattern of circles and squares as described on photo, and i need to animate it so that all objects smoothly increase to four times the size and then shrink back to their original size and this is repeated. i tried but i cant understand problem
{
size(500,500);
background(#A5A3A3);
noFill();
rectMode(CENTER);
ellipseMode(CENTER);
}
void pattern(int a, int b)
{
boolean isShrinking = false;
for(int x = 0; x <= width; x += a){
for(int y = 0; y <= height; y += a){
stroke(#1B08FF);
ellipse(x,y,a,a);
stroke(#FF0000);
rect(x,y,a,a);
stroke(#0BFF00);
ellipse(x+25,y+25,a/2,a/2);
if (isShrinking){a -= b;}
else {a += b;}
if (a == 50 || a == 200){
isShrinking = !isShrinking ; }
}
}
}
void draw()
{
pattern(50,1);
}
this is what pattern need to look like
Great that you've posted your attempt.
From what you presented I can't understand the problem either. If this is an assignment, perhaps try to get more clarifications ?
If you comment you the isShrinking part of the code indeed you have an drawing similar to image you posted.
animate it so that all objects smoothly increase to four times the size and then shrink back to their original size and this is repeated
Does that simply mean scaling the whole pattern ?
If so, you can make use of the sine function (sin()) and the map() function to achieve that:
sin(), as the reference mentions, returns a value between -1 and 1 when you pass it an angle between 0 and 2 * PI (because in Processing trig. functions use radians not degrees for angles)
You can use frameCount divided by a fractional value to mimic an even increasing angle. (Even if you go around the circle multiple times (angle > 2 * PI), sin() will still return a value between -1 and 1)
map() takes a single value from one number range and maps it to another. (In your case from sin()'s result (-1,1) to the scale range (1,4)
Here's a tweaked version of your code with the above notes:
void setup()
{
size(500, 500, FX2D);
background(#A5A3A3);
noFill();
rectMode(CENTER);
ellipseMode(CENTER);
}
void pattern(int a)
{
for (int x = 0; x <= width; x += a) {
for (int y = 0; y <= height; y += a) {
stroke(#1B08FF);
ellipse(x, y, a, a);
stroke(#FF0000);
rect(x, y, a, a);
stroke(#0BFF00);
ellipse(x+25, y+25, a/2, a/2);
}
}
}
void draw()
{
// clear frame (previous drawings)
background(255);
// use the frame number as if it's an angle
float angleInRadians = frameCount * .01;
// map the sin of the frame based angle to the scale range
float sinAsScale = map(sin(angleInRadians), -1, 1, 1, 4);
// apply the scale
scale(sinAsScale);
// render the pattern (at current scale)
pattern(50);
}
(I've chosen the FX2D renderer because it's smoother in this case.
Additionally I advise in the future formatting the code. It makes it so much easier to read and it barely takes any effort (press Ctrl+T). On the long run you'll read code more than you'll write it, especially on large programs and heaving code that's easy to read will save you plenty of time and potentially headaches.)

DX9 style intristics are disabled when not in dx9 compatibility mode?

I am currently writing an HLSL shader for a basic Gaussian blur. The shader code is straight forward, but I keep getting an error:
DX9 style intristics are disabled when not in dx9 compatibility mode. (LN#: 19)
This tells me that line 19 in my code is the issue, and I believe it is either due to tex2D or Sampler in that particular line.
#include "Common.hlsl"
Texture2D Texture0 : register(t0);
SamplerState Sampler : register(s0);
float4 PSMain(PixelShaderInput pixel) : SV_Target {
float2 uv = pixel.TextureUV; // This is TEXCOORD0.
float4 result = 0.0f;
float offsets[21] = { ... };
float weights[21] = { ... };
// Blur horizontally.
for (int x = 0; x < 21; x++)
result += tex2D(Sampler, float2(uv.x + offsets[x], uv.y)) * weights[x];
return result;
}
See below for notes about the code, and my questions.
Notes
I have to hand type my code into StackOverflow due to my code being on a computer without a connection. Therefore:
Any spelling or case errors present here do not exist in code.
The absence of values inside of offsets and weights is intentional.
This is because there are 21 values in each and I didn't feel like typing them all.
offsets is every integer from -10 to 10.
weights ranges from 0.01 to 0.25 and back to 0.01.
The line count here is smaller due to the absence mentioned prior.
The line number of the error here is 15.
The column range is 13 - 59 which encapsulates tex2D and Sampler.
My Questions
Am I using the wrong data type for Sampler in the tex2D call?
Is tex2D deprecated in DirectX 11?
What should I be using instead?
What am I doing wrong here as that is my only error.
After some extensive searching, I've found out that tex2D is no longer supported from ps_4_0 and up. Well, 4_0 will work in legacy mode, but it doesn't work it 5_0 which is what I am using.
Shader Model : Supported
Shader Model 4 : yes (pixel shader only), but you must use the legacy compile option when compiling.
Shader Model 3 (DirectX HLSL) : yes (pixel shader only)
Shader Model 2 (DirectX HLSL) : yes (pixel shader only)
Shader Model 1 (DirectX HLSL) : yes (pixel shader only)
This has been replaced by Texture2D; the documentation is available for it. Below is an example from the mentioned documentation:
// Object Declarations
Texture2D g_MeshTexture;
SamplerState MeshTextureSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
struct VS_OUTPUT
{
float4 Position : SV_POSITION;
float4 Diffuse : COLOR0;
float2 TextureUV : TEXCOORD0;
};
VS_OUTPUT In;
// Shader body calling the intrinsic function
Output.RGBColor = g_MeshTexture.Sample(MeshTextureSampler, In.TextureUV) * In.Diffuse;
To replace the tex2D call in my code:
result += Texture0.Sample(Sampler, float2(uv.x + offsets[x], uv.y)) * weights[x];
Also, note that the code in this post is for the horizontal pass of a Gaussian blur.

Processing error with this code

float speed = 1;
void setup() {
size(400, 300);
}
void draw() {
background(255);
move();
display();
}
void move() {
x = x + speed;
if (x > 350) {
speed = 0;
}
}
void display(x,y) {
fill(#FF2121);
translate(x,y);
ellipse(0, 0, 60, 60);
rect(-10, 15, 20, 100);
}
Unexpected token: x on "Void display (x,y)"
Basically this program moves the ellipse and rect to other side of the window. is this the right way to do it? or is there any other easy way.
Example
0 = ellipse
[] = rect
move to other side of window (speed of 1) and when it hit the edge, both them stop.
Parameters need types, just like variables do.
void display(float x, float y) {
Also note that since your display() function takes 2 parameters, it's illegal to call it without any parameters, which is what you're doing in your draw() function.
Also note that you've never defined the x variable, so that's another error.
Please get into the habit of working in smaller chunks instead of trying to write your whole program all at one time. You've got quite a few errors here, and it's going to be hard to fix one without fixing the others. I recommend starting over with something simpler, and only moving forward when you have something that works.

MCPE GLSL Conditionals

I'm writing an ios vertex shader that "flattens" a MC world in the x direction if a change in the z direction is detected, and vice versa (The xz plane is perp to height). I have several shaders that warp the world just fine, but writing this pseudo movement detection hasn't worked. I know conditionals are costly. I was comparing the position to a number and it worked:
if (worldPos.x != 4.) {
worldPos.z = 0.;
}
But comparing position to a static call of the position doesn't. So far I've tried assigning constant floats to the x and z components, uniform floats, and a POS4 uniform, but no success. I have a feeling the conditionals fail because of a data type problem? It would be easier to debug if PE version displayed coord like PC. Thanks for any/all help! Current code:
uniform POS4 CHUNK_ORIGIN_AND_SCALE;
attribute POS4 POSITION;
void main()
{
POS4 worldPos;
worldPos.xyz = (POSITION.xyz * CHUNK_ORIGIN_AND_SCALE.w) + CHUNK_ORIGIN_AND_SCALE.xyz;
worldPos.w = 1.;
const float staticPosx = worldPos.x;
const float staticPosz = worldPos.z;
if (worldPos.x != staticPosx) {
worldPos.z = 0.;
staticPosx = worldPos.x;
}
if (worldPos.z != staticPosz) {
worldPos.x = 0.;
staticPosz = worldPos.z;
}
etc.
i didn't try it in glsl but in C you can't
int a=5;
const int b=a;

Cocos2dx Inherited CCNode class not rotating around around its center?

I'm at a bit of a loss here, forgive me if this has already been asked - i've have searched google high and low but i cant find anything?
i'm trying to rotate a group of sprites that are generated in a class, then rotating this object in the main gamescene on a menuitem click but the rotation is not at the center of the sprite? it a some larger area probably the layer size?
I've tried setting the anchorpoint to every possible combination?
Here is what iv got
This is the gamecharacter.h
#define COMPUTE_X(x) ((abs(x)) * 16) + (16*2) + (16/2)
#define COMPUTE_Y(y) (386 - (abs(y) * 16)) + (16/2)
#define COMPUTE_X_Y(x,y) ccp( COMPUTE_X(x), COMPUTE_Y(y))
// Game character class
#include "cocos2d.h"
using namespace cocos2d;
//a class to encapsulate playable game character by creating a group of sprites etc..
#ifndef GAMECHARACTER_H
#define GAMECHARACTER_H
class GameCharacter : public CCNode {
private:
//some private methods etc....
public:
void addSprite(const char* filename);
void setInitialPosition(CCPoint* position);
//Various other methods.........
};
#endif
void GameCharacter::addSprite(const char* filename)
{
//go get the sprite sheet
CCTexture2D* gameArtTexture = CCTextureCache::sharedTextureCache()->addPVRImage("SpriteSheet.pvr.ccz");
CCSpriteBatchNode::batchNodeWithTexture(gameArtTexture);
CCSprite *tempBlock = CCSprite::spriteWithSpriteFrameName(filename);
this->addChild((CCSprite*)tempBlock,0);
}
void GameCharacter::setInitialPosition(CCPoint* position)
{
//loop through the positions and set the character up
CCArray *children = this->getChildren();
CCSprite *currentBlock;
for (int i=0;i<7;i++){
currentBlock = (CCSprite*) children->objectAtIndex(i);
//compute x y grid positions (1,1) ---> to real (72,394)
currentBlock->setPosition(COMPUTE_X_Y(position[i].x,position[i].y));
}
}
This is the gamecharacter.cpp
void GameScene::AddCharacter(CCPoint* position)
{
const char* filename;
GameCharacter* character = new GameCharacter();
for (int i = 0; i < 7; i++) {
filename = helperFunctions::Format("character%d.png",i+1); //character1.png -> character7.png
character->addSprite(filename);
}
character->setInitialPosition(position);
this->addChild((CCSprite*) character,-1,2);
_sprite = character;
}
//here is the menuitem click handler
void GameScene::menuRotateRightCallback(CCObject* pSender)
{
//rotate the character right
//really slowly so we can see whats happening
_sprite->runAction((CCRotateBy::actionWithDuration(2.50,90)));
}
Thanks
It is much easier to do, using
x = center.x + cos(angle) * radius;
y = center.y + sin(angle) * radius;
Ive figured it out, looking at the docs for CCNode made me think.
CCNode has a position of (0,0) by default, so the rotation was using this as an origin.
Setting the position to the center of where i want the character with a bit of maths to calculate the offsets works for me.

Resources