i'm an old glBegin() programmer and i need to port this structure
struct Line { float color[3]; float vertices[2][3];};
to a more new drawing paradigm.
how to draw this with glDrawArrays or glDrawElements, supposed that shaders are ok?
thank you, mic.
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, lines->vertices);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, lines->color);
glDrawElements(GL_LINES, 2, GL_UNSIGNED_SHORT, line_indices);
Assuming you've assigned your vertex shader's 'position' and 'color' vertex attributes to 0 and 1. Indices array should be something like GLshort indices[] = { 0, 1 };.
Related
I'm having code as follows:
let arr = new Float32Array([-0.4, 0.3, -0.4, 0, 0.3, -0.2,
-0.5, -0.25,
0.7, 0.25,
0.9, -0.7
]);
let position_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, position_buffer);
gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW)
gl.vertexAttribPointer(position_location, 2, gl.FLOAT, false, 0 , 0);
gl.enableVertexAttribArray(position_location);
gl.drawArrays(gl.TRIANGLES, 0, 6)
If i want to change the second triangle (initial vertex -0.5, -0.25 in the arr), is there a way, to reach a gpu buffer, and tell him to redraw the vertices with an offset 3, and length of 3 ? So my first triangle wouldn't be redrawn?
It's called instanced drawing in webgl. Maybe for somebody would be helpful.
I'm trying to make a combat system in haxe I made my sprites and now I need to find out the uv coordinates for them how do I achieve that ?
Example from my existing code :
animation.add("lr", [3, 4, 3, 5], 6, false);
animation.add("u", [6, 7, 6, 8], 6, false);
animation.add("d", [0, 1, 0, 2], 6, false);
I tried :
if (FlxG.mouse.justPressed) {
// Attack code here
}
Haxeflixel's sprite sheet system doesn't use UV coordinates, it assumes all sprites in the sheet are the same size and consistently spaced apart.
Look at the API reference for FlxSprite.loadGraphic. The animation tutorial uses that function like this:
loadGraphic(AssetPaths.player__png, true, 16, 16);
This means the sprites are in a 16x16 grid, and they're indexed from left to right, top to bottom (with the left-most being index 0).
Here's a visual example of how the coordinates map to indices:
And here's that same sprite, but with two rows instead of one:
Compare those indices to the values in the sample code:
animation.add("lr", [3, 4, 3, 5], 6, false);
animation.add("u", [6, 7, 6, 8], 6, false);
animation.add("d", [0, 1, 0, 2], 6, false);
The second parameter to add is an array of indices that make up the animation.
So to add a punch animation, just put the frames into the existing grid, and reference them using their indices in the overall spritesheet.
As far as implementing a "combat system", that's a more involved thing. If you just want to play an attack animation, you can make it a non-looping animation, and use a callback function to detect when it ends. Example:
//...defined somewhere in your sprite class
var is_punching:Bool = false;
//...somewhere inside your constructor maybe
animation.finishCallback = function(name:String){
switch(name){
case "punch-u", "punch-lr", "punch-d":
is_punching = false;
default:
trace("Some other animation finished: " + name);
}
}
//...somewhere in your update function
if (FlxG.mouse.justPressed && !is_punching) {
is_punching = true;
switch (facing)
{
case LEFT, RIGHT:
animation.play("punch-lr");
case UP:
animation.play("punch-u");
case DOWN:
animation.play("punch-d");
case _:
}
}
Then you can check if is_punching is true to prevent the player from walking while punching, or to damage an enemy if they collide with the player while they're punching.
I have implemented a syphon server within a GPUImage application. However it produces a triangular area as seen in the attached image.
Can anyone say what’s wrong looking at the image?
Or Code?
In MyView#viewDidLoad
NSOpenGLContext *ctx = [[GPUImageContext sharedImageProcessingContext] context];
syphonServer = [[SyphonServer alloc] initWithName:#”MyServer” context:ctx.CGLContextObj options:[NSDictionary dictionaryWithObject:SyphonImageFormatRGBA8 forKey:SyphonServerOptionImageFormat]];
At the end of the myFilter#renderToTextureWithVertices
[myServer publishFrameTexture:[firstInputFramebuffer texture] textureTarget:GL_TEXTURE_RECTANGLE_EXT imageRegion:NSMakeRect(0, 0, size.width, size.height) textureDimensions:size flipped:YES];
Thanks for the input.
My hypothesis is that two things are wrong:
You're not clearing the back buffer before drawing the texture, leading to the garbled text you see.
The full screen rectangle you draw uses the wrong indices for the second triangle. If numbered 0, 1, 2, 3 (clockwise, starting at top left), it looks like the indices are [0, 1, 3] and [0, 2, 3], instead of [0, 1, 3] and [1, 3, 2].
Below is a mininal reproducer:
GL_CHECK(glClearColor(0.4f, 0.4f, 0.4f, 1.0f));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT));
GL_CHECK(glUseProgram(this->pp_->shader_program));
GL_CHECK(glEnable(GL_TEXTURE_2D));
GL_CHECK(glActiveTexture(GL_TEXTURE0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, this->particle_->id));
GLfloat points[] = { 150.f, 150.f, 10.0f, 150.0f, 175.0f, 10.0f };
GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f };
shader_pass_set_uniform(this->pp_, HASH("mvp"), glm::value_ptr(this->camera_));
GL_CHECK(glEnableVertexAttribArray(0));
GL_CHECK(glEnableVertexAttribArray(3));
GL_CHECK(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &points[0]));
GL_CHECK(glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, &colors[0]));
GL_CHECK(glDrawArrays(GL_POINTS, 0, 2));
vertex shader:
#version 300 es
layout(location = 0) in highp vec3 vertex;
layout(location = 3) in highp vec4 color;
out lowp vec4 vcolor;
uniform mat4 mvp;
void main()
{
vcolor = color;
gl_Position = mvp * vec4(vertex.xy, 0.0, 1.0);
gl_PointSize = vertex.z;
}
fragment shader:
#version 300 es
uniform sampler2D stexture;
in lowp vec4 vcolor;
layout(location = 0) out lowp vec4 ocolor;
void main()
{
ocolor = texture(stexture, gl_PointCoord) * vcolor;
}
Nothing gets rendered on-screen, my glxinfo can be found in this pastebin. When I render the same texture onto a triangle it works.
Here is also the render loop as captured with apitrace:
250155 glClearColor(red = 0.5, green = 0.5, blue = 0.5, alpha = 1)
250157 glClear(mask = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
250159 glUseProgram(program = 9)
250161 glEnable(cap = GL_TEXTURE_2D)
250163 glActiveTexture(texture = GL_TEXTURE0)
250165 glBindTexture(target = GL_TEXTURE_2D, texture = 2)
250167 glUniformMatrix4fv(location = 0, count = 1, transpose = GL_FALSE, value = {0.001953125, 0, 0, 0, 0, 0.002604167, 0, 0, 0, 0, -1, 0, -1, -1, -0, 1})
250168 glEnableVertexAttribArray(index = 0)
250170 glEnableVertexAttribArray(index = 3)
250174 glVertexAttribPointer(index = 0, size = 3, type = GL_FLOAT, normalized = GL_FALSE, stride = 0, pointer = blob(24))
250175 glVertexAttribPointer(index = 3, size = 4, type = GL_FLOAT, normalized = GL_FALSE, stride = 0, pointer = blob(32))
250176 glDrawArrays(mode = GL_POINTS, first = 0, count = 2)
250178 glXSwapBuffers(dpy = 0x1564010, drawable = 50331661)
I guess that could mean, that the range of point sizes, that your GLES implementation supports, may be not what you are expecting. You can try something like:
GLint range[2];
glGetIntegerv(GL_ALIASED_POINT_SIZE_RANGE, range); // for example, (1, 511) in my implementation
to check it out. Any implementation guarantees, that range[1] is at least 1.0. The value gl_PointSize is getting clamped to the range, so in the worst case you won't be able to set point with size, greater than 1. In this case gl_PointCoord seems to evaluate to (1, 0) (according to formula from specs) at the only fragment, that is going to be drawn for each point. After the texture's wrap mode come into play, (1, 0) may turn into (0, 0), for example if you are using GL_REPEAT as the texture's wrap mode. If you'd like to safely draw a point with side, greater than one, you should try using the ordinary way to draw squares (for example using four vertices and GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP mode).
I have a 122 * 16 image.
It has seven colors, and I use only one color per block.
But, loadGraphic() in the FlxSprite class just uses width and height.
How do I use certain position of image?
For example, if I choose 0, loadGraphic() picks up the position width, height(0 to 15, 16), choose 1, (17 ~ 31, 16)
Could you use an animation but change the current frame manually? Something like this:
var sprite = new FlxSprite(100, 100);
sprite.loadGraphic("assets/image.png", true, 122, 16);
sprite.animation.add("my_animation", [0, 1, 2, 3, 4, 5, 6], 0, false);
sprite.animation.play("my_animation", true, 0);
//sprite.animation.paused = true;
sprite.animation.frameIndex = 2; // The frame you want to display